]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Channel.php
guessing game settings
[alttp.git] / app / Models / Channel.php
index 32ac875bb707c0760f9e75d2362a64416db7da61..738312dc3e82a60d6725a0289f2886a11b3dcc2e 100644 (file)
@@ -35,6 +35,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 +107,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) {
@@ -129,6 +153,7 @@ class Channel extends Model
                'chat' => 'boolean',
                'chat_commands' => 'array',
                'chat_settings' => 'array',
+               'guessing_settings' => 'array',
                'guessing_start' => 'datetime',
                'guessing_end' => 'datetime',
                'languages' => 'array',