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;
11 class User extends Authenticatable
13 use HasApiTokens, HasFactory, Notifiable;
15 public function findResult(Round $round) {
16 foreach ($round->results as $result) {
17 if ($this->id == $result->user_id) {
24 public function getName() {
25 if (!empty($this->nickname)) {
26 return $this->nickname;
28 return $this->username;
32 public function isAdmin() {
33 return $this->role === 'admin';
36 public function isApplicant(Tournament $tournament) {
37 foreach ($tournament->applications as $applicant) {
38 if ($applicant->user_id == $this->id) {
45 public function isDeniedApplicant(Tournament $tournament) {
46 foreach ($tournament->applications as $applicant) {
47 if ($applicant->user_id == $this->id) {
48 return $applicant->denied;
54 public function isParticipant(Tournament $tournament) {
55 foreach ($tournament->participants as $participant) {
56 if ($participant->user_id == $this->id) {
63 public function isRunner(Tournament $tournament) {
64 foreach ($tournament->participants as $participant) {
65 if ($participant->user_id == $this->id) {
66 return in_array('runner', $participant->roles);
72 public function isTournamentAdmin(Tournament $tournament) {
73 foreach ($tournament->participants as $participant) {
74 if ($participant->user_id == $this->id) {
75 return in_array('admin', $participant->roles);
81 public function isTournamentCrew(Tournament $tournament) {
82 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
85 public function isTournamentMonitor(Tournament $tournament) {
86 foreach ($tournament->participants as $participant) {
87 if ($participant->user_id == $this->id) {
88 return in_array('monitor', $participant->roles);
94 public function hasFinished(Round $round) {
95 foreach ($round->results as $result) {
96 if ($result->user_id != $this->id) continue;
97 return $result->has_finished;
103 public function participation() {
104 return $this->hasMany(Participant::class);
107 public function results() {
108 return $this->hasMany(Result::class);
111 public function rounds() {
112 return $this->belongsToMany(Round::class, 'results');
115 public function tournaments() {
116 return $this->belongsToMany(Tournament::class, 'participants');
120 public function round_first() {
121 return $this->rounds()
122 ->where('locked', true)
123 ->where('no_record', false)
124 ->wherePivot('forfeit', false)
125 ->wherePivot('placement', 1);
128 public function round_second() {
129 return $this->rounds()
130 ->where('locked', true)
131 ->where('no_record', false)
132 ->wherePivot('forfeit', false)
133 ->wherePivot('placement', 2);
136 public function round_third() {
137 return $this->rounds()
138 ->where('locked', true)
139 ->where('no_record', false)
140 ->wherePivot('forfeit', false)
141 ->wherePivot('placement', 3);
144 public function tournament_first() {
145 return $this->tournaments()
146 ->where('locked', true)
147 ->where('no_record', false)
148 ->wherePivot('placement', 1);
151 public function tournament_second() {
152 return $this->tournaments()
153 ->where('locked', true)
154 ->where('no_record', false)
155 ->wherePivot('placement', 2);
158 public function tournament_third() {
159 return $this->tournaments()
160 ->where('locked', true)
161 ->where('no_record', false)
162 ->wherePivot('placement', 3);
167 * The attributes that are mass assignable.
171 protected $fillable = [
185 * The attributes that should be hidden for serialization.
189 protected $hidden = [
197 * The attributes that should be cast.
203 'username' => 'string',
204 'discriminator' => 'string',
206 'avatar' => 'string',
207 'verified' => 'boolean',
208 'locale' => 'string',
209 'mfa_enabled' => 'boolean',
210 'refresh_token' => 'encrypted',