-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
569 lines (430 loc) · 19.6 KB
/
index.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Format: YEAR, MONTH, DAY, (HOUR, MINUTE, SECOND)
// Remember: MONTH starts at 0 for January!
let dateLimit = new Date(2020, 0, 31);
// PMD Category extensions take forever to load.
// Set this to "true" or "false" to enable or disable loading those.
let includePmdCategoryExtensions = false;
let hardDateLimit = new Date(2019, 9, 5);
let mainSeriesUrl = "https://www.speedrun.com/api/v1/series/pokemon/games?_bulk=yes&orderby=released";
let fanGamesSeriesUrl = "https://www.speedrun.com/api/v1/series/pkmnfangames/games?_bulk=yes&orderby=released";
let pokkenGamesSeriesUrl = "https://www.speedrun.com/api/v1/series/pokken_tournament_series/games?_bulk=yes&orderby=released";
let categoryExtensions = [
{id: "pkmnrbyext", names: {international: "Pokémon RBY Category Extensions "}, abbreviation: "pkmnrbyext", weblink: "https://www.speedrun.com/pkmnrbyext"},
{id: "pkmngscext", names: {international: "Pokémon GSC Category Extensions"}, abbreviation: "pkmngscext", weblink: "https://www.speedrun.com/pkmngscext"},
{id: "pkmngen3ext", names: {international: "Pokémon RSE/FRLG Category Extensions"}, abbreviation: "pkmngen3ext", weblink: "https://www.speedrun.com/pkmngen3ext"},
{id: "pkmngen4ext", names: {international: "Pokémon DPPt/HGSS Category Extensions"}, abbreviation: "pkmngen4ext", weblink: "https://www.speedrun.com/pkmngen4ext"},
{id: "pkmngen5ext", names: {international: "Pokémon BW/B2W2Category Extensions"}, abbreviation: "pkmngen5ext", weblink: "https://www.speedrun.com/pkmngen5ext"},
{id: "pkmngen6ext", names: {international: "Pokémon XY/ORAS Category Extensions"}, abbreviation: "pkmngen6ext", weblink: "https://www.speedrun.com/pkmngen6ext"},
{id: "pkmngen7ext", names: {international: "Pokémon SM/USUM/LGPE Category Extensions"}, abbreviation: "pkmngen7ext", weblink: "https://www.speedrun.com/pkmngen7ext"},
{id: "pkmngen8ext", names: {international: "Pokémon SWSH Category Extensions"}, abbreviation: "pkmngen8ext", weblink: "https://www.speedrun.com/pkmngen8ext"},
];
if (includePmdCategoryExtensions) {
console.log("PMD category extensions will be included. This may take a while!");
categoryExtensions.push({id: "pmdextensions", names: {international: "Pokémon Mystery Dungeon Category Extensions"}, abbreviation: "pkmngen7ext", weblink: "https://www.speedrun.com/pmdextensions"});
} else {
console.log("PMD category extensions will NOT be included.");
}
let gamesWithEmulatorHidden = [
"yd4qeg6e", // Pocket Monsters Stadium
"j1lqqj6g", // Pokémon Snap
"o6gnn812", // Pokémon Stadium
"m1zkk010", // Pokémon Stadium 2
"9d38801l", // Pokémon Ruby/Sapphire
"76r95468", // Pokémon Channel
"268w996p", // Pokémon Colosseum
"w6j7726j", // Pokémon FireRed/LeafGreen
"o1yjjv1q", // Pokémon Emerald
"9do8xk1p", // Pokémon XD: Gale of Darkness
"k6qwpm6g", // Pokémon Mystery Dungeon: Red/Blue Rescue Team
"yd4krg6e", // Pokémon Ranger
"3dxky51y", // Pokémon Diamond/Pearl
"3698w3dl", // Pokémon Battle Revolution
"76r34v68", // Pokémon Mystery Dungeon: Explorers of Time/Darkness
"nd2n3vd0", // Pokémon Ranger: Shadows of Almia
"46w2rl6r", // Pokémon Platinum
"j1lqnj6g", // Pokémon Mystery Dungeon: Explorers of Sky
"pdv29k6w", // Pokémon Rumble
"o6gnk812", // Pokémon HeartGold/SoulSilver
"pd0qn31e", // PokéPark Wii: Pikachu's Adventure
"4d757n67", // Pokémon Ranger: Guardian Signs
"y655576e", // Pokémon Black/White
"ldejwj13", // PokéPark 2: Wonders Beyond
"m1mne3d2", // Pokémon Conquest
"kdkmmx1m", // Pokémon Black 2/White 2
"v1pxlz68", // Pokémon Mystery Dungeon: Gates to Infinity
"pkmngen4ext", // Pokémon DPPt/HGSS Category Extensions
"pkmngen5ext", // Pokémon BW/B2W2 Category Extensions
"pkmngen6ext", // Pokémon XY/ORAS Category Extensions
"pkmngen7ext", // Pokémon SM/USUM/LGPE Category Extensions
"46w3vn71", // Pokémon CAWPS
];
let gamesWithEmulatorBanned = [
"m1z9v3d0", // Pokemon Rumble U
"4d7nnr67", // Pokémon X/Y
"nd288qd0", // Pokémon Omega Ruby/Alpha Sapphire
"kdk4ol1m", // Pokémon Sun/Moon
"lde39vl6", // Pokémon Ultra Sun/Ultra Moon
"j1llzoz1", // Pokémon Let's Go Pikachu/Eevee
"v1povxm6", // Pokémon Uranium
"pkmngen8ext", // Pokémon SWSH Category Extensions
];
let request = require("request");
let fs = require("fs");
let done = false;
/**
*
* @type {Array<{resolve: function, url: string}>}
*/
let requests = [];
/**
*
* @param url string
* @returns {Promise<object>}
*/
function enqueueRequest(url) {
return new Promise(resolve => {
requests.push({
resolve: resolve,
url: url,
});
});
}
function handleNextRequest() {
if (done) {
return;
}
if (requests.length === 0) {
return setTimeout(handleNextRequest, 50);
}
let r = requests.shift();
request(r.url, {
method: "GET",
headers: {
"User-Agent": "PSR Leaderboard Bot 0.1",
},
}, (error, response, body) => {
if (error || response.statusCode !== 200) {
requests.unshift(r);
} else {
r.resolve(JSON.parse(body));
}
setTimeout(handleNextRequest, 800);
});
}
function secondsToTimeExpression(seconds) {
let hours = Math.floor(seconds / 3600);
let minutes = Math.floor((seconds - hours * 3600) / 60);
seconds = seconds - hours * 3600 - minutes * 60;
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds.toLocaleString("en", {
minimumFractionDigits: 0,
maximumFractionDigits: 3,
});
} else {
seconds = seconds.toLocaleString("en", {
minimumFractionDigits: 0,
maximumFractionDigits: 3,
});
}
return `${hours}:${minutes}:${seconds}`;
}
function formatToDate(dateString) {
let date = new Date(dateString);
return date.toLocaleDateString("en", {
day: "2-digit",
month: "short",
year: "numeric",
});
}
function formatToDateTime(dateString) {
let date = new Date(dateString);
return date.toLocaleString("en", {
day: "2-digit",
month: "short",
year: "numeric",
hour12: false,
hour: "2-digit",
minute: "2-digit"
});
}
async function loadRunRunners(run) {
let runRunners = [];
for (const player of run.run.players) {
if (player.rel === "guest") {
runRunners.push({
type: "guest",
names: {
international: player.name,
}
});
} else {
if (!runners[player.id]) {
runners[player.id] = (await enqueueRequest(player.uri)).data;
console.log("Retrieved runner data: " + runners[player.id].names.international);
} else {
console.log("Backed up runner data: " + runners[player.id].names.international);
}
runRunners.push(runners[player.id]);
}
}
return runRunners;
}
handleNextRequest();
let results = {};
let runners = {};
try {
runners = JSON.parse(fs.readFileSync("./runners.json", "utf8"));
} catch (e) {
console.log("Runner file not found, creating a new one.");
}
(async () => {
console.log("Starting PSR Leaderboard Bot");
/**
* @type Array<object>
*/
let games = (await enqueueRequest(mainSeriesUrl)).data;
console.log("Loaded " + games.length + " games from Pokémon main series.");
games = games.concat(categoryExtensions);
console.log("\"Loaded\" " + categoryExtensions.length + " games as category extensions.");
let fanGames = (await enqueueRequest(fanGamesSeriesUrl)).data;
console.log("Loaded " + fanGames.length + " games from Pokémon Fan Games series.");
games = games.concat(fanGames);
let pokkenGames = (await enqueueRequest(pokkenGamesSeriesUrl)).data;
console.log("Loaded " + pokkenGames.length + " games from Pokkén Tournament series.");
games = games.concat(pokkenGames);
let categoryInfo = {};
for (let game of games) {
let emulatorBanned = gamesWithEmulatorBanned.indexOf(game.id) !== -1;
let emulatorHidden = gamesWithEmulatorHidden.indexOf(game.id) !== -1;
if (emulatorBanned) {
console.log("Emulators for this game are BANNED.");
} else if (emulatorHidden) {
console.log("Emulators for this game are HIDDEN.");
} else {
console.log("Emulators for this game are ALLOWED.");
}
console.log("Loading categories for: " + game.names.international);
let categories = (await enqueueRequest(`https://www.speedrun.com/api/v1/games/${game.id}/categories`)).data;
for (let category of categories) {
if (category.type !== "per-game") {
continue;
}
let variables = (await enqueueRequest(`https://www.speedrun.com/api/v1/categories/${category.id}/variables`)).data;
let variableList = [];
for (let variable of variables) {
if (variable["is-subcategory"]) {
variableList.push({
id: variable.id,
name: variable.name,
values: variable.values.choices,
});
}
}
let baseUrl = `https://www.speedrun.com/api/v1/leaderboards/${game.id}/category/${category.id}?`;
let urlList = [{
description: category.name,
url: baseUrl,
}];
for (let variable of variableList) {
let newUrlList = [];
for (let key in variable.values) {
if (!variable.values.hasOwnProperty(key)) {
continue;
}
for (let url of urlList) {
newUrlList.push({
description: url.description + " - " + variable.values[key],
url: url.url + "var-" + variable.id + "=" + key + "&",
});
}
}
urlList = newUrlList;
}
for (let url of urlList) {
categoryInfo[game.names.international] = categoryInfo[game.names.international] || {};
categoryInfo[game.names.international][url.description] = {
misc: category.miscellaneous,
wr: null,
};
console.log("Loading leaderboards for: " + game.names.international + " -- " + url.description);
let loadedUrl = url.url;
if (emulatorBanned) {
loadedUrl+= "emulators=0";
}
let runs = (await enqueueRequest(loadedUrl)).data.runs;
let wrPlace = 1;
for (let run of runs) {
if (!run.run.status || run.run.status.status !== "verified") {
continue;
}
if (run.place === wrPlace) {
if (emulatorHidden && run.run.system.platform.emulated) {
++wrPlace;
} else {
categoryInfo[game.names.international][url.description].wr = {
run: run,
runners: await loadRunRunners(run),
};
}
}
let date = new Date(run.run.date);
let submittedDate = new Date(run.run.submitted);
let addRunToList = false;
if (date < hardDateLimit) {
addRunToList = false
} else if (date > dateLimit || submittedDate > dateLimit) {
addRunToList = true;
} else if (run.run.status["verify-date"]) {
let verifyDate = new Date(run.run.status["verify-date"]);
if (verifyDate > dateLimit) {
addRunToList = true;
}
}
if (!addRunToList) {
continue;
}
console.log("New run to add (Rank " + run.place + ")");
results[game.names.international] = results[game.names.international] || {};
results[game.names.international][url.description] = results[game.names.international][url.description] || [];
let result = {
run: run,
runners: await loadRunRunners(run),
};
if (emulatorHidden) {
result.emuPlace = run.place;
} else {
result.place = run.place;
}
results[game.names.international][url.description].push(result);
}
if (!emulatorHidden || !results[game.names.international] || !results[game.names.international][url.description]) {
continue;
}
console.log("Loading leaderboards for (without emulator): " + game.names.international + " -- " + url.description);
loadedUrl = url.url + "&emulators=0";
runs = (await enqueueRequest(loadedUrl)).data.runs;
let currentResults = results[game.names.international][url.description];
for (let run of runs) {
for (let currentResult of currentResults) {
if (currentResult.run.run.id === run.run.id) {
currentResult.place = run.place;
break;
}
}
}
}
}
}
done = true;
function escapeHtml(text) {
if (!text) {
return "";
}
let map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
let html = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">\n" +
" <style>th {white-space: nowrap} img {height: 15px; display: inline-block; margin-right: 3px; margin-top: -3px;}h2 {margin-top: 35px;}</style>\n" +
" <title>Leaderboard Roundup</title>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"<div class=\"container-fluid\"><h1>Leaderboard roundup</h1>\n";
for (let game in results) {
if (!results.hasOwnProperty(game)) {
continue;
}
html+= "<h2>" + escapeHtml(game) + "</h2>";
html+= "<table class='table table-striped table-sm'>";
html+= "<thead>";
html+= "<tr><th>#</th><th>Runner(s)</th><th>Time</th><th>Run Date</th><th>Submission / Approval</th><th style='width: 50%'>Comment</th></tr>";
html+= "</thead>\n";
html+= "<tbody>";
for (let category in results[game]) {
if (!results[game].hasOwnProperty(category)) {
continue;
}
html+= "<tr><td class='align-middle' colspan='5' style='padding: 20px 15px;'><strong>" + escapeHtml(category) + "</strong>";
if (categoryInfo[game][category].misc) {
html+= " (Misc)";
}
html+= "</td>";
html+= "<td style='text-align: right; vertical-align: bottom; font-size: 0.9em;'>WR is <strong>" + secondsToTimeExpression(categoryInfo[game][category].wr.run.run.times.primary_t) + "</strong> by ";
for (let i = 0; i < categoryInfo[game][category].wr.runners.length; ++i) {
let runner = categoryInfo[game][category].wr.runners[i];
if (runner.type === "guest") {
html+= runner.names.international;
} else {
html += "<a href='" + runner.weblink + "'>" + (runner.names.international || runner.names.japanese) + "</a>";
if (i < runners.length - 1) {
html+= ", ";
}
}
}
html+= "</tr>\n";
for (let i = 0; i < results[game][category].length; ++i) {
let result = results[game][category][i];
let place = result.place;
let emuPlace = result.emuPlace;
let run = result.run;
let runners = results[game][category][i].runners;
if (place === 1) {
html+= "<tr class='table-info'>";
} else {
html+= "<tr>";
}
if (emuPlace) {
if (!place) {
place = "-";
}
html+= "<td class='align-middle' style='text-align: center'>" + place + "<br><span style='font-size: 0.75em'>Emu: " + emuPlace +"</span></td><td class='align-middle'>";
} else {
html+= "<td class='align-middle'>" + place + "</td><td class='align-middle'>";
}
for (let j = 0; j < runners.length; ++j) {
let runner = runners[j];
if (runner.type === "guest") {
html+= runner.names.international;
} else {
if (runner.location && runner.location.country) {
// html+= "(" + runner.location.country.code + ") ";
html += "<img src='https://www.speedrun.com/images/flags/" + runner.location.country.code + ".png' title='" + runner.location.country.names.international + "'> ";
}
html += "<a href='" + runner.weblink + "'>" + (runner.names.international || runner.names.japanese) + "</a>";
}
if (j < runners.length - 1) {
html+= "<br>";
}
}
html+= "</td>";
html+= "<td class='align-middle' style='white-space: nowrap'><a href='" + run.run.weblink +"'>" + secondsToTimeExpression(run.run.times.primary_t) +"</a></td>"
html+= "<td class='align-middle' style='white-space: nowrap'>" + (run.run.date ? formatToDate(run.run.date) : "-") +"</td>"
html+= "<td class='align-middle' style='white-space: nowrap; font-size: 0.8em; '>Submitted: " + (run.run.submitted ? formatToDate(run.run.submitted) : "-") +"<br>"
html+= "Verified: " + (run.run.status && run.run.status["verify-date"] ? formatToDate(run.run.status["verify-date"]) : "-") +"</td>"
html+= "<td class='align-middle' style='font-size: 0.8em; white-space: pre-line'>" + escapeHtml(run.run.comment) +"</td>"
// html+= "<td style='white-space: pre;font-size: 0.7em;'>" + JSON.stringify(run, null, 4) + "</td>";
html+= "</tr>\n";
}
}
html+= "</tbody></table>";
}
html+= "</div></body></html>";
fs.writeFileSync("./output.html", html);
fs.writeFileSync("./runners.json", JSON.stringify(runners));
})();