X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FRound.php;h=3777df9e8e96040717fa8ce3e97e0d878188463c;hb=d32516335ea2534e15256c948e9c38d3de40794b;hp=71a4273dc9435b21c609341082fe3c52d99efd33;hpb=2b3d5b3d98705cda41a7218d7133234b227e87b6;p=alttp.git diff --git a/app/Models/Round.php b/app/Models/Round.php index 71a4273..3777df9 100644 --- a/app/Models/Round.php +++ b/app/Models/Round.php @@ -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',