|
@@ -1,6 +1,6 @@
|
|
|
import { PRE_GAME, PRE_ROUND, IN_ROUND, POST_ROUND, POST_GAME } from "./GameState";
|
|
|
import { createStore } from "../store";
|
|
|
-import { joinGame, sendGuess } from "./apiMethods";
|
|
|
+import { joinGame, sendGuess, getCurrentRound } from "./apiMethods";
|
|
|
import {
|
|
|
saveGameInfoToLocalStorage,
|
|
|
clearGameInfoFromLocalStorage,
|
|
@@ -15,6 +15,8 @@ export const [
|
|
|
useLastRound,
|
|
|
usePlayerId,
|
|
|
useGameState,
|
|
|
+ useCurrentRound,
|
|
|
+ useTargetPoint,
|
|
|
useRoundSeconds,
|
|
|
},
|
|
|
dispatch,
|
|
@@ -32,6 +34,8 @@ export const [
|
|
|
},
|
|
|
playerId: null,
|
|
|
gameState: PRE_GAME,
|
|
|
+ currentRound: null,
|
|
|
+ targetPoint: null,
|
|
|
roundSeconds: 0,
|
|
|
}, {
|
|
|
setPlayerName: ([set], playerName) => set({ playerName }),
|
|
@@ -44,25 +48,42 @@ export const [
|
|
|
const gameId = get.gameId();
|
|
|
const name = get.playerName();
|
|
|
const { playerId } = await joinGame(gameId, name);
|
|
|
- set({ playerId });
|
|
|
+ const { currentRound, coord, timer } = await getCurrentRound(gameId, playerId);
|
|
|
+ set({
|
|
|
+ playerId,
|
|
|
+ currentRound,
|
|
|
+ targetPoint: coord,
|
|
|
+ roundSeconds: timer,
|
|
|
+ });
|
|
|
clearTimerFromLocalStorage();
|
|
|
clearPointFromLocalStorage();
|
|
|
saveGameInfoToLocalStorage(gameId, name, playerId);
|
|
|
},
|
|
|
- rejoinGame: ([set], gameId, playerName, playerId) => set({
|
|
|
- gameId,
|
|
|
- playerName,
|
|
|
- playerId,
|
|
|
- gameState: IN_ROUND,
|
|
|
- }),
|
|
|
+ rejoinGame: async ([set], gameId, playerName, playerId) => {
|
|
|
+ const { currentRound, coord, timer } = await getCurrentRound(gameId, playerId);
|
|
|
+ set({
|
|
|
+ gameId,
|
|
|
+ playerName,
|
|
|
+ playerId,
|
|
|
+ currentRound,
|
|
|
+ targetPoint: coord,
|
|
|
+ roundSeconds: timer,
|
|
|
+ gameState: IN_ROUND,
|
|
|
+ });
|
|
|
+ },
|
|
|
startRound: ([set]) => set({ gameState: IN_ROUND }),
|
|
|
- submitGuess: async ([set, get], selectedPoint, roundNum, targetPoint) => {
|
|
|
+ submitGuess: async ([set, get], selectedPoint) => {
|
|
|
+ const gameId = get.gameId();
|
|
|
+ const playerId = get.playerId();
|
|
|
+ const roundNum = get.currentRound();
|
|
|
+ const targetPoint = get.targetPoint();
|
|
|
const { score, totalScore } = await sendGuess(
|
|
|
- get.gameId(),
|
|
|
- get.playerId(),
|
|
|
+ gameId,
|
|
|
+ playerId,
|
|
|
roundNum,
|
|
|
selectedPoint || { timeout: true }
|
|
|
);
|
|
|
+ const { currentRound, coord, timer } = await getCurrentRound(gameId, playerId);
|
|
|
set({
|
|
|
lastRound: {
|
|
|
roundNum,
|
|
@@ -70,6 +91,9 @@ export const [
|
|
|
score,
|
|
|
totalScore,
|
|
|
},
|
|
|
+ currentRound,
|
|
|
+ targetPoint: coord,
|
|
|
+ roundSeconds: timer,
|
|
|
gameState: POST_ROUND,
|
|
|
});
|
|
|
clearTimerFromLocalStorage();
|