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