]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ChannelController.php
guessing game controls
[alttp.git] / app / Http / Controllers / ChannelController.php
index cbd01104cb3e7cd497d42a44fd33206fdf214909..4cc49c1981fa1c5c6e06be05f43ea5eb3570aa98 100644 (file)
@@ -138,6 +138,82 @@ class ChannelController extends Controller {
                return $channel->toJson();
        }
 
+       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']);
+                                       $names = [];
+                                       foreach ($winners as $winner) {
+                                               if ($winner->score > 0) {
+                                                       $names[] = $winner->uname;
+                                               }
+                                       }
+                                       if (empty($names)) {
+                                               $msg = $channel->getGuessingSetting('no_winners_message');
+                                       } else {
+                                               $msg = $channel->getGuessingSetting('winners_message');
+                                               $msg = str_replace('{names}', $channel->listAnd($names), $msg);
+                                       }
+                                       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 $channel->toJson();
+       }
+
+       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 saveGuessingGame(Request $request, Channel $channel, $name) {
                $this->authorize('editRestream', $channel);
 
@@ -150,7 +226,7 @@ class ChannelController extends Controller {
                        'points_exact_first' => 'numeric|min:1|max:5',
                        'points_exact_other' => 'numeric|min:0|max:5',
                        'points_close_first' => 'numeric|min:0|max:5',
-                       'points_close_min' => 'integer|min:0',
+                       'points_close_max' => 'integer|min:0',
                        'points_close_other' => 'numeric|min:0|max:5',
                        'start_message' => 'string',
                        'stop_message' => 'string',