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