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 if (empty($validatedData['category'])) {
64 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
66 TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick);
68 return $this->sendChannel($channel);
71 public function chatSettings(Request $request, Channel $channel) {
72 if (!$channel->twitch_chat) {
73 throw new \Exception('channel has no twitch chat set');
75 $validatedData = $request->validate([
76 'language' => 'string|in:de,en,es,fr',
77 'respond' => 'string|in:50,no,yes',
78 'wait_msgs_min' => 'integer|min:1',
79 'wait_msgs_max' => 'integer',
80 'wait_time_min' => 'integer',
81 'wait_time_max' => 'integer',
83 $this->authorize('editRestream', $channel);
84 $channel->chat_settings = $validatedData;
86 return $this->sendChannel($channel);
89 public function join(Request $request, Channel $channel) {
90 if (!$channel->twitch_chat) {
91 throw new \Exception('channel has no twitch chat set');
93 $validatedData = $request->validate([
94 'bot_nick' => 'string',
96 $this->authorize('editRestream', $channel);
97 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
98 if ($nick == 'localhorsttv') {
99 $channel->join = true;
100 } else if ($nick == 'horstiebot') {
101 $channel->chat = true;
104 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
105 return $this->sendChannel($channel);
108 public function part(Request $request, Channel $channel) {
109 if (!$channel->twitch_chat) {
110 throw new \Exception('channel has no twitch chat set');
112 $validatedData = $request->validate([
113 'bot_nick' => 'string',
115 $this->authorize('editRestream', $channel);
116 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
117 if ($nick == 'localhorsttv') {
118 $channel->join = false;
119 } else if ($nick == 'horstiebot') {
120 $channel->chat = false;
123 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
124 return $this->sendChannel($channel);
127 public function deleteCommand(Channel $channel, $command) {
128 $this->authorize('editRestream', $channel);
129 if (isset($channel->chat_commands[$command])) {
130 $cmds = $channel->chat_commands;
131 unset($cmds[$command]);
132 $channel->chat_commands = $cmds;
135 return $this->sendChannel($channel);
138 public function saveCommand(Request $request, Channel $channel, $command) {
139 $this->authorize('editRestream', $channel);
141 $validatedData = $request->validate([
142 'command' => 'string',
143 'restrict' => 'string',
147 $cmds = $channel->chat_commands;
148 $cmds[$command] = $validatedData;
149 $channel->chat_commands = $cmds;
152 return $this->sendChannel($channel);
155 public function controlGuessingGame(Request $request, Channel $channel, $name) {
156 $this->authorize('editRestream', $channel);
158 $validatedData = $request->validate([
159 'action' => 'required|in:cancel,solve,start,stop',
163 switch ($validatedData['action']) {
165 $channel->clearGuessing();
166 $msg = $channel->getGuessingSetting('cancel_message');
168 TwitchBotCommand::chat($channel->twitch_chat, $msg);
172 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
173 $winners = $channel->solveGuessing($validatedData['solution']);
174 $msg = $channel->listGuessingWinners($winners);
176 TwitchBotCommand::chat($channel->twitch_chat, $msg);
178 $channel->clearGuessing();
182 if (!$channel->hasActiveGuessing()) {
183 $channel->startGuessing($name);
184 $msg = $channel->getGuessingSetting('start_message');
186 TwitchBotCommand::chat($channel->twitch_chat, $msg);
191 if ($channel->hasActiveGuessing()) {
192 $channel->stopGuessing();
193 $msg = $channel->getGuessingSetting('stop_message');
195 TwitchBotCommand::chat($channel->twitch_chat, $msg);
201 return $this->sendChannel($channel);
204 public function getGuessingGame(Channel $channel, $name) {
205 $this->authorize('editRestream', $channel);
207 $cutoff = $channel->guessing_start;
208 if (is_null($cutoff)) {
209 $last = $channel->winners()->latest()->first();
210 $cutoff = $last->pod;
212 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
213 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
215 'guesses' => $guesses->toArray(),
216 'winners' => $winners->toArray(),
220 public function getGuessingGameLeaderboard(Channel $channel, $type) {
222 'all' => $channel->getGuessingLeaderboard(),
226 public function getGuessingGameMonitor($key) {
227 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
229 $cutoff = $channel->guessing_start;
230 if (is_null($cutoff)) {
231 $last = $channel->winners()->latest()->first();
232 $cutoff = $last->pod;
234 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
235 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
237 'channel' => $channel->toArray(),
238 'guesses' => $guesses->toArray(),
239 'winners' => $winners->toArray(),
243 public function saveGuessingGame(Request $request, Channel $channel, $name) {
244 $this->authorize('editRestream', $channel);
246 $validatedData = $request->validate([
247 'active_message' => 'string',
248 'cancel_message' => 'string',
249 'close_winners_message' => 'string',
250 'invalid_solution_message' => 'string',
251 'leaderboard_type' => 'string',
252 'no_winners_message' => 'string',
253 'not_active_message' => 'string',
254 'points_exact_first' => 'numeric|min:1|max:5',
255 'points_exact_other' => 'numeric|min:0|max:5',
256 'points_close_first' => 'numeric|min:0|max:5',
257 'points_close_max' => 'integer|min:0',
258 'points_close_other' => 'numeric|min:0|max:5',
259 'start_message' => 'string',
260 'stop_message' => 'string',
261 'winners_message' => 'string',
264 $settings = $channel->guessing_settings;
265 $settings[$name] = $validatedData;
266 $channel->guessing_settings = $settings;
269 return $this->sendChannel($channel);
272 protected function sendChannel(Channel $channel) {
273 if (Gate::allows('editRestream', $channel)) {
274 $this->unhideChannel($channel);
276 return $channel->toJson();
279 protected function sendChannels(Collection $channels) {
280 foreach ($channels as $channel) {
281 if (Gate::allows('editRestream', $channel)) {
282 $this->unhideChannel($channel);
285 return $channels->toJson();
288 private function unhideChannel(Channel $channel) {
289 $channel->makeVisible([