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