-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
112 lines (104 loc) · 3.12 KB
/
script.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const url = "https://mcsrranked.com/api";
if (localStorage.getItem("prefers-light-theme") === "true")
document.querySelector("html").classList.remove("dark");
window.onload = () => {
getEloLeaderboard();
getRunLeaderboard();
};
async function getEloLeaderboard() {
let res = await fetch(`${url}/leaderboard`);
res = await res.json();
res = res.data.users;
res.forEach(row => {
const tr = document.createElement("tr");
tr.innerHTML = `<td> ${row["elo_rank"]} </td> <td> <a href="./players.html?q=${
row.nickname
}" class="link"> ${row.nickname} </a> </td> <td style="color: ${colorElo(
row["elo_rate"]
)}" title="${addRank(row["elo_rate"])}"> ${row["elo_rate"]} </td>`;
tr.classList.add("border-bottom");
document.querySelector("#elo-table").appendChild(tr);
});
}
async function getRunLeaderboard() {
let res = await fetch(`${url}/record-leaderboard`);
res = await res.json();
res = res.data;
res.forEach(row => {
const tr = document.createElement("tr");
tr.innerHTML = `<td> ${row["final_time_rank"]} </td> <td> <a href="./players.html?q=${
row.user.nickname
}" class="link"> ${row.user.nickname} </a> </td> <td> ${new Date(row["final_time"])
.toISOString()
.slice(14, 23)} </td> <td> ${format(row["match_date"] * 1e3)} </td>`;
tr.classList.add("border-bottom");
document.querySelector("#runs-table").appendChild(tr);
});
}
function format(timeStamp) {
const rtf = new Intl.RelativeTimeFormat("en-US", {
numeric: "auto",
style: "narrow",
});
const time = [
{ label: "year", amount: 31536e6 },
{ label: "month", amount: 2592e6 },
{ label: "week", amount: 3456e5 },
{ label: "day", amount: 864e5 },
{ label: "hour", amount: 36e5 },
{ label: "minute", amount: 6e4 },
{ label: "second", amount: 1e3 },
];
const diff = Date.now() - timeStamp;
for (const measure of time) {
if (measure.amount < Math.abs(diff)) {
const count = Math.round(diff / measure.amount) * -1;
return rtf.format(count, measure.label);
}
}
return rtf.format(0, "second");
}
function colorElo(elo) {
return elo > 0 && elo < 599
? "#252525"
: (elo > 600) & (elo < 1199)
? "#E3E3E3"
: elo > 1200 && elo < 1799
? "#FFD700"
: elo > 1800
? "#99EBFF"
: null;
}
function addRank(elo) {
return elo > 0 && elo < 199
? "coal I"
: elo > 200 && elo < 399
? "coal II"
: elo > 400 && elo < 599
? "coal III"
: elo > 600 && elo < 799
? "iron I"
: elo > 800 && elo < 999
? "iron II"
: elo > 1000 && elo < 1199
? "iron III"
: elo > 1200 && elo < 1399
? "gold I"
: elo > 1400 && elo < 1599
? "gold II"
: elo > 1600 && elo < 1799
? "gold III"
: elo > 1800 && elo < 1999
? "diamond I"
: (elo > 2000) & (elo < 2199)
? "diamond II"
: elo > 2200 && elo < 2399
? "diamond III"
: null;
}
function manageTheme() {
localStorage.setItem(
"prefers-light-theme",
!document.querySelector("html").classList.contains("dark")
);
}