add cache confirmation check to Game Service

This commit is contained in:
sorrelbri 2020-05-30 16:43:41 -07:00
parent 181554124b
commit d54d43c42c
3 changed files with 14 additions and 4 deletions

View file

@ -297,6 +297,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
endGame: function () { endGame: function () {
// TODO manage territory counting // TODO manage territory counting
// form groups for empty points // form groups for empty points
this.gameRecord;
// for each empty point group determine territory // for each empty point group determine territory
// for each non-empty point group determine life // for each non-empty point group determine life
// submit board state to users // submit board state to users

View file

@ -20,7 +20,18 @@ const GameService = (moveQueries) => {
}, },
async makeMove({ id, move }) { 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); gamesInProgress[id] = gamesInProgress[id].makeMove(move);
if (gamesInProgress[id].success === false) if (gamesInProgress[id].success === false)
return { message: "illegal move" }; return { message: "illegal move" };

View file

@ -20,12 +20,10 @@ io.on("connection", async (socket) => {
socket.on("connect_game", (data) => { socket.on("connect_game", (data) => {
const game = `game-${data.game.id}`; const game = `game-${data.game.id}`;
socket.join(game, async () => { socket.join(game, async () => {
// ! temp
const gameRecord = await moveQueries.findGameRecord(data.game.id); const gameRecord = await moveQueries.findGameRecord(data.game.id);
console.log("gameRecord from db"); console.log("gameRecord from db");
console.log(gameRecord); console.log(gameRecord);
await gameServices.initGame({ id: data.game.id, gameRecord }); await gameServices.initGame({ id: data.game.id, gameRecord });
// ! end-temp
const { board, ...meta } = await gameServices.getDataForUI( const { board, ...meta } = await gameServices.getDataForUI(
data.game.id data.game.id
); );
@ -38,7 +36,7 @@ io.on("connection", async (socket) => {
try { try {
const { board, message, ...meta } = await gameServices.makeMove({ const { board, message, ...meta } = await gameServices.makeMove({
id: 1, id: data.game.id,
move, move,
}); });
const socketAction = message ? "error" : "update_board"; const socketAction = message ? "error" : "update_board";