+ return $this->sendChannel($channel);
+ }
+
+ public function controlGuessingGame(Request $request, Channel $channel, $name) {
+ $this->authorize('editRestream', $channel);
+
+ $validatedData = $request->validate([
+ 'action' => 'required|in:cancel,solve,start,stop',
+ 'solution' => '',
+ ]);
+
+ switch ($validatedData['action']) {
+ case 'cancel':
+ $channel->clearGuessing();
+ $msg = $channel->getGuessingSetting('cancel_message');
+ if (!empty($msg)) {
+ TwitchBotCommand::chat($channel->twitch_chat, $msg);
+ }
+ break;
+ case 'solve':
+ if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
+ $winners = $channel->solveGuessing($validatedData['solution']);
+ $msg = $channel->listGuessingWinners($winners);
+ if (!empty($msg)) {
+ TwitchBotCommand::chat($channel->twitch_chat, $msg);
+ }
+ $channel->clearGuessing();
+ }
+ break;
+ case 'start':
+ if (!$channel->hasActiveGuessing()) {
+ $channel->startGuessing($name);
+ $msg = $channel->getGuessingSetting('start_message');
+ if (!empty($msg)) {
+ TwitchBotCommand::chat($channel->twitch_chat, $msg);
+ }
+ }
+ break;
+ case 'stop':
+ if ($channel->hasActiveGuessing()) {
+ $channel->stopGuessing();
+ $msg = $channel->getGuessingSetting('stop_message');
+ if (!empty($msg)) {
+ TwitchBotCommand::chat($channel->twitch_chat, $msg);
+ }
+ }
+ break;
+ }
+
+ return $this->sendChannel($channel);
+ }
+
+ public function getGuessingGame(Channel $channel, $name) {
+ $this->authorize('editRestream', $channel);
+
+ $cutoff = $channel->guessing_start;
+ if (is_null($cutoff)) {
+ $last = $channel->winners()->latest()->first();
+ $cutoff = $last->pod;
+ }
+ $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
+ $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
+ return [
+ 'guesses' => $guesses->toArray(),
+ 'winners' => $winners->toArray(),
+ ];
+ }
+
+ public function getGuessingGameLeaderboard(Channel $channel, $type) {
+ return [
+ 'all' => $channel->getGuessingLeaderboard(),
+ ];
+ }
+
+ public function getGuessingGameMonitor($key) {
+ $channel = Channel::where('access_key', '=', $key)->firstOrFail();
+
+ $cutoff = $channel->guessing_start;
+ if (is_null($cutoff)) {
+ $last = $channel->winners()->latest()->first();
+ $cutoff = $last->pod;
+ }
+ $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
+ $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
+ return [
+ 'channel' => $channel->toArray(),
+ 'guesses' => $guesses->toArray(),
+ 'winners' => $winners->toArray(),
+ ];