]> git.localhorst.tv Git - alttp.git/blob - app/Models/User.php
164fb026bfecba420a784daf83b44697f37cd869
[alttp.git] / app / Models / User.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Foundation\Auth\User as Authenticatable;
7 use Illuminate\Notifications\Notifiable;
8 use Laravel\Sanctum\HasApiTokens;
9
10
11 class User extends Authenticatable
12 {
13         use HasApiTokens, HasFactory, Notifiable;
14
15         public function findResult(Round $round) {
16                 foreach ($round->results as $result) {
17                         if ($this->id == $result->user_id) {
18                                 return $result;
19                         }
20                 }
21                 return null;
22         }
23
24         public function getName() {
25                 if (!empty($this->nickname)) {
26                         return $this->nickname;
27                 }
28                 return $this->username;
29         }
30
31
32         public function isAdmin() {
33                 return $this->role === 'admin';
34         }
35
36         public function isApplicant(Tournament $tournament) {
37                 foreach ($tournament->applications as $applicant) {
38                         if ($applicant->user_id == $this->id) {
39                                 return true;
40                         }
41                 }
42                 return false;
43         }
44
45         public function isDeniedApplicant(Tournament $tournament) {
46                 foreach ($tournament->applications as $applicant) {
47                         if ($applicant->user_id == $this->id) {
48                                 return $applicant->denied;
49                         }
50                 }
51                 return false;
52         }
53
54         public function isParticipant(Tournament $tournament) {
55                 foreach ($tournament->participants as $participant) {
56                         if ($participant->user_id == $this->id) {
57                                 return true;
58                         }
59                 }
60                 return false;
61         }
62
63         public function isRunner(Tournament $tournament) {
64                 foreach ($tournament->participants as $participant) {
65                         if ($participant->user_id == $this->id) {
66                                 return in_array('runner', $participant->roles);
67                         }
68                 }
69                 return false;
70         }
71
72         public function isTournamentAdmin(Tournament $tournament) {
73                 foreach ($tournament->participants as $participant) {
74                         if ($participant->user_id == $this->id) {
75                                 return in_array('admin', $participant->roles);
76                         }
77                 }
78                 return false;
79         }
80
81         public function isTournamentCrew(Tournament $tournament) {
82                 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
83         }
84
85         public function isTournamentMonitor(Tournament $tournament) {
86                 foreach ($tournament->participants as $participant) {
87                         if ($participant->user_id == $this->id) {
88                                 return in_array('monitor', $participant->roles);
89                         }
90                 }
91                 return false;
92         }
93
94         public function hasFinished(Round $round) {
95                 foreach ($round->results as $result) {
96                         if ($result->user_id != $this->id) continue;
97                         return $result->has_finished;
98                 }
99                 return false;
100         }
101
102
103         public function participation() {
104                 return $this->hasMany(Participant::class);
105         }
106
107         public function results() {
108                 return $this->hasMany(Result::class);
109         }
110
111         public function rounds() {
112                 return $this->belongsToMany(Round::class, 'results');
113         }
114
115         public function tournaments() {
116                 return $this->belongsToMany(Tournament::class, 'participants');
117         }
118
119
120         public function round_first() {
121                 return $this->rounds()
122                         ->where('locked', true)
123                         ->where('no_record', false)
124                         ->wherePivot('forfeit', false)
125                         ->wherePivot('placement', 1);
126         }
127
128         public function round_second() {
129                 return $this->rounds()
130                         ->where('locked', true)
131                         ->where('no_record', false)
132                         ->wherePivot('forfeit', false)
133                         ->wherePivot('placement', 2);
134         }
135
136         public function round_third() {
137                 return $this->rounds()
138                         ->where('locked', true)
139                         ->where('no_record', false)
140                         ->wherePivot('forfeit', false)
141                         ->wherePivot('placement', 3);
142         }
143
144         public function tournament_first() {
145                 return $this->tournaments()
146                         ->where('locked', true)
147                         ->where('no_record', false)
148                         ->wherePivot('placement', 1);
149         }
150
151         public function tournament_second() {
152                 return $this->tournaments()
153                         ->where('locked', true)
154                         ->where('no_record', false)
155                         ->wherePivot('placement', 2);
156         }
157
158         public function tournament_third() {
159                 return $this->tournaments()
160                         ->where('locked', true)
161                         ->where('no_record', false)
162                         ->wherePivot('placement', 3);
163         }
164
165
166         /**
167          * The attributes that are mass assignable.
168          *
169          * @var string[]
170          */
171         protected $fillable = [
172                 'id',
173                 'username',
174                 'discriminator',
175                 'email',
176                 'avatar',
177                 'verified',
178                 'locale',
179                 'mfa_enabled',
180                 'refresh_token',
181                 'role',
182         ];
183
184         /**
185          * The attributes that should be hidden for serialization.
186          *
187          * @var array
188          */
189         protected $hidden = [
190                 'email',
191                 'mfa_enabled',
192                 'refresh_token',
193                 'remember_token',
194         ];
195
196         /**
197          * The attributes that should be cast.
198          *
199          * @var array
200          */
201         protected $casts = [
202                 'id' => 'string',
203                 'username' => 'string',
204                 'discriminator' => 'string',
205                 'email' => 'string',
206                 'avatar' => 'string',
207                 'verified' => 'boolean',
208                 'locale' => 'string',
209                 'mfa_enabled' => 'boolean',
210                 'refresh_token' => 'encrypted',
211         ];
212 }