Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/create-an-endpoint-logic-for-endgame #482

Merged
merged 7 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions server/src/controllers/game.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,54 @@ class GameController {
}

// End game logic by checkmate or draw
// async endGame (req, res){
// }
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));
}

// 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;
}

isGameExist.data.status = 2;
// update the Game Info with current result
const updated = await GameRepo.update(game_id, {
...isGameExist.data,
});

const payload = {
event: "end_game",
winner:
isGameExist.data.owner.user_id || isGameExist.data.opponent.user_id,
status: isGameExist.data.status,
};

await centrifugoController.publish(game_id, payload);
return res.status(200).send(response("Game ended!!!", updated));
} catch (error) {
next(`Unable to end game ${error}`);
}
}

// End game logic by resigning
//async resign (req, res){}
Expand Down
3 changes: 3 additions & 0 deletions server/src/models/game.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const game_schema = Joi.object({
})
)
.allow(null),

// game status
status: Joi.number().required(),
});

module.exports = game_schema;
2 changes: 1 addition & 1 deletion server/src/routes/game.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down