5 use Illuminate\Broadcasting\PrivateChannel;
6 use Illuminate\Database\Eloquent\BroadcastsEvents;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Model;
9 use Illuminate\Support\Arr;
11 class Channel extends Model {
16 public function broadcastOn($event) {
18 new PrivateChannel('Channel.'.$this->id),
23 public function getCurrentEpisode() {
24 return $this->episodes()
25 ->where('start', '<', now()->subMinutes(10))
26 ->orderBy('start', 'DESC')
30 public function getGuessingLeaderboard() {
31 return $this->winners()->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')->groupBy('uid')->orderBy('score', 'desc')->limit(10)->get();
34 public function hasActiveGuessing() {
35 return !is_null($this->guessing_start);
38 public function isAcceptingGuesses() {
39 return !is_null($this->guessing_start) && is_null($this->guessing_end);
42 public function startGuessing($type) {
43 $this->guessing_type = $type;
44 $this->guessing_start = now();
48 public function stopGuessing() {
49 $this->guessing_end = now();
53 public function getGuessingSetting($name, $default = null) {
54 if (empty($this->guessing_settings) ||
55 empty($this->guessing_type) ||
56 !array_key_exists($this->guessing_type, $this->guessing_settings) ||
57 !array_key_exists($name, $this->guessing_settings[$this->guessing_type])
61 return $this->guessing_settings[$this->guessing_type][$name];
64 public function solveGuessing($solution) {
65 $start = $this->guessing_start;
66 $end = is_null($this->guessing_end) ? now() : $this->guessing_end;
67 $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get();
69 foreach ($guesses as $guess) {
70 $unique_guesses[$guess->uid] = $guess;
73 foreach ($unique_guesses as $guess) {
74 if ($guess->guess == $solution) {
75 $candidates[] = $guess;
78 if (empty($candidates) && is_numeric($solution)) {
80 foreach ($unique_guesses as $guess) {
81 $distance = abs(intval($guess->guess) - intval($solution));
82 if (is_null($min_distance) || $distance == $min_distance) {
83 $candidates[] = $guess;
84 } else if ($distance < $min_distance) {
85 $candidates = [$guess];
86 $min_distance = $distance;
92 foreach ($candidates as $candidate) {
93 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
94 $winner = new GuessingWinner();
95 $winner->channel()->associate($this);
96 $winner->pod = $start;
97 $winner->uid = $candidate->uid;
98 $winner->uname = $candidate->uname;
99 $winner->guess = $candidate->guess;
100 $winner->solution = $solution;
101 $winner->score = $score;
103 $winners[] = $winner;
109 public function clearGuessing() {
110 $this->guessing_start = null;
111 $this->guessing_end = null;
115 public function registerGuess($uid, $uname, $guess) {
116 $model = new GuessingGuess();
117 $model->channel()->associate($this);
119 $model->uname = $uname;
120 $model->guess = $guess;
124 public function scoreGuessing($solution, $guess, $first) {
125 if ($guess == $solution) {
127 return $this->getGuessingSetting('points_exact_first', 1);
129 return $this->getGuessingSetting('points_exact_other', 1);
131 $distance = abs(intval($guess) - intval($solution));
132 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
134 return $this->getGuessingSetting('points_close_first', 1);
136 return $this->getGuessingSetting('points_close_other', 1);
141 public function isValidGuess($solution) {
142 if ($this->guessing_type == 'gtbk') {
143 $int_solution = intval($solution);
144 return $int_solution > 0 && $int_solution < 23;
149 public function listAnd($entries) {
150 $lang = empty($this->languages) ? 'en' : $this->languages[0];
152 return Arr::join($entries, ', ', ' und ');
154 return Arr::join($entries, ', ', ' and ');
157 public function crews() {
158 return $this->hasMany(ChannelCrew::class);
161 public function episodes() {
162 return $this->belongsToMany(Episode::class)
163 ->using(Restream::class)
164 ->withPivot('accept_comms', 'accept_tracker');
167 public function guesses() {
168 return $this->hasMany(GuessingGuess::class);
171 public function organization() {
172 return $this->belongsTo(Organization::class);
175 public function winners() {
176 return $this->hasMany(GuessingWinner::class);
181 'chat_commands' => 'array',
182 'chat_settings' => 'array',
183 'guessing_settings' => 'array',
184 'guessing_start' => 'datetime',
185 'guessing_end' => 'datetime',
186 'languages' => 'array',
190 protected $hidden = [