-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqRankedTracker.user.js
431 lines (376 loc) · 12.7 KB
/
amqRankedTracker.user.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
// ==UserScript==
// @name AMQ Ranked Tracker
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Track which days you played ranked games, and your score for that day. Access the data in the AMQ settings menu, under "Ranked Tracker".
// @author Einlar
// @match https://animemusicquiz.com/*
// @match https://*.animemusicquiz.com/*
// @downloadURL https://github.com/Einlar/AMQScripts/raw/main/amqRankedTracker.user.js
// @updateURL https://github.com/Einlar/AMQScripts/raw/main/amqRankedTracker.user.js
// @require https://github.com/joske2865/AMQ-Scripts/raw/master/common/amqScriptInfo.js
// @grant none
// @icon https://i.imgur.com/o8hOqsv.png
// ==/UserScript==
const regionDictionary = /** @type {const} */ ({
E: "Eastern",
C: "Central",
W: "Western",
});
const setupMetadata = () => {
AMQ_addScriptData({
name: "AMQ Ranked Tracker",
author: "Einlar",
version: "0.1",
link: "https://github.com/Einlar/AMQScripts",
description:
"<p>Tracks which days you played ranked games and your score for that day.</p>",
});
};
/**
* Create a dictionary that automatically persists in localStorage, with an optional validator.
*
* @template {{}} T
* @param {string} storageKey - The key to use in localStorage.
* @param {(data: any) => T} [validator] - A function to validate and transform the loaded data.
* @returns {T} - A proxy object that auto-saves to localStorage.
*/
function createPersistentDictionary(storageKey, validator) {
let data;
try {
data = JSON.parse(localStorage.getItem(storageKey) || "{}");
} catch {
data = {};
}
data = validator ? validator(data) : /** @type {T} */ (data);
const dictionary = new Proxy(data, {
set(target, key, value) {
target[key] = value;
localStorage.setItem(storageKey, JSON.stringify(target));
return true;
},
deleteProperty(target, key) {
delete target[key];
localStorage.setItem(storageKey, JSON.stringify(target));
return true;
},
});
return dictionary;
}
/**
* Compute the key for the current ranked game
*
* @example "2024-01-30 C Expert"
*/
const rankedKey = () => {
const region = $("#mpRankedTimer h3").text() || "";
const type = hostModal.$roomName.val()?.includes("Expert")
? "Expert"
: "Novice";
return `${new Date().toISOString().split("T")[0]} ${region} ${type}`;
};
/**
* @typedef RankedHistoryKey
* @property {string} date - The date in YYYY-MM-DD format.
* @property {keyof regionDictionary} region - The region of the game.
* @property {string} type - The type of ranked (e.g. "Expert").
*/
/**
* Parse the ranked key into its components. Returns undefined if the key is invalid.
*
* @param {string} key
* @returns {RankedHistoryKey | undefined}
*/
const parseRankedKey = (key) => {
const [date, region, type] = key.split(" ");
if (!date || !region || !type) return;
// Check if date is in the correct format
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) return;
// Check if region is valid
if (!Object.keys(regionDictionary).includes(region)) return;
return /** @type {RankedHistoryKey} */ ({ date, region, type });
};
const setupScript = () => {
/** @type {Record<string, number>} */
const rankedHistory = createPersistentDictionary(
"amqRankedTracker",
(data) => {
if (typeof data !== "object") return {};
// Remove invalid data
for (const key in data) {
if (!parseRankedKey(key)) delete data[key];
if (typeof data[key] !== "number") delete data[key];
}
return data;
}
);
new Listener("answer results", (payload) => {
if (quiz.inQuiz && quiz.gameMode === "Ranked") {
const myGamePlayerId = playerId();
if (myGamePlayerId === undefined) return;
const myScore = Object.values(payload.players).find(
(p) => p.gamePlayerId === myGamePlayerId
)?.score;
const key = rankedKey();
if (myScore !== undefined) rankedHistory[key] = myScore;
}
}).bindListener();
// Add a button in the settings
$("#optionListSettings").before(
/*html*/ `<li class="clickAble" onclick="$('#rankedTrackerModal').modal('show');">Ranked Tracker</li>`
);
// Add some way to visualize the stored data (& personal best)
$("#gameContainer").append(
$(/*html*/ `
<div class="modal fade tab-modal" id="rankedTrackerModal" tabIndex="-1" role="dialog" aria-labelledby="rankedTrackerModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document" style="width: 680px">
<div class="modal-content">
<!-- Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Ranked Tracker</h4>
<div class="tabContainer">
<div id="rtHistoryTab" class="tab clickAble selected">
<h5>History</h5>
</div>
<div id="rtStatsTab" class="tab clickAble">
<h5>Stats</h5>
</div>
</div>
</div>
<!-- Body -->
<div class="modal-body" style="overflow-y: auto; max-height: calc(100vh - 150px);">
<div id="rtHistoryContainer">
<div id="rankedTrackerContent"></div>
</div>
<div id="rtStatsContainer">
<div id="rankedTrackerStats"></div>
</div>
</div>
</div>
</div>
</div>
`)
);
const tabs = /** @type {const} */ (["rtHistory", "rtStats"]);
/**
* Switch to a different tab in the settings modal
*
* @param {tabs[number]} tab
*/
const switchTab = (tab) => {
tabs.forEach((t) => {
if (t === tab) {
$(`#${t}Tab`).addClass("selected");
$(`#${t}Container`).show();
} else {
$(`#${t}Tab`).removeClass("selected");
$(`#${t}Container`).hide();
}
});
};
tabs.forEach((tab) => {
$(`#${tab}Tab`).on("click", () => switchTab(tab));
});
switchTab("rtHistory");
const rankedTrackerContent = $("#rankedTrackerContent");
/**
* Render the ranked history within the modal.
*/
const renderRankedHistory = () => {
let currentMonth = new Date();
renderCalendar(currentMonth);
};
/**
* Show a simple calendar view with rows for each week, columns for each day, and cells for each day. Each cell should show the scores for each day, for each region (e.g. "E: 10"). Regions without a score in a cell should not be shown to save space. There are arrows to navigate between months.
* In the header, show the month and year.
*
* @param {Date} monthDate The date to render the calendar for.
*/
const renderCalendar = (monthDate) => {
rankedTrackerContent.empty();
// Header with month and year, and navigation arrows
const header = $(
'<div style="text-align: center; margin-bottom: 10px;"></div>'
);
const prevButton = $(
'<button class="btn-icon" style="margin-right: 10px;"><</button>'
);
const nextButton = $(
'<button class="btn-icon" style="margin-left: 10px;">></button>'
);
const monthYear = $(
'<span style="font-size: 18px; font-weight: bold;"></span>'
);
monthYear.text(
monthDate.toLocaleString("default", {
month: "long",
year: "numeric",
})
);
header.append(prevButton, monthYear, nextButton);
rankedTrackerContent.append(header);
// Event handlers for navigation
prevButton.on("click", () => {
monthDate.setMonth(monthDate.getMonth() - 1);
renderCalendar(monthDate);
});
nextButton.on("click", () => {
monthDate.setMonth(monthDate.getMonth() + 1);
renderCalendar(monthDate);
});
// Days of the week header
const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const calendarTable = $(
'<table style="width: 100%; border-collapse: collapse;"></table>'
);
const headerRow = $("<tr></tr>");
daysOfWeek.forEach((day) => {
headerRow.append(
$(
'<th style="border: 1px solid var(--accentColor, initial); padding: 5px;">'
).text(day)
);
});
calendarTable.append(headerRow);
// Get the first day of the month
const firstDay = new Date(monthDate.getFullYear(), monthDate.getMonth(), 1);
const startingDay = firstDay.getDay();
// Number of days in the month
const daysInMonth = new Date(
monthDate.getFullYear(),
monthDate.getMonth() + 1,
0
).getDate();
// Create the calendar rows
let date = 1;
for (let i = 0; i < 6; i++) {
// maximum 6 weeks
let row = $("<tr></tr>");
for (let j = 0; j < 7; j++) {
let cell = $(
'<td style="border: 1px solid var(--primaryColorContrast, initial); height: 80px; vertical-align: top; padding: 2px;"></td>'
);
if (i === 0 && j < startingDay) {
// Empty cell before first day of month
row.append(cell);
} else if (date > daysInMonth) {
// Empty cells after last day of month
row.append(cell);
} else {
// Display the date
const cellContent = $("<div></div>");
cellContent.append($('<div style="font-weight: bold;">').text(date));
// Check for scores on this date
const dateString = `${monthDate.getFullYear()}-${String(
monthDate.getMonth() + 1
).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
let hasScores = false;
for (const key in rankedHistory) {
const parsedKey = parseRankedKey(key);
if (parsedKey && parsedKey.date === dateString) {
hasScores = true;
const regionShort = parsedKey.region;
const score = rankedHistory[key];
cellContent.append(
$(
/*html*/ `<div style="color: ${
parsedKey.type === "Expert"
? "var(--wrongAnswerColor, rgb(134, 0, 0))"
: "var(--correctAnswerColor, rgb(0, 0, 0))"
}">`
).text(`${regionShort}: ${score}`)
);
}
}
// if (hasScores) {
// cell.css("background-color", "var(--accentColor, initial)"); // Highlight cells with scores
// }
cell.append(cellContent);
row.append(cell);
date++;
}
}
calendarTable.append(row);
if (date > daysInMonth) {
break; // Stop creating rows if we've reached the end
}
}
rankedTrackerContent.append(calendarTable);
};
const renderStats = () => {
const rankedTrackerStats = $("#rankedTrackerStats");
rankedTrackerStats.empty();
// Find highest score and most recent date
let highestScore = 0;
let mostRecentDate = "";
for (const key in rankedHistory) {
const score = rankedHistory[key];
if (score > highestScore) {
highestScore = score;
mostRecentDate = parseRankedKey(key)?.date || "";
} else if (score === highestScore && key > mostRecentDate) {
mostRecentDate = parseRankedKey(key)?.date || "";
}
}
// Calculate days ago
const daysAgo = mostRecentDate
? Math.floor(
(new Date().getTime() - new Date(mostRecentDate).getTime()) /
(1000 * 60 * 60 * 24)
)
: 0;
// Create stats box
const statsBox = $(/*html*/ `
<div style="border: 1px solid var(--primaryColorContrast, initial); padding: 10px; margin: 10px;">
<h4>Personal Best</h4>
<p>Highest Score: ${highestScore}</p>
<p>Achieved on: ${mostRecentDate}</p>
<p>(${daysAgo} days ago)</p>
</div>
`);
rankedTrackerStats.append(statsBox);
};
// Initial render
renderRankedHistory();
// Update each time the modal is shown
$("#rankedTrackerModal").on("shown.bs.modal", () => {
renderRankedHistory();
renderStats();
});
};
/**
* Retrieve the ID of the player
*/
const playerId = () =>
Object.values(quiz.players).find((p) => p.isSelf)?.gamePlayerId;
/**
* Setup the script
*
* @returns {Promise<void>}
*/
const waitForInitialLoad = () => {
return new Promise((resolve, reject) => {
if (!quiz) return reject(new Error("Quiz not found"));
const loadingScreen = document.getElementById("loadingScreen");
if (!loadingScreen) return reject(new Error("Loading screen not found"));
new MutationObserver((_record, observer) => {
try {
observer.disconnect();
resolve();
} catch (error) {
observer.disconnect();
reject(error);
}
}).observe(loadingScreen, { attributes: true });
});
};
/**
* Validate a state object
*/
waitForInitialLoad().then(() => {
setupMetadata();
setupScript();
});