]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
add up to 4 player support for zsr sync
[alttp.git] / app / Http / Controllers / ChannelController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
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;
11
12 class ChannelController extends Controller {
13
14         public function search(Request $request) {
15                 $validatedData = $request->validate([
16                         'id' => 'array',
17                         'id.*' => 'integer|numeric',
18                         'joinable' => 'boolean|nullable',
19                         'manageable' => 'boolean|nullable',
20                         'phrase' => 'string|nullable',
21                 ]);
22
23                 $channels = Channel::query();
24                 if (!empty($validatedData['id'])) {
25                         $channels = $channels->whereIn('id', $validatedData['id']);
26                 }
27                 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
28                         $channels = $channels->where('twitch_chat', '!=', '');
29                 }
30                 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
31                         $user = $request->user();
32                         if (!$user) {
33                                 return [];
34                         }
35                         $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
36                                 $query->where('user_id', '=', $user->id);
37                         });
38                 }
39                 if (!empty($validatedData['phrase'])) {
40                         $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
41                                 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
42                 }
43                 $channels = $channels->limit(5);
44                 return $this->sendChannels($channels->get());
45         }
46
47         public function single(Request $request, Channel $channel) {
48                 $this->authorize('view', $channel);
49                 return $this->sendChannel($channel);
50         }
51
52         public function chat(Request $request, Channel $channel) {
53                 if (!$channel->twitch_chat) {
54                         throw new \Exception('channel has no twitch chat set');
55                 }
56                 $validatedData = $request->validate([
57                         'adlib' => 'boolean',
58                         'bot_nick' => 'string',
59                         'category' => 'string',
60                         'text' => 'string',
61                 ]);
62                 $this->authorize('editRestream', $channel);
63                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
64                 if (isset($validatedData['adlib']) && $validatedData['adlib']) {
65                         TwitchBotCommand::adlibChat($channel, $request->user());
66                 } else if (empty($validatedData['category'])) {
67                         TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
68                 } else {
69                         TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick);
70                 }
71                 return $this->sendChannel($channel);
72         }
73
74         public function chatBotLog(Request $request, Channel $channel) {
75                 $this->authorize('editRestream', $channel);
76                 $log = $channel->chat_bot_logs()
77                         ->with(['origin', 'user'])
78                         ->orderBy('created_at', 'DESC')
79                         ->limit(150)
80                         ->get();
81                 return $log->values()->toJson();
82         }
83
84         public function chatSettings(Request $request, Channel $channel) {
85                 if (!$channel->twitch_chat) {
86                         throw new \Exception('channel has no twitch chat set');
87                 }
88                 $validatedData = $request->validate([
89                         'adlib' => 'integer|min:0|max:100',
90                         'language' => 'string|in:de,en,es,fr',
91                         'min_age' => 'integer|min:1',
92                         'respond' => 'string|in:50,no,yes',
93                         'source' => 'string|in:any,cat,catchan,chan',
94                         'wait_msgs_min' => 'integer|min:1',
95                         'wait_msgs_max' => 'integer|min:1',
96                         'wait_time_min' => 'integer',
97                         'wait_time_max' => 'integer',
98                 ]);
99                 $this->authorize('editRestream', $channel);
100                 $channel->chat_settings = $validatedData;
101                 $channel->save();
102                 return $this->sendChannel($channel);
103         }
104
105         public function join(Request $request, Channel $channel) {
106                 if (!$channel->twitch_chat) {
107                         throw new \Exception('channel has no twitch chat set');
108                 }
109                 $validatedData = $request->validate([
110                         'bot_nick' => 'string',
111                 ]);
112                 $this->authorize('editRestream', $channel);
113                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
114                 if ($nick == 'localhorsttv') {
115                         $channel->join = true;
116                 } else if ($nick == 'horstiebot') {
117                         $channel->chat = true;
118                 }
119                 $channel->save();
120                 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
121                 return $this->sendChannel($channel);
122         }
123
124         public function part(Request $request, Channel $channel) {
125                 if (!$channel->twitch_chat) {
126                         throw new \Exception('channel has no twitch chat set');
127                 }
128                 $validatedData = $request->validate([
129                         'bot_nick' => 'string',
130                 ]);
131                 $this->authorize('editRestream', $channel);
132                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
133                 if ($nick == 'localhorsttv') {
134                         $channel->join = false;
135                 } else if ($nick == 'horstiebot') {
136                         $channel->chat = false;
137                 }
138                 $channel->save();
139                 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
140                 return $this->sendChannel($channel);
141         }
142
143         public function deleteCommand(Channel $channel, $command) {
144                 $this->authorize('editRestream', $channel);
145                 if (isset($channel->chat_commands[$command])) {
146                         $cmds = $channel->chat_commands;
147                         unset($cmds[$command]);
148                         $channel->chat_commands = $cmds;
149                         $channel->save();
150                 }
151                 return $this->sendChannel($channel);
152         }
153
154         public function saveCommand(Request $request, Channel $channel, $command) {
155                 $this->authorize('editRestream', $channel);
156
157                 $validatedData = $request->validate([
158                         'command' => 'string',
159                         'restrict' => 'string',
160                         'type' => 'string',
161                 ]);
162
163                 $cmds = $channel->chat_commands;
164                 $cmds[$command] = $validatedData;
165                 $channel->chat_commands = $cmds;
166                 $channel->save();
167
168                 return $this->sendChannel($channel);
169         }
170
171         public function controlGuessingGame(Request $request, Channel $channel, $name) {
172                 $this->authorize('editRestream', $channel);
173
174                 $validatedData = $request->validate([
175                         'action' => 'required|in:cancel,solve,start,stop',
176                         'solution' => '',
177                 ]);
178
179                 switch ($validatedData['action']) {
180                         case 'cancel':
181                                 $channel->clearGuessing();
182                                 $msg = $channel->getGuessingSetting('cancel_message');
183                                 if (!empty($msg)) {
184                                         TwitchBotCommand::chat($channel->twitch_chat, $msg);
185                                 }
186                                 break;
187                         case 'solve':
188                                 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
189                                         $winners = $channel->solveGuessing($validatedData['solution']);
190                                         $msg = $channel->listGuessingWinners($winners);
191                                         if (!empty($msg)) {
192                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
193                                         }
194                                         $channel->clearGuessing();
195                                 }
196                                 break;
197                         case 'start':
198                                 if (!$channel->hasActiveGuessing()) {
199                                         $channel->startGuessing($name);
200                                         $msg = $channel->getGuessingSetting('start_message');
201                                         if (!empty($msg)) {
202                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
203                                         }
204                                 }
205                                 break;
206                         case 'stop':
207                                 if ($channel->hasActiveGuessing()) {
208                                         $channel->stopGuessing();
209                                         $msg = $channel->getGuessingSetting('stop_message');
210                                         if (!empty($msg)) {
211                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
212                                         }
213                                 }
214                                 break;
215                 }
216
217                 return $this->sendChannel($channel);
218         }
219
220         public function getGuessingGame(Channel $channel, $name) {
221                 $this->authorize('editRestream', $channel);
222
223                 $cutoff = $channel->guessing_start;
224                 if (is_null($cutoff)) {
225                         $last = $channel->winners()->latest()->first();
226                         $cutoff = $last->pod;
227                 }
228                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
229                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
230                 return [
231                         'guesses' => $guesses->toArray(),
232                         'winners' => $winners->toArray(),
233                 ];
234         }
235
236         public function getGuessingGameLeaderboard(Channel $channel, $type) {
237                 return [
238                         'all' => $channel->getGuessingLeaderboard(),
239                 ];
240         }
241
242         public function getGuessingGameMonitor($key) {
243                 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
244
245                 $cutoff = $channel->guessing_start;
246                 if (is_null($cutoff)) {
247                         $last = $channel->winners()->latest()->first();
248                         $cutoff = $last->pod;
249                 }
250                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
251                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
252                 return [
253                         'channel' => $channel->toArray(),
254                         'guesses' => $guesses->toArray(),
255                         'winners' => $winners->toArray(),
256                 ];
257         }
258
259         public function saveGuessingGame(Request $request, Channel $channel, $name) {
260                 $this->authorize('editRestream', $channel);
261
262                 $validatedData = $request->validate([
263                         'active_message' => 'string',
264                         'cancel_message' => 'string',
265                         'close_winners_message' => 'string',
266                         'invalid_solution_message' => 'string',
267                         'leaderboard_type' => 'string',
268                         'no_winners_message' => 'string',
269                         'not_active_message' => 'string',
270                         'points_exact_first' => 'numeric|min:1|max:5',
271                         'points_exact_other' => 'numeric|min:0|max:5',
272                         'points_close_first' => 'numeric|min:0|max:5',
273                         'points_close_max' => 'integer|min:0',
274                         'points_close_other' => 'numeric|min:0|max:5',
275                         'start_message' => 'string',
276                         'stop_message' => 'string',
277                         'winners_message' => 'string',
278                 ]);
279
280                 $settings = $channel->guessing_settings;
281                 $settings[$name] = $validatedData;
282                 $channel->guessing_settings = $settings;
283                 $channel->save();
284
285                 return $this->sendChannel($channel);
286         }
287
288         protected function sendChannel(Channel $channel) {
289                 if (Gate::allows('editRestream', $channel)) {
290                         $this->unhideChannel($channel);
291                 }
292                 return $channel->toJson();
293         }
294
295         protected function sendChannels(Collection $channels) {
296                 foreach ($channels as $channel) {
297                         if (Gate::allows('editRestream', $channel)) {
298                                 $this->unhideChannel($channel);
299                         }
300                 }
301                 return $channels->toJson();
302         }
303
304         private function unhideChannel(Channel $channel) {
305                 $channel->makeVisible([
306                         'access_key',
307                         'chat',
308                         'chat_commands',
309                         'chat_settings',
310                         'guessing_settings',
311                         'join',
312                         'twitch_chat',
313                 ]);
314         }
315
316 }