Skip to content

Commit

Permalink
added setWinner #22
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 15, 2021
1 parent d3955fe commit f481bde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 14 additions & 0 deletions server/helpers/setWinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = async function setWinner(user, target) {
const isWinner = user._id.toString() === target.toString()

if (isWinner) {
user.stats.wins++;
user.target = null;
user.party.isStarted = false;
user.party.winner = user._id;
await user.party.save();
}

return isWinner;
}

10 changes: 3 additions & 7 deletions server/routes/assassinate/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const router = require("express").Router();
const isLoggedIn = require("../../helpers/isLoggedIn");
const User = require("../../models/user.model");
const shuffle = require("../../helpers/shuffle");
const setWinner = require("../../helpers/setWinner");

router.route("/").get(isLoggedIn, async function (req, res) {
if (req.user.isPending) {
Expand All @@ -25,13 +26,7 @@ router.route("/").get(isLoggedIn, async function (req, res) {

switch (user.party.gameMode) {
case "Classic":
if (user._id.toString() === target.toString()) {
user.stats.wins++;
user.target = null;
user.party.isStarted = false;
user.party.winner = user._id;
await user.party.save();
} else {
if (!setWinner(user, target)) {
user.target = target;
}
break;
Expand All @@ -46,6 +41,7 @@ router.route("/").get(isLoggedIn, async function (req, res) {
}
player.save();
});
setWinner(user, target);
break;
}

Expand Down

0 comments on commit f481bde

Please sign in to comment.