X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FRound.php;h=3c191bc17efb200d65b1263371dec4599d93b508;hb=6609e9cbc3c9d3f9a7f0b2db9d8407f56957cef5;hp=942069255c74cff24f6fa55cd8d3ccd7f208056b;hpb=55f2d7cd6c290a0d26db177d54d20c393f890bbb;p=alttp.git diff --git a/app/Models/Round.php b/app/Models/Round.php index 9420692..3c191bc 100644 --- a/app/Models/Round.php +++ b/app/Models/Round.php @@ -9,12 +9,77 @@ 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); } + public function rolled_by_user() { + return $this->belongsTo(User::class, 'rolled_by'); + } + public function tournament() { return $this->belongsTo(Tournament::class); } + + protected $casts = [ + 'code' => 'array', + 'locked' => 'boolean', + 'no_record' => 'boolean', + ]; + + protected $fillable = [ + 'number', + 'no_record', + 'tournament_id', + ]; + + protected $with = [ + 'rolled_by_user', + ]; + }