diff --git a/backend/api/rounds.js b/backend/api/rounds.js index a19a2e2ac514b6e84217b5f488b09383fcb01ab6..f9d4a3730a3468e8dc8a0a85e52b7f20391107fb 100644 --- a/backend/api/rounds.js +++ b/backend/api/rounds.js @@ -45,35 +45,24 @@ router.get("/round/:roundId", (req, res) => { ); }); -function updateHighscore(gameid, playerid, value) { +function updateHighscore(client, gameid, playerid, value) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "highscores"; + + db.collection(collection).updateOne( + { + gameId: mongo.ObjectID(gameid), + playerId: mongo.ObjectID(playerid) + }, + { $max: { value: value } }, + { upsert: true }, + (err, result) => { if (err) { - resolve(500); // Internal server error + resolve(500); + return; } - - const db = client.db("gameWare"); - const collection = "highscores"; - - db.collection(collection).updateOne( - { - gameId: mongo.ObjectID(gameid), - playerId: mongo.ObjectID(playerid) - }, - { $max: { value: value } }, - { upsert: true }, - (err, result) => { - if (err) { - resolve(500); - return; - } - resolve(result); - } - ); + resolve(result); } ); }); @@ -419,11 +408,12 @@ router.put("/:roundid/:tournamentid", (req, res) => { if (updatedDocument.value) { //console.log("Successfully updated document"); updateHighscore( + client, updatedDocument.value.gameId, updatedDocument.value.playerId, updatedDocument.value.scoreValue ); - roundcheck(tournament, id).then(checkresult => { + roundcheck(client, tournament, id).then(checkresult => { res.json(checkresult); client.close(); return; @@ -440,9 +430,10 @@ router.put("/:roundid/:tournamentid", (req, res) => { ); }); -function roundcheck(tournament, roundid) { +function roundcheck(client, tournament, roundid) { return new Promise((resolve, reject) => { - getCurrentRound(tournament).then(roundResult => { + getCurrentRound(client, tournament).then(roundResult => { + // gets the current round of the tournament if (roundResult == 500) { resolve(500); return; @@ -451,294 +442,299 @@ function roundcheck(tournament, roundid) { const start = roundResult.startTime; const maxPlayers = roundResult.maxPlayers; - checkRounds(tournament, active, start, maxPlayers).then(result => { - if (!result) { - resolve(getRound(roundid)); //1. check if other rounds are done - } else { - fetchRounds(tournament, active).then(rounds => { - players = rounds.length; - for (let i = 0; i < rounds.length; i++) { - let round = rounds[i]; - updateScore(round._id, i + 1, rounds.length); - } - updateTournamentRound(tournament).then(result => { - if (result.value) { - tourny = result.value; - const game = - tourny.games[tourny.currentRound % tourny.games.length]; - const date = new Date(); - date.setTime( - date.getTime() + tourny.timePerRound * 60 * 60 * 1000 - ); - for (let i = 0; i < tourny.players.length; i++) { - newRound( - tournament, - tourny.players[i], - game, - tourny.currentRound, - date - ); + fetchRounds(client, tournament, active).then(rounds => { + // fetched the round objects of the active round + checkRounds(rounds, start, maxPlayers, active).then(resultarray => { + // check to find out if [0] tournament will move on, [1] which players are timed out + // [2] which players are still in the tournament + let result = resultarray[0]; + let timedOut = resultarray[1]; + let left = resultarray[2]; + let end = false; + if ((left.length < 2 && active != 1) || left.length == 0) { + end = true; + //if there are less then two people left in the first round, or zero people left in the first round + //the tournament ends + } + timeOut(client, tournament, timedOut, end, active).then( + //updates the state of the tournament and ends it if necessary + //TODO: notify the players that the tournament ended + timeoutresult => { + if (end) { + resolve(getRound(client, roundid)); + return; + } + if (result) { + //Tournament moves on to next round + let eligibleRounds = []; + for (let i = 0; i < rounds.length; i++) { + let round = rounds[i]; + if (round.hasPlayed === true) { + eligibleRounds.push(round); + } } + //Here we find the rounds that were actually completed + for (let i = 0; i < eligibleRounds.length; i++) { + let round = eligibleRounds[i]; + updateScore(client, round._id, i + 1, eligibleRounds.length); + //And here we update the tournament score + } + updateTournamentRound(client, tournament).then(result => { + //updates the tournaments round if there are more rounds left + if (result.value) { + tourny = result.value; + const game = + tourny.games[tourny.currentRound % tourny.games.length]; + const date = new Date(); + date.setTime( + date.getTime() + tourny.timePerRound * 60 * 60 * 1000 + ); + for (let i = 0; i < tourny.players.length; i++) { + newRound( + client, + tournament, + tourny.players[i], + game, + tourny.currentRound, + date + ); + // created new rounds for all the players left + } + resolve(getRound(client, roundid)); + } else { + // if the tournament now is done we simply end it + endTournament(client, tournament); + resolve(getRound(client, roundid)); + return; + } + }); + } else { + resolve(getRound(client, roundid)); // Tournament still waiting for other players } - resolve(getRound(roundid)); - }); - }); - } - - //2.1 Get rounds by roundnr, sort by score - //2.2 for each entry in fetched array, call the update method. - //3. set tournamentplayer score + } + ); + }); }); }); }); } -function getRound(id) { +function getRound(client, id) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database - if (err) { - resolve(500); // Internal server error - return; - } - const db = client.db("gameWare"); - const collection = "rounds"; + const db = client.db("gameWare"); + const collection = "rounds"; - db.collection(collection).findOne({ _id: id }, (err, result) => { - if (err) { - resolve(500); // Internal server error - return; - } - resolve(result); - return; - }); + db.collection(collection).findOne({ _id: id }, (err, result) => { + if (err) { + resolve(500); // Internal server error + return; } - ); + resolve(result); + return; + }); }); } -function getCurrentRound(tournamentId) { +function endTournament(client, tournament) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "tournaments"; + db.collection(collection).updateOne( + { _id: tournament }, + { $set: { active: false } }, + (err, result) => { if (err) { resolve(500); // Internal server error return; } - const db = client.db("gameWare"); - const collection = "tournaments"; - - db.collection(collection).findOne( - { - _id: tournamentId - }, - { currentRound: 1, startTime: 1, maxPlayers: 1 }, - (err, result) => { - if (err) { - resolve(500); // Internal server error - return; - } - resolve(result); - return; - } - ); + resolve(result); + return; } ); }); } -function newRound(tournamentId, playerId, gameId, roundNr, deadlineDate) { +function timeOut(client, tournament, players, end) { + if (!players) { + players = []; + } return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + db.collection("tournaments").updateOne( + { + _id: tournament + }, + { + $pull: { players: { $in: players } }, + $inc: { currentPlayers: -players.length }, + $set: { active: !end } + }, + (err, result) => { if (err) { - res.sendStatus(500); // Internal server error + resolve(500); // Internal server error return; } - - // Using the database gameWare and collection rounds - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection).insertOne( - { - tournamentId: mongo.ObjectID(tournamentId), - playerId: mongo.ObjectID(playerId), - gameId: mongo.ObjectID(gameId), - roundNr: roundNr, - deadlineDate: deadlineDate, - scoreValue: 0, - hasPlayed: false, - tournamentPoints: 0 - }, - (err, result) => { - if (err) { - resolve(500); // Internal server error - return; - } - resolve(result.ops[0]); - } - ); + resolve(result); + return; } ); }); } -function updateTournamentRound(tournament) { +function getCurrentRound(client, tournamentId) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "tournaments"; + + db.collection(collection).findOne( + { + _id: tournamentId + }, + { currentRound: 1, startTime: 1, maxPlayers: 1 }, + (err, result) => { if (err) { resolve(500); // Internal server error + return; } - - const db = client.db("gameWare"); - const collection = "tournaments"; - - db.collection(collection) - .findOneAndUpdate( - { - _id: tournament, - $expr: { - $lt: ["$currentRound", "$totalGames"] - } - }, - { - $inc: { currentRound: 1 } - }, - { - returnOriginal: false - } - ) - .then(updatedDocument => { - resolve(updatedDocument); - }); + resolve(result); + return; } ); }); } -function checkRounds(tournament, active, start, max) { +function newRound( + client, + tournamentId, + playerId, + gameId, + roundNr, + deadlineDate +) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + // Using the database gameWare and collection rounds + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection(collection).insertOne( + { + tournamentId: mongo.ObjectID(tournamentId), + playerId: mongo.ObjectID(playerId), + gameId: mongo.ObjectID(gameId), + roundNr: roundNr, + deadlineDate: deadlineDate, + scoreValue: 0, + hasPlayed: false, + tournamentPoints: 0 + }, + (err, result) => { if (err) { resolve(500); // Internal server error + return; } - - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection) - .find( - { - tournamentId: tournament, - roundNr: active - }, - { - hasPlayed: 1 - } - ) - .toArray((err, result) => { - if (err) { - resolve(500); - return; - } - let amountComplete = 0; - for (let i = 0; i < result.length; i++) { - if (result[i].hasPlayed === true) { - amountComplete++; - } - } - if (amountComplete == max) { - resolve(true); - } else if (amountComplete > 1 && new Date() >= start) { - resolve(true); - } - resolve(false); - }); + resolve(result.ops[0]); } ); }); } -function fetchRounds(tournamentId, activeRound) { +function updateTournamentRound(client, tournament) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database - if (err) { - resolve(500); // Internal server error + const db = client.db("gameWare"); + const collection = "tournaments"; + + db.collection(collection) + .findOneAndUpdate( + { + _id: tournament, + $expr: { + $lt: ["$currentRound", "$totalGames"] + } + }, + { + $inc: { currentRound: 1 } + }, + { + returnOriginal: false } + ) + .then(updatedDocument => { + resolve(updatedDocument); + }); + }); +} - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection("rounds") - .find({ - tournamentId: tournamentId, - roundNr: activeRound - }) - .sort({ - scoreValue: -1 - }) - .toArray((err, result) => { - if (err) { - return; - } - resolve(result); - }); +function checkRounds(result, start, max, roundNr) { + return new Promise((resolve, reject) => { + let timedOut = []; + let left = []; + let amountComplete = 0; + for (let i = 0; i < result.length; i++) { + if (result[i].hasPlayed === true) { + amountComplete++; + left.push(result[i].playerId); + } else if (new Date(result[i].deadlineDate) <= new Date()) { + timedOut.push(result[i].playerId); + } else { + left.push(result[i].playerId); } - ); + } + if (amountComplete == max) { + // All players have completed their round + resolve([true, [], left]); + } else if (amountComplete > 1 && new Date() >= start && roundNr == 1) { + // more than two have completed first round + // startDelay passed. + resolve([true, timedOut, left]); + } else if (amountComplete + timedOut.length == max && amountComplete >= 2) { + // More than two players have completed round and all players have either completed their round or timed out. + resolve([true, timedOut, left]); + } else { + resolve([false, timedOut, left]); + } }); } -function updateScore(id, placement, playerAmount) { - let tournamentPoints = (playerAmount - placement + 1) * 10; - +function fetchRounds(client, tournamentId, activeRound) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection("rounds") + .find({ + tournamentId: tournamentId, + roundNr: activeRound + }) + .sort({ + scoreValue: -1 + }) + .toArray((err, result) => { if (err) { - resolve(500); // Internal server error + return; } + resolve(result); + }); + }); +} - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection) - .findOneAndUpdate( - { - _id: mongo.ObjectID(id) - }, - { $set: { tournamentPoints: tournamentPoints } }, - { returnNewDocument: true } - ) - .then(updatedDocument => { - resolve(updatedDocument); - }); - } - ); +function updateScore(client, id, placement, playerAmount) { + let tournamentPoints = (playerAmount - placement + 1) * 10; + + return new Promise((resolve, reject) => { + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection(collection) + .findOneAndUpdate( + { + _id: mongo.ObjectID(id) + }, + { $set: { tournamentPoints: tournamentPoints } }, + { returnNewDocument: true } + ) + .then(updatedDocument => { + resolve(updatedDocument); + }); }); } diff --git a/backend/api/tournament.js b/backend/api/tournament.js index d01943897295b1acf1a9b12129f5d8a419c62394..75fca82ff4723dcf547206643122d5ba1e2213fc 100644 --- a/backend/api/tournament.js +++ b/backend/api/tournament.js @@ -340,283 +340,334 @@ router.get("/player/:userid/:active", (req, res) => { ); }); -router.post("/roundcheck", (req, res) => { - let tournament; - if (tournament == null) { - res.status(400).send("ID missing"); +router.put("/roundcheck/:tournamentId", (req, res) => { + let tournament = null; + try { + tournament = mongo.ObjectID(req.params.tournamentId); + } catch (error) { + res.status(400).send("No document with specified id was found"); return; } - try { - tournament = mongo.ObjectID(req.body.tournamentid); - } catch (err) { - res.status(400).send("Invalid ID"); + if (!tournament) { + res.status(400).send("Request body not valid"); return; } - getCurrentRound(tournament).then(roundResult => { - if (roundResult == 500) { - res.sendStatus(500); - return; - } - const active = roundResult; - checkRounds(tournament, active).then(result => { - if (!result) { - res.status(204).send("Round " + active + " in progress"); //1. check if other rounds are done - } else { - fetchRounds(tournament, active).then(rounds => { - players = rounds.length; - for (let i = 0; i < rounds.length; i++) { - let round = rounds[i]; - updateScore(round._id, i + 1, rounds.length); - } - updateTournamentRound(tournament).then(result => { - if (result.value) { - tourny = result.value; - const game = - tourny.games[tourny.currentRound % tourny.games.length]; - const date = new Date(); - date.setTime( - date.getTime() + tourny.timePerRound * 60 * 60 * 1000 - ); - for (let i = 0; i < tourny.players.length; i++) { - newRound( - tournament, - tourny.players[i], - game, - tourny.currentRound + 1, - date - ); - } - res.json(tourny); - client.close(); - } else { - res.status(200).send("Tournament over"); + MongoClient.connect( + connectionUrl, + { useNewUrlParser: true, useUnifiedTopology: true }, + (err, client) => { + // Unable to connect to database + if (err) { + res.sendStatus(500); // Internal server error + return; + } + + getCurrentRound(client, tournament).then(roundResult => { + // gets the current round of the tournament + if (roundResult == 500) { + res.sendStatus(500); + return; + } + const active = roundResult.currentRound; + const start = roundResult.startTime; + const maxPlayers = roundResult.maxPlayers; + + fetchRounds(client, tournament, active).then(rounds => { + // fetched the round objects of the active round + checkRounds(rounds, start, maxPlayers, active).then(resultarray => { + // check to find out if [0] tournament will move on, [1] which players are timed out + // [2] which players are still in the tournament + let result = resultarray[0]; + let timedOut = resultarray[1]; + let left = resultarray[2]; + let end = false; + if ((left.length < 2 && active != 1) || left.length == 0) { + end = true; + //if there are less then two people left in the first round, or zero people left in the first round + //the tournament ends } + timeOut(client, tournament, timedOut, end, active).then( + //updates the state of the tournament and ends it if necessary + //TODO: notify the players that the tournament ended + timeoutresult => { + if (end) { + res.json({ + active: false, + nextRound: false + }); + return; + } + if (result) { + //Tournament moves on to next round + let eligibleRounds = []; + for (let i = 0; i < rounds.length; i++) { + let round = rounds[i]; + if (round.hasPlayed === true) { + eligibleRounds.push(round); + } + } + //Here we find the rounds that were actually completed + for (let i = 0; i < eligibleRounds.length; i++) { + let round = eligibleRounds[i]; + updateScore( + client, + round._id, + i + 1, + eligibleRounds.length + ); + //And here we update the tournament score + } + updateTournamentRound(client, tournament).then(result => { + //updates the tournaments round if there are more rounds left + if (result.value) { + tourny = result.value; + const game = + tourny.games[tourny.currentRound % tourny.games.length]; + const date = new Date(); + date.setTime( + date.getTime() + tourny.timePerRound * 60 * 60 * 1000 + ); + for (let i = 0; i < tourny.players.length; i++) { + newRound( + client, + tournament, + tourny.players[i], + game, + tourny.currentRound, + date + ); + // created new rounds for all the players left + } + res.json({ + active: true, + nextRound: true + }); + } else { + // if the tournament now is done we simply end it + endTournament(client, tournament); + res.json({ + active: false, + nextRound: false + }); + return; + } + }); + } else { + res.json({ + active: true, + nextRound: false + }); // Tournament still waiting for other players + } + } + ); }); }); - } - - //2.1 Get rounds by roundnr, sort by score - //2.2 for each entry in fetched array, call the update method. - //3. set tournamentplayer score - }); - }); + }); + } + ); }); -function getCurrentRound(tournamentId) { +function endTournament(client, tournament) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "tournaments"; + db.collection(collection).updateOne( + { _id: tournament }, + { $set: { active: false } }, + (err, result) => { if (err) { resolve(500); // Internal server error return; } - const db = client.db("gameWare"); - const collection = "tournaments"; - - db.collection(collection).findOne( - { - _id: tournamentId - }, - { currentRound: 1 }, - (err, result) => { - if (err) { - resolve(500); // Internal server error - return; - } - resolve(result); - return; - } - ); + resolve(result); + return; } ); }); } -function newRound(tournamentId, playerId, gameId, roundNr, deadlineDate) { +function timeOut(client, tournament, players, end) { + if (!players) { + players = []; + } return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + db.collection("tournaments").updateOne( + { + _id: tournament + }, + { + $pull: { players: { $in: players } }, + $inc: { currentPlayers: -players.length }, + $set: { active: !end } + }, + (err, result) => { if (err) { - res.sendStatus(500); // Internal server error + resolve(500); // Internal server error return; } - - // Using the database gameWare and collection rounds - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection).insertOne( - { - tournamentId: mongo.ObjectID(tournamentId), - playerId: mongo.ObjectID(playerId), - gameId: mongo.ObjectID(gameId), - roundNr: roundNr, - deadlineDate: deadlineDate, - scoreValue: 0, - hasPlayed: false, - tournamentPoints: 0 - }, - (err, result) => { - if (err) { - resolve(500); // Internal server error - return; - } - resolve(result.ops[0]); - } - ); + resolve(result); + return; } ); }); } -function updateTournamentRound(tournament) { +function getCurrentRound(client, tournamentId) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "tournaments"; + + db.collection(collection).findOne( + { + _id: tournamentId + }, + { currentRound: 1, startTime: 1, maxPlayers: 1 }, + (err, result) => { if (err) { resolve(500); // Internal server error + return; } - - const db = client.db("gameWare"); - const collection = "tournaments"; - - db.collection(collection) - .findOneAndUpdate( - { - _id: tournament, - $expr: { - $lt: ["$currentRound", "$totalGames"] - } - }, - { - $inc: { currentRound: 1 } - }, - { - returnNewDocument: true - } - ) - .then(updatedDocument => { - resolve(updatedDocument); - }); + resolve(result); + return; } ); }); } -function checkRounds(tournament, active) { +function newRound( + client, + tournamentId, + playerId, + gameId, + roundNr, + deadlineDate +) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + // Using the database gameWare and collection rounds + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection(collection).insertOne( + { + tournamentId: mongo.ObjectID(tournamentId), + playerId: mongo.ObjectID(playerId), + gameId: mongo.ObjectID(gameId), + roundNr: roundNr, + deadlineDate: deadlineDate, + scoreValue: 0, + hasPlayed: false, + tournamentPoints: 0 + }, + (err, result) => { if (err) { resolve(500); // Internal server error + return; } - - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection) - .find( - { - tournamentId: tournament, - roundNr: active - }, - { - hasPlayed: 1 - } - ) - .toArray((err, result) => { - if (err) { - resolve(500); - return; - } - let complete = true; - for (let i = 0; i < result.length; i++) { - if (result[i].hasPlayed === false) { - complete = false; - } - } - resolve(complete); - }); + resolve(result.ops[0]); } ); }); } -function fetchRounds(tournamentId, activeRound) { +function updateTournamentRound(client, tournament) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database - if (err) { - resolve(500); // Internal server error + const db = client.db("gameWare"); + const collection = "tournaments"; + + db.collection(collection) + .findOneAndUpdate( + { + _id: tournament, + $expr: { + $lt: ["$currentRound", "$totalGames"] + } + }, + { + $inc: { currentRound: 1 } + }, + { + returnOriginal: false } + ) + .then(updatedDocument => { + resolve(updatedDocument); + }); + }); +} - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection("rounds") - .find({ - tournamentId: tournamentId, - roundNr: activeRound - }) - .sort({ - scoreValue: -1 - }) - .toArray((err, result) => { - if (err) { - return; - } - resolve(result); - }); +function checkRounds(result, start, max, roundNr) { + return new Promise((resolve, reject) => { + let timedOut = []; + let left = []; + let amountComplete = 0; + for (let i = 0; i < result.length; i++) { + if (result[i].hasPlayed === true) { + amountComplete++; + left.push(result[i].playerId); + } else if (new Date(result[i].deadlineDate) <= new Date()) { + timedOut.push(result[i].playerId); + } else { + left.push(result[i].playerId); } - ); + } + if (amountComplete == max) { + // All players have completed their round + resolve([true, [], left]); + } else if (amountComplete > 1 && new Date() >= start && roundNr == 1) { + // more than two have completed first round + // startDelay passed. + resolve([true, timedOut, left]); + } else if (amountComplete + timedOut.length == max && amountComplete >= 2) { + // More than two players have completed round and all players have either completed their round or timed out. + resolve([true, timedOut, left]); + } else { + resolve([false, timedOut, left]); + } }); } -function updateScore(id, placement, playerAmount) { - let tournamentPoints = (playerAmount - placement + 1) * 10; - +function fetchRounds(client, tournamentId, activeRound) { return new Promise((resolve, reject) => { - MongoClient.connect( - connectionUrl, - { useNewUrlParser: true, useUnifiedTopology: true }, - (err, client) => { - // Unable to connect to database + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection("rounds") + .find({ + tournamentId: tournamentId, + roundNr: activeRound + }) + .sort({ + scoreValue: -1 + }) + .toArray((err, result) => { if (err) { - resolve(500); // Internal server error + return; } + resolve(result); + }); + }); +} - const db = client.db("gameWare"); - const collection = "rounds"; - - db.collection(collection) - .findOneAndUpdate( - { - _id: mongo.ObjectID(id) - }, - { $set: { tournamentPoints: tournamentPoints } }, - { returnNewDocument: true } - ) - .then(updatedDocument => { - resolve(updatedDocument); - }); - } - ); +function updateScore(client, id, placement, playerAmount) { + let tournamentPoints = (playerAmount - placement + 1) * 10; + + return new Promise((resolve, reject) => { + const db = client.db("gameWare"); + const collection = "rounds"; + + db.collection(collection) + .findOneAndUpdate( + { + _id: mongo.ObjectID(id) + }, + { $set: { tournamentPoints: tournamentPoints } }, + { returnNewDocument: true } + ) + .then(updatedDocument => { + resolve(updatedDocument); + }); }); }