-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathamqAnswerTimeDifference.user.js
More file actions
576 lines (525 loc) · 24.5 KB
/
Copy pathamqAnswerTimeDifference.user.js
File metadata and controls
576 lines (525 loc) · 24.5 KB
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
// ==UserScript==
// @name AMQ Player Answer Time Difference
// @namespace http://tampermonkey.net/
// @version 1.6.7
// @description Makes you able to see how quickly people answered and the difference between the first player and everyone else, sends the result on chat at the end of a round and sends some stats at the end of the game
// @author 4Lajf (forked from Zolhungaj)
// @icon https://www.google.com/s2/favicons?sz=64&domain=animemusicquiz.com
// @match https://*.animemusicquiz.com/*
// @grant none
// @downloadURL https://github.com/4Lajf/amq-scripts/raw/main/amqAnswerTimeDifference.user.js
// @updateURL https://github.com/4Lajf/amq-scripts/raw/main/amqAnswerTimeDifference.user.js
// @require https://raw.githubusercontent.com/TheJoseph98/AMQ-Scripts/master/common/amqScriptInfo.js
// @copyright MIT license
// ==/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 settingsData = [
{
containerId: "smTimeDifferenceOptions",
title: "Time Difference Options",
data: [
{
label: "Enable Plugin",
id: "smTimeDifference",
popover: "Toggles TimeDifference",
enables: ["smTimeDifferenceChat", "smTimeDifferenceChatHidden", "smTimeDifferenceChatSilent", "smTimeDifferenceTimes", "smTimeDifferenceRoundLeaderboard", "smTimeDifferenceGameLeaderboard"],
offset: 0,
default: true,
},
{
label: "Write times to chat",
id: "smTimeDifferenceChat",
popover: "Send song's leaderboard at the end of each round",
unchecks: ["smTimeDifferenceChatHidden", "smTimeDifferenceChatSilent"],
offset: 1,
default: false,
},
{
label: "Write times to chat (only you)",
id: "smTimeDifferenceChatHidden",
popover: "Send song's leaderboard at the end of each round, but only you can see those messages",
unchecks: ["smTimeDifferenceChat", "smTimeDifferenceChatSilent"],
offset: 1,
default: true,
},
{
label: "Don't Write times to chat",
id: "smTimeDifferenceChatSilent",
popover: "Send song's leaderboard to chat",
unchecks: ["smTimeDifferenceChat", "smTimeDifferenceChatHidden"],
offset: 1,
default: false,
},
{
label: "Round's leaderboard",
id: "smTimeDifferenceRoundLeaderboard",
popover: "Toggles sending round's leaderboard to chat",
offset: 2,
default: true,
},
{
label: "Game's leaderboard",
id: "smTimeDifferenceGameLeaderboard",
popover: "Toggles sending game's leaderboard to chat",
offset: 2,
default: true,
},
{
label: "Time Differences",
id: "smTimeDifferenceTimes",
popover: "Toggle time differences at a place of player's answer",
offset: 1,
default: true,
},
],
},
];
// Create the "TimeDifference" tab in settings
$("#settingModal .tabContainer").append($("<div></div>").addClass("tab leftRightButtonTop clickAble").attr("onClick", "options.selectTab('timeDifferenceSettings', this)").append($("<h5></h5>").text("TimeDifference")));
// Create a separate container for TimeDifference settings
$("#settingModal .modal-body").append($("<div></div>").attr("id", "timeDifferenceSettings").addClass("settingContentContainer hide").append($("<div></div>").addClass("row")));
// Create the checkboxes
for (let setting of settingsData) {
$("#timeDifferenceSettings > .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");
}
}
}
// Modify the options object to handle the new tab
if (!options.oldSelectTab) {
options.oldSelectTab = options.selectTab;
options.selectTab = function (newTab, tabObject) {
// Hide all setting containers
$("#settingModal .settingContentContainer").addClass("hide");
// Show the selected container
$("#" + newTab).removeClass("hide");
// Update tab selection
$("#settingModal .tab").removeClass("selected");
$(tabObject).addClass("selected");
};
}
// 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);
}
}
}
//sort object by time (fastest first)
function compare(a, b) {
return a.time - b.time;
}
function mergeArray(data) {
return [...data].reduce((acc, val) => {
let { time, name } = val;
time = parseFloat(time);
const ind = acc.findIndex((el) => el.name === name);
if (ind !== -1) {
acc[ind].time += time;
} else {
acc.push({ time, name });
}
return acc;
}, []);
}
let ignoredPlayerIds = [],
fastestLeaderboard = null,
fastestLeaderboardToSum,
leader = null,
newLeader,
playerID,
gameRound,
summedUpLeaderBoard;
//Sends a message to lobby chat
function sendLobbyMessage(message) {
socket.sendCommand({
type: "lobby",
command: "game chat message",
data: { msg: message, teamMessage: false },
});
}
//Measure answer speed
const amqAnswerTimesUtility = new (function () {
"use strict";
this.songStartTime = 0;
this.playerTimes = [];
const that = quiz;
if (typeof Listener === "undefined") {
return;
}
new Listener("play next song", () => {
if ($("#smTimeDifference").prop("checked")) {
if (!$("#smTimeDifferenceChatSilent").prop("checked")) {
let gameRound = parseInt($("#qpCurrentSongCount").text()) + 1;
if ($("#smTimeDifferenceRoundLeaderboard").prop("checked")) {
const message = `===== ROUND ${gameRound} =====`;
if ($("#smTimeDifferenceChatHidden").prop("checked")) {
gameChat.systemMessage(message);
} else {
sendLobbyMessage(message);
}
}
}
this.songStartTime = Date.now();
this.playerTimes = [];
}
}).bindListener();
new Listener("player answered", (data) => {
if ($("#smTimeDifference").prop("checked")) {
const time = Date.now() - this.songStartTime;
data.forEach((gamePlayerId) => {
const quizPlayer = that.players[gamePlayerId];
this.playerTimes.push({
gamePlayerId: gamePlayerId,
time: time,
date: Date.now(),
name: quizPlayer._name,
});
// Deletes duplicate entry and leaves only the newest one
if (isDuplicate(this.playerTimes)) {
for (let i = 0; i < this.playerTimes.length; i++) {
if (this.playerTimes[i].gamePlayerId === gamePlayerId) {
this.playerTimes.splice(i, 1);
break;
}
}
}
// Sort object by time (faster is first)
this.playerTimes.sort(compare);
});
}
}).bindListener();
new Listener("Join Game", (data) => {
const quizState = data.quizState;
if (quizState) {
this.songStartTime = Date.now() - quizState.songTimer * 1000;
}
}).bindListener();
new Listener("Spectate Game", (data) => {
const quizState = data.quizState;
if (quizState) {
this.songStartTime = Date.now() - quizState.songTimer * 1000;
}
}).bindListener();
// Helper function to check for duplicates
function isDuplicate(values) {
const valueArr = values.map((item) => item.gamePlayerId);
return valueArr.some((item, idx) => valueArr.indexOf(item) != idx);
}
// Comparison function for sorting
function compare(a, b) {
return a.time - b.time;
}
})();
const ignorePlayersNexus = () => {
ignoredPlayerIds = [1, 2, 3, 4, 5, 6, 7, 8];
};
new Listener("nexus enemy encounter", () => {
ignorePlayersNexus();
}).bindListener();
new Listener("nexus map rejoin", () => {
ignorePlayersNexus();
}).bindListener();
new Listener("Game Starting", (data) => {
ignorePlayersRegular(data.players);
round = 1;
}).bindListener();
new Listener("Join Game", (data) => {
if (data.quizState) {
ignorePlayersRegular(data.quizState.players);
}
}).bindListener();
new Listener("Spectate Game", (data) => {
ignoredPlayerIds = [];
}).bindListener();
new Listener("Game Starting", ({ players }) => {
if ($("#smTimeDifference").prop("checked")) {
fastestLeaderboard = [];
ignoredPlayerIds = [];
leader = null;
newLeader = null;
playerID = null;
gameRound = 1;
const self = players.find((player) => player.name === selfName);
if (self) {
const teamNumber = self.teamNumber;
if (teamNumber) {
const teamMates = players.filter((player) => player.teamNumber === teamNumber);
if (teamMates.length > 1) {
ignoredPlayerIds = teamMates.map((player) => player.gamePlayerId);
}
}
}
}
}).bindListener();
//On player answering the quiz question
new Listener("player answered", (data) => {
if ($("#smTimeDifference").prop("checked") === true && $("#smTimeDifferenceTimes").prop("checked") === true) {
//Display timer
data.filter((gamePlayerId) => !ignoredPlayerIds.includes(gamePlayerId)).forEach((gamePlayerId) => {
//Make sure the '⚡' symbol will always follow the fastest player and update other players accordingly
//Make sure we are editing the right player
for (let i = 0; i < amqAnswerTimesUtility.playerTimes.length; i++) {
if (amqAnswerTimesUtility.playerTimes[i].gamePlayerId !== gamePlayerId) {
continue;
} else {
//If player is already the leader pass the '⚡' to the second fastest player
if (amqAnswerTimesUtility.playerTimes[i].gamePlayerId === leader) {
newLeader = amqAnswerTimesUtility.playerTimes[0].gamePlayerId;
for (let i = 0; i < amqAnswerTimesUtility.playerTimes.length; i++) {
if (amqAnswerTimesUtility.playerTimes[i].time === amqAnswerTimesUtility.playerTimes[0].time) {
quiz.players[newLeader].answer = `⚡ ${amqAnswerTimesUtility.playerTimes[i].time}ms`;
leader = newLeader;
//Update other players accordingly
} else {
if (playerID === leader) continue;
quiz.players[gamePlayerId].answer = `+${amqAnswerTimesUtility.playerTimes[i].time - amqAnswerTimesUtility.playerTimes[0].time}ms`;
}
}
//If the leader is yet to be chosen
} else {
if (amqAnswerTimesUtility.playerTimes[i].time === amqAnswerTimesUtility.playerTimes[0].time) {
quiz.players[gamePlayerId].answer = `⚡ ${amqAnswerTimesUtility.playerTimes[i].time}ms`;
leader = gamePlayerId;
//Everything else
} else {
quiz.players[gamePlayerId].answer = `+${amqAnswerTimesUtility.playerTimes[i].time - amqAnswerTimesUtility.playerTimes[0].time}ms`;
}
}
}
}
});
}
}).bindListener();
//On pre-show-answer phase
quiz._playerAnswerListner = new Listener("player answers", function (data) {
const that = quiz;
if ($("#smTimeDifference").prop("checked") === true && $("#smTimeDifferenceTimes").prop("checked") === true) {
data.answers.forEach((answer) => {
const quizPlayer = that.players[answer.gamePlayerId];
let answerText = answer.answer;
//Make sure we are getting the right player
for (let i = 0; i < amqAnswerTimesUtility.playerTimes.length; i++) {
if (amqAnswerTimesUtility.playerTimes[i].gamePlayerId !== quizPlayer.gamePlayerId) {
continue;
} else {
if (amqAnswerTimesUtility.playerTimes[i] !== undefined) {
if (amqAnswerTimesUtility.playerTimes[i].time === amqAnswerTimesUtility.playerTimes[0].time) {
answerText = `⚡ ${answerText} (${amqAnswerTimesUtility.playerTimes[i].time}ms)`;
} else {
answerText += ` (+${amqAnswerTimesUtility.playerTimes[i].time - amqAnswerTimesUtility.playerTimes[0].time}ms)`;
}
}
}
}
quizPlayer.answer = answerText;
quizPlayer.unknownAnswerNumber = answer.answerNumber;
quizPlayer.toggleTeamAnswerSharing(false);
});
if (!that.isSpectator) {
that.answerInput.showSubmitedAnswer();
that.answerInput.resetAnswerState();
}
that.videoTimerBar.updateState(data.progressBarState);
quizVideoController.checkForBufferingIssue();
} else {
data.answers.forEach((answer) => {
const quizPlayer = that.players[answer.gamePlayerId];
let answerText = answer.answer;
quizPlayer.answer = answerText;
quizPlayer.unknownAnswerNumber = answer.answerNumber;
quizPlayer.toggleTeamAnswerSharing(false);
});
}
if (!that.isSpectator) {
that.answerInput.showSubmitedAnswer();
that.answerInput.resetAnswerState();
if (that.hintGameMode) {
that.hintController.hide();
}
}
that.videoTimerBar.updateState(data.progressBarState);
quizVideoController.checkForBufferingIssue();
});
//On show answer phase
function answerResults(results) {
let gameRound = parseInt($("#qpCurrentSongCount").text()) + 1;
if ($("#smTimeDifference").prop("checked") && !$("#smTimeDifferenceChatSilent").prop("checked") && $("#smTimeDifferenceRoundLeaderboard").prop("checked")) {
let correctIds = results.players.filter((player) => player.correct).map((player) => player.gamePlayerId);
let displayPlayers = [];
// Process players who answered
amqAnswerTimesUtility.playerTimes.forEach((player) => {
const isCorrect = correctIds.includes(player.gamePlayerId);
const time = isCorrect ? player.time : quiz.nextSongPlayLength * 1000;
fastestLeaderboard.push({
name: player.name,
time: time,
round: gameRound - 1,
correct: isCorrect,
});
if (isCorrect) {
displayPlayers.push({ ...player, time: time });
}
});
// Process players who didn't answer
Object.values(quiz.players).forEach((player) => {
if (!amqAnswerTimesUtility.playerTimes.some((p) => p.gamePlayerId === player.gamePlayerId)) {
const time = quiz.nextSongPlayLength * 1000;
fastestLeaderboard.push({
name: player._name,
time: time,
round: gameRound - 1,
correct: false,
});
}
});
displayPlayers.sort(compare);
if (displayPlayers.length > 0) {
let placeNumber = ["⚡", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"];
displayPlayers.slice(0, 10).forEach((player, index) => {
let message = index === 0 ? `⚡ ${player.name} ➔ ${player.time}ms` : `${placeNumber[index]} ${player.name} ➔ ${player.time}ms (+${player.time - displayPlayers[0].time}ms)`;
if ($("#smTimeDifferenceChatHidden").prop("checked")) {
gameChat.systemMessage(message);
} else {
sendLobbyMessage(message);
}
});
} else if (results.players.length === 0) {
let message = "Not even trying? I see...";
$("#smTimeDifferenceChatHidden").prop("checked") ? gameChat.systemMessage(message) : sendLobbyMessage(message);
} else {
let message = "You are all terrible at this...";
$("#smTimeDifferenceChatHidden").prop("checked") ? gameChat.systemMessage(message) : sendLobbyMessage(message);
}
}
console.log(fastestLeaderboard);
}
function quizEndResult(results) {
if ($("#smTimeDifference").prop("checked") && $("#smTimeDifferenceGameLeaderboard").prop("checked")) {
let placeNumber = ["⚡", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"];
// Sort fastestLeaderboard by time
fastestLeaderboard.sort(compare);
// Display fastest correct answers
let correctAnswers = fastestLeaderboard.filter((entry) => entry.correct).slice(0, 10);
// Calculate and display summed up times (including all answers)
let summedUpLeaderBoard = mergeArray(fastestLeaderboard);
summedUpLeaderBoard.sort((a, b) => a.time - b.time);
if ($("#smTimeDifferenceChatHidden").prop("checked")) {
gameChat.systemMessage("===== FASTEST ANSWERS =====");
correctAnswers.forEach((entry, index) => {
gameChat.systemMessage(`${placeNumber[index]} ${entry.name} ➔ ${entry.time}ms (R${entry.round})`);
});
gameChat.systemMessage("===== SUMMED UP TIMES =====");
summedUpLeaderBoard.slice(0, 10).forEach((entry, index) => {
gameChat.systemMessage(`${placeNumber[index]} ${entry.name} ➔ ${entry.time}ms`);
});
} else {
sendLobbyMessage("===== FASTEST ANSWERS =====");
correctAnswers.forEach((entry, index) => {
sendLobbyMessage(`${placeNumber[index]} ${entry.name} ➔ ${entry.time}ms (R${entry.round})`);
});
sendLobbyMessage("===== SUMMED UP TIMES =====");
summedUpLeaderBoard.slice(0, 10).forEach((entry, index) => {
sendLobbyMessage(`${placeNumber[index]} ${entry.name} ➔ ${entry.time}ms`);
});
}
}
}
//Initialize listeners and 'Installed Userscripts' menu
function setup() {
new Listener("answer results", answerResults).bindListener();
new Listener("quiz end result", quizEndResult).bindListener();
AMQ_addScriptData({
name: "AMQ Player Answer Time Difference",
author: "4Lajf (forked from Zolhungaj)",
version: "1.6",
description: `Displays time difference in milliseconds between the fastest player and the rest, then at the end of the round sends results in chat. Sends the final leaderboard to chat once the game ends.`,
});
}
})();