]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
guessing game settings
[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 saveGuessingGame(Request $request, Channel $channel, $name) {
142                 $this->authorize('editRestream', $channel);
143
144                 $validatedData = $request->validate([
145                         'active_message' => 'string',
146                         'cancel_message' => 'string',
147                         'invalid_solution_message' => 'string',
148                         'no_winners_message' => 'string',
149                         'not_active_message' => 'string',
150                         'points_exact_first' => 'numeric|min:1|max:5',
151                         'points_exact_other' => 'numeric|min:0|max:5',
152                         'points_close_first' => 'numeric|min:0|max:5',
153                         'points_close_min' => 'integer|min:0',
154                         'points_close_other' => 'numeric|min:0|max:5',
155                         'start_message' => 'string',
156                         'stop_message' => 'string',
157                         'winners_message' => 'string',
158                 ]);
159
160                 $settings = $channel->guessing_settings;
161                 $settings[$name] = $validatedData;
162                 $channel->guessing_settings = $settings;
163                 $channel->save();
164
165                 return $channel->toJson();
166         }
167
168 }