Skip to content

Commit 61e2b07

Browse files
committed
Add a 'random' command that will dip into the online levels (or offline ones if there are no online levels).
1 parent 3e82f00 commit 61e2b07

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

chat.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ void Chat::HandleMessage(std::stringstream message, std::string sender) {
238238
WriteMessage(NextLevelMessage(l));
239239
} else if (command == "current") {
240240
WriteMessage(CurrentLevelMessage(_qq.Current()));
241+
} else if (command == "random" && sender == Auth::channel) {
242+
_timer.Reset();
243+
std::optional<Level> l = _qq.Random();
244+
if (l) {
245+
WriteMessage(std::string("/marker " + l->levelCode + ", submitted by "
246+
+ l->submitter));
247+
}
248+
WriteMessage(NextLevelMessage(l));
241249
} else if (command == "list") {
242250
WriteMessage(LevelListMessage(_qq.Current(), _qq.List()));
243251
} else if (command == "position") {

quesoqueue.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ std::optional<Level> QuesoQueue::Current() {
222222
return _current;
223223
}
224224

225+
std::optional<Level> QuesoQueue::Random() {
226+
auto list = List();
227+
std::deque<Level> levels(std::get<0>(list)); // Use online levels first.
228+
229+
if (levels.empty()) {
230+
levels = std::get<1>(list); // See if there are offline levels.
231+
if (levels.empty()) {
232+
_current = std::nullopt;
233+
return _current;
234+
}
235+
}
236+
std::vector<Level> new_current = {};
237+
std::sample(levels.begin(), levels.end(), std::back_inserter(new_current),
238+
1, std::mt19937{std::random_device{}()});
239+
240+
if (new_current.empty()) {
241+
_current = std::nullopt;
242+
} else {
243+
_current = std::make_optional(new_current[0]);
244+
}
245+
246+
// Remove current (it's in a special current place now)
247+
_levels.erase(
248+
std::find_if(
249+
_levels.begin(),
250+
_levels.end(),
251+
[&](auto l){
252+
return l.submitter == _current->submitter &&
253+
l.levelCode == _current->levelCode;
254+
}
255+
)
256+
);
257+
SaveState();
258+
return _current;
259+
}
260+
225261
PriorityQueso QuesoQueue::List() {
226262
std::deque<Level> online, offline;
227263
std::set<std::string> online_users = _twitch.getOnlineUsers(Auth::channel);

quesoqueue.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class QuesoQueue {
4646
*/
4747
std::optional<Level> Punt();
4848

49+
/**
50+
* Selects a random level from the queue and returns the new top (subject to priority queue split)
51+
*/
52+
std::optional<Level> Random();
53+
4954
/**
5055
* Split the stored level queue into online and offline for printing
5156
*/

0 commit comments

Comments
 (0)