123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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,
- };
|