X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FRound.php;h=3777df9e8e96040717fa8ce3e97e0d878188463c;hb=d32516335ea2534e15256c948e9c38d3de40794b;hp=3b84c554e3e6320b2290c8b751930bef0d7ea69e;hpb=edd0e97bfdc544114f30bf4c13a929631c44a555;p=alttp.git diff --git a/app/Models/Round.php b/app/Models/Round.php index 3b84c55..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,7 +61,14 @@ class Round extends Model return $this->belongsTo(Tournament::class); } + + protected $casts = [ + 'code' => 'array', + 'locked' => 'boolean', + ]; + protected $fillable = [ + 'number', 'tournament_id', ];