feat/FRO-31: Added ingame (#20)
Force push of Janis ingame changes Co-authored-by: Janis <janis.e.20@gmx.de> Reviewed-on: #20
This commit is contained in:
29
src/composables/useIngame.ts
Normal file
29
src/composables/useIngame.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import {ref, type Ref} from 'vue'
|
||||
import type {GameInfo, LobbyInfo, TieInfo, TrumpInfo, WonInfo} from "@/types/GameTypes.ts";
|
||||
import axios from "axios";
|
||||
|
||||
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||
|
||||
export const useIngame = defineStore('ingame', () => {
|
||||
const state: Ref<'Lobby' | 'InGame' | 'SelectTrump' | 'TieBreak' | 'FinishedMatch' | null> = ref(null);
|
||||
const data: Ref<GameInfo | LobbyInfo | TieInfo | TrumpInfo | WonInfo | null> = ref(null);
|
||||
|
||||
function setIngame(newState: 'Lobby' | 'InGame' | 'SelectTrump' | 'TieBreak' | 'FinishedMatch', newData: GameInfo | LobbyInfo | TieInfo | TrumpInfo | WonInfo) {
|
||||
state.value = newState;
|
||||
data.value = newData;
|
||||
}
|
||||
|
||||
function requestGame(gameId: string) {
|
||||
axios.get(`${api}/status/${gameId}`, {withCredentials: true}).then((response) => {
|
||||
setIngame(response.data.state, response.data.data);
|
||||
});
|
||||
}
|
||||
|
||||
function clearIngame() {
|
||||
state.value = null;
|
||||
data.value = null;
|
||||
}
|
||||
|
||||
return { state, data, requestGame, setIngame, clearIngame };
|
||||
});
|
||||
@@ -1,19 +1,40 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import {ref, type Ref} from 'vue'
|
||||
import axios from "axios";
|
||||
|
||||
const api = window?.__RUNTIME_CONFIG__?.API_URL;
|
||||
|
||||
export const useUserInfo = defineStore('userInfo', () => {
|
||||
const username: Ref<string | null> = ref(null);
|
||||
const userId: Ref<number | null> = ref(null);
|
||||
const gameId: Ref<string | null> = ref(null);
|
||||
|
||||
function setUserInfo(name: string, id: number) {
|
||||
username.value = name;
|
||||
userId.value = id;
|
||||
}
|
||||
|
||||
function setGameId(id: string) {
|
||||
gameId.value = id;
|
||||
}
|
||||
|
||||
function requestState() {
|
||||
axios.get(`${api}/status`, {withCredentials: true}).then((response) => {
|
||||
username.value = response.data.username;
|
||||
if (response.data.ingame) {
|
||||
gameId.value = response.data.gameId;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function clearUserInfo() {
|
||||
username.value = null;
|
||||
userId.value = null;
|
||||
}
|
||||
|
||||
return { username, userId, setUserInfo, clearUserInfo };
|
||||
function clearGameId() {
|
||||
gameId.value = null;
|
||||
}
|
||||
|
||||
return { username, userId, gameId, setUserInfo, requestState, clearUserInfo, setGameId, clearGameId };
|
||||
});
|
||||
|
||||
@@ -6,8 +6,13 @@ import {
|
||||
sendEventAndWait,
|
||||
onEvent,
|
||||
isWebSocketConnected,
|
||||
setDefaultHandler,
|
||||
} from "@/services/ws";
|
||||
|
||||
function defaultEventHandler<T = any>(data: (data: T) => void) {
|
||||
setDefaultHandler(data);
|
||||
}
|
||||
|
||||
export function useWebSocket() {
|
||||
const isConnected = ref(isWebSocketConnected());
|
||||
const lastMessage = ref<any>(null);
|
||||
@@ -51,5 +56,6 @@ export function useWebSocket() {
|
||||
sendAndWait: sendEventAndWait,
|
||||
|
||||
useEvent,
|
||||
defaultEventHandler
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user