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 'text' => 'string|required',
60 $this->authorize('editRestream', $channel);
61 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
62 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
63 return $this->sendChannel($channel);
66 public function chatSettings(Request $request, Channel $channel) {
67 if (!$channel->twitch_chat) {
68 throw new \Exception('channel has no twitch chat set');
70 $validatedData = $request->validate([
71 'wait_msgs_min' => 'integer|min:1',
72 'wait_msgs_max' => 'integer',
73 'wait_time_min' => 'integer',
74 'wait_time_max' => 'integer',
76 $this->authorize('editRestream', $channel);
77 $channel->chat_settings = $validatedData;
79 return $this->sendChannel($channel);
82 public function join(Request $request, Channel $channel) {
83 if (!$channel->twitch_chat) {
84 throw new \Exception('channel has no twitch chat set');
86 $validatedData = $request->validate([
87 'bot_nick' => 'string',
89 $this->authorize('editRestream', $channel);
90 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
91 if ($nick == 'localhorsttv') {
92 $channel->join = true;
93 } else if ($nick == 'horstiebot') {
94 $channel->chat = true;
97 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
98 return $this->sendChannel($channel);
101 public function part(Request $request, Channel $channel) {
102 if (!$channel->twitch_chat) {
103 throw new \Exception('channel has no twitch chat set');
105 $validatedData = $request->validate([
106 'bot_nick' => 'string',
108 $this->authorize('editRestream', $channel);
109 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
110 if ($nick == 'localhorsttv') {
111 $channel->join = false;
112 } else if ($nick == 'horstiebot') {
113 $channel->chat = false;
116 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
117 return $this->sendChannel($channel);
120 public function deleteCommand(Channel $channel, $command) {
121 $this->authorize('editRestream', $channel);
122 if (isset($channel->chat_commands[$command])) {
123 $cmds = $channel->chat_commands;
124 unset($cmds[$command]);
125 $channel->chat_commands = $cmds;
128 return $this->sendChannel($channel);
131 public function saveCommand(Request $request, Channel $channel, $command) {
132 $this->authorize('editRestream', $channel);
134 $validatedData = $request->validate([
135 'command' => 'string',
136 'restrict' => 'string',
140 $cmds = $channel->chat_commands;
141 $cmds[$command] = $validatedData;
142 $channel->chat_commands = $cmds;
145 return $this->sendChannel($channel);
148 public function controlGuessingGame(Request $request, Channel $channel, $name) {
149 $this->authorize('editRestream', $channel);
151 $validatedData = $request->validate([
152 'action' => 'required|in:cancel,solve,start,stop',
156 switch ($validatedData['action']) {
158 $channel->clearGuessing();
159 $msg = $channel->getGuessingSetting('cancel_message');
161 TwitchBotCommand::chat($channel->twitch_chat, $msg);
165 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
166 $winners = $channel->solveGuessing($validatedData['solution']);
167 $msg = $channel->listGuessingWinners($winners);
169 TwitchBotCommand::chat($channel->twitch_chat, $msg);
171 $channel->clearGuessing();
175 if (!$channel->hasActiveGuessing()) {
176 $channel->startGuessing($name);
177 $msg = $channel->getGuessingSetting('start_message');
179 TwitchBotCommand::chat($channel->twitch_chat, $msg);
184 if ($channel->hasActiveGuessing()) {
185 $channel->stopGuessing();
186 $msg = $channel->getGuessingSetting('stop_message');
188 TwitchBotCommand::chat($channel->twitch_chat, $msg);
194 return $this->sendChannel($channel);
197 public function getGuessingGame(Channel $channel, $name) {
198 $this->authorize('editRestream', $channel);
200 $cutoff = $channel->guessing_start;
201 if (is_null($cutoff)) {
202 $last = $channel->winners()->latest()->first();
203 $cutoff = $last->pod;
205 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
206 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
208 'guesses' => $guesses->toArray(),
209 'winners' => $winners->toArray(),
213 public function getGuessingGameLeaderboard(Channel $channel, $type) {
215 'all' => $channel->getGuessingLeaderboard(),
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 'close_winners_message' => 'string',
243 'invalid_solution_message' => 'string',
244 'leaderboard_type' => 'string',
245 'no_winners_message' => 'string',
246 'not_active_message' => 'string',
247 'points_exact_first' => 'numeric|min:1|max:5',
248 'points_exact_other' => 'numeric|min:0|max:5',
249 'points_close_first' => 'numeric|min:0|max:5',
250 'points_close_max' => 'integer|min:0',
251 'points_close_other' => 'numeric|min:0|max:5',
252 'start_message' => 'string',
253 'stop_message' => 'string',
254 'winners_message' => 'string',
257 $settings = $channel->guessing_settings;
258 $settings[$name] = $validatedData;
259 $channel->guessing_settings = $settings;
262 return $this->sendChannel($channel);
265 protected function sendChannel(Channel $channel) {
266 if (Gate::allows('editRestream', $channel)) {
267 $this->unhideChannel($channel);
269 return $channel->toJson();
272 protected function sendChannels(Collection $channels) {
273 foreach ($channels as $channel) {
274 if (Gate::allows('editRestream', $channel)) {
275 $this->unhideChannel($channel);
278 return $channels->toJson();
281 private function unhideChannel(Channel $channel) {
282 $channel->makeVisible([