Skip to content
Snippets Groups Projects

Resolve "Join tournament"

Merged Tobias Ingebrigt Ørstad requested to merge 28-join-tournament into dev
1 file
+ 59
10
Compare changes
  • Side-by-side
  • Inline
+ 59
10
@@ -85,19 +85,68 @@ router.post("/join", (req, res) => {
@@ -85,19 +85,68 @@ router.post("/join", (req, res) => {
const db = client.db("gameWare");
const db = client.db("gameWare");
const collection = "tournaments";
const collection = "tournaments";
db.collection(collection)
db.collection(collection).findOneAndUpdate(
.find({
{
$expr: { $lt: ["$maxPlayers", "$players.length"] }
// the length of the array of players must be less than the maximum amount
})
$expr: { $lt: ["$maxPlayers", "$players.length"] },
.toArray((err, result) => {
currentRound: 1,
 
active: true,
 
// the array of players cannot already contain the player
 
players: { $nin: [mongo.ObjectID(req.body.userid)] }
 
},
 
// Add the player to the array
 
{ $push: { players: mongo.ObjectID(req.body.userid) } },
 
{
 
// make sure we join the oldest tournament that matches our query.
 
sort: { dateCreated: 1 },
 
// the returned document should be the modified version
 
returnNewDocument: true
 
},
 
(err, result) => {
if (err) {
if (err) {
res.sendStatus(500);
res.sendStatus(500); // Internal server error
console.log(err);
return;
return;
}
}
res.json(result);
try {
});
tournamentdate = result.value.dateCreated;
db.collection("rounds").insertOne({});
tournamentdate.setTime(
 
tournamentdate.getTime() +
 
result.value.timePerRound * 60 * 60 * 1000
 
);
 
// create the first round for the newly joined player
 
createFirstRound(
 
result.value._id,
 
result.value,
 
tournamentdate,
 
req.body.userid,
 
result.value.games[0]
 
);
 
} catch (err) {
 
res.json(404); // Element not found
 
}
 
}
 
);
 
//Function for creating first round of tournament for hosting player
 
function createFirstRound(tournyid, tournyresult, date, userid, gameid) {
 
db.collection("rounds").insertOne(
 
{
 
tournamentId: mongo.ObjectID(tournyid),
 
playerId: mongo.ObjectID(userid),
 
gameId: mongo.ObjectID(gameid),
 
scoreValue: 0,
 
roundNr: 1,
 
hasPlayed: false,
 
deadlineDate: date
 
},
 
err => {
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
res.json(tournyresult);
 
}
 
);
 
}
}
}
);
);
});
});
Loading