diff --git a/backend/api/highscores.js b/backend/api/highscores.js index e0d3306fe073494e058f0f94711498b6c4a259bb..5517e3e55ac56fcb66fd8919124605166c26ab70 100644 --- a/backend/api/highscores.js +++ b/backend/api/highscores.js @@ -45,8 +45,10 @@ router.get("/gamescore/:playerId/:gameId", (req, res) => { //console.log(err); return; } - res.json(result); - client.close(); + addNameToResult(result, db).then(objects => { + res.json(objects); + client.close(); + }); }); } ); @@ -193,6 +195,7 @@ router.get("/gamescores/:gameId", (req, res) => { if (gameId == null) { res.status(400).send("Fields missing"); + return; } db.collection(collection) @@ -208,12 +211,49 @@ router.get("/gamescores/:gameId", (req, res) => { //console.log(err); return; } - res.json(result); - client.close(); + addNameToResult(result, db).then(objects => { + res.json(objects); + client.close(); + }); }); } ); }); +function addNameToResult(result, db) { + return new Promise((resolve, reject) => { + const playerIds = result.map(o => o.playerId); + db.collection("players") + .find({ + _id: { $in: playerIds } + }) + .project({ name: 1 }) + .toArray((err, nameresult) => { + if (err) { + res.sendStatus(500); + //console.log(err); + return; + } + nameresult = nameresult.map(doc => { + // rename _id => playerId + doc.playerId = doc._id; + delete doc._id; + return doc; + }); + let objects = []; + for (let i = 0; i < result.length; i++) { + objects.push({ + ...result[i], + ...nameresult.find( + itmInner => + String(itmInner.playerId) === String(result[i].playerId) + ) + }); + } + resolve(objects); + }); + }); +} + // Export API routes module.exports = router; diff --git a/frontend/core/src/com/gameware/game/models/Highscore.java b/frontend/core/src/com/gameware/game/models/Highscore.java index 52e92707305ba5314ef056d28a0e5e9387b14c85..242fd987acddfba4149e18e9c627ee75feb2966f 100644 --- a/frontend/core/src/com/gameware/game/models/Highscore.java +++ b/frontend/core/src/com/gameware/game/models/Highscore.java @@ -6,15 +6,17 @@ public class Highscore { private String _id; private String gameId; private String playerId; + private String name; private double value; public Highscore() { } - public Highscore(String _id, String gameId, String userId, double value) { + public Highscore(String _id, String gameId, String userId, String name, double value) { this._id = _id; this.gameId = gameId; this.playerId = userId; + this.name = name; this.value = value; } @@ -30,6 +32,10 @@ public class Highscore { return playerId; } + public String getName() { + return name; + } + public double getValue() { return value; } @@ -38,6 +44,7 @@ public class Highscore { _id = null; gameId = null; playerId = null; + name = null; value = Double.parseDouble(null); }