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 () {
// 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

View file

@ -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" };

View file

@ -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";