X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FChannelController.php;h=78648af8dea8ece74e65e2987ea06a37e7479a58;hb=167f986f468014e00d82fa2df8193f6be8dca19d;hp=f6840f87f837f27e94dc36d4dad096d8d4a4ba17;hpb=4b0f87a8b0683579c53c68f35b18e5d08c30b3b9;p=alttp.git diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index f6840f8..78648af 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -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,34 +36,256 @@ 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) { + if (!$channel->twitch_chat) { + throw new \Exception('channel has no twitch chat set'); + } + $validatedData = $request->validate([ + 'bot_nick' => 'string', + 'text' => 'string|required', + ]); + $this->authorize('editRestream', $channel); + $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; + TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick); + return $this->sendChannel($channel); + } + + public function chatSettings(Request $request, Channel $channel) { + if (!$channel->twitch_chat) { + throw new \Exception('channel has no twitch chat set'); + } + $validatedData = $request->validate([ + 'wait_msgs_min' => 'integer|min:1', + 'wait_msgs_max' => 'integer', + 'wait_time_min' => 'integer', + 'wait_time_max' => 'integer', + ]); + $this->authorize('editRestream', $channel); + $channel->chat_settings = $validatedData; + $channel->save(); + return $this->sendChannel($channel); } public function join(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } + $validatedData = $request->validate([ + 'bot_nick' => 'string', + ]); $this->authorize('editRestream', $channel); - $channel->join = true; + $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; + if ($nick == 'localhorsttv') { + $channel->join = true; + } else if ($nick == 'horstiebot') { + $channel->chat = true; + } $channel->save(); - TwitchBotCommand::join($channel->twitch_chat, $request->user()); - return $channel->toJson(); + TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick); + return $this->sendChannel($channel); } public function part(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } + $validatedData = $request->validate([ + 'bot_nick' => 'string', + ]); $this->authorize('editRestream', $channel); - $channel->join = false; + $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; + if ($nick == 'localhorsttv') { + $channel->join = false; + } else if ($nick == 'horstiebot') { + $channel->chat = false; + } $channel->save(); - TwitchBotCommand::part($channel->twitch_chat, $request->user()); + TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick); + return $this->sendChannel($channel); + } + + public function deleteCommand(Channel $channel, $command) { + $this->authorize('editRestream', $channel); + if (isset($channel->chat_commands[$command])) { + $cmds = $channel->chat_commands; + unset($cmds[$command]); + $channel->chat_commands = $cmds; + $channel->save(); + } + return $this->sendChannel($channel); + } + + public function saveCommand(Request $request, Channel $channel, $command) { + $this->authorize('editRestream', $channel); + + $validatedData = $request->validate([ + 'command' => 'string', + 'restrict' => 'string', + 'type' => 'string', + ]); + + $cmds = $channel->chat_commands; + $cmds[$command] = $validatedData; + $channel->chat_commands = $cmds; + $channel->save(); + + 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']); + $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 $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 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); + + $validatedData = $request->validate([ + 'active_message' => 'string', + 'cancel_message' => 'string', + 'invalid_solution_message' => 'string', + 'no_winners_message' => 'string', + 'not_active_message' => 'string', + '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_max' => 'integer|min:0', + 'points_close_other' => 'numeric|min:0|max:5', + 'start_message' => 'string', + 'stop_message' => 'string', + 'winners_message' => 'string', + ]); + + $settings = $channel->guessing_settings; + $settings[$name] = $validatedData; + $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', + ]); + } + }