]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
chat bot config
[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                         'text' => 'string|required',
59                 ]);
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);
64         }
65
66         public function chatSettings(Request $request, Channel $channel) {
67                 if (!$channel->twitch_chat) {
68                         throw new \Exception('channel has no twitch chat set');
69                 }
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',
77                 ]);
78                 $this->authorize('editRestream', $channel);
79                 $channel->chat_settings = $validatedData;
80                 $channel->save();
81                 return $this->sendChannel($channel);
82         }
83
84         public function join(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                         'bot_nick' => 'string',
90                 ]);
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;
97                 }
98                 $channel->save();
99                 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
100                 return $this->sendChannel($channel);
101         }
102
103         public function part(Request $request, Channel $channel) {
104                 if (!$channel->twitch_chat) {
105                         throw new \Exception('channel has no twitch chat set');
106                 }
107                 $validatedData = $request->validate([
108                         'bot_nick' => 'string',
109                 ]);
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;
116                 }
117                 $channel->save();
118                 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
119                 return $this->sendChannel($channel);
120         }
121
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;
128                         $channel->save();
129                 }
130                 return $this->sendChannel($channel);
131         }
132
133         public function saveCommand(Request $request, Channel $channel, $command) {
134                 $this->authorize('editRestream', $channel);
135
136                 $validatedData = $request->validate([
137                         'command' => 'string',
138                         'restrict' => 'string',
139                         'type' => 'string',
140                 ]);
141
142                 $cmds = $channel->chat_commands;
143                 $cmds[$command] = $validatedData;
144                 $channel->chat_commands = $cmds;
145                 $channel->save();
146
147                 return $this->sendChannel($channel);
148         }
149
150         public function controlGuessingGame(Request $request, Channel $channel, $name) {
151                 $this->authorize('editRestream', $channel);
152
153                 $validatedData = $request->validate([
154                         'action' => 'required|in:cancel,solve,start,stop',
155                         'solution' => '',
156                 ]);
157
158                 switch ($validatedData['action']) {
159                         case 'cancel':
160                                 $channel->clearGuessing();
161                                 $msg = $channel->getGuessingSetting('cancel_message');
162                                 if (!empty($msg)) {
163                                         TwitchBotCommand::chat($channel->twitch_chat, $msg);
164                                 }
165                                 break;
166                         case 'solve':
167                                 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
168                                         $winners = $channel->solveGuessing($validatedData['solution']);
169                                         $msg = $channel->listGuessingWinners($winners);
170                                         if (!empty($msg)) {
171                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
172                                         }
173                                         $channel->clearGuessing();
174                                 }
175                                 break;
176                         case 'start':
177                                 if (!$channel->hasActiveGuessing()) {
178                                         $channel->startGuessing($name);
179                                         $msg = $channel->getGuessingSetting('start_message');
180                                         if (!empty($msg)) {
181                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
182                                         }
183                                 }
184                                 break;
185                         case 'stop':
186                                 if ($channel->hasActiveGuessing()) {
187                                         $channel->stopGuessing();
188                                         $msg = $channel->getGuessingSetting('stop_message');
189                                         if (!empty($msg)) {
190                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
191                                         }
192                                 }
193                                 break;
194                 }
195
196                 return $this->sendChannel($channel);
197         }
198
199         public function getGuessingGame(Channel $channel, $name) {
200                 $this->authorize('editRestream', $channel);
201
202                 $cutoff = $channel->guessing_start;
203                 if (is_null($cutoff)) {
204                         $last = $channel->winners()->latest()->first();
205                         $cutoff = $last->pod;
206                 }
207                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
208                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
209                 return [
210                         'guesses' => $guesses->toArray(),
211                         'winners' => $winners->toArray(),
212                 ];
213         }
214
215         public function getGuessingGameLeaderboard(Channel $channel, $type) {
216                 return [
217                         'all' => $channel->getGuessingLeaderboard(),
218                 ];
219         }
220
221         public function getGuessingGameMonitor($key) {
222                 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
223
224                 $cutoff = $channel->guessing_start;
225                 if (is_null($cutoff)) {
226                         $last = $channel->winners()->latest()->first();
227                         $cutoff = $last->pod;
228                 }
229                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
230                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
231                 return [
232                         'channel' => $channel->toArray(),
233                         'guesses' => $guesses->toArray(),
234                         'winners' => $winners->toArray(),
235                 ];
236         }
237
238         public function saveGuessingGame(Request $request, Channel $channel, $name) {
239                 $this->authorize('editRestream', $channel);
240
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',
257                 ]);
258
259                 $settings = $channel->guessing_settings;
260                 $settings[$name] = $validatedData;
261                 $channel->guessing_settings = $settings;
262                 $channel->save();
263
264                 return $this->sendChannel($channel);
265         }
266
267         protected function sendChannel(Channel $channel) {
268                 if (Gate::allows('editRestream', $channel)) {
269                         $this->unhideChannel($channel);
270                 }
271                 return $channel->toJson();
272         }
273
274         protected function sendChannels(Collection $channels) {
275                 foreach ($channels as $channel) {
276                         if (Gate::allows('editRestream', $channel)) {
277                                 $this->unhideChannel($channel);
278                         }
279                 }
280                 return $channels->toJson();
281         }
282
283         private function unhideChannel(Channel $channel) {
284                 $channel->makeVisible([
285                         'access_key',
286                         'chat',
287                         'chat_commands',
288                         'chat_settings',
289                         'guessing_settings',
290                         'join',
291                         'twitch_chat',
292                 ]);
293         }
294
295 }