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