X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FChannelController.php;h=883ca65eff2360d93bcd20d9fb2d2bf93640caad;hb=ebdf8e5f6761de2abd85b01096a67dee62d7d4aa;hp=4cc49c1981fa1c5c6e06be05f43ea5eb3570aa98;hpb=c66d9d3c5eda563842c683827da1abf445b65483;p=alttp.git diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index 4cc49c1..883ca65 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -6,17 +6,24 @@ 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 { public function search(Request $request) { $validatedData = $request->validate([ + 'id' => 'array', + 'id.*' => 'integer|numeric', 'joinable' => 'boolean|nullable', 'manageable' => 'boolean|nullable', 'phrase' => 'string|nullable', ]); $channels = Channel::query(); + if (!empty($validatedData['id'])) { + $channels = $channels->whereIn('id', $validatedData['id']); + } if (isset($validatedData['joinable']) && $validatedData['joinable']) { $channels = $channels->where('twitch_chat', '!=', ''); } @@ -34,12 +41,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) { @@ -48,12 +55,17 @@ class ChannelController extends Controller { } $validatedData = $request->validate([ 'bot_nick' => 'string', - 'text' => 'string|required', + 'category' => 'string', + 'text' => 'string', ]); $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(); + if (empty($validatedData['category'])) { + TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick); + } else { + TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick); + } + return $this->sendChannel($channel); } public function chatSettings(Request $request, Channel $channel) { @@ -61,6 +73,8 @@ class ChannelController extends Controller { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ + 'language' => 'string|in:de,en,es,fr', + 'respond' => 'string|in:50,no,yes', 'wait_msgs_min' => 'integer|min:1', 'wait_msgs_max' => 'integer', 'wait_time_min' => 'integer', @@ -69,7 +83,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 +102,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 +121,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 +132,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 +149,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) { @@ -157,18 +171,7 @@ class ChannelController extends Controller { 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); - } + $msg = $channel->listGuessingWinners($winners); if (!empty($msg)) { TwitchBotCommand::chat($channel->twitch_chat, $msg); } @@ -195,7 +198,7 @@ class ChannelController extends Controller { break; } - return $channel->toJson(); + return $this->sendChannel($channel); } public function getGuessingGame(Channel $channel, $name) { @@ -214,13 +217,38 @@ class ChannelController extends Controller { ]; } + 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(), + ]; + } + public function saveGuessingGame(Request $request, Channel $channel, $name) { $this->authorize('editRestream', $channel); $validatedData = $request->validate([ 'active_message' => 'string', 'cancel_message' => 'string', + 'close_winners_message' => 'string', 'invalid_solution_message' => 'string', + 'leaderboard_type' => 'string', 'no_winners_message' => 'string', 'not_active_message' => 'string', 'points_exact_first' => 'numeric|min:1|max:5', @@ -238,7 +266,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', + ]); + } + }