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 getGuessingLeaderboard() {
35 $query = $this->winners()
36 ->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')
38 ->orderBy('score', 'desc')
40 $type = $this->getGuessingSetting('leaderboard_type', 'all');
41 if ($type == 'month') {
42 $query->where('created_at', '>=', now()->startOfMonth());
43 } else if ($type == 'year') {
44 $query->where('created_at', '>=', now()->startOfYear());
45 } else if (is_numeric($type)) {
46 $query->where('created_at', '>=', now()->sub($type, 'days'));
51 public function hasActiveGuessing() {
52 return !is_null($this->guessing_start);
55 public function isAcceptingGuesses() {
56 return !is_null($this->guessing_start) && is_null($this->guessing_end);
59 public function startGuessing($type) {
60 $this->guessing_type = $type;
61 $this->guessing_start = now();
65 public function stopGuessing() {
66 $this->guessing_end = now();
70 public function getGuessingSetting($name, $default = null) {
71 if (empty($this->guessing_settings) ||
72 empty($this->guessing_type) ||
73 !array_key_exists($this->guessing_type, $this->guessing_settings) ||
74 !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
78 return $this->guessing_settings[$this->guessing_type][$name];
81 public function solveGuessing($solution) {
82 $start = $this->guessing_start;
83 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
84 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
86 foreach ($guesses as $guess) {
87 $unique_guesses[$guess->uid] = $guess;
90 foreach ($unique_guesses as $guess) {
91 if ($guess->guess == $solution) {
92 $candidates[] = $guess;
95 if (empty($candidates) && is_numeric($solution)) {
97 foreach ($unique_guesses as $guess) {
98 $distance = abs(intval($guess->guess) - intval($solution));
99 if (is_null($min_distance) || $distance == $min_distance) {
100 $candidates[] = $guess;
101 if (is_null($min_distance)) {
102 $min_distance = $distance;
104 } else if ($distance < $min_distance) {
105 $candidates = [$guess];
106 $min_distance = $distance;
112 foreach ($candidates as $candidate) {
113 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
114 $winner = new GuessingWinner();
115 $winner->channel()->associate($this);
116 $winner->pod = $start;
117 $winner->uid = $candidate->uid;
118 $winner->uname = $candidate->uname;
119 $winner->guess = $candidate->guess;
120 $winner->solution = $solution;
121 $winner->score = $score;
123 $winners[] = $winner;
129 public function clearGuessing() {
130 $this->guessing_start = null;
131 $this->guessing_end = null;
135 public function transformGuess($original) {
136 $transformed = trim($original);
137 if ($this->guessing_type == 'gtbk') {
138 $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
139 $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
144 public function registerGuess($uid, $uname, $guess) {
145 $model = new GuessingGuess();
146 $model->channel()->associate($this);
148 $model->uname = $uname;
149 $model->guess = $this->transformGuess($guess);
153 public function scoreGuessing($solution, $guess, $first) {
154 $transformed = $this->transformGuess($guess);
155 if ($transformed == $solution) {
157 return $this->getGuessingSetting('points_exact_first', 1);
159 return $this->getGuessingSetting('points_exact_other', 1);
161 $distance = abs(intval($transformed) - intval($solution));
162 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
164 return $this->getGuessingSetting('points_close_first', 1);
166 return $this->getGuessingSetting('points_close_other', 1);
171 public function isValidGuess($solution) {
172 $transformed = $this->transformGuess($solution);
173 if ($this->guessing_type == 'gtbk') {
174 $int_solution = intval($transformed);
175 return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
180 public function listGuessingWinners($winners) {
183 foreach ($winners as $winner) {
184 if ($winner->score > 0) {
185 $names[] = $winner->uname;
186 $distance = abs(intval($winner->guess) - intval($winner->solution));
191 $msg = $this->getGuessingSetting('no_winners_message');
193 $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
194 $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
199 public function listAnd($entries) {
200 $lang = empty($this->languages) ? 'en' : $this->languages[0];
202 return Arr::join($entries, ', ', ' und ');
204 return Arr::join($entries, ', ', ' and ');
207 public function crews() {
208 return $this->hasMany(ChannelCrew::class);
211 public function episodes() {
212 return $this->belongsToMany(Episode::class)
213 ->using(Restream::class)
214 ->withPivot('accept_comms', 'accept_tracker');
217 public function guesses() {
218 return $this->hasMany(GuessingGuess::class);
221 public function organization() {
222 return $this->belongsTo(Organization::class);
225 public function winners() {
226 return $this->hasMany(GuessingWinner::class);
231 'chat_commands' => 'array',
232 'chat_settings' => 'array',
233 'guessing_end' => 'datetime',
234 'guessing_settings' => 'array',
235 'guessing_start' => 'datetime',
236 'languages' => 'array',
240 protected $hidden = [