]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
guessing game controls
[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
10 class ChannelController extends Controller {
11
12         public function search(Request $request) {
13                 $validatedData = $request->validate([
14                         'joinable' => 'boolean|nullable',
15                         'manageable' => 'boolean|nullable',
16                         'phrase' => 'string|nullable',
17                 ]);
18
19                 $channels = Channel::query();
20                 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
21                         $channels = $channels->where('twitch_chat', '!=', '');
22                 }
23                 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
24                         $user = $request->user();
25                         if (!$user) {
26                                 return [];
27                         }
28                         $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
29                                 $query->where('user_id', '=', $user->id);
30                         });
31                 }
32                 if (!empty($validatedData['phrase'])) {
33                         $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
34                                 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
35                 }
36                 $channels = $channels->limit(5);
37                 return $channels->get()->toJson();
38         }
39
40         public function single(Request $request, Channel $channel) {
41                 $this->authorize('view', $channel);
42                 return $channel->toJson();
43         }
44
45         public function chat(Request $request, Channel $channel) {
46                 if (!$channel->twitch_chat) {
47                         throw new \Exception('channel has no twitch chat set');
48                 }
49                 $validatedData = $request->validate([
50                         'bot_nick' => 'string',
51                         'text' => 'string|required',
52                 ]);
53                 $this->authorize('editRestream', $channel);
54                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
55                 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
56                 return $channel->toJson();
57         }
58
59         public function chatSettings(Request $request, Channel $channel) {
60                 if (!$channel->twitch_chat) {
61                         throw new \Exception('channel has no twitch chat set');
62                 }
63                 $validatedData = $request->validate([
64                         'wait_msgs_min' => 'integer|min:1',
65                         'wait_msgs_max' => 'integer',
66                         'wait_time_min' => 'integer',
67                         'wait_time_max' => 'integer',
68                 ]);
69                 $this->authorize('editRestream', $channel);
70                 $channel->chat_settings = $validatedData;
71                 $channel->save();
72                 return $channel->toJson();
73         }
74
75         public function join(Request $request, Channel $channel) {
76                 if (!$channel->twitch_chat) {
77                         throw new \Exception('channel has no twitch chat set');
78                 }
79                 $validatedData = $request->validate([
80                         'bot_nick' => 'string',
81                 ]);
82                 $this->authorize('editRestream', $channel);
83                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
84                 if ($nick == 'localhorsttv') {
85                         $channel->join = true;
86                 } else if ($nick == 'horstiebot') {
87                         $channel->chat = true;
88                 }
89                 $channel->save();
90                 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
91                 return $channel->toJson();
92         }
93
94         public function part(Request $request, Channel $channel) {
95                 if (!$channel->twitch_chat) {
96                         throw new \Exception('channel has no twitch chat set');
97                 }
98                 $validatedData = $request->validate([
99                         'bot_nick' => 'string',
100                 ]);
101                 $this->authorize('editRestream', $channel);
102                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
103                 if ($nick == 'localhorsttv') {
104                         $channel->join = false;
105                 } else if ($nick == 'horstiebot') {
106                         $channel->chat = false;
107                 }
108                 $channel->save();
109                 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
110                 return $channel->toJson();
111         }
112
113         public function deleteCommand(Channel $channel, $command) {
114                 $this->authorize('editRestream', $channel);
115                 if (isset($channel->chat_commands[$command])) {
116                         $cmds = $channel->chat_commands;
117                         unset($cmds[$command]);
118                         $channel->chat_commands = $cmds;
119                         $channel->save();
120                 }
121                 return $channel->toJson();
122         }
123
124         public function saveCommand(Request $request, Channel $channel, $command) {
125                 $this->authorize('editRestream', $channel);
126
127                 $validatedData = $request->validate([
128                         'command' => 'string',
129                         'restrict' => 'string',
130                         'type' => 'string',
131                 ]);
132
133                 $cmds = $channel->chat_commands;
134                 $cmds[$command] = $validatedData;
135                 $channel->chat_commands = $cmds;
136                 $channel->save();
137
138                 return $channel->toJson();
139         }
140
141         public function controlGuessingGame(Request $request, Channel $channel, $name) {
142                 $this->authorize('editRestream', $channel);
143
144                 $validatedData = $request->validate([
145                         'action' => 'required|in:cancel,solve,start,stop',
146                         'solution' => '',
147                 ]);
148
149                 switch ($validatedData['action']) {
150                         case 'cancel':
151                                 $channel->clearGuessing();
152                                 $msg = $channel->getGuessingSetting('cancel_message');
153                                 if (!empty($msg)) {
154                                         TwitchBotCommand::chat($channel->twitch_chat, $msg);
155                                 }
156                                 break;
157                         case 'solve':
158                                 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
159                                         $winners = $channel->solveGuessing($validatedData['solution']);
160                                         $names = [];
161                                         foreach ($winners as $winner) {
162                                                 if ($winner->score > 0) {
163                                                         $names[] = $winner->uname;
164                                                 }
165                                         }
166                                         if (empty($names)) {
167                                                 $msg = $channel->getGuessingSetting('no_winners_message');
168                                         } else {
169                                                 $msg = $channel->getGuessingSetting('winners_message');
170                                                 $msg = str_replace('{names}', $channel->listAnd($names), $msg);
171                                         }
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 $channel->toJson();
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 saveGuessingGame(Request $request, Channel $channel, $name) {
218                 $this->authorize('editRestream', $channel);
219
220                 $validatedData = $request->validate([
221                         'active_message' => 'string',
222                         'cancel_message' => 'string',
223                         'invalid_solution_message' => 'string',
224                         'no_winners_message' => 'string',
225                         'not_active_message' => 'string',
226                         'points_exact_first' => 'numeric|min:1|max:5',
227                         'points_exact_other' => 'numeric|min:0|max:5',
228                         'points_close_first' => 'numeric|min:0|max:5',
229                         'points_close_max' => 'integer|min:0',
230                         'points_close_other' => 'numeric|min:0|max:5',
231                         'start_message' => 'string',
232                         'stop_message' => 'string',
233                         'winners_message' => 'string',
234                 ]);
235
236                 $settings = $channel->guessing_settings;
237                 $settings[$name] = $validatedData;
238                 $channel->guessing_settings = $settings;
239                 $channel->save();
240
241                 return $channel->toJson();
242         }
243
244 }