From 31e81bd0dafa8f2c5b43c0d426b333836c32a60e Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 16 Jan 2022 22:25:11 +0100 Subject: Fix bug allowing for negative ongoing games Previously if the game ended, the players could still spam the resign button or close their tab to decrement the `stats.ongoingGames' counter. This patch removes the ability to do so by checking if the game is still in the environment before doing anything. --- draughts/app.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/draughts/app.js b/draughts/app.js index 94dca46..e7fd6a1 100644 --- a/draughts/app.js +++ b/draughts/app.js @@ -58,11 +58,11 @@ wss.on("connection", ws => { * array. */ ws.on("close", () => { - if (game.ongoing) { + if (game.ongoing && env.games.includes(game)) { game.messageOpponent({ head: Messages.DISCONNECT }, ws) stats.ongoingGames-- + stats.totalGames-- } - stats.totalGames-- env.removeGame(game) }) @@ -70,10 +70,11 @@ wss.on("connection", ws => { msg = JSON.parse(msg) switch (msg.head) { case Messages.RESIGN: - if (game.ongoing) + if (game.ongoing && env.games.includes(game)) { game.messageOpponent(msg, ws) - stats.ongoingGames-- - stats.totalGames-- + stats.ongoingGames-- + stats.totalGames-- + } env.removeGame(game) break case Messages.MOVED: -- cgit v1.2.3