-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotPotato.user.js
341 lines (303 loc) Β· 10.5 KB
/
hotPotato.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
// ==UserScript==
// @name Hot Potato Gamemode
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Utilities for the hot potato gamemode. Alt+click on an avatar to pass the potato to them.
// Commands:
// - /potato rules: Send a pastebin link with the rules
// - /potato roll: Randomly assign a player of each team to have the potato before starting a game
// - /potato track: Enable/disable auto-tracking of the potato (experimental). If enabled, at the start of each round, the script will autothrow a message showing who currently has the potato.
// You will need to manually tell the script who has the potato using ALT+click. If exactly one player gives a valid answer, and you have not manually passed the potato,
// the script will automatically do it for you.
// @author Einlar
// @match https://animemusicquiz.com/*
// @match https://*.animemusicquiz.com/*
// @downloadURL https://github.com/Einlar/AMQScripts/raw/main/hotPotato.user.js
// @updateURL https://github.com/Einlar/AMQScripts/raw/main/hotPotato.user.js
// @grant none
// ==/UserScript==
/**
* CHANGELOG
*
* v1.2
* - Make the script work also on AMQ subdomains (since at the moment the main AMQ domain is not working).
*
* v1.1
* - Fixed a bug in potato tracking, where if a player with auto-send enabled started writing something starting with "k" or "f" they would immediately receive the potato (because their answer for a brief moment was a valid anime name, and thus they were considered by the script the first to answer). Auto-passing now waits for a valid guess to persist for at least 0.5s before auto-passing.
* - Potato tracking will now detect passes made by other players using the script
*/
/**
* Enable/disable potato tracking
*/
let potatoTracking = false;
/**
* Track the current potato haver (for the user's team)
*
* @type {string | null}
*/
let potatoHaver = null;
/**
* Track who will have the potato during the next round
*
* @type {string | null}
*/
let nextPotatoHaver = null;
/**
* Timeout for auto-passing the potato
*
* @type {number | null}
*/
let autoPassTimeout = null;
/**
* Click on an avatar to pass the potato to that player
*
* @param {string} playerName
*/
const passPotato = (playerName) => {
sendAnswer(`π₯ to ${playerName}`, true);
nextPotatoHaver = playerName;
};
/**
* Show who currently has the potato
*/
const hasPotato = () => {
if (potatoHaver) sendAnswer(`(${potatoHaver} has π₯)`);
else gameChat.systemMessage("No one has the π₯");
};
/**
* Send a message as answer
*
* @param {string} message
* @param {boolean} [fillInput = false] If true, fill the answer input with the message
*/
const sendAnswer = (message, fillInput = false) => {
socket.sendCommand({
type: "quiz",
command: "quiz answer",
data: { answer: message },
});
if (fillInput) {
$("#qpAnswerInput").val(message);
quiz.answerInput.submitAnswer(true);
}
};
/**
* Retrieve the current player's answer
*/
const getCurrentAnswer = () =>
Object.values(quiz.players).find((p) => p.isSelf)?.answer;
/**
* Check if the player is playing Ranked
*/
const isRanked = () =>
(lobby.inLobby && lobby.settings.gamemode === "Ranked") ||
(quiz.inQuiz && quiz.gameMode === "Ranked");
/**
* Send a message in chat
*
* @param {string} msg
* @param {boolean} teamMessage
*/
const sendChatMessage = (msg, teamMessage = false) => {
socket.sendCommand({
type: "lobby",
command: "game chat message",
data: {
msg,
teamMessage,
},
});
};
/**
* Get a dictionary of team number => player names for the current quiz.
* Adapted from https://github.com/kempanator/amq-scripts/blob/main/amqMegaCommands.user.js
*
* @returns {Record<number, string[]>}
*/
const getTeamDictionary = () => {
if (!quiz.inQuiz && !lobby.inLobby) return {};
const players = quiz.inQuiz ? quiz.players : lobby.players;
return Object.values(players).reduce((acc, player) => {
// Coalesce missing team numbers to 0
const teamNumber = player.teamNumber ?? 0;
if (!acc[teamNumber]) acc[teamNumber] = [];
acc[teamNumber].push(player.name);
return acc;
}, /** @type {Record<number, string[]>} */ ({}));
};
/**
* Check if a string is a valid anime name (i.e. it appears in the dropdown list). Case insensitive.
*
* @param {string} animeName
*/
const isValidAnime = (animeName) => {
return quiz.answerInput.typingInput.autoCompleteController.list.some(
(anime) => anime.toLowerCase() === animeName.toLowerCase()
);
};
/**
* Get the team number of the current player
*
* @returns {number | null}
*/
const getMyTeam = () =>
Object.values(quiz.players).find((p) => p.isSelf)?.teamNumber ?? null;
/**
* Display the script status as a system message
*/
const showStatus = () => {
gameChat.systemMessage(
`Hot Potato tracking is ${potatoTracking ? "enabled" : "disabled"}`
);
};
/**
* Setup listeners
*/
const setupHotPotato = () => {
// Patch the quiz setup function to add a listener to the avatars. Works even after rejoining a quiz.
const originalSetupQuiz = quiz.setupQuiz;
quiz.setupQuiz = (...args) => {
originalSetupQuiz.apply(quiz, args);
// Skip setup if in ranked
if (isRanked()) return;
const myTeam = getMyTeam();
// Alt+click to pass the potato
Object.values(quiz.players)
// Can only pass to teammates
.filter((p) => p.teamNumber === myTeam)
.forEach((player) => {
player.avatarSlot.$body.on("click", (event) => {
if (event.altKey) {
passPotato(player.name);
}
});
});
showStatus();
};
// Handle chat commands
new Listener("game chat update", (chat) => {
// Disable everything during ranked
if (isRanked()) return;
chat.messages
.filter((m) => m.sender === selfName && m.message.startsWith("/"))
.forEach((m) => {
if (/^\/potato rules$/i.test(m.message)) {
/** --- Potato rules --- */
sendChatMessage("Hot Potato rules: https://pastebin.com/qdr4g6Jp");
} else if (/^\/potato roll$/i.test(m.message)) {
/** --- Potato starting rolls --- */
const teams = getTeamDictionary();
if (Object.keys(teams).length === 0)
return sendChatMessage("No teams found");
Object.entries(teams)
// When in team chat, only roll for the current team
.filter(([_, players]) =>
m.teamMessage ? players.includes(selfName) : true
)
.forEach(([team, players], i) => {
// Randomly choose a player to have the potato for each team
const potatoPlayer =
players[Math.floor(Math.random() * players.length)];
// Set the next potato haver
if (players.includes(selfName)) nextPotatoHaver = potatoPlayer;
// When sending multiple messages, send them with a small delay between each other
setTimeout(() => {
sendChatMessage(
`Team ${team}: ${potatoPlayer} has the π₯`,
m.teamMessage
);
}, (i + 1) * 200);
});
} else if (/^\/potato track$/i.test(m.message)) {
/** --- Potato tracking --- */
potatoTracking = !potatoTracking;
setTimeout(
() =>
gameChat.systemMessage(
`Potato tracking is now ${
potatoTracking ? "enabled" : "disabled"
}`
),
200
);
}
});
// Parse potato rolls made by other players
chat.messages
.filter((m) => m.sender !== selfName)
.forEach((m) => {
const match = m.message.match(/Team (\d+): (.+) has the π₯/);
if (match) {
const team = parseInt(match[1]);
const player = match[2];
if (getMyTeam() === team) {
gameChat.systemMessage(`Auto-passing π₯ to ${player}`);
nextPotatoHaver = player;
}
}
});
}).bindListener();
// Auto-pass the potato to the first player who inputs a valid anime name (if the potato has not been passed yet)
new Listener("team member answer", (answer) => {
if (!potatoTracking) return;
if (nextPotatoHaver == null) {
if (isValidAnime(answer.answer)) {
autoPassTimeout = setTimeout(() => {
// Skip the auto-pass if the potato has been passed manually in the meantime
if (nextPotatoHaver) return;
const toPlayer = quiz.players[answer.gamePlayerId].name;
gameChat.systemMessage(`Auto-passing π₯ to ${toPlayer}`);
nextPotatoHaver = toPlayer;
// Avoid replacing your answer
if (toPlayer !== selfName) passPotato(toPlayer);
}, 500);
} else if (autoPassTimeout) {
// Skip auto-passing if the anime is changed to something else
clearTimeout(autoPassTimeout);
}
}
// Match the potato message in the answer "π₯ to ${playerName}", and in that case update the potato haver
const selfId = Object.values(quiz.players).find(
(p) => p.isSelf
)?.gamePlayerId; // Is this even necessary?
const match = answer.answer.match(/π₯ to (.+)$/);
if (match && answer.gamePlayerId !== selfId) {
const toPlayer = match[1];
// Check if there is a team member with that name
const myTeam = getMyTeam();
const player = Object.values(quiz.players).find(
(p) => p.name === toPlayer && p.teamNumber === myTeam
);
if (player) {
gameChat.systemMessage(`Auto-passing π₯ to ${toPlayer}`);
potatoHaver = toPlayer;
nextPotatoHaver = toPlayer;
// Update the potato message (but only if you are not answering)
if (getCurrentAnswer()?.includes("π₯")) {
hasPotato();
}
}
}
}).bindListener();
// Show who has the potato when a new song is played
new Listener("play next song", () => {
if (!potatoTracking) return;
potatoHaver = nextPotatoHaver;
nextPotatoHaver = null;
hasPotato();
}).bindListener();
// Reset potato rolls when the quiz ends
new Listener("quiz over", () => {
potatoHaver = null;
nextPotatoHaver = null;
}).bindListener();
};
/**
* Entrypoint: load the script after the LOADING screen is hidden
*/
let loadInterval = setInterval(() => {
if ($("#loadingScreen").hasClass("hidden")) {
clearInterval(loadInterval);
setupHotPotato();
}
}, 500);