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