]> git.localhorst.tv Git - alttp.git/blob - app/Models/User.php
discord discriminator stuff round 2
[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 channel_crews() {
119                 return $this->hasMany(ChannelCrew::class);
120         }
121
122         public function participation() {
123                 return $this->hasMany(Participant::class);
124         }
125
126         public function results() {
127                 return $this->hasMany(Result::class);
128         }
129
130         public function rounds() {
131                 return $this->belongsToMany(Round::class, 'results');
132         }
133
134         public function tournaments() {
135                 return $this->belongsToMany(Tournament::class, 'participants');
136         }
137
138
139         public function round_first() {
140                 return $this->rounds()
141                         ->where('locked', true)
142                         ->where('no_record', false)
143                         ->wherePivot('forfeit', false)
144                         ->wherePivot('placement', 1);
145         }
146
147         public function round_second() {
148                 return $this->rounds()
149                         ->where('locked', true)
150                         ->where('no_record', false)
151                         ->wherePivot('forfeit', false)
152                         ->wherePivot('placement', 2);
153         }
154
155         public function round_third() {
156                 return $this->rounds()
157                         ->where('locked', true)
158                         ->where('no_record', false)
159                         ->wherePivot('forfeit', false)
160                         ->wherePivot('placement', 3);
161         }
162
163         public function tournament_first() {
164                 return $this->tournaments()
165                         ->where('locked', true)
166                         ->where('no_record', false)
167                         ->wherePivot('placement', 1);
168         }
169
170         public function tournament_second() {
171                 return $this->tournaments()
172                         ->where('locked', true)
173                         ->where('no_record', false)
174                         ->wherePivot('placement', 2);
175         }
176
177         public function tournament_third() {
178                 return $this->tournaments()
179                         ->where('locked', true)
180                         ->where('no_record', false)
181                         ->wherePivot('placement', 3);
182         }
183
184
185         /**
186          * The attributes that are mass assignable.
187          *
188          * @var string[]
189          */
190         protected $fillable = [
191                 'id',
192                 'username',
193                 'discord_nickname',
194                 'discriminator',
195                 'email',
196                 'avatar',
197                 'verified',
198                 'locale',
199                 'mfa_enabled',
200                 'refresh_token',
201                 'role',
202         ];
203
204         /**
205          * The attributes that should be hidden for serialization.
206          *
207          * @var array
208          */
209         protected $hidden = [
210                 'email',
211                 'mfa_enabled',
212                 'refresh_token',
213                 'remember_token',
214         ];
215
216         /**
217          * The attributes that should be cast.
218          *
219          * @var array
220          */
221         protected $casts = [
222                 'id' => 'string',
223                 'username' => 'string',
224                 'discriminator' => 'string',
225                 'email' => 'string',
226                 'avatar' => 'string',
227                 'avatar_cached' => 'datetime',
228                 'verified' => 'boolean',
229                 'locale' => 'string',
230                 'mfa_enabled' => 'boolean',
231                 'refresh_token' => 'encrypted',
232         ];
233 }