5 use App\Events\ParticipantChanged;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
9 class Tournament extends Model
14 public function getRunners() {
16 foreach ($this->participants as $participant) {
17 if (in_array('runner', $participant->roles)) {
18 $runners[] = $participant;
24 public function updatePlacement() {
26 foreach ($this->participants as $p) {
31 $p->updatePlacement(null, null);
34 if (empty($runners)) {
37 foreach ($this->rounds as $round) {
38 foreach ($runners as $p) {
39 $result = $p->findResult($round);
41 $p->score += $result->score;
46 usort($runners, [Participant::class, 'compareScore']);
47 $reversed = array_reverse($runners);
48 $placement = count($runners);
50 $lastScore = $runners[0]->score;
51 foreach ($runners as $p) {
52 if ($p->score > $lastScore) {
53 $placement -= $skipped;
55 $lastScore = $p->score;
59 $p->updatePlacement($p->score, $placement);
64 public function participants() {
65 return $this->hasMany(Participant::class);
68 public function protocols() {
69 return $this->hasMany(Protocol::class)->orderBy('created_at', 'DESC');
72 public function rounds() {
73 return $this->hasMany(Round::class)->orderBy('number', 'DESC');