From 00d88655b220d5303f8e26f11b0ab571aee31dc8 Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 06:17:42 +0100 Subject: [PATCH 1/7] feat/create-an-endpoint-logic-for-endgame --- server/src/controllers/game.controller.js | 42 +++++++++++++++++++++-- server/src/routes/game.route.js | 2 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index 32f0f5ae..75e479d6 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -201,8 +201,46 @@ class GameController { } // End game logic by checkmate or draw - // async endGame (req, res){ - // } + async endGame(req, res, next) { + try { + const { game_id, user_id } = req.body; + const isGameExist = await GameRepo.fetchOne(game_id); + + if (!isGameExist.data) { + return res + .status(400) + .send(response("Game does not exist", null, false)); + } + + if (!user_id) { + return res + .status(400) + .send(response("User does not exist", null, false)); + } + + let permission; + + const updated = await GameRepo.update(game_id, { + ...isGameExist.data, + player: { + user_id, + }, + }); + + permission = null; + + const payload = { + event: "end_game", + permission, + player: updated.data, + }; + + await centrifugoController.publish(game_id, payload); + return res.status(200).send(response("Game ended!!!", isGameExist.data)); + } catch (error) { + next(`Unable to end game ${error}`); + } + } // End game logic by resigning //async resign (req, res){} diff --git a/server/src/routes/game.route.js b/server/src/routes/game.route.js index 43282afc..0bbfad79 100644 --- a/server/src/routes/game.route.js +++ b/server/src/routes/game.route.js @@ -20,7 +20,7 @@ router.patch("/watch", GameCtrl.addSpectator); router.patch("/pieceemove", GameCtrl.pieceMove); // End Game - Not Implemented -- AfricanDev -// router.patch("/end", GameCtrl.endGame) +router.patch("/end", GameCtrl.endGame); // Resign - Not implemented -- Ace Anyanwu // router.patch('/resign', GameCtrl.resign) From ba2784d4d3a136e6379f1776f6197ad382a2cb42 Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 07:02:17 +0100 Subject: [PATCH 2/7] refractor/add-a-logic-to-check-who-end-the-game --- server/src/controllers/game.controller.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index 75e479d6..232f42af 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -218,12 +218,19 @@ class GameController { .send(response("User does not exist", null, false)); } + let winner; + if (isGameExist.data.owner) { + winner = isGameExist.data.opponent; + } else { + winner = isGameExist.data.owner; + } + let permission; const updated = await GameRepo.update(game_id, { ...isGameExist.data, - player: { - user_id, + checkmate: { + winner, }, }); @@ -236,7 +243,7 @@ class GameController { }; await centrifugoController.publish(game_id, payload); - return res.status(200).send(response("Game ended!!!", isGameExist.data)); + return res.status(200).send(response("Game ended!!!", updated)); } catch (error) { next(`Unable to end game ${error}`); } From 531d7bff6c57c17a7951c0e00c07303dc45e5d7f Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 07:08:17 +0100 Subject: [PATCH 3/7] refractor/make little changes on the logic --- server/src/controllers/game.controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index 232f42af..4598da24 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -239,7 +239,7 @@ class GameController { const payload = { event: "end_game", permission, - player: updated.data, + winner: updated.checkmate, }; await centrifugoController.publish(game_id, payload); From 48314549c88d5c75957c57ae392c72396c8a69a2 Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 14:04:04 +0100 Subject: [PATCH 4/7] refractor/endgame logic --- server/src/controllers/game.controller.js | 29 ++++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index 4598da24..76756999 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -203,43 +203,44 @@ class GameController { // End game logic by checkmate or draw async endGame(req, res, next) { try { + // request an info from the user const { game_id, user_id } = req.body; + + // fetch the game from the database const isGameExist = await GameRepo.fetchOne(game_id); + // check if the game data exists if (!isGameExist.data) { return res .status(400) .send(response("Game does not exist", null, false)); } + // check if that particular user exist in the database if (!user_id) { return res .status(400) .send(response("User does not exist", null, false)); } - let winner; - if (isGameExist.data.owner) { - winner = isGameExist.data.opponent; - } else { - winner = isGameExist.data.owner; + // checking if user (winner) is equivalent relating to the data fetched + if (user_id === isGameExist.data.owner.user_id) { + isGameExist.data.is_owner_winner = true; + } else if (user_id == isGameExist.data.opponent.user_id) { + isGameExist.data.is_owner_winner = true; } - let permission; - + // update the Game Info with current result const updated = await GameRepo.update(game_id, { ...isGameExist.data, - checkmate: { - winner, - }, + gameWinner: isGameExist.data.is_owner_winner, + gameStatus: "ended", }); - permission = null; - const payload = { event: "end_game", - permission, - winner: updated.checkmate, + winner: updated.gameWinner, + status: updated.gameStatus, }; await centrifugoController.publish(game_id, payload); From 0166c71b875ee192d81c9dfc03a3992c59edb972 Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 16:01:25 +0100 Subject: [PATCH 5/7] refractor/change to actual model name --- server/src/controllers/game.controller.js | 9 +++++---- server/src/models/game.model.js | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index 76756999..c71d3355 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -230,17 +230,18 @@ class GameController { isGameExist.data.is_owner_winner = true; } + isGameExist.data.status = true; // update the Game Info with current result const updated = await GameRepo.update(game_id, { ...isGameExist.data, - gameWinner: isGameExist.data.is_owner_winner, - gameStatus: "ended", + status: "end", }); const payload = { event: "end_game", - winner: updated.gameWinner, - status: updated.gameStatus, + winner: + isGameExist.data.owner.user_id || isGameExist.data.opponent.user_id, + status: updated.status, }; await centrifugoController.publish(game_id, payload); diff --git a/server/src/models/game.model.js b/server/src/models/game.model.js index 0bf95fcd..3a34de8f 100644 --- a/server/src/models/game.model.js +++ b/server/src/models/game.model.js @@ -58,6 +58,9 @@ const game_schema = Joi.object({ }) ) .allow(null), + + // game status + status: Joi.string().required(), }); module.exports = game_schema; From 1d701d19b0167c41cd9b12a1ba3b629ec0d3c97e Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 17:31:38 +0100 Subject: [PATCH 6/7] fix/logic --- server/src/controllers/game.controller.js | 5 ++--- server/src/models/game.model.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index c71d3355..fa1425c3 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -230,18 +230,17 @@ class GameController { isGameExist.data.is_owner_winner = true; } - isGameExist.data.status = true; + isGameExist.data.status = 3; // update the Game Info with current result const updated = await GameRepo.update(game_id, { ...isGameExist.data, - status: "end", }); const payload = { event: "end_game", winner: isGameExist.data.owner.user_id || isGameExist.data.opponent.user_id, - status: updated.status, + status: isGameExist.data.status, }; await centrifugoController.publish(game_id, payload); diff --git a/server/src/models/game.model.js b/server/src/models/game.model.js index 3a34de8f..2bb3c333 100644 --- a/server/src/models/game.model.js +++ b/server/src/models/game.model.js @@ -60,7 +60,7 @@ const game_schema = Joi.object({ .allow(null), // game status - status: Joi.string().required(), + status: Joi.number().required(), }); module.exports = game_schema; From b63abdb7f62daa3673a4e49a5fb8b8cac1a85bea Mon Sep 17 00:00:00 2001 From: Alao-Abiodun <46402042+Alao-Abiodun@users.noreply.github.com> Date: Thu, 9 Sep 2021 17:43:20 +0100 Subject: [PATCH 7/7] fix/logic --- server/src/controllers/game.controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/controllers/game.controller.js b/server/src/controllers/game.controller.js index fa1425c3..1292aa2c 100644 --- a/server/src/controllers/game.controller.js +++ b/server/src/controllers/game.controller.js @@ -230,7 +230,7 @@ class GameController { isGameExist.data.is_owner_winner = true; } - isGameExist.data.status = 3; + isGameExist.data.status = 2; // update the Game Info with current result const updated = await GameRepo.update(game_id, { ...isGameExist.data,