-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.js
325 lines (306 loc) · 9.56 KB
/
players.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const url = "https://mcsrranked.com/api";
let matches = [];
if (localStorage.getItem("prefers-light-theme") === "true")
document.querySelector("html").classList.remove("dark");
document.querySelectorAll(".card").length > 0
? document
.querySelectorAll(".card")
.forEach(card => card.classList.add("visually-hidden"))
: "";
function clearRows() {
if (document.querySelectorAll(".tr").length > 0)
document.querySelectorAll(".tr").forEach(row => row.remove());
}
const q = new URLSearchParams(new URL(window.location.href).search).get("q");
window.onload = async () => {
generateChart();
if (q) {
document.querySelector("#players").value = q;
matches = [];
await getStats(q);
await getMatches(q);
}
};
playerSuggestions();
document.querySelector("#players").addEventListener("keydown", async e => {
if (e.key === "Enter") {
clearRows();
const player = document.querySelector("#players").value;
matches = [];
await getStats(player);
await getMatches(player);
}
});
document.querySelector("#submit").onclick = async () => {
matches = [];
clearRows();
const player = document.querySelector("#players").value;
await getStats(player);
await getMatches(player);
};
async function getStats(user) {
let res = await fetch(`${url}/users/${user}`);
res = await res.json();
res = res.data;
const tr = document.createElement("tr");
tr.innerHTML = `<td> ${res["total_played"]} </td> <td> ${(
(res.records[2].win /
(res.records[2].win + res.records[2].lose + res.records[2].draw)) *
100
).toFixed(1)}% </td> <td style="color: ${colorElo(
res["elo_rate"]
)}" title="${addRank(res["elo_rate"])}"> ${res["elo_rate"]} </td> <td> #${
res["elo_rank"]
}`;
tr.classList.add("border-bottom");
tr.classList.add("tr");
document.querySelector("#stats").appendChild(tr);
const tr3 = document.createElement("tr");
tr3.innerHTML = `<th> best W/S </th> <th> current W/S </th> <th> best time </th> <th> last online </th>`;
tr3.classList.add("border-bottom");
tr3.classList.add("tr");
document.querySelector("#stats").appendChild(tr3);
const tr2 = document.createElement("tr");
tr2.innerHTML = `<td> ${res["highest_winstreak"]} </td> <td> ${
res["current_winstreak"]
} </td> <td> ${new Date(res["best_record_time"])
.toISOString()
.slice(14, 23)} </td> <td> ${format(res["latest_time"] * 1e3)}`;
tr2.classList.add("border-bottom");
tr2.classList.add("tr");
document.querySelector("#stats").appendChild(tr2);
document.querySelector("#youtube").classList.remove("visually-hidden");
document.querySelector("#twitch").classList.remove("visually-hidden");
document.querySelector("#discord").classList.remove("visually-hidden");
document.querySelector("#youtube").href = res.connections.youtube
? `https://youtube.com/channel/${res.connections.youtube.id}`
: document.querySelector("#youtube").classList.add("visually-hidden");
document.querySelector("#twitch").href = res.connections.twitch
? `https://twitch.com/${res.connections.twitch.name}`
: document.querySelector("#twitch").classList.add("visually-hidden");
if (res.connections.discord) {
document
.querySelector("#discord")
.addEventListener("click", () => alert(res.connections.discord.name));
} else {
document.querySelector("#discord").classList.add("visually-hidden");
}
document.querySelector("#player-title").innerText = `• stats for ${user}:`;
}
async function getUUID(user) {
let res = await fetch(`https://api.minetools.eu/uuid/${user}`);
res = await res.json();
return res.id;
}
async function getMatches(user) {
const uuid = await getUUID(user);
const offset = document.querySelectorAll(".match").length / 20;
// filter=2 is temporary
if (Number.isInteger(offset)) {
let res = await fetch(
`${url}/users/${user}/matches?page=${offset}&filter=2`
);
res = await res.json();
res = res.data;
res.forEach(match => {
if (!match["is_decay"]) {
matches.push(match);
let didWin = "";
match.members.forEach(member => {
if (user === member.nickname) {
didWin = member.uuid === match.winner ? "won" : "lost";
}
});
const opponents = listOpponents(uuid, match.members);
const tr = document.createElement("tr");
const finalTime = match.forfeit
? "<i> forfeit </i>"
: didWin === "lost"
? "-"
: new Date(match["final_time"]).toISOString().slice(14, 23);
tr.innerHTML = `<td> ${opponents} </td> <td style="color: ${colorResult(
didWin
)}"> ${didWin} </td> <td> ${finalTime} </td> <td> ${format(
match["match_date"] * 1e3
)} </td> <td> <a href="./matches.html?q=${
match["match_id"]
}" class="link"> more... </a> </td>`;
tr.classList.add("border-bottom");
tr.classList.add("tr");
tr.classList.add("match");
document.querySelector("#matches").appendChild(tr);
}
});
}
await updateChart(user);
}
function colorResult(result) {
return result === "won" ? "#55ff00" : result === "lost" ? "#ff0055" : "";
}
function listOpponents(main, members) {
const opponents = [];
members.forEach(member => {
if (member.uuid !== main) {
opponents.push(
`<a href="./players.html?q=${member.nickname}" class="link"> ${member.nickname}</a>`
);
}
});
return opponents;
}
function colorElo(elo) {
return elo > 0 && elo < 599
? "#252525"
: (elo > 600) & (elo < 1199)
? "#E3E3E3"
: elo > 1200 && elo < 1799
? "#FFD700"
: elo > 1800
? "#99EBFF"
: "";
}
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"
: "";
}
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 generateChart() {
const data = {
labels: 0,
datasets: [
{
label: "Elo",
tension: 0.2,
borderColor: "#222",
},
],
};
const config = {
type: "line",
data: data,
options: {
legend: { display: false },
plugins: {
title: {
display: true,
text: "elo over the past few games",
},
},
scales: {
x: {
title: {
display: true,
text: "games",
},
},
y: {
title: {
display: true,
text: "elo",
},
},
},
},
};
new Chart(document.querySelector("canvas"), config);
}
async function updateChart(user) {
const uuid = await getUUID(user);
const newURL = new URL(window.location.href);
newURL.search = new URLSearchParams({ q: user });
window.history.pushState(null, null, newURL);
const elo = [];
console.log(matches);
matches.forEach(match => {
if (!match["is_decay"]) {
match["score_changes"].forEach(member => {
if (uuid === member.uuid) {
elo.push(member.score);
}
});
}
});
elo.reverse();
Chart.getChart(document.querySelector("canvas")).data.datasets[0].data = elo;
Chart.getChart(document.querySelector("canvas")).data.labels = [
...Array(matches.length).keys(),
].map(i => i + 1);
Chart.getChart(document.querySelector("canvas")).update();
if (document.querySelectorAll(".card").length > 0)
document
.querySelectorAll(".card")
.forEach(card => card.classList.remove("visually-hidden"));
}
function downloadChart() {
const base64URL = Chart.getChart(
document.querySelector("canvas")
).toBase64Image();
const dl = document.createElement("a");
dl.href = base64URL;
dl.download = "elo-chart.png";
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
}
async function playerSuggestions() {
let res = await fetch(`${url}/leaderboard`);
res = await res.json();
res = res.data.users;
res.forEach(player => {
const option = document.createElement("option");
option.value = player.nickname;
option.innerText = player.nickname;
document.querySelector("#suggestions").appendChild(option);
});
}
function manageTheme() {
localStorage.setItem(
"prefers-light-theme",
!document.querySelector("html").classList.contains("dark")
);
}