5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
8 class Round extends Model
13 public function isComplete() {
14 if (count($this->tournament->participants) == 0) return false;
15 if (count($this->results) == 0) return false;
16 foreach ($this->tournament->getRunners() as $participant) {
17 $result = $participant->findResult($this);
18 if (!$result || !$result->has_finished) return false;
23 public function updatePlacement() {
25 foreach ($this->tournament->participants as $p) {
29 $result = $p->findResult($this);
31 $result->updatePlacement(null, null);
36 usort($runners, Participant::compareResult($this));
37 $mapped = array_map(function ($p) {
38 return ['participant' => $p, 'result' => $p->findResult($this)];
40 $filtered = array_filter($mapped, function($r) {
41 return $r['result'] && ($r['result']->time || $r['result']->forfeit);
43 $reversed = array_reverse($filtered);
48 foreach ($reversed as $r) {
49 $betterThanLast = is_null($lastResult) || $r['result']->time < $lastResult;
50 if (!$r['result']->forfeit && $betterThanLast) {
52 $lastResult = $r['result']->time;
57 if (!$r['result']->forfeit) {
58 $r['result']->updatePlacement($running, count($filtered) - $running + 1);
60 $r['result']->updatePlacement(0, count($filtered));
66 public function hideResults() {
67 foreach ($this->results as $result) {
68 $result->makeHidden(['forfeit', 'placement', 'score', 'time']);
73 public function results() {
74 return $this->hasMany(Result::class);
77 public function rolled_by_user() {
78 return $this->belongsTo(User::class, 'rolled_by');
81 public function tournament() {
82 return $this->belongsTo(Tournament::class);
88 'locked' => 'boolean',
89 'no_record' => 'boolean',
92 protected $fillable = [