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 if (is_null($min_distance)) {
85 $min_distance = $distance;
87 } else if ($distance < $min_distance) {
88 $candidates = [$guess];
89 $min_distance = $distance;
95 foreach ($candidates as $candidate) {
96 $score = $this->scoreGuessing($solution, $candidate->guess, $first);
97 $winner = new GuessingWinner();
98 $winner->channel()->associate($this);
99 $winner->pod = $start;
100 $winner->uid = $candidate->uid;
101 $winner->uname = $candidate->uname;
102 $winner->guess = $candidate->guess;
103 $winner->solution = $solution;
104 $winner->score = $score;
106 $winners[] = $winner;
112 public function clearGuessing() {
113 $this->guessing_start = null;
114 $this->guessing_end = null;
118 public function registerGuess($uid, $uname, $guess) {
119 $model = new GuessingGuess();
120 $model->channel()->associate($this);
122 $model->uname = $uname;
123 $model->guess = $guess;
127 public function scoreGuessing($solution, $guess, $first) {
128 if ($guess == $solution) {
130 return $this->getGuessingSetting('points_exact_first', 1);
132 return $this->getGuessingSetting('points_exact_other', 1);
134 $distance = abs(intval($guess) - intval($solution));
135 if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
137 return $this->getGuessingSetting('points_close_first', 1);
139 return $this->getGuessingSetting('points_close_other', 1);
144 public function isValidGuess($solution) {
145 if ($this->guessing_type == 'gtbk') {
146 $int_solution = intval($solution);
147 return $int_solution > 0 && $int_solution < 23;
152 public function listAnd($entries) {
153 $lang = empty($this->languages) ? 'en' : $this->languages[0];
155 return Arr::join($entries, ', ', ' und ');
157 return Arr::join($entries, ', ', ' and ');
160 public function crews() {
161 return $this->hasMany(ChannelCrew::class);
164 public function episodes() {
165 return $this->belongsToMany(Episode::class)
166 ->using(Restream::class)
167 ->withPivot('accept_comms', 'accept_tracker');
170 public function guesses() {
171 return $this->hasMany(GuessingGuess::class);
174 public function organization() {
175 return $this->belongsTo(Organization::class);
178 public function winners() {
179 return $this->hasMany(GuessingWinner::class);
184 'chat_commands' => 'array',
185 'chat_settings' => 'array',
186 'guessing_settings' => 'array',
187 'guessing_start' => 'datetime',
188 'guessing_end' => 'datetime',
189 'languages' => 'array',
193 protected $hidden = [