import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME } from "./gameStates"; import { createStore, consoleMonitor } from "../store"; import { joinGame, sendGuess, getCurrentRound, sendTimeout } from "./apiMethods"; import { saveGameInfoToLocalStorage, clearGameInfoFromLocalStorage, saveTimerToLocalStorage, clearRoundInfoFromLocalStorage, getInfoFromLocalStorage, } from "./localStorageMethods"; const [ hooks, set, get ] = createStore({ gameId: null, playerName: null, lastRound: { roundNum: -1, targetPoint: { lat: 0, lng: 0, }, score: -1, totalScore: -1, }, playerId: null, gameState: PRE_GAME, currentRound: null, targetPoint: null, roundSeconds: 0, panoStartPosition: null, panoStartPov: null, }, process.env.REACT_APP_MONITOR_STORE === "true" ? consoleMonitor : null); export const { useGameId, usePlayerName, useLastRound, usePlayerId, useGameState, useCurrentRound, useTargetPoint, useRoundSeconds, usePanoStartPosition, usePanoStartPov, } = hooks; const setPlayerName = playerName => set({ playerName }); const goToLobby = gameId => set({ gameId, playerId: null, gameState: PRE_ROUND, }); const updateCurrentRound = async () => { const { currentRound, coord, timer } = await getCurrentRound( get.gameId(), get.playerId() ); set({ currentRound, targetPoint: coord, panoStartPosition: coord, panoStartPov: { heading: 0, pitch: 0 }, roundSeconds: timer, }); }; const joinGameAction = async () => { const gameId = get.gameId(); const name = get.playerName(); const { playerId } = await joinGame(gameId, name); set({ playerId }); await updateCurrentRound(); saveGameInfoToLocalStorage(gameId, name, playerId); }; const rejoinGame = async () => { const { gameId, playerName, playerId, timer, position, pov } = getInfoFromLocalStorage(); set({ gameId, playerName, playerId }); await updateCurrentRound(); set({ roundSeconds: timer ?? get.roundSeconds(), panoStartPosition: position ?? get.panoStartPosition(), panoStartPov: pov ?? get.panoStartPov(), gameState: IN_ROUND, }); }; const startRound = () => set({ gameState: IN_ROUND }); const submitGuess = async selectedPoint => { clearRoundInfoFromLocalStorage(); const gameId = get.gameId(); const playerId = get.playerId(); const roundNum = get.currentRound(); const targetPoint = get.targetPoint(); const roundSeconds = get.roundSeconds(); const { score, totalScore } = selectedPoint ? await sendGuess( gameId, playerId, roundNum, selectedPoint || { timeout: true }, roundSeconds ) : await sendTimeout(gameId, playerId, roundNum); set({ lastRound: { roundNum, targetPoint, score, totalScore, }, gameState: POST_ROUND, }); await updateCurrentRound(); }; const goToSummary = (gameId, clearSavedGame = true) => { if (gameId) { set({ gameId }); } set({ gameState: POST_GAME }); if (clearSavedGame) { clearRoundInfoFromLocalStorage(); clearGameInfoFromLocalStorage(); } }; const updateRoundSeconds = update => { const roundSeconds = update(get.roundSeconds()); set({ roundSeconds }); saveTimerToLocalStorage(roundSeconds); }; export const dispatch = { setPlayerName, goToLobby, joinGame: joinGameAction, rejoinGame, startRound, submitGuess, goToSummary, updateRoundSeconds, };