This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
/
leaderboard.js
72 lines (65 loc) · 1.89 KB
/
leaderboard.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
70
71
72
async function leaderboard(engine, score) {
const LB_SERVER = "https://misguided.enterprises/gamelabscores/";
const hash = engine.gameHash;
const res = await fetch(LB_SERVER, {
method: "post",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ score, hash }),
}).then((x) => x.json());
let lb = await fetch(LB_SERVER + hash).then((x) => x.json());
let scores = window.Object.entries(lb);
scores.sort(([, a], [, b]) => b.score - a.score);
scores = scores.splice(0, 5);
engine.ctx.globalAlpha = 0.66;
engine.ctx.fillRect(0, 0, engine.canvas.width, engine.canvas.height);
clearText(engine.canvas.parentNode);
let x = engine.canvas.width * 0.5;
let y = engine.canvas.height * 0.2;
let size = engine.canvas.height * 0.1;
engine.addText(
(() => {
if (res.prev && res.prev > score) return "PERSONAL BEST";
else if (scores.length) return "LEADERBOARD";
else return "NO SCORES :(";
})(),
x,
y,
{ size }
);
y += size + 5;
for (const [slack_id, { name, score }] of scores) {
let opts = {
size: size - 4,
href:
"https://app.slack.com/client/T0266FRGM/C0266FRGV/user_profile/" +
slack_id,
newTab: true,
color: "blue",
};
engine.addText(name.substring(0, 3), x - 50, y, opts);
delete opts.href;
delete opts.color;
engine.addText(score, x + 50, y, opts);
y += size;
}
if (res.err)
engine.addText("login to add your " + score, x, y, {
size: size - 4,
color: "blue",
href:
LB_SERVER +
"login/" +
"?from=" +
encodeURIComponent(window.location) +
"&score=" +
encodeURIComponent(score) +
"&hash=" +
encodeURIComponent(hash),
});
if (res.prev) {
engine.addText(`score: ${score}, best: ${res.prev}`, x, y, {
size: size - 4,
});
}
}