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 return $this->queryChatlog()
36 ->where('classification', '=', $class)
40 public function queryChatlog() {
41 return ChatLog::where('type', '=', 'chat')
42 ->where('banned', '=', false)
43 ->where('created_at', '<', now()->sub(1, 'day'))
44 ->where(function ($query) {
45 $query->whereNull('detected_language');
46 $query->orWhereIn('detected_language', $this->getPreferredLanguages());
51 public function getPreferredLanguages() {
52 $setting = $this->getChatSetting('language');
56 if (!empty($this->languages)) {
57 return $this->languages;
62 public function getChatSetting($name, $default = null) {
63 if (array_key_exists($name, $this->chat_settings)) {
64 return $this->chat_settings[$name];
69 public function getGuessingLeaderboard() {
70 $query = $this->winners()
71 ->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')
72 ->where('score', '!=', 0)
74 ->orderBy('score', 'desc')
76 $type = $this->getGuessingSetting('leaderboard_type', 'all');
77 if ($type == 'month') {
78 $query->where('created_at', '>=', now()->startOfMonth());
79 } else if ($type == 'year') {
80 $query->where('created_at', '>=', now()->startOfYear());
81 } else if (is_numeric($type)) {
82 $query->where('created_at', '>=', now()->sub($type, 'days'));
87 public function hasActiveGuessing() {
88 return !is_null($this->guessing_start);
91 public function isAcceptingGuesses() {
92 return !is_null($this->guessing_start) && is_null($this->guessing_end);
95 public function startGuessing($type) {
96 $this->guessing_type = $type;
97 $this->guessing_start = now();
101 public function stopGuessing() {
102 $this->guessing_end = now();
106 public function getGuessingSetting($name, $default = null) {
107 if (empty($this->guessing_settings) ||
108 empty($this->guessing_type) ||
109 !array_key_exists($this->guessing_type, $this->guessing_settings) ||
110 !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
114 return $this->guessing_settings[$this->guessing_type][$name];
117 public function solveGuessing($solution) {
118 $start = $this->guessing_start;
119 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
120 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
121 $unique_guesses = [];
122 foreach ($guesses as $guess) {
123 $unique_guesses[$guess->uid] = $guess;
126 foreach ($unique_guesses as $guess) {
127 if ($guess->guess == $solution) {
128 $candidates[] = $guess;
131 if (empty($candidates) && is_numeric($solution)) {
132 $min_distance = null;
133 foreach ($unique_guesses as $guess) {
134 $distance = abs(intval($guess->guess) - intval($solution));
135 if (is_null($min_distance) || $distance == $min_distance) {
136 $candidates[] = $guess;
137 if (is_null($min_distance)) {
138 $min_distance = $distance;
140 } else if ($distance < $min_distance) {
141 $candidates = [$guess];
142 $min_distance = $distance;
148 foreach ($candidates as $candidate) {
149 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
150 $winner = new GuessingWinner();
151 $winner->channel()->associate($this);
152 $winner->pod = $start;
153 $winner->uid = $candidate->uid;
154 $winner->uname = $candidate->uname;
155 $winner->guess = $candidate->guess;
156 $winner->solution = $solution;
157 $winner->score = $score;
159 $winners[] = $winner;
165 public function clearGuessing() {
166 $this->guessing_start = null;
167 $this->guessing_end = null;
171 public function transformGuess($original) {
172 $transformed = trim($original);
173 if ($this->guessing_type == 'gtbk') {
174 $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
175 $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
180 public function registerGuess($uid, $uname, $guess) {
181 $model = new GuessingGuess();
182 $model->channel()->associate($this);
184 $model->uname = $uname;
185 $model->guess = $this->transformGuess($guess);
189 public function scoreGuessing($solution, $guess, $first) {
190 $transformed = $this->transformGuess($guess);
191 if ($transformed == $solution) {
193 return $this->getGuessingSetting('points_exact_first', 1);
195 return $this->getGuessingSetting('points_exact_other', 1);
197 $distance = abs(intval($transformed) - intval($solution));
198 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
200 return $this->getGuessingSetting('points_close_first', 1);
202 return $this->getGuessingSetting('points_close_other', 1);
207 public function isValidGuess($solution) {
208 $transformed = $this->transformGuess($solution);
209 if ($this->guessing_type == 'gtbk') {
210 $int_solution = intval($transformed);
211 return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
216 public function listGuessingWinners($winners) {
219 foreach ($winners as $winner) {
220 if ($winner->score > 0) {
221 $names[] = $winner->uname;
222 $distance = abs(intval($winner->guess) - intval($winner->solution));
227 $msg = $this->getGuessingSetting('no_winners_message');
229 $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
230 $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
235 public function listAnd($entries) {
236 $lang = empty($this->languages) ? 'en' : $this->languages[0];
238 return Arr::join($entries, ', ', ' und ');
240 return Arr::join($entries, ', ', ' and ');
243 public function crews() {
244 return $this->hasMany(ChannelCrew::class);
247 public function episodes() {
248 return $this->belongsToMany(Episode::class)
249 ->using(Restream::class)
250 ->withPivot('accept_comms', 'accept_tracker');
253 public function guesses() {
254 return $this->hasMany(GuessingGuess::class);
257 public function organization() {
258 return $this->belongsTo(Organization::class);
261 public function winners() {
262 return $this->hasMany(GuessingWinner::class);
267 'chat_commands' => 'array',
268 'chat_settings' => 'array',
269 'guessing_end' => 'datetime',
270 'guessing_settings' => 'array',
271 'guessing_start' => 'datetime',
272 'languages' => 'array',
276 protected $hidden = [