]> git.localhorst.tv Git - alttp.git/blob - app/Models/Tournament.php
adlib chat
[alttp.git] / app / Models / Tournament.php
1 <?php
2
3 namespace App\Models;
4
5 use App\Events\ParticipantChanged;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8
9 class Tournament extends Model
10 {
11         use HasFactory;
12
13
14         public function getRunners() {
15                 $runners = [];
16                 foreach ($this->participants as $participant) {
17                         if (in_array('runner', $participant->roles)) {
18                                 $runners[] = $participant;
19                         }
20                 }
21                 return $runners;
22         }
23
24         public function hasScoreboard() {
25                 return $this->type == 'signup-async';
26         }
27
28         public function updatePlacement() {
29                 $runners = [];
30                 foreach ($this->participants as $p) {
31                         if ($p->isRunner()) {
32                                 $p->score = 0;
33                                 $runners[] = $p;
34                         } else {
35                                 $p->updatePlacement(null, null);
36                         }
37                 }
38                 if (empty($runners)) {
39                         return;
40                 }
41                 foreach ($this->rounds as $round) {
42                         foreach ($runners as $p) {
43                                 $result = $p->findResult($round);
44                                 if ($result) {
45                                         $p->score += $result->score;
46                                 }
47                         }
48                 }
49
50                 usort($runners, [Participant::class, 'compareScore']);
51                 $reversed = array_reverse($runners);
52                 $placement = count($runners);
53                 $skipped = 0;
54                 $lastScore = $runners[0]->score;
55                 foreach ($runners as $p) {
56                         if ($p->score > $lastScore) {
57                                 $placement -= $skipped;
58                                 $skipped = 1;
59                                 $lastScore = $p->score;
60                         } else {
61                                 ++$skipped;
62                         }
63                         $p->updatePlacement($p->score, $placement);
64                 }
65         }
66
67
68         public function applications() {
69                 return $this->hasMany(Application::class);
70         }
71
72         public function participants() {
73                 return $this->hasMany(Participant::class);
74         }
75
76         public function protocols() {
77                 return $this->hasMany(Protocol::class)->orderBy('created_at', 'DESC');
78         }
79
80         public function rounds() {
81                 return $this->hasMany(Round::class)->orderBy('number', 'DESC');
82         }
83
84
85         protected $casts = [
86                 'accept_applications' => 'boolean',
87                 'locked' => 'boolean',
88                 'no_record' => 'boolean',
89                 'show_numbers' => 'boolean',
90         ];
91
92 }