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