-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqueue.js
290 lines (261 loc) · 8.7 KB
/
queue.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
const settings = require('./settings.js');
const twitch = require('./twitch.js').twitch();
const fs = require('fs');
var current_level = undefined;
var levels = new Array();
const cache_filename = "queso.save";
const isValidLevelCode = (level_code) => {
const level_bit = '[A-Ha-hJ-Nj-nP-Yp-y0-9]{3}';
const delim_bit = '[-. ]?';
const valid_level_code = level_bit + delim_bit + level_bit + delim_bit + level_bit;
return level_code.match(valid_level_code);
};
const queue = {
add: (level) => {
if (levels.length >= settings.max_size) {
return "Sorry, the level queue is full!";
}
if (!isValidLevelCode(level.code)) {
return "I'm pretty sure '" + level.code + "' isn't a valid code. Try again.";
}
if (current_level != undefined && current_level.submitter == level.submitter && level.submitter != settings.channel) {
return "Wait for your level to be completed before you submit again.";
}
var result = levels.find(x => x.submitter == level.submitter);
if (result == undefined || level.submitter == settings.channel) {
levels.push(level);
queue.save();
return level.submitter + ", " + level.code + " has been added to the queue.";
} else {
return "Sorry, viewers are limited to one submission at a time.";
}
},
modRemove: (username) => {
if (username == '') {
return "You can use !remove <username> to kick out someone else's level; if you want to skip the current one, use !next.";
}
levels = levels.filter(x => x.submitter != username);
return "Ok, I removed " + username + "'s level from the queue.";
},
remove: (username) => {
if (current_level != undefined && current_level.submitter == username) {
return "We're playing that level right now! Don't take this away from us!";
}
levels = levels.filter(x => x.submitter != username);
return username + "'s level removed from the queue.";
},
replace: (username, new_level_code) => {
if (!isValidLevelCode(new_level_code)) {
return "I'm pretty sure '" + new_level_code + "' isn't a valid code. Try again.";
}
var old_level = levels.find(x => x.submitter == username);
if (old_level != undefined) {
old_level.code = new_level_code;
queue.save();
return "Ok " + username + ", your code in the queue is now " + new_level_code + ".";
} else if (current_level != undefined && current_level.submitter == username) {
current_level.code = new_level_code;
queue.save();
return "Ok " + username + ", your code in the queue is now " + new_level_code + ".";
} else {
return "I didn't find a level for " + username + " in the queue. Use !add to add one.";
}
},
position: async (username) => {
if (current_level != undefined && current_level.submitter == username) {
return 0;
}
if (levels.length == 0) {
return -1;
}
var list = await queue.list();
var both = list.online.concat(list.offline);
var index = both.findIndex(x => x.submitter == username);
if (index != -1) {
return (index + 1) + ((current_level != undefined) ? 1 : 0);
}
return -1;
},
punt: async () => {
if (current_level === undefined) {
return "The nothing you aren't playing cannot be punted.";
}
var top = current_level;
current_level = undefined;
queue.add(top);
return 'Ok, adding the current level back into the queue.';
},
next: async () => {
var list = await queue.list();
var both = list.online.concat(list.offline);
if (both.length === 0) {
current_level = undefined;
} else {
current_level = both.shift();
}
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
subnext: async () => {
var list = await queue.sublist();
var both = list.online.concat(list.offline);
if (both.length === 0) {
current_level = undefined;
} else {
current_level = both.shift();
}
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
modnext: async () => {
var list = await queue.modlist();
var both = list.online.concat(list.offline);
if (both.length === 0) {
current_level = undefined;
} else {
current_level = both.shift();
}
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
dip: (username) => {
var index = levels.findIndex(x => x.submitter == username);
if (index != -1) {
current_level = levels[index];
levels.splice(index, 1);
queue.save();
return current_level;
}
return undefined;
},
current: () => {
return current_level;
},
random: async () => {
var list = await queue.list();
var eligible_levels = list.online;
if (eligible_levels.length == 0) {
eligible_levels = list.offline;
if (eligible_levels.length == 0) {
current_level = undefined;
return current_level;
}
}
var random_index = Math.floor(Math.random() * eligible_levels.length);
current_level = eligible_levels[random_index];
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
subrandom: async () => {
var list = await queue.sublist();
var eligible_levels = list.online;
if (eligible_levels.length == 0) {
eligible_levels = list.offline;
if (eligible_levels.length == 0) {
current_level = undefined;
return current_level;
}
}
var random_index = Math.floor(Math.random() * eligible_levels.length);
current_level = eligible_levels[random_index];
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
modrandom: async () => {
var list = await queue.modlist();
var eligible_levels = list.online;
if (eligible_levels.length == 0) {
eligible_levels = list.offline;
if (eligible_levels.length == 0) {
current_level = undefined;
return current_level;
}
}
var random_index = Math.floor(Math.random() * eligible_levels.length);
current_level = eligible_levels[random_index];
var index = levels.findIndex(x => x.code == current_level.code);
levels.splice(index, 1);
queue.save();
return current_level;
},
list: async () => {
var online = new Array();
var offline = new Array();
await twitch.getOnlineUsers(settings.channel).then(online_users => {
online = levels.filter(x => online_users.has(x.username));
offline = levels.filter(x => !online_users.has(x.username));
});
return {
online: online,
offline: offline
};
},
sublist: async () => {
var online = new Array();
var offline = new Array();
await twitch.getOnlineSubscribers(settings.channel).then(online_users => {
online = levels.filter(x => online_users.has(x.username));
offline = levels.filter(x => !online_users.has(x.username));
});
return {
online: online,
offline: offline
};
},
modlist: async () => {
var online = new Array();
var offline = new Array();
await twitch.getOnlineMods(settings.channel).then(online_users => {
online = levels.filter(x => online_users.has(x.username));
offline = levels.filter(x => !online_users.has(x.username));
});
return {
online: online,
offline: offline
};
},
save: () => {
var levels_to_save = levels;
if (current_level != undefined) {
levels_to_save = [current_level].concat(levels_to_save);
}
var new_data = JSON.stringify(levels_to_save, null, 2);
fs.writeFileSync(cache_filename, new_data);
},
load: () => {
if (fs.existsSync(cache_filename)) {
var raw_data = fs.readFileSync(cache_filename);
levels = JSON.parse(raw_data);
const username_missing = level => !level.hasOwnProperty('username');
if (levels.some(username_missing)) {
console.warn(`Usernames are not set in the file ${cache_filename}!`);
console.warn('Assuming that usernames are lowercase Display Names which does work with Localized Display Names.');
console.warn('To be safe, clear the queue with !clear.');
levels.forEach(level => {
if (username_missing(level)) {
level.username = level.submitter.toLowerCase();
}
});
}
current_level = undefined;
}
},
clear: () => {
current_level = undefined;
levels = new Array();
queue.save();
}
};
module.exports = {
quesoqueue: () => { return queue; }
};