1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME } from "./GameState";
- import { createStore } from "../store";
- import { joinGame, sendGuess } from "./apiMethods";
- export const [
- {
- useGameId,
- usePlayerName,
- useLastRound,
- useGameJoined,
- usePlayerId,
- useGameState,
- },
- dispatch,
- ] = createStore({
- gameId: null,
- playerName: null,
- lastRound: {
- roundNum: -1,
- targetPoint: {
- lat: 0,
- lng: 0,
- },
- score: -1,
- totalScore: -1,
- },
- gameJoined: false,
- playerId: null,
- gameState: PRE_GAME,
- }, {
- setPlayerName: ([set], playerName) => set({ playerName }),
- goToLobby: ([set], gameId) => set({
- gameId,
- gameJoined: false,
- playerId: null,
- gameState: PRE_ROUND,
- }),
- joinGame: async ([set, get]) => {
- const gameId = get.gameId();
- const name = get.playerName();
- const { playerId } = await joinGame(gameId, name);
- set({ gameJoined: true, playerId });
- },
- startRound: ([set]) => set({ gameState: IN_ROUND }),
- submitGuess: async ([set, get], selectedPoint, roundNum, targetPoint) => {
- const { score, totalScore } = await sendGuess(
- get.gameId(),
- get.playerId(),
- roundNum,
- selectedPoint || { timeout: true }
- );
- set({
- lastRound: {
- roundNum,
- targetPoint,
- score,
- totalScore,
- },
- gameState: POST_ROUND,
- });
- },
- goToSummary: ([set, get], gameId) => set({
- gameId: gameId || get.gameId(),
- gameState: POST_GAME,
- }),
- resetGame: ([set]) => set({
- gameJoined: false,
- playerId: null,
- gameState: PRE_GAME,
- }),
- });
|