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