- if (!$r['result']->forfeit) {
- $r['result']->updatePlacement($running, count($filtered) - $running + 1);
- } else {
- $r['result']->updatePlacement(0, count($filtered));
+
+ 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));
+ }