forked from joske2865/AMQ-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqRigTracker.user.js
676 lines (644 loc) · 25.1 KB
/
amqRigTracker.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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// ==UserScript==
// @name AMQ Rig Tracker
// @namespace https://github.com/TheJoseph98
// @version 1.3.4
// @description Rig tracker for AMQ, supports writing rig to chat for AMQ League games and writing rig to the scoreboard for general use (supports infinitely many players and all modes), many customisable options available
// @author TheJoseph98
// @match https://animemusicquiz.com/*
// @downloadURL https://github.com/TheJoseph98/AMQ-Scripts/raw/master/amqRigTracker.user.js
// @updateURL https://github.com/TheJoseph98/AMQ-Scripts/raw/master/amqRigTracker.user.js
// @grant none
// @require https://raw.githubusercontent.com/TheJoseph98/AMQ-Scripts/master/common/amqScriptInfo.js
// @updateURL https://github.com/TheJoseph98/AMQ-Scripts/raw/master/amqRigTracker.user.js
// ==/UserScript==
// don't load on login page
if (document.getElementById("startPage")) return;
// Wait until the LOADING... screen is hidden and load script
let loadInterval = setInterval(() => {
if (document.getElementById("loadingScreen").classList.contains("hidden")) {
setup();
clearInterval(loadInterval);
}
}, 500);
let scoreboardReady = false;
let playerDataReady = false;
let returningToLobby = false;
let missedFromOwnList = 0;
let playerData = {};
// listeners
let quizReadyRigTracker;
let answerResultsRigTracker;
let quizEndRigTracker;
let returnLobbyVoteListener;
let joinLobbyListener;
let spectateLobbyListener;
// data for the checkboxes
let settingsData = [
{
containerId: "smRigTrackerOptions",
title: "Rig Tracker Options",
data: [
{
label: "Track Rig",
id: "smRigTracker",
popover: "Enables or disabled the rig tracker",
enables: ["smRigTrackerChat", "smRigTrackerScoreboard", "smRigTrackerMissedOwn"],
offset: 0,
default: true
},
{
label: "Write rig to chat",
id: "smRigTrackerChat",
popover: "Writes the rig to chat. Used for AMQ League games, requires 2 players, automatically disabled if the requirement is not met",
enables: ["smRigTrackerAnime", "smRigTrackerPlayerNames", "smRigTrackerScore", "smRigTrackerFinalResult"],
offset: 1,
default: false
},
{
label: "Anime Name",
id: "smRigTrackerAnime",
popover: "Include the anime name when writing rig to chat",
enables: ["smRigTrackerAnimeEnglish", "smRigTrackerAnimeRomaji"],
offset: 2,
default: true
},
{
label: "English",
id: "smRigTrackerAnimeEnglish",
popover: "English anime names",
unchecks: ["smRigTrackerAnimeRomaji"],
offset: 3,
default: false
},
{
label: "Romaji",
id: "smRigTrackerAnimeRomaji",
popover: "Romaji anime names",
unchecks: ["smRigTrackerAnimeEnglish"],
offset: 3,
default: true
},
{
label: "Player Names",
id: "smRigTrackerPlayerNames",
popover: "Include the player names when writing rig to chat",
offset: 2,
default: true
},
{
label: "Score",
id: "smRigTrackerScore",
popover: "Include the players' scores when writing rig to chat",
offset: 2,
default: false
},
{
label: "Final results",
id: "smRigTrackerFinalResult",
popover: "Write the final results of the game",
enables: ["smRigTrackerQuizEnd", "smRigTrackerLobby"],
offset: 2,
default: true
},
{
label: "Write rig to scoreboard",
id: "smRigTrackerScoreboard",
popover: "Writes the rig to the scoreboards next to each person's score",
offset: 1,
default: true
},
{
label: "Display missed from list",
id: "smRigTrackerMissedOwn",
popover: "Display the number of songs you missed from your own list in the chat at the end of the quiz",
enables: ["smRigTrackerMissedAll"],
offset: 1,
default: true
},
{
label: "Display missed from all lists",
id: "smRigTrackerMissedAll",
popover: "Display the number of songs all players missed from their own lists in the chat at the end of the quiz",
offset: 2,
default: false
}
]
},
{
containerId: "smRigTrackerFinalOptions",
title: "Final Results Options",
data: [
{
label: "On quiz end",
id: "smRigTrackerQuizEnd",
popover: "Write the final results at the end of the quiz",
enables: ["smRigTrackerQuizEndNames", "smRigTrackerQuizEndScore", "smRigTrackerQuizEndRig"],
offset: 0,
default: true
},
{
label: "Player Names",
id: "smRigTrackerQuizEndNames",
popover: "Include player names on final results when the quiz ends",
offset: 1,
default: true
},
{
label: "Score",
id: "smRigTrackerQuizEndScore",
popover: "Include the final score on final result when the quiz ends",
offset: 1,
default: true
},
{
label: "Rig",
id: "smRigTrackerQuizEndRig",
popover: "Include the final rig on final results when the quiz ends",
offset: 1,
default: true
},
{
label: "On returning to lobby",
id: "smRigTrackerLobby",
popover: "Write the final results when returning to lobby",
enables: ["smRigTrackerLobbyNames", "smRigTrackerLobbyScore", "smRigTrackerLobbyRig"],
offset: 0,
default: false
},
{
label: "Player Names",
id: "smRigTrackerLobbyNames",
popover: "Include player names on final results when returning to lobby",
offset: 1,
default: false
},
{
label: "Score",
id: "smRigTrackerLobbyScore",
popover: "Include the final score on final result when returning to lobby",
offset: 1,
default: false
},
{
label: "Rig",
id: "smRigTrackerLobbyRig",
popover: "Include the final rig on final results when returning to lobby",
offset: 1,
default: false
}
]
}
];
// Create the "Rig Tracker" tab in settings
$("#settingModal .tabContainer")
.append($("<div></div>")
.addClass("tab leftRightButtonTop clickAble")
.attr("onClick", "options.selectTab('settingsCustomContainer', this)")
.append($("<h5></h5>")
.text("Rig Tracker")
)
);
// Create the body base
$("#settingModal .modal-body")
.append($("<div></div>")
.attr("id", "settingsCustomContainer")
.addClass("settingContentContainer hide")
.append($("<div></div>")
.addClass("row")
)
);
// Create the checkboxes
for (let setting of settingsData) {
$("#settingsCustomContainer > .row")
.append($("<div></div>")
.addClass("col-xs-6")
.attr("id", setting.containerId)
.append($("<div></div>")
.attr("style", "text-align: center")
.append($("<label></label>")
.text(setting.title)
)
)
);
for (let data of setting.data) {
$("#" + setting.containerId)
.append($("<div></div>")
.addClass("customCheckboxContainer")
.addClass(data.offset !== 0 ? "offset" + data.offset : "")
.addClass(data.offset !== 0 ? "disabled" : "")
.append($("<div></div>")
.addClass("customCheckbox")
.append($("<input id='" + data.id + "' type='checkbox'>")
.prop("checked", data.default !== undefined ? data.default : false)
)
.append($("<label for='" + data.id + "'><i class='fa fa-check' aria-hidden='true'></i></label>"))
)
.append($("<label></label>")
.addClass("customCheckboxContainerLabel")
.text(data.label)
)
);
if (data.popover !== undefined) {
$("#" + data.id).parent().parent().find("label:contains(" + data.label + ")")
.attr("data-toggle", "popover")
.attr("data-content", data.popover)
.attr("data-trigger", "hover")
.attr("data-html", "true")
.attr("data-placement", "top")
.attr("data-container", "#settingModal")
}
}
}
// Update the enabled and checked checkboxes
for (let setting of settingsData) {
for (let data of setting.data) {
updateEnabled(data.id);
$("#" + data.id).click(function () {
updateEnabled(data.id);
if (data.unchecks !== undefined) {
data.unchecks.forEach((settingId) => {
if ($(this).prop("checked")) {
$("#" + settingId).prop("checked", false);
}
else {
$(this).prop("checked", true);
}
})
}
});
}
}
// Updates the enabled checkboxes, checks each node recursively
function updateEnabled(settingId) {
let current;
settingsData.some((setting) => {
current = setting.data.find((data) => {
return data.id === settingId;
});
return current !== undefined;
});
if (current === undefined) {
return;
}
if (current.enables === undefined) {
return;
}
else {
for (let enableId of current.enables) {
if ($("#" + current.id).prop("checked") && !$("#" + current.id).parent().parent().hasClass("disabled")) {
$("#" + enableId).parent().parent().removeClass("disabled");
}
else {
$("#" + enableId).parent().parent().addClass("disabled");
}
updateEnabled(enableId);
}
}
}
// Creates the rig counters on the scoreboard and sets them to 0
function initialiseScoreboard() {
clearScoreboard();
for (let entryId in quiz.scoreboard.playerEntries) {
let tmp = quiz.scoreboard.playerEntries[entryId];
let rig = $(`<span class="qpsPlayerRig">0</span>`);
tmp.$entry.find(".qpsPlayerName").before(rig);
}
scoreboardReady = true;
}
// Creates the player data for counting rig (and score)
function initialisePlayerData() {
clearPlayerData();
for (let entryId in quiz.players) {
playerData[entryId] = {
rig: 0,
score: 0,
missedList: 0,
name: quiz.players[entryId]._name
};
}
playerDataReady = true;
}
// Clears the rig counters from scoreboard
function clearScoreboard() {
$(".qpsPlayerRig").remove();
scoreboardReady = false;
}
// Clears player data
function clearPlayerData() {
playerData = {};
playerDataReady = false;
missedFromOwnList = 0;
}
// Writes the current rig to scoreboard
function writeRigToScoreboard() {
if (playerDataReady) {
for (let entryId in quiz.scoreboard.playerEntries) {
let entry = quiz.scoreboard.playerEntries[entryId];
let rigCounter = entry.$entry.find(".qpsPlayerRig");
rigCounter.text(playerData[entryId].rig);
}
}
}
// Writes the rig to chat (for 2 players, automatically disables if there's more or less than 2 players)
function writeRigToChat(animeTitle) {
let tmpData = [];
let message = "";
if (Object.keys(playerData).length !== 2) {
gameChat.systemMessage("Writing rig to chat requires exactly 2 players, writing rig to chat has been disabled");
$("#smRigTrackerChat").prop("checked", false);
updateEnabled("smRigTrackerChat");
}
else {
for (let key of Object.keys(playerData)) {
tmpData.push(playerData[key]);
}
if ($("#smRigTrackerPlayerNames").prop("checked")) {
message += tmpData[0].name + " " + tmpData[0].rig + "-" + tmpData[1].rig + " " + tmpData[1].name;
}
else {
message += tmpData[0].rig + "-" + tmpData[1].rig;
}
if ($("#smRigTrackerScore").prop("checked")) {
message += ", Score: " + tmpData[0].score + "-" + tmpData[1].score;
}
if ($("#smRigTrackerAnime").prop("checked")) {
if ($("#smRigTrackerAnimeRomaji").prop("checked")) {
message += " (" + animeTitle.romaji + ")";
}
else if ($("#smRigTrackerAnimeEnglish").prop("checked")){
message += " (" + animeTitle.english + ")";
}
else {
message += " (" + animeTitle.romaji + ")";
}
}
}
let oldMessage = gameChat.$chatInputField.val();
gameChat.$chatInputField.val(message);
gameChat.sendMessage();
gameChat.$chatInputField.val(oldMessage);
}
// Write the final result at the end of the game
function writeResultsToChat() {
let tmpData = [];
for (let key of Object.keys(playerData)) {
tmpData.push(playerData[key]);
}
let oldMessage = gameChat.$chatInputField.val();
gameChat.$chatInputField.val("========FINAL RESULT========");
gameChat.sendMessage();
if (!returningToLobby) {
if ($("#smRigTrackerQuizEndScore").prop("checked")) {
if ($("#smRigTrackerQuizEndNames").prop("checked")) {
gameChat.$chatInputField.val("Score: " + tmpData[0].name + " " + tmpData[0].score + "-" + tmpData[1].score + " " + tmpData[1].name);
gameChat.sendMessage();
}
else {
gameChat.$chatInputField.val("Score: " + tmpData[0].score + "-" + tmpData[1].score);
gameChat.sendMessage();
}
}
if ($("#smRigTrackerQuizEndRig").prop("checked")) {
if ($("#smRigTrackerQuizEndNames").prop("checked")) {
gameChat.$chatInputField.val("Rig: " + tmpData[0].name + " " + tmpData[0].rig + "-" + tmpData[1].rig + " " + tmpData[1].name);
gameChat.sendMessage();
}
else {
gameChat.$chatInputField.val("Rig: " + tmpData[0].rig + "-" + tmpData[1].rig);
gameChat.sendMessage();
}
}
}
else {
if ($("#smRigTrackerLobbyScore").prop("checked")) {
if ($("#smRigTrackerLobbyNames").prop("checked")) {
gameChat.$chatInputField.val("Score: " + tmpData[0].name + " " + tmpData[0].score + "-" + tmpData[1].score + " " + tmpData[1].name);
gameChat.sendMessage();
}
else {
gameChat.$chatInputField.val("Score: " + tmpData[0].score + "-" + tmpData[1].score);
gameChat.sendMessage();
}
}
if ($("#smRigTrackerLobbyRig").prop("checked")) {
if ($("#smRigTrackerLobbyNames").prop("checked")) {
gameChat.$chatInputField.val("Rig: " + tmpData[0].name + " " + tmpData[0].rig + "-" + tmpData[1].rig + " " + tmpData[1].name);
gameChat.sendMessage();
}
else {
gameChat.$chatInputField.val("Rig: " + tmpData[0].rig + "-" + tmpData[1].rig);
gameChat.sendMessage();
}
}
}
gameChat.$chatInputField.val(oldMessage);
}
function displayMissedList() {
let inQuiz = Object.values(quiz.players).some(player => player.isSelf === true);
if ($("#smRigTrackerMissedOwn").prop("checked") && !$("#smRigTrackerMissedAll").prop("checked") && inQuiz && quiz.gameMode !== "Ranked") {
gameChat.systemMessage(`You missed ${missedFromOwnList === 1 ? missedFromOwnList + " song" : missedFromOwnList + " songs"} from your own list`);
}
if ($("#smRigTrackerMissedAll").prop("checked") && $("#smRigTrackerMissedOwn").prop("checked") && quiz.gameMode !== "Ranked") {
for (let id in playerData) {
gameChat.systemMessage(`${playerData[id].name} missed ${playerData[id].missedList === 1 ? playerData[id].missedList + " song" : playerData[id].missedList + " songs"} from their own list`);
}
}
}
function setup() {
// Updates the preset settings tabs and container, this is mostly to allow interaction with the newly added "Custom" tab
options.$SETTING_TABS = $("#settingModal .tab");
options.$SETTING_CONTAINERS = $(".settingContentContainer");
// Initial setup on quiz start
quizReadyRigTracker = new Listener("quiz ready", (data) => {
returningToLobby = false;
clearPlayerData();
clearScoreboard();
if ($("#smRigTracker").prop("checked") && quiz.gameMode !== "Ranked") {
answerResultsRigTracker.bindListener();
quizEndRigTracker.bindListener();
returnLobbyVoteListener.bindListener();
if ($("#smRigTrackerScoreboard").prop("checked")) {
initialiseScoreboard();
}
initialisePlayerData();
}
else {
answerResultsRigTracker.unbindListener();
quizEndRigTracker.unbindListener();
returnLobbyVoteListener.unbindListener();
}
});
// stuff to do on answer reveal
answerResultsRigTracker = new Listener("answer results", (result) => {
if (quiz.gameMode === "Ranked") {
return;
}
if (!playerDataReady) {
initialisePlayerData();
}
if (!scoreboardReady && $("#smRigTrackerScoreboard").prop("checked")) {
initialiseScoreboard();
if (playerDataReady) {
writeRigToScoreboard();
}
}
if (playerDataReady) {
for (let player of result.players) {
if (player.listStatus !== null && player.listStatus !== undefined && player.listStatus !== false && player.listStatus !== 0) {
playerData[player.gamePlayerId].rig++;
if (player.correct === false) {
playerData[player.gamePlayerId].missedList++;
}
if (player.correct === false && quiz.players[player.gamePlayerId]._name === selfName) {
missedFromOwnList++;
}
}
if (player.correct === true) {
playerData[player.gamePlayerId].score++;
}
}
if ($("#smRigTrackerChat").prop("checked") && !returningToLobby) {
writeRigToChat(result.songInfo.animeNames);
}
if (scoreboardReady) {
writeRigToScoreboard();
}
}
});
// stuff to do on quiz end
quizEndRigTracker = new Listener("quiz end result", (result) => {
if ($("#smRigTrackerChat").prop("checked") && $("#smRigTrackerFinalResult").prop("checked") && $("#smRigTrackerQuizEnd").prop("checked")) {
writeResultsToChat();
}
displayMissedList();
});
// stuff to do on returning to lobby
returnLobbyVoteListener = new Listener("return lobby vote result", (payload) => {
if (payload.passed) {
returningToLobby = true;
if ($("#smRigTrackerChat").prop("checked") && $("#smRigTrackerFinalResult").prop("checked") && $("#smRigTrackerLobby").prop("checked")) {
writeResultsToChat();
}
//displayMissedList();
}
});
// Reset data when joining a lobby
joinLobbyListener = new Listener("Join Game", (payload) => {
if (payload.error) {
return;
}
if ($("#smRigTracker").prop("checked") && payload.settings.gameMode !== "Ranked") {
answerResultsRigTracker.bindListener();
quizEndRigTracker.bindListener();
returnLobbyVoteListener.bindListener();
}
else {
answerResultsRigTracker.unbindListener();
quizEndRigTracker.unbindListener();
returnLobbyVoteListener.unbindListener();
}
clearPlayerData();
clearScoreboard();
});
// Reset data when spectating a lobby
spectateLobbyListener = new Listener("Spectate Game", (payload) => {
if (payload.error) {
return;
}
if ($("#smRigTracker").prop("checked") && payload.settings.gameMode !== "Ranked") {
answerResultsRigTracker.bindListener();
quizEndRigTracker.bindListener();
returnLobbyVoteListener.bindListener();
}
else {
answerResultsRigTracker.unbindListener();
quizEndRigTracker.unbindListener();
returnLobbyVoteListener.unbindListener();
}
clearPlayerData();
clearScoreboard();
});
// Enable or disable rig tracking on checking or unchecking the rig tracker checkbox
$("#smRigTracker").click(function () {
let rigTrackerEnabled = $(this).prop("checked");
if (!rigTrackerEnabled) {
quizReadyRigTracker.unbindListener();
answerResultsRigTracker.unbindListener();
quizEndRigTracker.unbindListener();
returnLobbyVoteListener.unbindListener();
clearScoreboard();
}
else {
quizReadyRigTracker.bindListener();
answerResultsRigTracker.bindListener();
quizEndRigTracker.bindListener();
returnLobbyVoteListener.bindListener();
if ($("#smRigTrackerScoreboard").prop("checked")) {
initialiseScoreboard();
writeRigToScoreboard();
}
}
});
// Enable or disable rig display on the scoreboard on checking or unchecking the scoreboard checkbox
$("#smRigTrackerScoreboard").click(function () {
let rigTrackerScoreboardEnabled = $(this).prop("checked");
if (rigTrackerScoreboardEnabled) {
initialiseScoreboard();
writeRigToScoreboard();
}
else {
clearScoreboard();
}
});
// bind listeners
quizReadyRigTracker.bindListener();
answerResultsRigTracker.bindListener();
quizEndRigTracker.bindListener();
returnLobbyVoteListener.bindListener();
joinLobbyListener.bindListener();
spectateLobbyListener.bindListener();
AMQ_addScriptData({
name: "Rig Tracker",
author: "TheJoseph98",
description: `
<p>Rig tracker for AMQ counts how many times a certain player's list has appeared in a quiz, mainly created for AMQ League games to reduce the need for dedicated players who track the rig</p>
<p>Rig is only counted if the player has enabled "Share Entries" in their AMQ list settings (noted by the blue ribbon in their answer field during answer reveal)</p>
<p>Rig tracker has multiple options available which can be accessed by opening AMQ settings and selecting the "Rig Tracker" tab</p>
<a href="https://i.imgur.com/LQE4PGg.png" target="_blank"><img src="https://i.imgur.com/LQE4PGg.png" /></a>
<p>Rig tracker also has an option of writing rig to the scoreboard next to players' scores for non-league and more than 2 players games</p>
<a href="https://i.imgur.com/4jF8vja.png" target="_blank"><img src="https://i.imgur.com/4jF8vja.png" /></a>
<p>If you're looking for a smaller version without these options and which can only write rig to scoreboard, check out <a href="https://github.com/TheJoseph98/AMQ-Scripts/raw/master/amqRigTrackerLite.user.js">Rig Tracker Lite</a>
`
});
// CSS stuff
AMQ_addStyle(`
.qpsPlayerRig {
padding-right: 5px;
opacity: 0.3;
}
.customCheckboxContainer {
display: flex;
}
.customCheckboxContainer > div {
display: inline-block;
margin: 5px 0px;
}
.customCheckboxContainer > .customCheckboxContainerLabel {
margin-left: 5px;
margin-top: 5px;
font-weight: normal;
}
.offset1 {
margin-left: 20px;
}
.offset2 {
margin-left: 40px;
}
.offset3 {
margin-left: 60px;
}
.offset4 {
margin-left: 80px;
}
`);
}