-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
425 lines (413 loc) · 14 KB
/
server.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
const tmi = require("tmi.js");
const request = require("request");
const express = require("express");
const app = express();
const port = 3000;
const client = new tmi.Client({
options: { debug: true },
identity: {
username: process.env.bot_account,
password: process.env.oauth,
},
channels: [process.env.twitch_channel],
});
client.connect();
//Queue closed by default
var queue_open = false;
var queue = [];
var turn_counter = [];
var firsts_first = false;
app.use(express.static("public"));
app.set("view engine", "ejs");
app.get("/", function (req, res) {
res.render("index.ejs", {
queue: queue,
turns: turn_counter,
queue_open: queue_open,
first_turn_first: firsts_first,
banner_image: process.env.banner_image
});
});
app.listen(port, () => {
console.log(`BootcutBot listening on port ${port}`);
});
client.on("message", (channel, tags, message, self) => {
// Ignore echoed messages.
if (self) return;
let isMod = tags.mod || tags['user-type'] === 'mod';
let isBroadcaster = channel.slice(1) === tags.username;
let isModUp = isMod || isBroadcaster;
if (message.toLowerCase() === "!hello") {
client.say(channel, `@${tags.username}, heya!`);
}
//Let a user join the queue
if (message.toLowerCase().startsWith("!join")) {
if (queue_open) {
//check if user is in queue
var in_queue = queue.findIndex(function (queue, index) {
if (queue.username == tags.username) return true;
});
if (in_queue < 0) {
//check the turn counter
var turn_count = turn_counter.reduce(
(acc, cur) => (cur.username === tags.username ? ++acc : acc),
0
);
tags.turn_count = turn_count;
//add user to queue
queue.push(tags);
//Get their position
var place_in_queue = queue.length;
var place_ordinal = ordinal_suffix_of(place_in_queue);
//Check if in firsts_first mode
if (firsts_first && turn_count > 0) {
var turn_ordinal = ordinal_suffix_of(turn_count + 1);
//Let them know they are in
client.say(
channel,
`@${tags["display-name"]}, added to queue! You're ${place_ordinal} in the queue, this will be your ${turn_ordinal} turn. Note: Firsts Turns First Mode is on, so any players who haven't gone yet will be picked before you.`
);
} else {
//Let them know they are in
client.say(
channel,
`@${tags["display-name"]}, added to queue! You're ${place_ordinal} in the queue.`
);
}
} else {
//Get their position
var place_in_queue = in_queue + 1;
var place_ordinal = ordinal_suffix_of(place_in_queue);
//Light shame for trying to requeue while in queue
client.say(
channel,
`@${tags["display-name"]} you're already queued silly. You're ${place_ordinal} in the queue.`
);
}
} else {
client.say(
channel,
`@${tags["display-name"]} the queue is closed. Try again later.`
);
}
}
if (message.toLowerCase() === "!leave") {
//Check if the user is in the queue
var in_queue = queue.findIndex(function (queue, index) {
if (queue.username == tags.username) return true;
});
//check if user is in queue
if (in_queue < 0) {
//Let them know they aren't in the queue
client.say(channel, `@${tags["display-name"]} you're not in the queue.`);
} else {
//remove user from queue
queue.splice(in_queue, 1);
client.say(channel, `@${tags["display-name"]} has left the queue.`);
}
}
if (message.toLowerCase() === "!next") {
//check if broadcaster
if (isBroadcaster || tags.username == "zilchgnu") {
//Check for firsts_first
if (firsts_first) {
var found_zero_turn = false;
var i = 0;
var dupe_queue = queue.slice();
//loop over the whole queue
while (i < queue.length) {
//get the next element from the dupe_queue
var current_player = dupe_queue[0];
//check the turn counter
var turn_count = turn_counter.reduce(
(acc, cur) =>
cur.username === current_player.username ? ++acc : acc,
0
);
//see if this a 0 turn player
if (turn_count === 0) {
found_zero_turn = true;
break;
} else {
//remove this player from the dupe queue
dupe_queue.splice(0, 1);
}
i++;
}
//If we didn't find a 0 turn player then just go random
if (!found_zero_turn) {
client.say(
channel,
`No Zero Turn Players found. Picking the next player.`
);
current_player = queue[0];
}
} else {
var current_player = queue[0];
}
//Check if user is in chat
//'+ process.env.twitch_account +'
request.get(
"https://tmi.twitch.tv/group/user/" +
process.env.twitch_channel +
"/chatters",
function (error, response, body) {
if (!error && response.statusCode == 200) {
//this page returns a list of the users in chat refreshed like every 5 min as json
var chat = JSON.parse(body);
var in_chat = false;
//Different types of chatters are in different objects loop through and check each of them
for (const [key, value] of Object.entries(chat.chatters)) {
console.log(current_player);
//see if the user is in any of the chats
var in_chat = value.includes(current_player.username);
if (in_chat) {
break;
}
}
if (in_chat) {
//remove user from queue
queue.splice(0, 1);
//check the turn counter
const turn_count = turn_counter.reduce(
(acc, cur) =>
cur.username === current_player.username ? ++acc : acc,
0
);
//add them to the turn counter
turn_counter.push(current_player);
//let them know
if (turn_count > 0) {
var turn_ordinal = ordinal_suffix_of(turn_count + 1);
client.say(
channel,
`@${current_player["display-name"]} has been choosen again, this is your ${turn_ordinal} turn.`
);
} else {
client.say(
channel,
`@${current_player["display-name"]} step on up to the battle board of doom!`
);
}
} else {
//remove user from queue
queue.splice(0, 1);
//Add them to the end of the queue
queue.push(current_player);
//let them know
client.say(
channel,
`@${current_player["display-name"]} hasn't been in chat for a while, moving them to the back of the queue. Please run !next again`
);
}
}
//TODO: Handle any errors getting the list of chatters
}
);
}
}
if (message.toLowerCase() === "!random") {
//check if broadcaster
if (isBroadcaster || tags.username == "zilchgnu") {
//Check for firsts_first
if (firsts_first) {
var found_zero_turn = false;
var i = 0;
var dupe_queue = queue.slice();
//loop over the whole queue
while (i < queue.length) {
//get a random element from the dupe_queue
var random_index = Math.floor(Math.random() * dupe_queue.length);
//get the player
var current_player = queue[random_index];
//check the turn counter
var turn_count = turn_counter.reduce(
(acc, cur) =>
cur.username === current_player.username ? ++acc : acc,
0
);
//see if this a 0 turn player
if ((turn_count = 0)) {
found_zero_turn = true;
break;
} else {
//remove this player from the dupe queue
dupe_queue.splice(random_index, 1);
}
i++;
}
//If we didn't find a 0 turn player then just go random
if (!found_zero_turn) {
client.say(
channel,
`No Zero Turn Players found. Picking any random player.`
);
//Pick a random player from the queue
var random_index = Math.floor(Math.random() * queue.length);
var current_player = queue[random_index];
}
} else {
//Pick a random player from the queue
var random_index = Math.floor(Math.random() * queue.length);
var current_player = queue[random_index];
}
//check the turn counter
turn_count = turn_counter.reduce(
(acc, cur) => (cur.username === current_player.username ? ++acc : acc),
0
);
//Check if user is in chat
request.get(
"https://tmi.twitch.tv/group/user/" +
process.env.twitch_channel +
"/chatters",
function (error, response, body) {
if (!error && response.statusCode == 200) {
//this page returns a list of the users in chat refreshed like every 5 min as json
var chat = JSON.parse(body);
var in_chat = false;
//Different types of chatters are in different objects loop through and check each of them
for (const [key, value] of Object.entries(chat.chatters)) {
//see if the user is in any of the chats
var in_chat = value.includes(current_player.username);
if (in_chat) {
break;
}
}
if (in_chat) {
//remove user from queue
queue.splice(random_index, 1);
//add them to the turn counter
turn_counter.push(current_player);
//let them know
if (turn_count > 0) {
var turn_ordinal = ordinal_suffix_of(turn_count + 1);
client.say(
channel,
`@${current_player["display-name"]} has been randomly selected, this is your ${turn_ordinal} turn.`
);
} else {
client.say(
channel,
`@${current_player["display-name"]} has been randomly selected, step on up to the battle board of doom!`
);
}
} else {
//remove user from queue
queue.splice(random_index, 1);
//Add them to the end of the queue
queue.push(current_player);
//let them know
client.say(
channel,
`@${current_player["display-name"]} hasn't been in chat for a while, moving them to the back of the queue. Please run !random again`
);
}
}
//TODO: Handle any errors getting the list of chatters
}
);
}
}
if (message.toLowerCase() === "!list") {
//return page with list of queue
client.say(
channel,
`@${tags["display-name"]} you can view the full queue and previous turns here. ${process.env.queue_list_url}`
);
}
if (message.toLowerCase() === "!position") {
//reply to user with their position in queue
//Check if the user is in the queue
var in_queue = queue.findIndex(function (queue, index) {
if (queue.username == tags.username) return true;
});
//See if they are in the queue
if (in_queue < 0) {
//tell them they aren't in the queue
client.say(
channel,
`@${tags["display-name"]} can't find you in the queue babe.`
);
} else {
//Get their position
var place_in_queue = in_queue + 1;
var place_ordinal = ordinal_suffix_of(place_in_queue);
//let the know their place.
client.say(
channel,
`@${tags["display-name"]} you are ${place_ordinal} in the queue babe.`
);
}
}
//allow people to join the queue
if (message.toLowerCase() === "!open") {
//check if mod
if (isBroadcaster || tags.username == "zilchgnu") {
//set queue_open to true
queue_open = true;
//let the chat know what is up
client.say(channel, `@${tags["display-name"]} has opened the queue!`);
}
}
//stop people from joining the queue
if (message.toLowerCase() === "!close") {
//check if mod
if (isBroadcaster|| tags.username == "zilchgnu") {
//set queue_open to true
queue_open = false;
//let the chat know what is up
client.say(channel, `@${tags["display-name"]} has closed the queue!`);
}
}
//stop people from joining the queue
if (message.toLowerCase() === "!firsts_first") {
//check if mod
if (isBroadcaster || tags.username == "zilchgnu") {
//toggle firsts only
if (!firsts_first) {
//set queue_open to true
firsts_first = true;
//let the chat know what is up
client.say(
channel,
`First Turns First Mode ACTIVATED. The queue will choose players without a turn first.`
);
} else {
//set queue_open to true
firsts_first = false;
//let the chat know what is up
client.say(
channel,
`First Turns First Mode DEACTIVATED. The queue has been returned to normal. `
);
}
}
}
if (message.toLowerCase() === "!clear") {
//clear the queue
//check if mod
if (isBroadcaster || tags.username == "zilchgnu") {
queue = [];
turn_counter = [];
client.say(
channel,
`The Glorious @${tags["display-name"]} has cleared the queue!`
);
}
}
});
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}