This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
forked from iamkun/tower_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscore.js
55 lines (48 loc) · 1.64 KB
/
highscore.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
let PLAYERS = {};
function getHighscoreFor(addr) {
return PLAYERS[addr] ? PLAYERS[addr].score : 0;
}
function setHighscoreFor(addr, name, score) {
if (getHighscoreFor(addr) < score) {
PLAYERS[addr] = {'name': name, 'score': score}
return true
}
return false
}
function updateSelfHighscore(score) {
const addr = window.webxdc.selfAddr;
const name = window.webxdc.selfName;
if (setHighscoreFor(addr, name, score)) {
const info = name + ' scored ' + score + ' in Tower Builder!';
const payload = {addr: addr, name: name, score: score};
const summary = 'Top builder is ' + getAllHighscores().shift().name;
window.webxdc.sendUpdate({payload: payload, summary: summary, info: info}, info);
}
}
function getAllHighscores() {
return Object.keys(PLAYERS).map((addr) => {
const player = PLAYERS[addr];
player.addr = addr;
return player;
}).sort((a, b) => b.score - a.score);
}
function escapeHtml(raw) {
return raw.replace(/[&<>"']/g, function onReplace(match) {
return '&#' + match.charCodeAt(0) + ';';
});
}
function getAllHighscoresHtml() {
var ret = "Great Builders:"
var cnt = 0
getAllHighscores().forEach(item => {
if (cnt < 5) {
const name = item.name.length > 17 ? item.name.substring(0, 15) + '…' : item.name;
ret += "<br>"
ret += cnt === 0 ? '<span style="color:#fcbd6d; text-shadow: .05em .05em white;">' : ''
ret += escapeHtml(name) + " - " + escapeHtml(item.score + '')
ret += cnt === 0 ? '</span>' : ''
cnt += 1
}
});
return ret;
}