-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighscores.js
More file actions
41 lines (32 loc) · 846 Bytes
/
Copy pathhighscores.js
File metadata and controls
41 lines (32 loc) · 846 Bytes
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
"use strict";
var CONFIG_FILE = ".data/highscores.json";
var NUM_HIGHSCORES = 30;
// Loading the data in the config file
var fs = require("fs");
var scores = JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8"));
function getScores() {
return scores || [];
}
function insertScore(scores, score) {
var ixNew = -1;
var i = 0;
while(i < scores.length &&
(score.level < scores[i].level ||
(score.level == scores[i].level && score.score <= scores[i].score)))
i++;
if (i < NUM_HIGHSCORES) {
scores.splice(i, 0, score);
ixNew = i;
scores = scores.slice(0, NUM_HIGHSCORES);
}
return ixNew;
}
function save() {
fs.writeFileSync(".data/highscores.json", JSON.stringify(scores));
}
module.exports = {
NUM_HIGHSCORES: NUM_HIGHSCORES,
getScores: getScores,
insertScore: insertScore,
save: save,
};