]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Channel.php
guessing game leaderboard
[alttp.git] / app / Models / Channel.php
index 32ac875bb707c0760f9e75d2362a64416db7da61..edc2ba8664be682bc3638fbb16e485b905ca55a1 100644 (file)
@@ -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',