X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=app%2FModels%2FChannel.php;h=edc2ba8664be682bc3638fbb16e485b905ca55a1;hb=0b25d0e8a22a13f09efbeab94d0b353ae603bc16;hp=32ac875bb707c0760f9e75d2362a64416db7da61;hpb=7fc357a5943bf280ce2fa9aa97ec516af61efd69;p=alttp.git diff --git a/app/Models/Channel.php b/app/Models/Channel.php index 32ac875..edc2ba8 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -16,6 +16,10 @@ class Channel extends Model ->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); } @@ -35,6 +39,17 @@ class Channel extends Model $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; @@ -96,7 +111,20 @@ class Channel extends Model } public function scoreGuessing($solution, $guess, $first) { - return 1; + 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) { @@ -125,10 +153,15 @@ class Channel extends Model return $this->belongsTo(Organization::class); } + public function winners() { + return $this->hasMany(GuessingWinner::class); + } + protected $casts = [ 'chat' => 'boolean', 'chat_commands' => 'array', 'chat_settings' => 'array', + 'guessing_settings' => 'array', 'guessing_start' => 'datetime', 'guessing_end' => 'datetime', 'languages' => 'array',