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;
10 class ChannelController extends Controller {
12 public function search(Request $request) {
13 $validatedData = $request->validate([
14 'joinable' => 'boolean|nullable',
15 'manageable' => 'boolean|nullable',
16 'phrase' => 'string|nullable',
19 $channels = Channel::query();
20 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
21 $channels = $channels->where('twitch_chat', '!=', '');
23 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
24 $user = $request->user();
28 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
29 $query->where('user_id', '=', $user->id);
32 if (!empty($validatedData['phrase'])) {
33 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
34 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
36 $channels = $channels->limit(5);
37 return $channels->get()->toJson();
40 public function single(Request $request, Channel $channel) {
41 $this->authorize('view', $channel);
42 return $channel->toJson();
45 public function chat(Request $request, Channel $channel) {
46 if (!$channel->twitch_chat) {
47 throw new \Exception('channel has no twitch chat set');
49 $validatedData = $request->validate([
50 'bot_nick' => 'string',
51 'text' => 'string|required',
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();
59 public function chatSettings(Request $request, Channel $channel) {
60 if (!$channel->twitch_chat) {
61 throw new \Exception('channel has no twitch chat set');
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',
69 $this->authorize('editRestream', $channel);
70 $channel->chat_settings = $validatedData;
72 return $channel->toJson();
75 public function join(Request $request, Channel $channel) {
76 if (!$channel->twitch_chat) {
77 throw new \Exception('channel has no twitch chat set');
79 $validatedData = $request->validate([
80 'bot_nick' => 'string',
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;
90 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
91 return $channel->toJson();
94 public function part(Request $request, Channel $channel) {
95 if (!$channel->twitch_chat) {
96 throw new \Exception('channel has no twitch chat set');
98 $validatedData = $request->validate([
99 'bot_nick' => 'string',
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;
109 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
110 return $channel->toJson();
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;
121 return $channel->toJson();
124 public function saveCommand(Request $request, Channel $channel, $command) {
125 $this->authorize('editRestream', $channel);
127 $validatedData = $request->validate([
128 'command' => 'string',
129 'restrict' => 'string',
133 $cmds = $channel->chat_commands;
134 $cmds[$command] = $validatedData;
135 $channel->chat_commands = $cmds;
138 return $channel->toJson();
141 public function controlGuessingGame(Request $request, Channel $channel, $name) {
142 $this->authorize('editRestream', $channel);
144 $validatedData = $request->validate([
145 'action' => 'required|in:cancel,solve,start,stop',
149 switch ($validatedData['action']) {
151 $channel->clearGuessing();
152 $msg = $channel->getGuessingSetting('cancel_message');
154 TwitchBotCommand::chat($channel->twitch_chat, $msg);
158 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
159 $winners = $channel->solveGuessing($validatedData['solution']);
161 foreach ($winners as $winner) {
162 if ($winner->score > 0) {
163 $names[] = $winner->uname;
167 $msg = $channel->getGuessingSetting('no_winners_message');
169 $msg = $channel->getGuessingSetting('winners_message');
170 $msg = str_replace('{names}', $channel->listAnd($names), $msg);
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 $channel->toJson();
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 saveGuessingGame(Request $request, Channel $channel, $name) {
218 $this->authorize('editRestream', $channel);
220 $validatedData = $request->validate([
221 'active_message' => 'string',
222 'cancel_message' => 'string',
223 'invalid_solution_message' => 'string',
224 'no_winners_message' => 'string',
225 'not_active_message' => 'string',
226 'points_exact_first' => 'numeric|min:1|max:5',
227 'points_exact_other' => 'numeric|min:0|max:5',
228 'points_close_first' => 'numeric|min:0|max:5',
229 'points_close_max' => 'integer|min:0',
230 'points_close_other' => 'numeric|min:0|max:5',
231 'start_message' => 'string',
232 'stop_message' => 'string',
233 'winners_message' => 'string',
236 $settings = $channel->guessing_settings;
237 $settings[$name] = $validatedData;
238 $channel->guessing_settings = $settings;
241 return $channel->toJson();