]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ChannelController.php
add simple guessing game browser source
[alttp.git] / app / Http / Controllers / ChannelController.php
index 4cc49c1981fa1c5c6e06be05f43ea5eb3570aa98..78648af8dea8ece74e65e2987ea06a37e7479a58 100644 (file)
@@ -6,6 +6,8 @@ use App\Models\Channel;
 use App\Models\TwitchBotCommand;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Http\Request;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Gate;
 
 class ChannelController extends Controller {
 
@@ -34,12 +36,12 @@ class ChannelController extends Controller {
                                ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
                }
                $channels = $channels->limit(5);
-               return $channels->get()->toJson();
+               return $this->sendChannels($channels->get());
        }
 
        public function single(Request $request, Channel $channel) {
                $this->authorize('view', $channel);
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function chat(Request $request, Channel $channel) {
@@ -53,7 +55,7 @@ class ChannelController extends Controller {
                $this->authorize('editRestream', $channel);
                $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
                TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function chatSettings(Request $request, Channel $channel) {
@@ -69,7 +71,7 @@ class ChannelController extends Controller {
                $this->authorize('editRestream', $channel);
                $channel->chat_settings = $validatedData;
                $channel->save();
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function join(Request $request, Channel $channel) {
@@ -88,7 +90,7 @@ class ChannelController extends Controller {
                }
                $channel->save();
                TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function part(Request $request, Channel $channel) {
@@ -107,7 +109,7 @@ class ChannelController extends Controller {
                }
                $channel->save();
                TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function deleteCommand(Channel $channel, $command) {
@@ -118,7 +120,7 @@ class ChannelController extends Controller {
                        $channel->chat_commands = $cmds;
                        $channel->save();
                }
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function saveCommand(Request $request, Channel $channel, $command) {
@@ -135,7 +137,7 @@ class ChannelController extends Controller {
                $channel->chat_commands = $cmds;
                $channel->save();
 
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function controlGuessingGame(Request $request, Channel $channel, $name) {
@@ -195,7 +197,7 @@ class ChannelController extends Controller {
                                break;
                }
 
-               return $channel->toJson();
+               return $this->sendChannel($channel);
        }
 
        public function getGuessingGame(Channel $channel, $name) {
@@ -214,6 +216,23 @@ class ChannelController extends Controller {
                ];
        }
 
+       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(),
+               ];
+       }
+
        public function saveGuessingGame(Request $request, Channel $channel, $name) {
                $this->authorize('editRestream', $channel);
 
@@ -238,7 +257,35 @@ class ChannelController extends Controller {
                $channel->guessing_settings = $settings;
                $channel->save();
 
+               return $this->sendChannel($channel);
+       }
+
+       protected function sendChannel(Channel $channel) {
+               if (Gate::allows('editRestream', $channel)) {
+                       $this->unhideChannel($channel);
+               }
                return $channel->toJson();
        }
 
+       protected function sendChannels(Collection $channels) {
+               foreach ($channels as $channel) {
+                       if (Gate::allows('editRestream', $channel)) {
+                               $this->unhideChannel($channel);
+                       }
+               }
+               return $channels->toJson();
+       }
+
+       private function unhideChannel(Channel $channel) {
+               $channel->makeVisible([
+                       'access_key',
+                       'chat',
+                       'chat_commands',
+                       'chat_settings',
+                       'guessing_settings',
+                       'join',
+                       'twitch_chat',
+               ]);
+       }
+
 }