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