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 isPrivileged() {
48 return $this->role === 'special' || $this->isAdmin();
51 public function isApplicant(Tournament $tournament) {
52 foreach ($tournament->applications as $applicant) {
53 if ($applicant->user_id == $this->id) {
60 public function isDeniedApplicant(Tournament $tournament) {
61 foreach ($tournament->applications as $applicant) {
62 if ($applicant->user_id == $this->id) {
63 return $applicant->denied;
69 public function isParticipant(Tournament $tournament) {
70 foreach ($tournament->participants as $participant) {
71 if ($participant->user_id == $this->id) {
78 public function isRunner(Tournament $tournament) {
79 foreach ($tournament->participants as $participant) {
80 if ($participant->user_id == $this->id) {
81 return in_array('runner', $participant->roles);
87 public function isTournamentAdmin(Tournament $tournament) {
88 foreach ($tournament->participants as $participant) {
89 if ($participant->user_id == $this->id) {
90 return in_array('admin', $participant->roles);
96 public function isTournamentCrew(Tournament $tournament) {
97 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
100 public function isTournamentMonitor(Tournament $tournament) {
101 foreach ($tournament->participants as $participant) {
102 if ($participant->user_id == $this->id) {
103 return in_array('monitor', $participant->roles);
109 public function hasFinished(Round $round) {
110 foreach ($round->results as $result) {
111 if ($result->user_id != $this->id) continue;
112 return $result->has_finished;
118 public function channel_crews() {
119 return $this->hasMany(ChannelCrew::class);
122 public function participation() {
123 return $this->hasMany(Participant::class);
126 public function results() {
127 return $this->hasMany(Result::class);
130 public function rounds() {
131 return $this->belongsToMany(Round::class, 'results');
134 public function tournaments() {
135 return $this->belongsToMany(Tournament::class, 'participants');
139 public function round_first() {
140 return $this->rounds()
141 ->where('locked', true)
142 ->where('no_record', false)
143 ->wherePivot('forfeit', false)
144 ->wherePivot('placement', 1);
147 public function round_second() {
148 return $this->rounds()
149 ->where('locked', true)
150 ->where('no_record', false)
151 ->wherePivot('forfeit', false)
152 ->wherePivot('placement', 2);
155 public function round_third() {
156 return $this->rounds()
157 ->where('locked', true)
158 ->where('no_record', false)
159 ->wherePivot('forfeit', false)
160 ->wherePivot('placement', 3);
163 public function tournament_first() {
164 return $this->tournaments()
165 ->where('locked', true)
166 ->where('no_record', false)
167 ->wherePivot('placement', 1);
170 public function tournament_second() {
171 return $this->tournaments()
172 ->where('locked', true)
173 ->where('no_record', false)
174 ->wherePivot('placement', 2);
177 public function tournament_third() {
178 return $this->tournaments()
179 ->where('locked', true)
180 ->where('no_record', false)
181 ->wherePivot('placement', 3);
186 * The attributes that are mass assignable.
190 protected $fillable = [
204 * The attributes that should be hidden for serialization.
208 protected $hidden = [
216 * The attributes that should be cast.
222 'username' => 'string',
223 'discriminator' => 'string',
225 'avatar' => 'string',
226 'avatar_cached' => 'datetime',
227 'verified' => 'boolean',
228 'locale' => 'string',
229 'mfa_enabled' => 'boolean',
230 'refresh_token' => 'encrypted',