3 namespace App\Http\Controllers;
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Http\Request;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Facades\Gate;
12 class ChannelController extends Controller {
14 public function search(Request $request) {
15 $validatedData = $request->validate([
17 'id.*' => 'integer|numeric',
18 'joinable' => 'boolean|nullable',
19 'manageable' => 'boolean|nullable',
20 'phrase' => 'string|nullable',
23 $channels = Channel::query();
24 if (!empty($validatedData['id'])) {
25 $channels = $channels->whereIn('id', $validatedData['id']);
27 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
28 $channels = $channels->where('twitch_chat', '!=', '');
30 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
31 $user = $request->user();
35 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
36 $query->where('user_id', '=', $user->id);
39 if (!empty($validatedData['phrase'])) {
40 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
41 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
43 $channels = $channels->limit(5);
44 return $this->sendChannels($channels->get());
47 public function single(Request $request, Channel $channel) {
48 $this->authorize('view', $channel);
49 return $this->sendChannel($channel);
52 public function chat(Request $request, Channel $channel) {
53 if (!$channel->twitch_chat) {
54 throw new \Exception('channel has no twitch chat set');
56 $validatedData = $request->validate([
57 'bot_nick' => 'string',
58 'text' => 'string|required',
60 $this->authorize('editRestream', $channel);
61 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
62 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
63 return $this->sendChannel($channel);
66 public function chatSettings(Request $request, Channel $channel) {
67 if (!$channel->twitch_chat) {
68 throw new \Exception('channel has no twitch chat set');
70 $validatedData = $request->validate([
71 'language' => 'string|in:de,en,es,fr',
72 'respond' => 'string|in:50,no,yes',
73 'wait_msgs_min' => 'integer|min:1',
74 'wait_msgs_max' => 'integer',
75 'wait_time_min' => 'integer',
76 'wait_time_max' => 'integer',
78 $this->authorize('editRestream', $channel);
79 $channel->chat_settings = $validatedData;
81 return $this->sendChannel($channel);
84 public function join(Request $request, Channel $channel) {
85 if (!$channel->twitch_chat) {
86 throw new \Exception('channel has no twitch chat set');
88 $validatedData = $request->validate([
89 'bot_nick' => 'string',
91 $this->authorize('editRestream', $channel);
92 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
93 if ($nick == 'localhorsttv') {
94 $channel->join = true;
95 } else if ($nick == 'horstiebot') {
96 $channel->chat = true;
99 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
100 return $this->sendChannel($channel);
103 public function part(Request $request, Channel $channel) {
104 if (!$channel->twitch_chat) {
105 throw new \Exception('channel has no twitch chat set');
107 $validatedData = $request->validate([
108 'bot_nick' => 'string',
110 $this->authorize('editRestream', $channel);
111 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
112 if ($nick == 'localhorsttv') {
113 $channel->join = false;
114 } else if ($nick == 'horstiebot') {
115 $channel->chat = false;
118 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
119 return $this->sendChannel($channel);
122 public function deleteCommand(Channel $channel, $command) {
123 $this->authorize('editRestream', $channel);
124 if (isset($channel->chat_commands[$command])) {
125 $cmds = $channel->chat_commands;
126 unset($cmds[$command]);
127 $channel->chat_commands = $cmds;
130 return $this->sendChannel($channel);
133 public function saveCommand(Request $request, Channel $channel, $command) {
134 $this->authorize('editRestream', $channel);
136 $validatedData = $request->validate([
137 'command' => 'string',
138 'restrict' => 'string',
142 $cmds = $channel->chat_commands;
143 $cmds[$command] = $validatedData;
144 $channel->chat_commands = $cmds;
147 return $this->sendChannel($channel);
150 public function controlGuessingGame(Request $request, Channel $channel, $name) {
151 $this->authorize('editRestream', $channel);
153 $validatedData = $request->validate([
154 'action' => 'required|in:cancel,solve,start,stop',
158 switch ($validatedData['action']) {
160 $channel->clearGuessing();
161 $msg = $channel->getGuessingSetting('cancel_message');
163 TwitchBotCommand::chat($channel->twitch_chat, $msg);
167 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
168 $winners = $channel->solveGuessing($validatedData['solution']);
169 $msg = $channel->listGuessingWinners($winners);
171 TwitchBotCommand::chat($channel->twitch_chat, $msg);
173 $channel->clearGuessing();
177 if (!$channel->hasActiveGuessing()) {
178 $channel->startGuessing($name);
179 $msg = $channel->getGuessingSetting('start_message');
181 TwitchBotCommand::chat($channel->twitch_chat, $msg);
186 if ($channel->hasActiveGuessing()) {
187 $channel->stopGuessing();
188 $msg = $channel->getGuessingSetting('stop_message');
190 TwitchBotCommand::chat($channel->twitch_chat, $msg);
196 return $this->sendChannel($channel);
199 public function getGuessingGame(Channel $channel, $name) {
200 $this->authorize('editRestream', $channel);
202 $cutoff = $channel->guessing_start;
203 if (is_null($cutoff)) {
204 $last = $channel->winners()->latest()->first();
205 $cutoff = $last->pod;
207 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
208 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
210 'guesses' => $guesses->toArray(),
211 'winners' => $winners->toArray(),
215 public function getGuessingGameLeaderboard(Channel $channel, $type) {
217 'all' => $channel->getGuessingLeaderboard(),
221 public function getGuessingGameMonitor($key) {
222 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
224 $cutoff = $channel->guessing_start;
225 if (is_null($cutoff)) {
226 $last = $channel->winners()->latest()->first();
227 $cutoff = $last->pod;
229 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
230 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
232 'channel' => $channel->toArray(),
233 'guesses' => $guesses->toArray(),
234 'winners' => $winners->toArray(),
238 public function saveGuessingGame(Request $request, Channel $channel, $name) {
239 $this->authorize('editRestream', $channel);
241 $validatedData = $request->validate([
242 'active_message' => 'string',
243 'cancel_message' => 'string',
244 'close_winners_message' => 'string',
245 'invalid_solution_message' => 'string',
246 'leaderboard_type' => 'string',
247 'no_winners_message' => 'string',
248 'not_active_message' => 'string',
249 'points_exact_first' => 'numeric|min:1|max:5',
250 'points_exact_other' => 'numeric|min:0|max:5',
251 'points_close_first' => 'numeric|min:0|max:5',
252 'points_close_max' => 'integer|min:0',
253 'points_close_other' => 'numeric|min:0|max:5',
254 'start_message' => 'string',
255 'stop_message' => 'string',
256 'winners_message' => 'string',
259 $settings = $channel->guessing_settings;
260 $settings[$name] = $validatedData;
261 $channel->guessing_settings = $settings;
264 return $this->sendChannel($channel);
267 protected function sendChannel(Channel $channel) {
268 if (Gate::allows('editRestream', $channel)) {
269 $this->unhideChannel($channel);
271 return $channel->toJson();
274 protected function sendChannels(Collection $channels) {
275 foreach ($channels as $channel) {
276 if (Gate::allows('editRestream', $channel)) {
277 $this->unhideChannel($channel);
280 return $channels->toJson();
283 private function unhideChannel(Channel $channel) {
284 $channel->makeVisible([