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"; import {initWebSocket} from "@/services/ws.ts"; 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 = ref(null); const isWsConnected: Ref = ref(false); function setIngame(newState: 'Lobby' | 'InGame' | 'SelectTrump' | 'TieBreak' | 'FinishedMatch', newData: GameInfo | LobbyInfo | TieInfo | TrumpInfo | WonInfo) { state.value = newState; data.value = newData; } function setWsConnected(connected: boolean) { isWsConnected.value = connected; } async function requestGame(gameId: string) { await axios.get(`${api}/status/${gameId}`, {withCredentials: true}).then((response) => { setIngame(response.data.state, response.data.data); }); } function clearIngame() { state.value = null; data.value = null; isWsConnected.value = false; } return { state, data, isWsConnected, requestGame, setIngame, clearIngame, setWsConnected }; });