From cffa0474dd892c13c3b0deee6d2e14e4637874b7 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 11 Feb 2023 21:46:06 +0100 Subject: Improved games controller tests --- controllers/games/gameId.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'controllers/games/gameId.js') diff --git a/controllers/games/gameId.js b/controllers/games/gameId.js index 9487743..2bc0a5e 100644 --- a/controllers/games/gameId.js +++ b/controllers/games/gameId.js @@ -9,16 +9,19 @@ function makePageData(user, game) { } export function gameId_get(req, res) { - const game = JSON.parse(getGame(req.params.gameId).data); - if (game) { - if (game.player1.id === req.session.user.id) { - game.letters = game.player1.letters; - delete game.player2.letters; - } else { - game.letters = game.player2.letters; - delete game.player1.letters; + const gameData = getGame(req.params.gameId); + if (gameData) { + const game = JSON.parse(gameData.data); + if (game) { + if (game.player1.id === req.session.user.id) { + game.letters = game.player1.letters; + delete game.player2.letters; + } else { + game.letters = game.player2.letters; + delete game.player1.letters; + } + return res.render('game', makePageData(req.session.user, game)); } - return res.render('game', makePageData(req.session.user, game)); - } + } // we got null or undefined from the getGame database request return res.redirect('/games'); } -- cgit v1.2.3