]> git.localhorst.tv Git - alttp.git/blob - app/Models/User.php
e5bb4f174b49b5f2a96446627ab0148421f7f725
[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
60         public function participation() {
61                 return $this->hasMany(Participant::class);
62         }
63
64         public function results() {
65                 return $this->hasMany(Result::class);
66         }
67
68         public function rounds() {
69                 return $this->belongsToMany(Round::class, 'results');
70         }
71
72         public function tournaments() {
73                 return $this->belongsToMany(Tournament::class, 'participants');
74         }
75
76
77         public function round_first() {
78                 return $this->rounds()
79                         ->where('locked', true)
80                         ->where('no_record', false)
81                         ->wherePivot('forfeit', false)
82                         ->wherePivot('placement', 1);
83         }
84
85         public function round_second() {
86                 return $this->rounds()
87                         ->where('locked', true)
88                         ->where('no_record', false)
89                         ->wherePivot('forfeit', false)
90                         ->wherePivot('placement', 2);
91         }
92
93         public function round_third() {
94                 return $this->rounds()
95                         ->where('locked', true)
96                         ->where('no_record', false)
97                         ->wherePivot('forfeit', false)
98                         ->wherePivot('placement', 3);
99         }
100
101         public function tournament_first() {
102                 return $this->tournaments()
103                         ->where('locked', true)
104                         ->where('no_record', false)
105                         ->wherePivot('placement', 1);
106         }
107
108         public function tournament_second() {
109                 return $this->tournaments()
110                         ->where('locked', true)
111                         ->where('no_record', false)
112                         ->wherePivot('placement', 2);
113         }
114
115         public function tournament_third() {
116                 return $this->tournaments()
117                         ->where('locked', true)
118                         ->where('no_record', false)
119                         ->wherePivot('placement', 3);
120         }
121
122
123         /**
124          * The attributes that are mass assignable.
125          *
126          * @var string[]
127          */
128         protected $fillable = [
129                 'id',
130                 'username',
131                 'discriminator',
132                 'email',
133                 'avatar',
134                 'verified',
135                 'locale',
136                 'mfa_enabled',
137                 'refresh_token',
138                 'role',
139         ];
140
141         /**
142          * The attributes that should be hidden for serialization.
143          *
144          * @var array
145          */
146         protected $hidden = [
147                 'email',
148                 'mfa_enabled',
149                 'refresh_token',
150                 'remember_token',
151         ];
152
153         /**
154          * The attributes that should be cast.
155          *
156          * @var array
157          */
158         protected $casts = [
159                 'id' => 'string',
160                 'username' => 'string',
161                 'discriminator' => 'string',
162                 'email' => 'string',
163                 'avatar' => 'string',
164                 'verified' => 'boolean',
165                 'locale' => 'string',
166                 'mfa_enabled' => 'boolean',
167                 'refresh_token' => 'encrypted',
168         ];
169 }