]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
some guessing game fixes
[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                                         $msg = $channel->listGuessingWinners($winners);
168                                         if (!empty($msg)) {
169                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
170                                         }
171                                         $channel->clearGuessing();
172                                 }
173                                 break;
174                         case 'start':
175                                 if (!$channel->hasActiveGuessing()) {
176                                         $channel->startGuessing($name);
177                                         $msg = $channel->getGuessingSetting('start_message');
178                                         if (!empty($msg)) {
179                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
180                                         }
181                                 }
182                                 break;
183                         case 'stop':
184                                 if ($channel->hasActiveGuessing()) {
185                                         $channel->stopGuessing();
186                                         $msg = $channel->getGuessingSetting('stop_message');
187                                         if (!empty($msg)) {
188                                                 TwitchBotCommand::chat($channel->twitch_chat, $msg);
189                                         }
190                                 }
191                                 break;
192                 }
193
194                 return $this->sendChannel($channel);
195         }
196
197         public function getGuessingGame(Channel $channel, $name) {
198                 $this->authorize('editRestream', $channel);
199
200                 $cutoff = $channel->guessing_start;
201                 if (is_null($cutoff)) {
202                         $last = $channel->winners()->latest()->first();
203                         $cutoff = $last->pod;
204                 }
205                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
206                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
207                 return [
208                         'guesses' => $guesses->toArray(),
209                         'winners' => $winners->toArray(),
210                 ];
211         }
212
213         public function getGuessingGameLeaderboard(Channel $channel, $type) {
214                 return [
215                         'all' => $channel->getGuessingLeaderboard(),
216                 ];
217         }
218
219         public function getGuessingGameMonitor($key) {
220                 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
221
222                 $cutoff = $channel->guessing_start;
223                 if (is_null($cutoff)) {
224                         $last = $channel->winners()->latest()->first();
225                         $cutoff = $last->pod;
226                 }
227                 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
228                 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
229                 return [
230                         'channel' => $channel->toArray(),
231                         'guesses' => $guesses->toArray(),
232                         'winners' => $winners->toArray(),
233                 ];
234         }
235
236         public function saveGuessingGame(Request $request, Channel $channel, $name) {
237                 $this->authorize('editRestream', $channel);
238
239                 $validatedData = $request->validate([
240                         'active_message' => 'string',
241                         'cancel_message' => 'string',
242                         'close_winners_message' => 'string',
243                         'invalid_solution_message' => 'string',
244                         'leaderboard_type' => 'string',
245                         'no_winners_message' => 'string',
246                         'not_active_message' => 'string',
247                         'points_exact_first' => 'numeric|min:1|max:5',
248                         'points_exact_other' => 'numeric|min:0|max:5',
249                         'points_close_first' => 'numeric|min:0|max:5',
250                         'points_close_max' => 'integer|min:0',
251                         'points_close_other' => 'numeric|min:0|max:5',
252                         'start_message' => 'string',
253                         'stop_message' => 'string',
254                         'winners_message' => 'string',
255                 ]);
256
257                 $settings = $channel->guessing_settings;
258                 $settings[$name] = $validatedData;
259                 $channel->guessing_settings = $settings;
260                 $channel->save();
261
262                 return $this->sendChannel($channel);
263         }
264
265         protected function sendChannel(Channel $channel) {
266                 if (Gate::allows('editRestream', $channel)) {
267                         $this->unhideChannel($channel);
268                 }
269                 return $channel->toJson();
270         }
271
272         protected function sendChannels(Collection $channels) {
273                 foreach ($channels as $channel) {
274                         if (Gate::allows('editRestream', $channel)) {
275                                 $this->unhideChannel($channel);
276                         }
277                 }
278                 return $channels->toJson();
279         }
280
281         private function unhideChannel(Channel $channel) {
282                 $channel->makeVisible([
283                         'access_key',
284                         'chat',
285                         'chat_commands',
286                         'chat_settings',
287                         'guessing_settings',
288                         'join',
289                         'twitch_chat',
290                 ]);
291         }
292
293 }