Skip to content
Snippets Groups Projects

Resolve "Get tournamentscore"

Merged Tobias Ingebrigt Ørstad requested to merge 46-get-tournamentscore into dev
1 file
+ 110
2
Compare changes
  • Side-by-side
  • Inline
+ 110
2
@@ -4,7 +4,7 @@ const mongo = require("mongodb");
const MongoClient = mongo.MongoClient;
const connectionUrl = process.env.MONGO_CONNECTION_STRING;
router.get("/:roundid", (req, res) => {
router.get("round/:roundid", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
@@ -78,7 +78,115 @@ function updateHighscore(gameid, userid, value) {
});
}
router.get("/:tournamentId/:playerId/:roundNr", (req, res) => {
router.get("/latestpoints/:tournamentId/:playerId", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
// Unable to connect to database
if (err) {
res.sendStatus(500); // Internal server error
return;
}
// Using the database gameWare and collection rounds
const db = client.db("gameWare");
const collection = "rounds";
try {
tournamentid = mongo.ObjectID(req.params.tournamentId); // get tournamentId
playerid = mongo.ObjectID(req.params.playerId); // get playerId
} catch (error) {
res.status(404).send("Invalid IDs");
return;
}
db.collection(collection)
.find({
tournamentId: tournamentid,
playerId: playerid,
tournamentPoints: { $gt: 0 }
})
.sort({ roundNr: -1 })
.limit(1)
.project({ tournamentPoints: 1 })
.toArray((err, result) => {
if (err) {
res.sendStatus(500);
//console.log(err);
return;
}
res.json(result);
});
}
);
});
router.get("/tournamentpoints/:tournamentId/:playerId", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
// Unable to connect to database
if (err) {
res.sendStatus(500); // Internal server error
return;
}
// Using the database gameWare and collection rounds
const db = client.db("gameWare");
const collection = "rounds";
try {
tournamentid = mongo.ObjectID(req.params.tournamentId); // get tournamentId
playerid = mongo.ObjectID(req.params.playerId); // get playerId
} catch (error) {
res.status(404).send("Invalid IDs");
return;
}
//console.log(tournamentid + playerid);
/*db.collection(collection).aggregate(
[
{
$match: { tournamentId: tournamentid, playerId: playerid }
},
{
$group: {
_id: null,
tps: { $sum: 1 }
}
}
],
(err, result) => {
if (err) {
res.json(500);
return;
}
console.log(result.hasNext());
return;
}
);*/
db.collection(collection)
.find({
tournamentId: tournamentid,
playerId: playerid
})
.toArray((err, result) => {
if (err) {
res.sendStatus(500);
//console.log(err);
return;
}
let sum = 0;
for (let i = 0; i < result.length; i++) {
sum += result[i].tournamentPoints;
}
res.json({ tournamentPoints: sum });
});
}
);
});
router.get("specific/:tournamentId/:playerId/:roundNr", (req, res) => {
// Connect to database
MongoClient.connect(
connectionUrl,
Loading