-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquesoqueue.cpp
291 lines (260 loc) · 8.95 KB
/
quesoqueue.cpp
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
#include "quesoqueue.h"
#include "../keys.h"
#include <algorithm>
#include <iostream>
#include <fstream>
#include <regex>
#include <sstream>
#include <tuple>
QuesoQueue::QuesoQueue(const Twitch &twitch) : _twitch(twitch) {
// ...
}
bool QuesoQueue::isValidLevelCode(std::string levelCode) {
std::string levelBit("["
"A-Ha-h" // exclude I
"J-Nj-n" // exclude O
"P-Yp-y" // exclude Z
"0-9" // numbers
"]");
std::string delimBit("[-. ]");
std::regex validLevelCode(
levelBit + "{3}" + // the first chonk
delimBit + "?" + // it's ok not to use a delimiter
levelBit + "{3}" + // the second chonk
delimBit + "?" + // still ok not to use a delimiter
levelBit + "{3}", // the last chonk
std::regex_constants::egrep
);
return std::regex_match(levelCode, validLevelCode);
}
std::string QuesoQueue::Add(Level level) {
if (_levels.size() >= maxSize) {
return std::string("Sorry, the level queue is full!");
}
if (!isValidLevelCode(level.levelCode)) {
return std::string("I'm pretty sure '" + level.levelCode + "' isn't " +
"valid. Try again!");
}
if (Current()->submitter == level.submitter && level.submitter != Auth::channel) {
return std::string("Wait til your level has been completed before you submit again.");
}
// Does the viewer already have a level in queue?
auto result = std::find_if(std::begin(_levels),
std::end(_levels),
[level] (Level l) {
return l.submitter==level.submitter;
});
// Or, if the submitter is the channel name, then THEY OWN US.
if (result == _levels.end() || level.submitter == Auth::channel) {
auto online_levels = std::get<0>(List()).size();
// push to the end of the queue
if (_levels.empty() && !Current().has_value()) {
_current = std::make_optional(level);
} else {
_levels.push_back(level);
online_levels++;
}
std::stringstream ss;
// Since they JUST added it, we can pretty safely assume they're online.
ss << level.submitter;
ss << ", ";
ss << level.levelCode;
ss << " has been added to the queue. Currently in position #";
ss << online_levels + 1;
ss << ".";
SaveState();
return ss.str();
}
else {
return std::string("Sorry, viewers are limited to one submission at a time.");
}
}
std::string QuesoQueue::ModRemove(std::string username) {
if (username.empty()) {
return std::string("You can use !remove <username> to kick out someone "
"else's level; if you want to skip the current one use !next.");
}
// remove the level with the matching code
auto toRemove = std::find_if(_levels.begin(),
_levels.end(),
[username](Level l) {
return l.submitter == username;
});
// delet
if (toRemove != _levels.end()) {
_levels.erase(toRemove);
SaveState();
return std::string("Ok, I removed " + username + "'s level from the queue.");
} else {
return std::string("The level by " + username + " isn't in the queue " +
"now. It wasn't before, but it isn't now, too.");
}
}
std::string QuesoQueue::Remove(std::string username) {
auto toRemove = std::find_if(_levels.begin(),
_levels.end(),
[username](Level l) {
return l.submitter == username;
});
// delet
if (Current()->submitter == username) {
return std::string("We're playing that now! Don't take this away from us!");
} else if (toRemove != _levels.end()) {
_levels.erase(toRemove);
SaveState();
return std::string("Ok " + username + ", your level was removed from the queue.");
} else {
return std::string("Ok " + username + ", your level isn't in the queue " +
"now. It wasn't before, but it isn't now, too.");
}
}
std::string QuesoQueue::Replace(std::string username, std::string newLevelCode) {
if (!isValidLevelCode(newLevelCode)) {
return std::string("I'm pretty sure '" + newLevelCode + "' isn't " +
"valid. Try again!");
}
auto toReplace = std::find_if(_levels.begin(),
_levels.end(),
[username](Level l) {
return l.submitter == username;
});
if (toReplace != _levels.end()) {
toReplace->levelCode = newLevelCode;
SaveState();
return std::string("Ok " + username + ", you are now in queue with level "
+ newLevelCode + ".");
} else if (Current()->submitter == username) {
_current->levelCode = newLevelCode;
SaveState();
return std::string("Ok " + username + ", you are now in queue with level "
+ newLevelCode + ".");
} else {
return std::string("I didn't find a level for " + username
+ ". Try !add.");
}
}
int QuesoQueue::Position(std::string username) {
if (Current()->submitter == username) {
return 0;
}
if (_levels.empty()) {
return -1;
}
int position = 0;
auto list = List();
auto both = std::get<0>(list);
for (Level l : std::get<1>(list)) {
both.push_back(l);
}
for (Level l : both) {
position++;
if (l.submitter == username) {
return position;
}
}
// not in queue
return -1;
}
std::optional<Level> QuesoQueue::Punt() {
if (_levels.empty()) {
return std::nullopt;
}
auto top = Current();
if (!top) {
return std::nullopt;
}
auto next = Next();
std::cout << Add(top.value()) << std::endl;
return next;
}
std::optional<Level> QuesoQueue::Next() {
auto list = List();
// Concatenate both lists
std::deque<Level> both(std::get<0>(list));
for (Level l : std::get<1>(list)) {
both.push_back(l);
}
if (both.empty()) {
_current = std::nullopt;
} else {
_current = std::make_optional(both.front());
}
// Remove current (it's in a special current place now)
for (auto pos = _levels.begin(); pos != _levels.end(); pos++) {
if (pos->submitter == _current->submitter &&
pos->levelCode == _current ->levelCode) {
_levels.erase(pos);
break;
}
}
SaveState();
return _current;
}
std::optional<Level> QuesoQueue::Dip(std::string username) {
// Find the earliest submitted level from that username.
// Use iterators so we can delete it.
for (auto lit = _levels.begin(); lit != _levels.end(); lit++) {
// Found it!
if (lit->submitter == username) {
// Set _current. (This pops the old current.)
_current = *lit;
// Remove this one from the list.
_levels.erase(lit);
// We modified the queue, so save.
SaveState();
// Return the new current.
return Current();
}
}
// Didn't find it. :(
return std::nullopt;
}
std::optional<Level> QuesoQueue::Current() {
//// This is the case when we haven't started yet?
//if (!_current && !_levels.empty()) {
// _current = Next();
//}
return _current;
}
PriorityQueso QuesoQueue::List() {
std::deque<Level> online, offline;
std::set<std::string> online_users = _twitch.getOnlineUsers(Auth::channel);
for (Level l : _levels) {
if (online_users.find(l.submitter) != online_users.end()) {
online.push_back(l);
} else {
offline.push_back(l);
}
}
return std::make_tuple(online, offline);
}
void QuesoQueue::SaveState() {
std::ofstream savefile("queso.save", std::ios_base::out | std::ios_base::trunc);
if (_current && !(_current->submitter.empty() || _current->levelCode.empty())) {
savefile << _current->submitter << " " << _current->levelCode << std::endl;
}
for (Level l : _levels) {
savefile << l.submitter << " " << l.levelCode << std::endl;
}
}
void QuesoQueue::LoadLastState() {
std::ifstream savefile("queso.save");
_levels.clear();
if (savefile) {
std::string submitter;
if (savefile >> submitter) {
_current = std::make_optional(Level());
_current->submitter = submitter;
std::getline(savefile >> std::ws, _current->levelCode);
}
}
while(savefile) {
Level l;
savefile >> l.submitter;
std::getline(savefile >> std::ws, l.levelCode);
if (l.submitter.empty() || l.levelCode.empty()) {
continue;
}
_levels.push_back(l);
}
}