Skip to content

Commit e166cf9

Browse files
committed
pause on reload
Forced clients to be paused even on join or reload.
1 parent c2d7b5e commit e166cf9

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

src/client/game/game.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ export function Game(): JSX.Element {
104104
"game-state",
105105
async () => {
106106
return get("/game-state").then((gameState) => {
107-
setChess(new ChessEngine(gameState.position));
108-
if (gameState.gameEndReason !== undefined) {
109-
setGameInterruptedReason(gameState.gameEndReason);
107+
setChess(new ChessEngine(gameState.state.position));
108+
setPause(gameState.pause);
109+
if (gameState.state.gameEndReason !== undefined) {
110+
setGameInterruptedReason(gameState.state.gameEndReason);
110111
}
111-
return gameState;
112+
return gameState.state;
112113
});
113114
},
114115
false,
@@ -161,12 +162,7 @@ export function Game(): JSX.Element {
161162
: null
162163
: null;
163164

164-
const gamePauseDialog =
165-
gameHoldReason !== undefined ?
166-
gameHoldReason === GameHoldReason.GAME_PAUSED ?
167-
<PauseDialog />
168-
: null
169-
: null;
165+
const gamePauseDialog = paused ? <PauseDialog /> : null;
170166

171167
const gameUnpauseDialog =
172168
gameHoldReason !== undefined ?

src/server/api/api.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ apiRouter.get("/game-state", (req, res) => {
221221
return res.status(400).send({ message: "No game is currently active" });
222222
}
223223
const clientType = clientManager.getClientType(req.cookies.id);
224-
return res.send(gameManager.getGameState(clientType));
224+
return res.send({
225+
state: gameManager.getGameState(clientType),
226+
pause: gamePaused.flag,
227+
});
225228
});
226229

227230
/**
@@ -575,26 +578,32 @@ apiRouter.get("/pause-game", (_, res) => {
575578
* Todo: add authentication instead of an exposed unpause call
576579
*/
577580
apiRouter.get("/unpause-game", async (_, res) => {
578-
gamePaused.flag = false;
579-
const ids = clientManager.getIds();
580-
if (ids) {
581-
const oldSave = SaveManager.loadGame(ids[0]);
582-
gameManager?.chess.loadFen(oldSave!.oldPos);
583-
setAllRobotsToDefaultPositions(
584-
new Map(
585-
oldSave!.oldRobotPos?.map<[string, GridIndices]>((obj) => [
586-
obj[1],
587-
new GridIndices(
588-
parseInt(obj[0].split(", ")[0]),
589-
parseInt(obj[0].split(", ")[1]),
590-
),
591-
]),
592-
),
581+
if (gamePaused.flag) {
582+
gamePaused.flag = false;
583+
const ids = clientManager.getIds();
584+
if (ids) {
585+
const oldSave = SaveManager.loadGame(ids[0]);
586+
gameManager?.chess.loadFen(oldSave!.oldPos);
587+
setAllRobotsToDefaultPositions(
588+
new Map(
589+
oldSave!.oldRobotPos?.map<[string, GridIndices]>((obj) => [
590+
obj[1],
591+
new GridIndices(
592+
parseInt(obj[0].split(", ")[0]),
593+
parseInt(obj[0].split(", ")[1]),
594+
),
595+
]),
596+
),
597+
);
598+
socketManager.sendToAll(new SetChessMessage(oldSave!.oldPos));
599+
}
600+
socketManager.sendToAll(
601+
new GameHoldMessage(GameHoldReason.GAME_UNPAUSED),
593602
);
594-
socketManager.sendToAll(new SetChessMessage(oldSave!.oldPos));
603+
return res.send({ message: "success" });
604+
} else {
605+
return res.send({ message: "game not paused" });
595606
}
596-
socketManager.sendToAll(new GameHoldMessage(GameHoldReason.GAME_UNPAUSED));
597-
return res.send({ message: "success" });
598607
});
599608

600609
/**

src/server/api/save-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class SaveManager {
6262
};
6363
const oldGame = SaveManager.loadGame(hostId + "+" + clientID);
6464
if (oldGame && oldGame.pos !== null) {
65-
saveContents.oldPos = oldGame.game;
65+
saveContents.oldPos = oldGame.pos;
6666
saveContents.oldRobotPos = oldGame.robotPos;
6767
}
6868
return FileManager.writeFile(hostId + "+" + clientID, saveContents);

0 commit comments

Comments
 (0)