]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Channel.php
some guessing game fixes
[alttp.git] / app / Models / Channel.php
index 8debe4f9434ab554d70eb5e16086bf875ed1875e..bf3b98bc2b231c384a3079613e551514a7435c02 100644 (file)
@@ -32,7 +32,20 @@ class Channel extends Model {
        }
 
        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();
+               $query = $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);
+               $type = $this->getGuessingSetting('leaderboard_type', 'all');
+               if ($type == 'month') {
+                       $query->where('created_at', '>=', now()->startOfMonth());
+               } else if ($type == 'year') {
+                       $query->where('created_at', '>=', now()->startOfYear());
+               } else if (is_numeric($type)) {
+                       $query->where('created_at', '>=', now()->sub($type, 'days'));
+               }
+               return $query->get();
        }
 
        public function hasActiveGuessing() {
@@ -119,23 +132,33 @@ class Channel extends Model {
                $this->save();
        }
 
+       public function transformGuess($original) {
+               $transformed = trim($original);
+               if ($this->guessing_type == 'gtbk') {
+                       $transformed = str_replace(['roodyo1Gtbigkey'], ['2'], $transformed);
+                       $transformed = str_ireplace(['vier 4Head'], ['4'], $transformed);
+               }
+               return $transformed;
+       }
+
        public function registerGuess($uid, $uname, $guess) {
                $model = new GuessingGuess();
                $model->channel()->associate($this);
                $model->uid = $uid;
                $model->uname = $uname;
-               $model->guess = $guess;
+               $model->guess = $this->transformGuess($guess);
                $model->save();
        }
 
        public function scoreGuessing($solution, $guess, $first) {
-               if ($guess == $solution) {
+               $transformed = $this->transformGuess($guess);
+               if ($transformed == $solution) {
                        if ($first) {
                                return $this->getGuessingSetting('points_exact_first', 1);
                        }
                        return $this->getGuessingSetting('points_exact_other', 1);
                }
-               $distance = abs(intval($guess) - intval($solution));
+               $distance = abs(intval($transformed) - intval($solution));
                if ($distance <= $this->getGuessingSetting('points_close_max', 3)) {
                        if ($first) {
                                return $this->getGuessingSetting('points_close_first', 1);
@@ -146,13 +169,33 @@ class Channel extends Model {
        }
 
        public function isValidGuess($solution) {
+               $transformed = $this->transformGuess($solution);
                if ($this->guessing_type == 'gtbk') {
-                       $int_solution = intval($solution);
-                       return $int_solution > 0 && $int_solution < 23;
+                       $int_solution = intval($transformed);
+                       return is_numeric($transformed) && $int_solution > 0 && $int_solution < 23;
                }
                return false;
        }
 
+       public function listGuessingWinners($winners) {
+               $names = [];
+               $distance = 0;
+               foreach ($winners as $winner) {
+                       if ($winner->score > 0) {
+                               $names[] = $winner->uname;
+                               $distance = abs(intval($winner->guess) - intval($winner->solution));
+                       }
+               }
+               $msg = '';
+               if (empty($names)) {
+                       $msg = $this->getGuessingSetting('no_winners_message');
+               } else {
+                       $msg = $this->getGuessingSetting($distance ? 'close_winners_message' : 'winners_message', $this->getGuessingSetting('winners_message'));
+                       $msg = str_replace(['{distance}', '{names}'], [$distance, $this->listAnd($names)], $msg);
+               }
+               return $msg;
+       }
+
        public function listAnd($entries) {
                $lang = empty($this->languages) ? 'en' : $this->languages[0];
                if ($lang == 'de') {