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