X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FTournament.php;h=96e89b8939358aea1b88acfdfa8f1c89270e86aa;hb=07a88747f8a252b41b739185fcb68bdee3a60f9a;hp=217622bb9681c950d6b4113b38464ef6bf4ed167;hpb=a90987ef670ed4c4de7d4191821e6c863342577d;p=alttp.git diff --git a/app/Models/Tournament.php b/app/Models/Tournament.php index 217622b..96e89b8 100644 --- a/app/Models/Tournament.php +++ b/app/Models/Tournament.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\ParticipantChanged; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -9,6 +10,65 @@ class Tournament extends Model { use HasFactory; + + public function getRunners() { + $runners = []; + foreach ($this->participants as $participant) { + if (in_array('runner', $participant->roles)) { + $runners[] = $participant; + } + } + return $runners; + } + + public function hasScoreboard() { + return $this->type == 'signup-async'; + } + + public function updatePlacement() { + $runners = []; + foreach ($this->participants as $p) { + if ($p->isRunner()) { + $p->score = 0; + $runners[] = $p; + } else { + $p->updatePlacement(null, null); + } + } + if (empty($runners)) { + return; + } + foreach ($this->rounds as $round) { + foreach ($runners as $p) { + $result = $p->findResult($round); + if ($result) { + $p->score += $result->score; + } + } + } + + usort($runners, [Participant::class, 'compareScore']); + $reversed = array_reverse($runners); + $placement = count($runners); + $skipped = 0; + $lastScore = $runners[0]->score; + foreach ($runners as $p) { + if ($p->score > $lastScore) { + $placement -= $skipped; + $skipped = 1; + $lastScore = $p->score; + } else { + ++$skipped; + } + $p->updatePlacement($p->score, $placement); + } + } + + + public function applications() { + return $this->hasMany(Application::class); + } + public function participants() { return $this->hasMany(Participant::class); } @@ -21,4 +81,12 @@ class Tournament extends Model return $this->hasMany(Round::class)->orderBy('number', 'DESC'); } + + protected $casts = [ + 'accept_applications' => 'boolean', + 'locked' => 'boolean', + 'no_record' => 'boolean', + 'show_numbers' => 'boolean', + ]; + }