-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
331 lines (277 loc) · 10.1 KB
/
main.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
console.log("Hello, World!");
const GAME = "mc";
const API_URL = "https://www.speedrun.com/api/v1/";
var gameId = "";
var categoryNameMap = null;
var subcategoryNameMap = null;
var subcategoryPosMap = null;
var savedPlatforms = new Map();
var gameData = null;
var runData = null;
var categoryData = null;
var variableData = null;
var apiCallSavedPromises = new Map();
// Taken and modified from https://stackoverflow.com/a/29153059
var srcIso8601DurationRegex = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?/;
function parseSrcISO8601Duration(srcIso8601Duration)
{
var matches = srcIso8601Duration.match(srcIso8601DurationRegex);
return {
hours: matches[1] === undefined ? null : matches[1],
minutes: matches[2] === undefined ? 0 : matches[2],
seconds: matches[3] === undefined ? 0 : matches[3],
milliseconds: matches[4] === undefined ? null : matches[4]
};
}
function srcApiGetFromUrlAwait(urlStr)
{
console.log(`Starting ${urlStr} at ${performance.now()}.`);
let savedPromise = apiCallSavedPromises.get(urlStr);
if (savedPromise !== undefined) {
console.log(`${urlStr}: using saved result`);
return savedPromise;
}
let timeStart = performance.now();
let promise = new Promise(function (resolve, reject) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", urlStr, true);
xmlHttp.onload = function () {
if (this.status >= 200 && this.status < 300) {
let data = JSON.parse(xmlHttp.response);
let timeEnd = performance.now();
console.log(`${urlStr}: ${(timeEnd - timeStart)/1000.0}`);
resolve(data);
} else {
reject({
status: this.status,
statusText: xmlHttp.statusText
});
}
};
xmlHttp.onerror = function () {
reject({
status: this.status,
statusText: xmlHttp.statusText
});
};
xmlHttp.send();
});
apiCallSavedPromises.set(urlStr, promise);
return promise;
}
function srcApiGetAwait(endpoint)
{
return srcApiGetFromUrlAwait(API_URL + endpoint);
}
function srcApiGetFromUrlCallback(urlStr, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
callback(JSON.parse(xmlHttp.responseText));
}
}
xmlHttp.open("GET", urlStr, true); // true for asynchronous
//xmlHttp.setRequestHeader("User-Agent", "custom-src-client/0.1");
xmlHttp.send(null);
}
function srcApiGetCallback(endpoint, callback)
{
srcApiGetFromUrlCallback(API_URL + endpoint, callback);
}
async function getGameIdFromAbbreviation(game)
{
gameData = await srcApiGetAwait(`games?abbreviation=${game}&max=1&_bulk=yes`);
gameId = gameData["data"][0]["id"];
}
async function fetchVerificationQueue()
{
let failsafeCount = 0;
let runsAndPagination = await srcApiGetAwait(`runs?game=${gameId}&status=new&orderby=date&direction=desc&embed=players&max=200`);
runData = runsAndPagination["data"];
let pagination = runsAndPagination["pagination"];
while (pagination["size"] == pagination["max"]) {
if (failsafeCount >= 5) {
console.log("Note: hit failsafe limit of 5!");
break;
}
let paginationLinks = pagination["links"];
runsAndPagination = await srcApiGetFromUrlAwait(paginationLinks[paginationLinks.length - 1]["uri"]);
runData.push(...runsAndPagination["data"]);
pagination = runsAndPagination["pagination"];
failsafeCount++;
}
}
async function createCategoryNameMapping()
{
categoryData = await srcApiGetAwait(`games/${gameId}/categories`);
categoryNameMap = new Map();
for (category of categoryData["data"]) {
categoryNameMap.set(category["id"], category["name"]);
}
}
async function createSubcategoryNameMapping()
{
variableData = await srcApiGetAwait(`games/${gameId}/variables`);
subcategoryNameMap = new Map();
subcategoryPosMap = new Map();
for (const [i, subcategory] of variableData["data"].entries()) {
if (subcategory["is-subcategory"] === false) {
continue;
}
let subcategoryId = subcategory["id"];
subcategoryPosMap.set(subcategoryId, i);
let subcategoryValueIdToNameMap = new Map();
subcategoryValues = subcategory["values"]["values"];
for (const [subcategoryValueId, subcategoryValueData] of Object.entries(subcategoryValues)) {
subcategoryValueIdToNameMap.set(subcategoryValueId, subcategoryValueData["label"]);
}
subcategoryNameMap.set(subcategoryId, subcategoryValueIdToNameMap);
}
}
function createFullCategoryNameFromRun(run)
{
let categoryName = categoryNameMap.get(run["category"]);
let subcategoryEntries = [];
for (const [variableId, variableValue] of Object.entries(run["values"])) {
let subcategoryValueIdToNameMap = subcategoryNameMap.get(variableId);
if (subcategoryValueIdToNameMap !== undefined) {
subcategoryEntries.push([variableId, subcategoryValueIdToNameMap.get(variableValue)]);
}
}
if (subcategoryEntries.length === 0) {
return categoryName;
}
subcategoryEntries.sort(function(subcatEntry1, subcatEntry2) {
let subcategory1Pos = subcategoryPosMap.get(subcatEntry1[0]);
let subcategory2Pos = subcategoryPosMap.get(subcatEntry2[0]);
if (subcategory1Pos < subcategory2Pos) {
return -1;
} else if (subcategory1Pos > subcategory2Pos) {
return 1;
} else {
console.warn(`Found two different subcategories with same position! (${subcatEntry1[0]}, ${subcatEntry2[0]})`);
return 0;
}
});
let subcategoryNames = [];
for (subcategoryEntry of subcategoryEntries) {
subcategoryNames.push(subcategoryEntry[1]);
}
subcategoryNamesJoined = subcategoryNames.join(", ");
return `${categoryName} - ${subcategoryNamesJoined}`;
}
function getPlayerNameFromRun(run)
{
let playerNames = [];
for (player of run["players"]["data"]) {
if (player["rel"] === "user") {
playerNames.push(player["names"]["international"]);
} else if (player["rel"] === "guest") {
playerNames.push(player["name"]);
} else {
console.warn(`Run "${run["id"]}" has unknown rel "${player["rel"]}"!`);
playerNames.push("<unknown>");
}
}
return playerNames.join("\n");
}
function getRunTime(run)
{
let duration = parseSrcISO8601Duration(run["times"]["primary"]);
let output = "";
if (duration.hours !== null) {
output = `${duration.hours}h ${duration.minutes.toString().padStart(2, "0")}m `;
} else if (duration.minutes !== null) {
output = `${duration.minutes}m `;
}
if (duration.seconds !== null) {
output += `${duration.seconds.toString().padStart(2, "0")}s`;
} if (duration.milliseconds !== null) {
output += ` ${duration.milliseconds}ms`;
}
return output;
}
async function getRunPlatform(run)
{
let platformId = run["system"]["platform"];
let platformName = savedPlatforms.get(platformId);
if (platformName === undefined) {
let platformData = await srcApiGetAwait(`platforms/${platformId}`);
platformName = platformData["data"]["name"];
savedPlatforms.set(platformId, platformName);
}
if (run["system"]["emulated"] === true) {
return `${platformName} [EMU]`;
} else {
return platformName;
}
}
function getDaysSinceRunDone(run)
{
let runDateStr = run["date"];
if (runDateStr === null) {
return "?? days ago";
}
let splitDate = runDateStr.split("-");
let runDateObj = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
console.log(runDateObj.toLocaleDateString());
let numDaysSince = Math.floor((Date.now() - runDateObj.getTime()) / (1000 * 60 * 60 * 24));
if (numDaysSince === 0) {
return "Today";
} else if (numDaysSince === 1) {
return "1 day ago";
} else {
return `${numDaysSince} days ago`
}
}
function addCellToRow(row, cellText)
{
row.append($("<td>").html(cellText));
}
// category | placement | user | time | platform | date | video available
// request categories https://www.speedrun.com/api/v1/games/nd2e9erd/categories
// and variables https://www.speedrun.com/api/v1/games/nd2e9erd/variables
// to be able to find the category name and variable names to print out the full category
// maybe do individual requests for categories https://www.speedrun.com/api/v1/categories/vdom79v2
// but don't request individual categories variables (too many duplicates)
// probably better to just request all to save requests
// request leaderboard for placement
// https://www.speedrun.com/api/v1/leaderboards/nd2e9erd/category/vdom79v2
async function createLeaderboard()
{
let fragment = document.createDocumentFragment();
for (run of runData) {
let row = $("<tr>");
addCellToRow(row, createFullCategoryNameFromRun(run));
addCellToRow(row, "??th");
addCellToRow(row, getPlayerNameFromRun(run));
addCellToRow(row, getRunTime(run));
let runPlatform = await getRunPlatform(run);
addCellToRow(row, runPlatform);
addCellToRow(row, getDaysSinceRunDone(run));
row.attr("data-href", run["weblink"]).addClass("row-link");
fragment.appendChild(row[0]);
}
$("<table>").addClass("col-padding")
.append(fragment)
.appendTo("#mcceQueue");
$("table .row-link").on("click", function(e) {
e.preventDefault();
let runWeblink = $(this).data("href");
if (e.ctrlKey) {
window.open(runWeblink, "_blank");
} else {
window.location = runWeblink;
}
});
}
async function main()
{
await getGameIdFromAbbreviation(GAME);
await Promise.allSettled([fetchVerificationQueue(), createCategoryNameMapping(), createSubcategoryNameMapping()]);
createLeaderboard();
}
$(function() {
main();
});