5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Foundation\Auth\User as Authenticatable;
8 use Illuminate\Notifications\Notifiable;
9 use Laravel\Sanctum\HasApiTokens;
12 class User extends Authenticatable
14 use HasApiTokens, HasFactory, Notifiable;
16 public function findResult(Round $round) {
17 foreach ($round->results as $result) {
18 if ($this->id == $result->user_id) {
25 public function getName() {
26 if (!empty($this->nickname)) {
27 return $this->nickname;
29 return $this->username;
32 public function getRandomQuoteAttribute() {
33 return $this->results()
34 ->where('comment', '!=', '')
35 ->whereHas('round', function(Builder $query) {
36 $query->where('locked', true);
43 public function isAdmin() {
44 return $this->role === 'admin';
47 public function isApplicant(Tournament $tournament) {
48 foreach ($tournament->applications as $applicant) {
49 if ($applicant->user_id == $this->id) {
56 public function isDeniedApplicant(Tournament $tournament) {
57 foreach ($tournament->applications as $applicant) {
58 if ($applicant->user_id == $this->id) {
59 return $applicant->denied;
65 public function isParticipant(Tournament $tournament) {
66 foreach ($tournament->participants as $participant) {
67 if ($participant->user_id == $this->id) {
74 public function isRunner(Tournament $tournament) {
75 foreach ($tournament->participants as $participant) {
76 if ($participant->user_id == $this->id) {
77 return in_array('runner', $participant->roles);
83 public function isTournamentAdmin(Tournament $tournament) {
84 foreach ($tournament->participants as $participant) {
85 if ($participant->user_id == $this->id) {
86 return in_array('admin', $participant->roles);
92 public function isTournamentCrew(Tournament $tournament) {
93 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
96 public function isTournamentMonitor(Tournament $tournament) {
97 foreach ($tournament->participants as $participant) {
98 if ($participant->user_id == $this->id) {
99 return in_array('monitor', $participant->roles);
105 public function hasFinished(Round $round) {
106 foreach ($round->results as $result) {
107 if ($result->user_id != $this->id) continue;
108 return $result->has_finished;
114 public function participation() {
115 return $this->hasMany(Participant::class);
118 public function results() {
119 return $this->hasMany(Result::class);
122 public function rounds() {
123 return $this->belongsToMany(Round::class, 'results');
126 public function tournaments() {
127 return $this->belongsToMany(Tournament::class, 'participants');
131 public function round_first() {
132 return $this->rounds()
133 ->where('locked', true)
134 ->where('no_record', false)
135 ->wherePivot('forfeit', false)
136 ->wherePivot('placement', 1);
139 public function round_second() {
140 return $this->rounds()
141 ->where('locked', true)
142 ->where('no_record', false)
143 ->wherePivot('forfeit', false)
144 ->wherePivot('placement', 2);
147 public function round_third() {
148 return $this->rounds()
149 ->where('locked', true)
150 ->where('no_record', false)
151 ->wherePivot('forfeit', false)
152 ->wherePivot('placement', 3);
155 public function tournament_first() {
156 return $this->tournaments()
157 ->where('locked', true)
158 ->where('no_record', false)
159 ->wherePivot('placement', 1);
162 public function tournament_second() {
163 return $this->tournaments()
164 ->where('locked', true)
165 ->where('no_record', false)
166 ->wherePivot('placement', 2);
169 public function tournament_third() {
170 return $this->tournaments()
171 ->where('locked', true)
172 ->where('no_record', false)
173 ->wherePivot('placement', 3);
178 * The attributes that are mass assignable.
182 protected $fillable = [
196 * The attributes that should be hidden for serialization.
200 protected $hidden = [
208 * The attributes that should be cast.
214 'username' => 'string',
215 'discriminator' => 'string',
217 'avatar' => 'string',
218 'verified' => 'boolean',
219 'locale' => 'string',
220 'mfa_enabled' => 'boolean',
221 'refresh_token' => 'encrypted',