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