3 namespace App\Http\Controllers;
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;
12 class ChannelController extends Controller {
14 public function search(Request $request) {
15 $validatedData = $request->validate([
17 'id.*' => 'integer|numeric',
18 'joinable' => 'boolean|nullable',
19 'manageable' => 'boolean|nullable',
20 'phrase' => 'string|nullable',
23 $channels = Channel::query();
24 if (!empty($validatedData['id'])) {
25 $channels = $channels->whereIn('id', $validatedData['id']);
27 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
28 $channels = $channels->where('twitch_chat', '!=', '');
30 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
31 $user = $request->user();
35 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
36 $query->where('user_id', '=', $user->id);
39 if (!empty($validatedData['phrase'])) {
40 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
41 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
43 $channels = $channels->limit(5);
44 return $this->sendChannels($channels->get());
47 public function single(Request $request, Channel $channel) {
48 $this->authorize('view', $channel);
49 return $this->sendChannel($channel);
52 public function chat(Request $request, Channel $channel) {
53 if (!$channel->twitch_chat) {
54 throw new \Exception('channel has no twitch chat set');
56 $validatedData = $request->validate([
57 'bot_nick' => 'string',
58 'category' => 'string',
61 $this->authorize('editRestream', $channel);
62 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
63 $text = empty($validatedData['category']) ? $validatedData['text'] : $channel->randomOfClass($validatedData['category']);
64 TwitchBotCommand::chat($channel->twitch_chat, $text, $request->user(), $nick);
65 return $this->sendChannel($channel);
68 public function chatSettings(Request $request, Channel $channel) {
69 if (!$channel->twitch_chat) {
70 throw new \Exception('channel has no twitch chat set');
72 $validatedData = $request->validate([
73 'language' => 'string|in:de,en,es,fr',
74 'respond' => 'string|in:50,no,yes',
75 'wait_msgs_min' => 'integer|min:1',
76 'wait_msgs_max' => 'integer',
77 'wait_time_min' => 'integer',
78 'wait_time_max' => 'integer',
80 $this->authorize('editRestream', $channel);
81 $channel->chat_settings = $validatedData;
83 return $this->sendChannel($channel);
86 public function join(Request $request, Channel $channel) {
87 if (!$channel->twitch_chat) {
88 throw new \Exception('channel has no twitch chat set');
90 $validatedData = $request->validate([
91 'bot_nick' => 'string',
93 $this->authorize('editRestream', $channel);
94 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
95 if ($nick == 'localhorsttv') {
96 $channel->join = true;
97 } else if ($nick == 'horstiebot') {
98 $channel->chat = true;
101 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
102 return $this->sendChannel($channel);
105 public function part(Request $request, Channel $channel) {
106 if (!$channel->twitch_chat) {
107 throw new \Exception('channel has no twitch chat set');
109 $validatedData = $request->validate([
110 'bot_nick' => 'string',
112 $this->authorize('editRestream', $channel);
113 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
114 if ($nick == 'localhorsttv') {
115 $channel->join = false;
116 } else if ($nick == 'horstiebot') {
117 $channel->chat = false;
120 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
121 return $this->sendChannel($channel);
124 public function deleteCommand(Channel $channel, $command) {
125 $this->authorize('editRestream', $channel);
126 if (isset($channel->chat_commands[$command])) {
127 $cmds = $channel->chat_commands;
128 unset($cmds[$command]);
129 $channel->chat_commands = $cmds;
132 return $this->sendChannel($channel);
135 public function saveCommand(Request $request, Channel $channel, $command) {
136 $this->authorize('editRestream', $channel);
138 $validatedData = $request->validate([
139 'command' => 'string',
140 'restrict' => 'string',
144 $cmds = $channel->chat_commands;
145 $cmds[$command] = $validatedData;
146 $channel->chat_commands = $cmds;
149 return $this->sendChannel($channel);
152 public function controlGuessingGame(Request $request, Channel $channel, $name) {
153 $this->authorize('editRestream', $channel);
155 $validatedData = $request->validate([
156 'action' => 'required|in:cancel,solve,start,stop',
160 switch ($validatedData['action']) {
162 $channel->clearGuessing();
163 $msg = $channel->getGuessingSetting('cancel_message');
165 TwitchBotCommand::chat($channel->twitch_chat, $msg);
169 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
170 $winners = $channel->solveGuessing($validatedData['solution']);
171 $msg = $channel->listGuessingWinners($winners);
173 TwitchBotCommand::chat($channel->twitch_chat, $msg);
175 $channel->clearGuessing();
179 if (!$channel->hasActiveGuessing()) {
180 $channel->startGuessing($name);
181 $msg = $channel->getGuessingSetting('start_message');
183 TwitchBotCommand::chat($channel->twitch_chat, $msg);
188 if ($channel->hasActiveGuessing()) {
189 $channel->stopGuessing();
190 $msg = $channel->getGuessingSetting('stop_message');
192 TwitchBotCommand::chat($channel->twitch_chat, $msg);
198 return $this->sendChannel($channel);
201 public function getGuessingGame(Channel $channel, $name) {
202 $this->authorize('editRestream', $channel);
204 $cutoff = $channel->guessing_start;
205 if (is_null($cutoff)) {
206 $last = $channel->winners()->latest()->first();
207 $cutoff = $last->pod;
209 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
210 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
212 'guesses' => $guesses->toArray(),
213 'winners' => $winners->toArray(),
217 public function getGuessingGameLeaderboard(Channel $channel, $type) {
219 'all' => $channel->getGuessingLeaderboard(),
223 public function getGuessingGameMonitor($key) {
224 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
226 $cutoff = $channel->guessing_start;
227 if (is_null($cutoff)) {
228 $last = $channel->winners()->latest()->first();
229 $cutoff = $last->pod;
231 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
232 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
234 'channel' => $channel->toArray(),
235 'guesses' => $guesses->toArray(),
236 'winners' => $winners->toArray(),
240 public function saveGuessingGame(Request $request, Channel $channel, $name) {
241 $this->authorize('editRestream', $channel);
243 $validatedData = $request->validate([
244 'active_message' => 'string',
245 'cancel_message' => 'string',
246 'close_winners_message' => 'string',
247 'invalid_solution_message' => 'string',
248 'leaderboard_type' => 'string',
249 'no_winners_message' => 'string',
250 'not_active_message' => 'string',
251 'points_exact_first' => 'numeric|min:1|max:5',
252 'points_exact_other' => 'numeric|min:0|max:5',
253 'points_close_first' => 'numeric|min:0|max:5',
254 'points_close_max' => 'integer|min:0',
255 'points_close_other' => 'numeric|min:0|max:5',
256 'start_message' => 'string',
257 'stop_message' => 'string',
258 'winners_message' => 'string',
261 $settings = $channel->guessing_settings;
262 $settings[$name] = $validatedData;
263 $channel->guessing_settings = $settings;
266 return $this->sendChannel($channel);
269 protected function sendChannel(Channel $channel) {
270 if (Gate::allows('editRestream', $channel)) {
271 $this->unhideChannel($channel);
273 return $channel->toJson();
276 protected function sendChannels(Collection $channels) {
277 foreach ($channels as $channel) {
278 if (Gate::allows('editRestream', $channel)) {
279 $this->unhideChannel($channel);
282 return $channels->toJson();
285 private function unhideChannel(Channel $channel) {
286 $channel->makeVisible([