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([
16 'joinable' => 'boolean|nullable',
17 'manageable' => 'boolean|nullable',
18 'phrase' => 'string|nullable',
21 $channels = Channel::query();
22 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
23 $channels = $channels->where('twitch_chat', '!=', '');
25 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
26 $user = $request->user();
30 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
31 $query->where('user_id', '=', $user->id);
34 if (!empty($validatedData['phrase'])) {
35 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
36 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
38 $channels = $channels->limit(5);
39 return $this->sendChannels($channels->get());
42 public function single(Request $request, Channel $channel) {
43 $this->authorize('view', $channel);
44 return $this->sendChannel($channel);
47 public function chat(Request $request, Channel $channel) {
48 if (!$channel->twitch_chat) {
49 throw new \Exception('channel has no twitch chat set');
51 $validatedData = $request->validate([
52 'bot_nick' => 'string',
53 'text' => 'string|required',
55 $this->authorize('editRestream', $channel);
56 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
57 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
58 return $this->sendChannel($channel);
61 public function chatSettings(Request $request, Channel $channel) {
62 if (!$channel->twitch_chat) {
63 throw new \Exception('channel has no twitch chat set');
65 $validatedData = $request->validate([
66 'wait_msgs_min' => 'integer|min:1',
67 'wait_msgs_max' => 'integer',
68 'wait_time_min' => 'integer',
69 'wait_time_max' => 'integer',
71 $this->authorize('editRestream', $channel);
72 $channel->chat_settings = $validatedData;
74 return $this->sendChannel($channel);
77 public function join(Request $request, Channel $channel) {
78 if (!$channel->twitch_chat) {
79 throw new \Exception('channel has no twitch chat set');
81 $validatedData = $request->validate([
82 'bot_nick' => 'string',
84 $this->authorize('editRestream', $channel);
85 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
86 if ($nick == 'localhorsttv') {
87 $channel->join = true;
88 } else if ($nick == 'horstiebot') {
89 $channel->chat = true;
92 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
93 return $this->sendChannel($channel);
96 public function part(Request $request, Channel $channel) {
97 if (!$channel->twitch_chat) {
98 throw new \Exception('channel has no twitch chat set');
100 $validatedData = $request->validate([
101 'bot_nick' => 'string',
103 $this->authorize('editRestream', $channel);
104 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
105 if ($nick == 'localhorsttv') {
106 $channel->join = false;
107 } else if ($nick == 'horstiebot') {
108 $channel->chat = false;
111 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
112 return $this->sendChannel($channel);
115 public function deleteCommand(Channel $channel, $command) {
116 $this->authorize('editRestream', $channel);
117 if (isset($channel->chat_commands[$command])) {
118 $cmds = $channel->chat_commands;
119 unset($cmds[$command]);
120 $channel->chat_commands = $cmds;
123 return $this->sendChannel($channel);
126 public function saveCommand(Request $request, Channel $channel, $command) {
127 $this->authorize('editRestream', $channel);
129 $validatedData = $request->validate([
130 'command' => 'string',
131 'restrict' => 'string',
135 $cmds = $channel->chat_commands;
136 $cmds[$command] = $validatedData;
137 $channel->chat_commands = $cmds;
140 return $this->sendChannel($channel);
143 public function controlGuessingGame(Request $request, Channel $channel, $name) {
144 $this->authorize('editRestream', $channel);
146 $validatedData = $request->validate([
147 'action' => 'required|in:cancel,solve,start,stop',
151 switch ($validatedData['action']) {
153 $channel->clearGuessing();
154 $msg = $channel->getGuessingSetting('cancel_message');
156 TwitchBotCommand::chat($channel->twitch_chat, $msg);
160 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
161 $winners = $channel->solveGuessing($validatedData['solution']);
163 foreach ($winners as $winner) {
164 if ($winner->score > 0) {
165 $names[] = $winner->uname;
169 $msg = $channel->getGuessingSetting('no_winners_message');
171 $msg = $channel->getGuessingSetting('winners_message');
172 $msg = str_replace('{names}', $channel->listAnd($names), $msg);
175 TwitchBotCommand::chat($channel->twitch_chat, $msg);
177 $channel->clearGuessing();
181 if (!$channel->hasActiveGuessing()) {
182 $channel->startGuessing($name);
183 $msg = $channel->getGuessingSetting('start_message');
185 TwitchBotCommand::chat($channel->twitch_chat, $msg);
190 if ($channel->hasActiveGuessing()) {
191 $channel->stopGuessing();
192 $msg = $channel->getGuessingSetting('stop_message');
194 TwitchBotCommand::chat($channel->twitch_chat, $msg);
200 return $this->sendChannel($channel);
203 public function getGuessingGame(Channel $channel, $name) {
204 $this->authorize('editRestream', $channel);
206 $cutoff = $channel->guessing_start;
207 if (is_null($cutoff)) {
208 $last = $channel->winners()->latest()->first();
209 $cutoff = $last->pod;
211 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
212 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
214 'guesses' => $guesses->toArray(),
215 'winners' => $winners->toArray(),
219 public function getGuessingGameMonitor($key) {
220 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
222 $cutoff = $channel->guessing_start;
223 if (is_null($cutoff)) {
224 $last = $channel->winners()->latest()->first();
225 $cutoff = $last->pod;
227 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
228 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
230 'channel' => $channel->toArray(),
231 'guesses' => $guesses->toArray(),
232 'winners' => $winners->toArray(),
236 public function saveGuessingGame(Request $request, Channel $channel, $name) {
237 $this->authorize('editRestream', $channel);
239 $validatedData = $request->validate([
240 'active_message' => 'string',
241 'cancel_message' => 'string',
242 'invalid_solution_message' => 'string',
243 'no_winners_message' => 'string',
244 'not_active_message' => 'string',
245 'points_exact_first' => 'numeric|min:1|max:5',
246 'points_exact_other' => 'numeric|min:0|max:5',
247 'points_close_first' => 'numeric|min:0|max:5',
248 'points_close_max' => 'integer|min:0',
249 'points_close_other' => 'numeric|min:0|max:5',
250 'start_message' => 'string',
251 'stop_message' => 'string',
252 'winners_message' => 'string',
255 $settings = $channel->guessing_settings;
256 $settings[$name] = $validatedData;
257 $channel->guessing_settings = $settings;
260 return $this->sendChannel($channel);
263 protected function sendChannel(Channel $channel) {
264 if (Gate::allows('editRestream', $channel)) {
265 $this->unhideChannel($channel);
267 return $channel->toJson();
270 protected function sendChannels(Collection $channels) {
271 foreach ($channels as $channel) {
272 if (Gate::allows('editRestream', $channel)) {
273 $this->unhideChannel($channel);
276 return $channels->toJson();
279 private function unhideChannel(Channel $channel) {
280 $channel->makeVisible([