remove game from activeGames immediately on end

This commit is contained in:
Nathaniel Tampus
2023-04-09 02:41:46 +08:00
parent 5cf4f599a6
commit 24861fec36
3 changed files with 20 additions and 3 deletions

View File

@@ -149,6 +149,9 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
};
io.to(game.code as string).emit("gameOver", gameOver);
if (game.timeout) clearTimeout(game.timeout);
activeGames.splice(activeGames.indexOf(game), 1);
}
export async function getLatestGame(this: Socket) {
@@ -205,6 +208,9 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
const { id } = (await GameModel.save(game)) as Game; // save game to db
game.id = id;
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide, id });
if (game.timeout) clearTimeout(game.timeout);
activeGames.splice(activeGames.indexOf(game), 1);
}
} else {
throw new Error("invalid move");