forked from locationestimatr/locationestimatr.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scores.js
69 lines (58 loc) · 1.67 KB
/
Scores.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Scores {
constructor(key = "scores") {
this.key = key;
this.localScores = [];
if (localStorage.getItem(key) === null) {
this.saveLocal(this.localScores);
}
else {
this.localScores = this.updateLocal();
}
this.db = firebase.firestore();
}
async getGlobalHighScores(map, rules, n = 20) {
let records = await this.db
.collection("scores")
.where("map", "==", map)
// .where("rules", "==", rules)
.orderBy("totalScore", "desc")
// .orderBy("time")
.limit(n)
.get();
let results = [];
records.forEach(record => {
results.push(record.data());
});
return results;
}
getLocalHighScores(map, rules, n = 10) {
return this.localScores
// .filter(score => this.equals(score, rules))
.filter(score => score.map === map)
.sort((a, b) => b.totalScore - a.totalScore)
.slice(0, n);
}
equals(objectA, objectB) {
for (let key in objectA) {
if (objectB.hasOwnProperty(key) && objectA[key] !== objectB[key])
return false
}
return true;
}
saveLocal(object = this.localScores) {
localStorage[this.key] = JSON.stringify(object);
}
addLocal(score) {
this.localScores.push(score);
this.saveLocal();
}
addGlobal(score) {
return this.db.collection("scores").add(score);
}
getLocalScores() {
return this.localScores;
}
updateLocal() {
return JSON.parse(localStorage[this.key]);
}
}