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