]> git.localhorst.tv Git - alttp.git/blob - app/Models/User.php
fb21113b39497f7f9a68f4fd81d837d755386cb3
[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 isApplicant(Tournament $tournament) {
48                 foreach ($tournament->applications as $applicant) {
49                         if ($applicant->user_id == $this->id) {
50                                 return true;
51                         }
52                 }
53                 return false;
54         }
55
56         public function isDeniedApplicant(Tournament $tournament) {
57                 foreach ($tournament->applications as $applicant) {
58                         if ($applicant->user_id == $this->id) {
59                                 return $applicant->denied;
60                         }
61                 }
62                 return false;
63         }
64
65         public function isParticipant(Tournament $tournament) {
66                 foreach ($tournament->participants as $participant) {
67                         if ($participant->user_id == $this->id) {
68                                 return true;
69                         }
70                 }
71                 return false;
72         }
73
74         public function isRunner(Tournament $tournament) {
75                 foreach ($tournament->participants as $participant) {
76                         if ($participant->user_id == $this->id) {
77                                 return in_array('runner', $participant->roles);
78                         }
79                 }
80                 return false;
81         }
82
83         public function isTournamentAdmin(Tournament $tournament) {
84                 foreach ($tournament->participants as $participant) {
85                         if ($participant->user_id == $this->id) {
86                                 return in_array('admin', $participant->roles);
87                         }
88                 }
89                 return false;
90         }
91
92         public function isTournamentCrew(Tournament $tournament) {
93                 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
94         }
95
96         public function isTournamentMonitor(Tournament $tournament) {
97                 foreach ($tournament->participants as $participant) {
98                         if ($participant->user_id == $this->id) {
99                                 return in_array('monitor', $participant->roles);
100                         }
101                 }
102                 return false;
103         }
104
105         public function hasFinished(Round $round) {
106                 foreach ($round->results as $result) {
107                         if ($result->user_id != $this->id) continue;
108                         return $result->has_finished;
109                 }
110                 return false;
111         }
112
113
114         public function participation() {
115                 return $this->hasMany(Participant::class);
116         }
117
118         public function results() {
119                 return $this->hasMany(Result::class);
120         }
121
122         public function rounds() {
123                 return $this->belongsToMany(Round::class, 'results');
124         }
125
126         public function tournaments() {
127                 return $this->belongsToMany(Tournament::class, 'participants');
128         }
129
130
131         public function round_first() {
132                 return $this->rounds()
133                         ->where('locked', true)
134                         ->where('no_record', false)
135                         ->wherePivot('forfeit', false)
136                         ->wherePivot('placement', 1);
137         }
138
139         public function round_second() {
140                 return $this->rounds()
141                         ->where('locked', true)
142                         ->where('no_record', false)
143                         ->wherePivot('forfeit', false)
144                         ->wherePivot('placement', 2);
145         }
146
147         public function round_third() {
148                 return $this->rounds()
149                         ->where('locked', true)
150                         ->where('no_record', false)
151                         ->wherePivot('forfeit', false)
152                         ->wherePivot('placement', 3);
153         }
154
155         public function tournament_first() {
156                 return $this->tournaments()
157                         ->where('locked', true)
158                         ->where('no_record', false)
159                         ->wherePivot('placement', 1);
160         }
161
162         public function tournament_second() {
163                 return $this->tournaments()
164                         ->where('locked', true)
165                         ->where('no_record', false)
166                         ->wherePivot('placement', 2);
167         }
168
169         public function tournament_third() {
170                 return $this->tournaments()
171                         ->where('locked', true)
172                         ->where('no_record', false)
173                         ->wherePivot('placement', 3);
174         }
175
176
177         /**
178          * The attributes that are mass assignable.
179          *
180          * @var string[]
181          */
182         protected $fillable = [
183                 'id',
184                 'username',
185                 'discriminator',
186                 'email',
187                 'avatar',
188                 'verified',
189                 'locale',
190                 'mfa_enabled',
191                 'refresh_token',
192                 'role',
193         ];
194
195         /**
196          * The attributes that should be hidden for serialization.
197          *
198          * @var array
199          */
200         protected $hidden = [
201                 'email',
202                 'mfa_enabled',
203                 'refresh_token',
204                 'remember_token',
205         ];
206
207         /**
208          * The attributes that should be cast.
209          *
210          * @var array
211          */
212         protected $casts = [
213                 'id' => 'string',
214                 'username' => 'string',
215                 'discriminator' => 'string',
216                 'email' => 'string',
217                 'avatar' => 'string',
218                 'avatar_cached' => 'datetime',
219                 'verified' => 'boolean',
220                 'locale' => 'string',
221                 'mfa_enabled' => 'boolean',
222                 'refresh_token' => 'encrypted',
223         ];
224 }