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')
37 ->where('score', '!=', 0)
39 ->orderBy('score', 'desc')
41 $type = $this->getGuessingSetting('leaderboard_type', 'all');
42 if ($type == 'month') {
43 $query->where('created_at', '>=', now()->startOfMonth());
44 } else if ($type == 'year') {
45 $query->where('created_at', '>=', now()->startOfYear());
46 } else if (is_numeric($type)) {
47 $query->where('created_at', '>=', now()->sub($type, 'days'));
52 public function hasActiveGuessing() {
53 return !is_null($this->guessing_start);
56 public function isAcceptingGuesses() {
57 return !is_null($this->guessing_start) && is_null($this->guessing_end);
60 public function startGuessing($type) {
61 $this->guessing_type = $type;
62 $this->guessing_start = now();
66 public function stopGuessing() {
67 $this->guessing_end = now();
71 public function getGuessingSetting($name, $default = null) {
72 if (empty($this->guessing_settings) ||
73 empty($this->guessing_type) ||
74 !array_key_exists($this->guessing_type, $this->guessing_settings) ||
75 !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
79 return $this->guessing_settings[$this->guessing_type][$name];
82 public function solveGuessing($solution) {
83 $start = $this->guessing_start;
84 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
85 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
87 foreach ($guesses as $guess) {
88 $unique_guesses[$guess->uid] = $guess;
91 foreach ($unique_guesses as $guess) {
92 if ($guess->guess == $solution) {
93 $candidates[] = $guess;
96 if (empty($candidates) && is_numeric($solution)) {
98 foreach ($unique_guesses as $guess) {
99 $distance = abs(intval($guess->guess) - intval($solution));
100 if (is_null($min_distance) || $distance == $min_distance) {
101 $candidates[] = $guess;
102 if (is_null($min_distance)) {
103 $min_distance = $distance;
105 } else if ($distance < $min_distance) {
106 $candidates = [$guess];
107 $min_distance = $distance;
113 foreach ($candidates as $candidate) {
114 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
115 $winner = new GuessingWinner();
116 $winner->channel()->associate($this);
117 $winner->pod = $start;
118 $winner->uid = $candidate->uid;
119 $winner->uname = $candidate->uname;
120 $winner->guess = $candidate->guess;
121 $winner->solution = $solution;
122 $winner->score = $score;
124 $winners[] = $winner;
130 public function clearGuessing() {
131 $this->guessing_start = null;
132 $this->guessing_end = null;
136 public function transformGuess($original) {
137 $transformed = trim($original);
138 if ($this->guessing_type == 'gtbk') {
139 $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
140 $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
145 public function registerGuess($uid, $uname, $guess) {
146 $model = new GuessingGuess();
147 $model->channel()->associate($this);
149 $model->uname = $uname;
150 $model->guess = $this->transformGuess($guess);
154 public function scoreGuessing($solution, $guess, $first) {
155 $transformed = $this->transformGuess($guess);
156 if ($transformed == $solution) {
158 return $this->getGuessingSetting('points_exact_first', 1);
160 return $this->getGuessingSetting('points_exact_other', 1);
162 $distance = abs(intval($transformed) - intval($solution));
163 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
165 return $this->getGuessingSetting('points_close_first', 1);
167 return $this->getGuessingSetting('points_close_other', 1);
172 public function isValidGuess($solution) {
173 $transformed = $this->transformGuess($solution);
174 if ($this->guessing_type == 'gtbk') {
175 $int_solution = intval($transformed);
176 return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
181 public function listGuessingWinners($winners) {
184 foreach ($winners as $winner) {
185 if ($winner->score > 0) {
186 $names[] = $winner->uname;
187 $distance = abs(intval($winner->guess) - intval($winner->solution));
192 $msg = $this->getGuessingSetting('no_winners_message');
194 $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
195 $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
200 public function listAnd($entries) {
201 $lang = empty($this->languages) ? 'en' : $this->languages[0];
203 return Arr::join($entries, ', ', ' und ');
205 return Arr::join($entries, ', ', ' and ');
208 public function crews() {
209 return $this->hasMany(ChannelCrew::class);
212 public function episodes() {
213 return $this->belongsToMany(Episode::class)
214 ->using(Restream::class)
215 ->withPivot('accept_comms', 'accept_tracker');
218 public function guesses() {
219 return $this->hasMany(GuessingGuess::class);
222 public function organization() {
223 return $this->belongsTo(Organization::class);
226 public function winners() {
227 return $this->hasMany(GuessingWinner::class);
232 'chat_commands' => 'array',
233 'chat_settings' => 'array',
234 'guessing_end' => 'datetime',
235 'guessing_settings' => 'array',
236 'guessing_start' => 'datetime',
237 'languages' => 'array',
241 protected $hidden = [