X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FChannel.php;h=8debe4f9434ab554d70eb5e16086bf875ed1875e;hb=167f986f468014e00d82fa2df8193f6be8dca19d;hp=d68ba5884888838271dbceac3e9ab8b3a3314c4f;hpb=15132749249f6418fd5695547b5c323a0ad10939;p=alttp.git diff --git a/app/Models/Channel.php b/app/Models/Channel.php index d68ba58..8debe4f 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -2,24 +2,208 @@ namespace App\Models; +use Illuminate\Broadcasting\Channel as PublicChannel; +use Illuminate\Broadcasting\PrivateChannel; +use Illuminate\Database\Eloquent\BroadcastsEvents; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; -class Channel extends Model -{ +class Channel extends Model { + + use BroadcastsEvents; use HasFactory; + public function broadcastOn($event) { + $channels = [ + new PrivateChannel('Channel.'.$this->id), + ]; + if (!empty($this->access_key)) { + $channels[] = new PublicChannel('ChannelKey.'.$this->access_key); + } + return $channels; + } + + public function getCurrentEpisode() { + return $this->episodes() + ->where('start', '<', now()->subMinutes(10)) + ->orderBy('start', 'DESC') + ->first(); + } + + public function getGuessingLeaderboard() { + 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(); + } + + public function hasActiveGuessing() { + return !is_null($this->guessing_start); + } + + public function isAcceptingGuesses() { + return !is_null($this->guessing_start) && is_null($this->guessing_end); + } + + public function startGuessing($type) { + $this->guessing_type = $type; + $this->guessing_start = now(); + $this->save(); + } + + public function stopGuessing() { + $this->guessing_end = now(); + $this->save(); + } + + public function getGuessingSetting($name, $default = null) { + if (empty($this->guessing_settings) || + empty($this->guessing_type) || + !array_key_exists($this->guessing_type, $this->guessing_settings) || + !array_key_exists($name, $this->guessing_settings[$this->guessing_type]) + ) { + return $default; + } + return $this->guessing_settings[$this->guessing_type][$name]; + } + + public function solveGuessing($solution) { + $start = $this->guessing_start; + $end = is_null($this->guessing_end) ? now() : $this->guessing_end; + $guesses = $this->guesses()->whereBetween('created_at', [$start, $end])->orderBy('created_at', 'ASC')->get(); + $unique_guesses = []; + foreach ($guesses as $guess) { + $unique_guesses[$guess->uid] = $guess; + } + $candidates = []; + foreach ($unique_guesses as $guess) { + if ($guess->guess == $solution) { + $candidates[] = $guess; + } + } + if (empty($candidates) && is_numeric($solution)) { + $min_distance = null; + foreach ($unique_guesses as $guess) { + $distance = abs(intval($guess->guess) - intval($solution)); + if (is_null($min_distance) || $distance == $min_distance) { + $candidates[] = $guess; + if (is_null($min_distance)) { + $min_distance = $distance; + } + } else if ($distance < $min_distance) { + $candidates = [$guess]; + $min_distance = $distance; + } + } + } + $winners = []; + $first = true; + foreach ($candidates as $candidate) { + $score = $this->scoreGuessing($solution, $candidate->guess, $first); + $winner = new GuessingWinner(); + $winner->channel()->associate($this); + $winner->pod = $start; + $winner->uid = $candidate->uid; + $winner->uname = $candidate->uname; + $winner->guess = $candidate->guess; + $winner->solution = $solution; + $winner->score = $score; + $winner->save(); + $winners[] = $winner; + $first = false; + } + return $winners; + } + + public function clearGuessing() { + $this->guessing_start = null; + $this->guessing_end = null; + $this->save(); + } + + public function registerGuess($uid, $uname, $guess) { + $model = new GuessingGuess(); + $model->channel()->associate($this); + $model->uid = $uid; + $model->uname = $uname; + $model->guess = $guess; + $model->save(); + } + + public function scoreGuessing($solution, $guess, $first) { + if ($guess == $solution) { + if ($first) { + return $this->getGuessingSetting('points_exact_first', 1); + } + return $this->getGuessingSetting('points_exact_other', 1); + } + $distance = abs(intval($guess) - intval($solution)); + if ($distance <= $this->getGuessingSetting('points_close_max', 3)) { + if ($first) { + return $this->getGuessingSetting('points_close_first', 1); + } + return $this->getGuessingSetting('points_close_other', 1); + } + return 0; + } + + public function isValidGuess($solution) { + if ($this->guessing_type == 'gtbk') { + $int_solution = intval($solution); + return $int_solution > 0 && $int_solution < 23; + } + return false; + } + + public function listAnd($entries) { + $lang = empty($this->languages) ? 'en' : $this->languages[0]; + if ($lang == 'de') { + return Arr::join($entries, ', ', ' und '); + } + return Arr::join($entries, ', ', ' and '); + } + + public function crews() { + return $this->hasMany(ChannelCrew::class); + } + + public function episodes() { + return $this->belongsToMany(Episode::class) + ->using(Restream::class) + ->withPivot('accept_comms', 'accept_tracker'); + } + + public function guesses() { + return $this->hasMany(GuessingGuess::class); + } + public function organization() { return $this->belongsTo(Organization::class); } + public function winners() { + return $this->hasMany(GuessingWinner::class); + } + protected $casts = [ + 'chat' => 'boolean', + 'chat_commands' => 'array', + 'chat_settings' => 'array', + 'guessing_end' => 'datetime', + 'guessing_settings' => 'array', + 'guessing_start' => 'datetime', 'languages' => 'array', + 'join' => 'boolean', ]; protected $hidden = [ + 'access_key', + 'chat', + 'chat_commands', + 'chat_settings', 'created_at', 'ext_id', + 'guessing_settings', + 'join', + 'twitch_chat', 'updated_at', ];