]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Round.php
server calculated scoring
[alttp.git] / app / Models / Round.php
index 71a4273dc9435b21c609341082fe3c52d99efd33..3777df9e8e96040717fa8ce3e97e0d878188463c 100644 (file)
@@ -9,6 +9,50 @@ class Round extends Model
 {
        use HasFactory;
 
+
+       public function updatePlacement() {
+               $runners = [];
+               foreach ($this->tournament->participants as $p) {
+                       if ($p->isRunner()) {
+                               $runners[] = $p;
+                       } else {
+                               $result = $p->findResult($this);
+                               if ($result) {
+                                       $result->updatePlacement(null, null);
+                               }
+                       }
+               }
+
+               usort($runners, Participant::compareResult($this));
+               $mapped = array_map(function ($p) {
+                       return ['participant' => $p, 'result' => $p->findResult($this)];
+               }, $runners);
+               $filtered = array_filter($mapped, function($r) {
+                       return $r['result'] && ($r['result']->time || $r['result']->forfeit);
+               });
+               $reversed = array_reverse($filtered);
+
+               $running = 0;
+               $bonus = 1;
+               $lastResult = null;
+               foreach ($reversed as $r) {
+                       $betterThanLast = is_null($lastResult) || $r['result']->time < $lastResult;
+                       if (!$r['result']->forfeit && $betterThanLast) {
+                               $running += $bonus;
+                               $lastResult = $r['result']->time;
+                               $bonus = 1;
+                       } else {
+                               ++$bonus;
+                       }
+                       if (!$r['result']->forfeit) {
+                               $r['result']->updatePlacement($running, count($filtered) - $running + 1);
+                       } else {
+                               $r['result']->updatePlacement(0, count($filtered));
+                       }
+               }
+       }
+
+
        public function results() {
                return $this->hasMany(Result::class);
        }
@@ -17,6 +61,7 @@ class Round extends Model
                return $this->belongsTo(Tournament::class);
        }
 
+
        protected $casts = [
                'code' => 'array',
                'locked' => 'boolean',