5 use Illuminate\Broadcasting\Channel as PublicChannel;
6 use Illuminate\Broadcasting\PrivateChannel;
7 use Illuminate\Database\Eloquent\BroadcastsEvents;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Support\Arr;
12 class Channel extends Model {
17 public function broadcastOn($event) {
19 new PrivateChannel('Channel.'.$this->id),
21 if (!empty($this->access_key)) {
22 $channels[] = new PublicChannel('ChannelKey.'.$this->access_key);
27 public function getCurrentEpisode() {
28 return $this->episodes()
29 ->where('start', '<', now()->subMinutes(10))
30 ->orderBy('start', 'DESC')
34 public function randomOfClass($class) {
35 if (is_array($class)) {
36 return $this->queryChatlog()
37 ->whereIn('classification', $class)
40 return $this->queryChatlog()
41 ->where('classification', '=', $class)
45 public function queryChatlog() {
46 return ChatLog::where('type', '=', 'chat')
47 ->where('banned', '=', false)
48 ->where('created_at', '<', now()->sub(1, 'day'))
49 ->where(function ($query) {
50 $query->whereNull('detected_language');
51 $query->orWhereIn('detected_language', $this->getPreferredLanguages());
56 public function getPreferredLanguages() {
57 $setting = $this->getChatSetting('language');
61 if (!empty($this->languages)) {
62 return $this->languages;
67 public function getChatSetting($name, $default = null) {
68 if (array_key_exists($name, $this->chat_settings)) {
69 return $this->chat_settings[$name];
74 public function getGuessingLeaderboard() {
75 $query = $this->winners()
76 ->selectRaw('(select t2.uname from guessing_winners t2 where t2.uid = guessing_winners.uid order by created_at desc limit 1) as name, sum(score) as score')
77 ->where('score', '!=', 0)
79 ->orderBy('score', 'desc')
81 $type = $this->getGuessingSetting('leaderboard_type', 'all');
82 if ($type == 'month') {
83 $query->where('created_at', '>=', now()->startOfMonth());
84 } else if ($type == 'year') {
85 $query->where('created_at', '>=', now()->startOfYear());
86 } else if (is_numeric($type)) {
87 $query->where('created_at', '>=', now()->sub($type, 'days'));
92 public function hasActiveGuessing() {
93 return !is_null($this->guessing_start);
96 public function isAcceptingGuesses() {
97 return !is_null($this->guessing_start) && is_null($this->guessing_end);
100 public function startGuessing($type) {
101 $this->guessing_type = $type;
102 $this->guessing_start = now();
106 public function stopGuessing() {
107 $this->guessing_end = now();
111 public function getGuessingSetting($name, $default = null) {
112 if (empty($this->guessing_settings) ||
113 empty($this->guessing_type) ||
114 !array_key_exists($this->guessing_type, $this->guessing_settings) ||
115 !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
119 return $this->guessing_settings[$this->guessing_type][$name];
122 public function solveGuessing($solution) {
123 $start = $this->guessing_start;
124 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
125 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
126 $unique_guesses = [];
127 foreach ($guesses as $guess) {
128 $unique_guesses[$guess->uid] = $guess;
131 foreach ($unique_guesses as $guess) {
132 if ($guess->guess == $solution) {
133 $candidates[] = $guess;
136 if (empty($candidates) && is_numeric($solution)) {
137 $min_distance = null;
138 foreach ($unique_guesses as $guess) {
139 $distance = abs(intval($guess->guess) - intval($solution));
140 if (is_null($min_distance) || $distance == $min_distance) {
141 $candidates[] = $guess;
142 if (is_null($min_distance)) {
143 $min_distance = $distance;
145 } else if ($distance < $min_distance) {
146 $candidates = [$guess];
147 $min_distance = $distance;
153 foreach ($candidates as $candidate) {
154 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
155 $winner = new GuessingWinner();
156 $winner->channel()->associate($this);
157 $winner->pod = $start;
158 $winner->uid = $candidate->uid;
159 $winner->uname = $candidate->uname;
160 $winner->guess = $candidate->guess;
161 $winner->solution = $solution;
162 $winner->score = $score;
164 $winners[] = $winner;
170 public function clearGuessing() {
171 $this->guessing_start = null;
172 $this->guessing_end = null;
176 public function transformGuess($original) {
177 $transformed = trim($original);
178 if ($this->guessing_type == 'gtbk') {
179 $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
180 $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
185 public function registerGuess($uid, $uname, $guess) {
186 $model = new GuessingGuess();
187 $model->channel()->associate($this);
189 $model->uname = $uname;
190 $model->guess = $this->transformGuess($guess);
194 public function scoreGuessing($solution, $guess, $first) {
195 $transformed = $this->transformGuess($guess);
196 if ($transformed == $solution) {
198 return $this->getGuessingSetting('points_exact_first', 1);
200 return $this->getGuessingSetting('points_exact_other', 1);
202 $distance = abs(intval($transformed) - intval($solution));
203 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
205 return $this->getGuessingSetting('points_close_first', 1);
207 return $this->getGuessingSetting('points_close_other', 1);
212 public function isValidGuess($solution) {
213 $transformed = $this->transformGuess($solution);
214 if ($this->guessing_type == 'gtbk') {
215 $int_solution = intval($transformed);
216 return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
221 public function listGuessingWinners($winners) {
224 foreach ($winners as $winner) {
225 if ($winner->score > 0) {
226 $names[] = $winner->uname;
227 $distance = abs(intval($winner->guess) - intval($winner->solution));
232 $msg = $this->getGuessingSetting('no_winners_message');
234 $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
235 $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
240 public function listAnd($entries) {
241 $lang = empty($this->languages) ? 'en' : $this->languages[0];
243 return Arr::join($entries, ', ', ' und ');
245 return Arr::join($entries, ', ', ' and ');
248 public function chat_bot_logs() {
249 return $this->hasMany(ChatBotLog::class);
252 public function crews() {
253 return $this->hasMany(ChannelCrew::class);
256 public function episodes() {
257 return $this->belongsToMany(Episode::class)
258 ->using(Restream::class)
259 ->withPivot('accept_comms', 'accept_tracker');
262 public function guesses() {
263 return $this->hasMany(GuessingGuess::class);
266 public function organization() {
267 return $this->belongsTo(Organization::class);
270 public function winners() {
271 return $this->hasMany(GuessingWinner::class);
276 'chat_commands' => 'array',
277 'chat_settings' => 'array',
278 'guessing_end' => 'datetime',
279 'guessing_settings' => 'array',
280 'guessing_start' => 'datetime',
281 'languages' => 'array',
285 protected $hidden = [