-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscoreboard.js
50 lines (45 loc) · 1.53 KB
/
scoreboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { Score } = require('./collections/scores');
const { validateScore } = require('./validator');
async function getGameOptions() {
let games = await Score.findGameOptions();
return games;
}
async function insertNewScore(game, name, score, metaData, validation = "") {
return await new Promise((resolve, reject) => {
if (validateScore(game, name, score, metaData, validation)) {
let scoreObj = Score.create({
game,
name,
score,
metaData,
date: Date.now()
});
let higherLower = getScoresHigherAndLowerThan(game, score);
Promise.all([scoreObj, higherLower]).then((results) => {
return resolve({
score: results[0],
...results[1]
});
});
} else {
console.log(`Score submission for ${game} for name ${name} failed to validate and was rejected`);
return resolve(false);
}
});
}
async function getGame(game, limit, ascending, page) {
return await Score.findByGame(game, limit, ascending, page);
}
async function getScoresHigherAndLowerThan(game, score){
let scoresGreater = await Score.find({game, score: {"$gt": score}}).countDocuments();
let scoresLesser = await Score.find({game, score: {"$lt": score}}).countDocuments();
return {
scoresGreater,
scoresLesser
};
}
module.exports = {
getGameOptions,
insertNewScore,
getGame
};