-
Notifications
You must be signed in to change notification settings - Fork 1
/
teemo.js
351 lines (300 loc) · 10.4 KB
/
teemo.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
import { CountUp } from './js/countUp.min.js';
////////////////////////////////////////////////////////////////////////////////
// Global Data
////////////////////////////////////////////////////////////////////////////////
let bettingactive = false;
// Username -> {"team": "blue", amount: 1000}
var attemptedbets = {};
// Username -> [{"team": "blue", amount: 1000}]
var acceptedbets = {};
////////////////////////////////////////////////////////////////////////////////
// Display/rendering components and initialization
////////////////////////////////////////////////////////////////////////////////
function TeamBetSummary() {
var prevSum = 0;
var betSum = 0;
const sumreducer = (prev, current) => prev + current;
return {
view: function(vnode) {
let team = vnode.attrs.team;
let bets = vnode.attrs.bets;
let otherbets = vnode.attrs.otherbets;
let successmultiplediv;
if (bets.length > 0 && otherbets.length > 0) {
let successmultiple = otherbets.reduce(sumreducer) / bets.reduce(sumreducer)
successmultiplediv = m("div",
`Bet return is ${successmultiple.toFixed(2)}x`)
}
let totalHS = m("span", { class: 'bet-count' }, bets.length)
let textHS = m("span", { class: 'bet-count-label' }, `bets for ${team}`)
let childArr = [];
// if (team === "red") {
// childArr = [totalHS, textHS]
// } else {
// childArr = [textHS, totalHS]
// }
childArr = [totalHS, textHS]
return m("div", { class: 'summary-container' }, [
m("div", { class: 'sum', id: `${team}-breakdown-counter` },
betSum,
),
m("span",
childArr,
),
successmultiplediv,
]
)
},
// Before we update the component, store the old bet info so that the
// CountUp can be initialized correctly in onupdate.
onbeforeupdate: function(vnode, old) {
let oldbets = old.attrs.bets
let newbets = vnode.attrs.bets
prevSum = oldbets.length === 0 ? 0 : oldbets.reduce(sumreducer)
betSum = newbets.length === 0 ? 0 : newbets.reduce(sumreducer)
},
onupdate: function(vnode) {
let team = vnode.attrs.team;
let cu = new CountUp(
`${team}-breakdown-counter`,
betSum,
{ startVal: prevSum },
)
cu.start();
}
}
}
// Contains the list of bets
var TeamBetsContainer = {
view: function(vnode) {
let team = vnode.attrs.team;
let bets = vnode.attrs.bets.sort((a, b) => b - a);
return m("div",
{ class: 'individual-container' },
m("div",
{ class: 'individual-bets' },
bets.map(amount => m("div", amount))
)
)
}
}
var Menu = {
view: function() {
return m("div", { id: "menu-column" }, [
m("div", [
m("input", {
type: "range", id: "volume", name: "volume",
min: "0", max: "100", step: "1", value: "25",
}),
m("label", { for: "volume" }, "Volume"),
]),
m("div",
m("button", { id: "volume-test" }, "Test volume")),
m("div", { id: "reset-bets" },
m("button", { id: "reset-bets-button" }, "Reset bets")),
])
}
}
var FullPage = {
view: function(vnode) {
let redbets = [];
let bluebets = [];
// Construct bets for each team
for (const [username, betsinfo] of Object.entries(acceptedbets)) {
betsinfo.forEach((betinfo, i) => {
if (betinfo["team"] === "red") {
redbets.push(betinfo["amount"]);
} else if (betinfo["team"] === "blue") {
bluebets.push(betinfo["amount"]);
}
})
}
return m("div", { id: "grid-wrapper" }, [
m("div", { id: "grid-blue-bet-list", class: "blue" },
m(TeamBetsContainer, { team: "blue", bets: bluebets })),
m("div", { id: "grid-blue-bet-summary", class: "blue" },
m(TeamBetSummary, { team: "blue", bets: bluebets, otherbets: redbets })),
m("div", { id: "menu-column" },
m(Menu)),
m("div", { id: "grid-red-bet-summary", class: "red" },
m(TeamBetSummary, { team: "red", bets: redbets, otherbets: bluebets })),
m("div", { id: "grid-red-bet-list", class: "red" },
m(TeamBetsContainer, { team: "red", bets: redbets })),
])
}
}
let top = document.getElementById("mainpage");
m.mount(top, FullPage)
////////////////////////////////////////////////////////////////////////////////
// Twitch chat message handling
////////////////////////////////////////////////////////////////////////////////
let alertaudio = new Audio('elegantdoor.mp3');
function playAlertSound() {
try {
let volumeLevelInt = document.getElementById("volume").valueAsNumber
alertaudio.volume = volumeLevelInt / 100.0
alertaudio.play()
} catch (e) {
console.log("Failed to play alert: ", e)
}
}
var tmiclient;
tmiclient = new tmi.Client({
options: { debug: true, messageLogLevel: "info" },
connection: {
reconnect: true,
secure: true,
},
skipMembership: true,
channels: ['saltyteemo']
});
tmiclient.connect().catch((e) => {
console.log("Failed to connect:", e);
})
let redre = /!red (\d+)/;
let bluere = /!blue (\d+)/;
let atre = /@(\S+)/;
function onIRCMessage(channel, tags, message, self) {
if (self) return; // shouldn't happen, we won't be sending
// The tags include a lowercased username but the message might contain an
// @Username that has mixed case. Force lowercasing to avoid problems.
let username = tags["username"].toLowerCase();
message = message.toLowerCase();
// Help messages from moobot sometimes include !blue or !red commands as
// examples
if (username === "moobot") return;
if (username === "xxsaltbotxx" || username === "malphite_bot" || tags.mod) {
if (message.includes("you placed")) {
let atmatch = atre.exec(message);
if (atmatch === null) {
return;
}
// When we get the first bet of a betting round, alert the user, reset the
// bet data, and start a delayed function that will mark the betting round
// as over.
if (!bettingactive) {
bettingactive = true;
playAlertSound();
acceptedbets = {};
setTimeout(() => {
console.log("betting is over, flipping flag");
bettingactive = false;
// Clear attempts but not accepted bets. Accepted will get cleared
// on flag flip.
attemptedbets = {};
}, 1000 * 60 * 4);
}
let acceptedUsername = atmatch[1];
if (!(acceptedUsername in attemptedbets)) {
console.log(`Expected username '${acceptedUsername}' to be in attempted bets, but it wasn't`);
return
}
if (!(acceptedUsername in acceptedbets)) {
acceptedbets[acceptedUsername] = [];
}
acceptedbets[acceptedUsername].push(attemptedbets[acceptedUsername])
delete attemptedbets[acceptedUsername]
m.redraw();
return;
}
return;
}
// console.log(tags)
// console.log(message)
let redmatch = redre.exec(message);
let bluematch = bluere.exec(message);
if (redmatch !== null) {
attemptedbets[username] = {
"team": "red",
"amount": parseInt(redmatch[1]),
};
m.redraw();
return;
} else if (bluematch !== null) {
attemptedbets[username] = {
"team": "blue",
"amount": parseInt(bluematch[1]),
};
m.redraw();
return;
}
}
tmiclient.on('message', onIRCMessage);
////////////////////////////////////////////////////////////////////////////////
// Button handlers
////////////////////////////////////////////////////////////////////////////////
document.getElementById("reset-bets-button").addEventListener('click', function() {
attemptedbets = {};
acceptedbets = {};
m.redraw();
});
document.getElementById("volume-test").addEventListener('click', function() {
playAlertSound();
});
////////////////////////////////////////////////////////////////////////////////
// Testing utilities
////////////////////////////////////////////////////////////////////////////////
// This is for local testing without requiring actual twitch messages to be sent
//
// Use with browser console `document.test("!blue 100")`
//
// Attaching to document makes this accessible from the console. I'm sure there
// is a better way.
document.test = (message) => {
onIRCMessage(null, { "username": "tester" }, message, false);
onIRCMessage(null, { "username": "Malphite_Bot" }, "@tester - ... You placed ... mushrooms on", false);
}
// Test data from real chat log 2022-01-14
//
// If processed improperly (including unaccepted bets):
// Blue: 43977
// Red: 22148
//
// If processed correctly:
// Blue: 38987
// Red: 7148
const testmessages = [
"erbtastic: !blue 200",
"Malphite_Bot: @erbtastic You placed 200 mushrooms on BLUE.",
"erbtastic: !blue 10",
"Malphite_Bot: @erbtastic - You placed 10 mushrooms on BLUE.",
"erbtastic: !red 5",
"Malphite_Bot: @erbtastic - ??? 5",
"krispkratos: !blue 8000",
"Malphite_Bot: @krispkratos You placed 8,000 mushrooms on BLUE.",
"Bongat_: !red 5000",
"Malphite_Bot: @Bongat_ - You do not have enough. You're current balance is 3148. If you are new, type !register to get your starting balance.",
"Ebelisk: !blue 2000",
"Malphite_Bot: @Ebelisk You placed 2,000 mushrooms on BLUE.",
"Bongat_: !balance",
"Malphite_Bot: @Bongat_ - You have 3 148 mashrooms.",
"andrewch783: !blue 7777",
"Malphite_Bot: @andrewch783 You placed 7,777 mushrooms on BLUE.",
"Bongat_: !red 3148",
"VoyboyisMyWife: !blue 1000",
"Malphite_Bot: @Bongat_ You placed 3,148 mushrooms on RED.",
"Malphite_Bot: @VoyboyisMyWife You placed 1,000 mushrooms on BLUE.",
"SleepyG807: !blue 10000",
"Malphite_Bot: @SleepyG807 You placed 10,000 mushrooms on BLUE.",
"duckycheese: !red 4000",
"Malphite_Bot: @duckycheese You placed 4,000 mushrooms on RED.",
"calzoking: !blue 10000",
"Malphite_Bot: @calzoking You placed 10,0000 mushrooms on BLUE.",
"SpaceFootballKing: !blue 5000",
"Malphite_Bot: @SpaceFootballKing - It had over for this game. Maybe next time.....yes...",
"bongatde: !red 10000",
"Malphite_Bot: @bongatde - It had over for this game. Maybe next time.....yes...",
"Skaigerah: !farm",
];
document.sendSampleInput = () => {
const parsed = testmessages.map(raw => {
let sp = raw.split(": ");
return {
"username": sp[0],
"message": sp[1]
}
})
parsed.forEach(obj => {
onIRCMessage("saltyteemo", {"username": obj["username"]}, obj["message"], null)
})
}