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() {
24 if ($this->tournament->type == 'open-async') {
25 $results = $this->results->sort([Result::class, 'compareResult']);
26 $reversed = $results->reverse();
31 foreach ($reversed as $result) {
32 $betterThanLast = is_null($lastResult) || $result->time < $lastResult;
33 if (!$result->forfeit && $betterThanLast) {
35 $lastResult = $result->time;
40 if (!$result->forfeit) {
41 $result->updatePlacement($running, count($results) - $running + 1);
43 $result->updatePlacement(0, count($results));
48 foreach ($this->tournament->participants as $p) {
52 $result = $p->findResult($this);
54 $result->updatePlacement(null, null);
59 usort($runners, Participant::compareResult($this));
60 $mapped = array_map(function ($p) {
61 return ['participant' => $p, 'result' => $p->findResult($this)];
63 $filtered = array_filter($mapped, function($r) {
64 return $r['result'] && ($r['result']->time || $r['result']->forfeit);
66 $reversed = array_reverse($filtered);
71 foreach ($reversed as $r) {
72 $betterThanLast = is_null($lastResult) || $r['result']->time < $lastResult;
73 if (!$r['result']->forfeit && $betterThanLast) {
75 $lastResult = $r['result']->time;
80 if (!$r['result']->forfeit) {
81 $r['result']->updatePlacement($running, count($filtered) - $running + 1);
83 $r['result']->updatePlacement(0, count($filtered));
90 public function hideResults() {
91 foreach ($this->results as $result) {
92 $result->makeHidden(['forfeit', 'placement', 'score', 'time']);
97 public function results() {
98 return $this->hasMany(Result::class);
101 public function rolled_by_user() {
102 return $this->belongsTo(User::class, 'rolled_by');
105 public function tournament() {
106 return $this->belongsTo(Tournament::class);
112 'locked' => 'boolean',
113 'no_record' => 'boolean',
114 'rolled_by' => 'string',
117 protected $fillable = [