diff --git a/packages/server/services/Game.js b/packages/server/services/Game.js index 94a4c97..58c7d1d 100644 --- a/packages/server/services/Game.js +++ b/packages/server/services/Game.js @@ -297,6 +297,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => { endGame: function () { // TODO manage territory counting // form groups for empty points + this.gameRecord; // for each empty point group determine territory // for each non-empty point group determine life // submit board state to users diff --git a/packages/server/services/gameServices.js b/packages/server/services/gameServices.js index bbdd9e4..f1df37b 100644 --- a/packages/server/services/gameServices.js +++ b/packages/server/services/gameServices.js @@ -20,7 +20,18 @@ const GameService = (moveQueries) => { }, async makeMove({ id, move }) { - if (!gamesInProgress[id]) storeGame({ id }).initGame(); + // check cache + if (!gamesInProgress[id]) { + try { + let gameRecord; + if (moveQueries) { + gameRecord = await moveQueries.findGameRecord(id); + } + storeGame({ id, gameRecord }).initGame(); + } catch { + return { message: "error restoring game" }; + } + } gamesInProgress[id] = gamesInProgress[id].makeMove(move); if (gamesInProgress[id].success === false) return { message: "illegal move" }; diff --git a/packages/server/socket.js b/packages/server/socket.js index a48ba89..14bad8d 100644 --- a/packages/server/socket.js +++ b/packages/server/socket.js @@ -20,12 +20,10 @@ io.on("connection", async (socket) => { socket.on("connect_game", (data) => { const game = `game-${data.game.id}`; socket.join(game, async () => { - // ! temp const gameRecord = await moveQueries.findGameRecord(data.game.id); console.log("gameRecord from db"); console.log(gameRecord); await gameServices.initGame({ id: data.game.id, gameRecord }); - // ! end-temp const { board, ...meta } = await gameServices.getDataForUI( data.game.id ); @@ -38,7 +36,7 @@ io.on("connection", async (socket) => { try { const { board, message, ...meta } = await gameServices.makeMove({ - id: 1, + id: data.game.id, move, }); const socketAction = message ? "error" : "update_board";