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 hasGlobalRole($name) {
44 return !empty($this->global_roles) && in_array($name, $this->global_roles);
47 public function isAdmin() {
48 return $this->role === 'admin';
51 public function isPrivileged() {
52 return $this->role === 'special' || $this->isAdmin();
55 public function isApplicant(Tournament $tournament) {
56 foreach ($tournament->applications as $applicant) {
57 if ($applicant->user_id == $this->id) {
64 public function isDeniedApplicant(Tournament $tournament) {
65 foreach ($tournament->applications as $applicant) {
66 if ($applicant->user_id == $this->id) {
67 return $applicant->denied;
73 public function isParticipant(Tournament $tournament) {
74 foreach ($tournament->participants as $participant) {
75 if ($participant->user_id == $this->id) {
82 public function isRunner(Tournament $tournament) {
83 foreach ($tournament->participants as $participant) {
84 if ($participant->user_id == $this->id) {
85 return in_array('runner', $participant->roles);
91 public function isTournamentAdmin(Tournament $tournament) {
92 foreach ($tournament->participants as $participant) {
93 if ($participant->user_id == $this->id) {
94 return in_array('admin', $participant->roles);
100 public function isTournamentCrew(Tournament $tournament) {
101 return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
104 public function isTournamentMonitor(Tournament $tournament) {
105 foreach ($tournament->participants as $participant) {
106 if ($participant->user_id == $this->id) {
107 return in_array('monitor', $participant->roles);
113 public function hasFinished(Round $round) {
114 foreach ($round->results as $result) {
115 if ($result->user_id != $this->id) continue;
116 return $result->has_finished;
122 public function channel_crews() {
123 return $this->hasMany(ChannelCrew::class);
126 public function participation() {
127 return $this->hasMany(Participant::class);
130 public function results() {
131 return $this->hasMany(Result::class);
134 public function rounds() {
135 return $this->belongsToMany(Round::class, 'results');
138 public function tournaments() {
139 return $this->belongsToMany(Tournament::class, 'participants');
143 public function round_first() {
144 return $this->rounds()
145 ->where('locked', true)
146 ->where('no_record', false)
147 ->wherePivot('forfeit', false)
148 ->wherePivot('placement', 1);
151 public function round_second() {
152 return $this->rounds()
153 ->where('locked', true)
154 ->where('no_record', false)
155 ->wherePivot('forfeit', false)
156 ->wherePivot('placement', 2);
159 public function round_third() {
160 return $this->rounds()
161 ->where('locked', true)
162 ->where('no_record', false)
163 ->wherePivot('forfeit', false)
164 ->wherePivot('placement', 3);
167 public function tournament_first() {
168 return $this->tournaments()
169 ->where('locked', true)
170 ->where('no_record', false)
171 ->wherePivot('placement', 1);
174 public function tournament_second() {
175 return $this->tournaments()
176 ->where('locked', true)
177 ->where('no_record', false)
178 ->wherePivot('placement', 2);
181 public function tournament_third() {
182 return $this->tournaments()
183 ->where('locked', true)
184 ->where('no_record', false)
185 ->wherePivot('placement', 3);
190 * The attributes that are mass assignable.
194 protected $fillable = [
209 * The attributes that should be hidden for serialization.
213 protected $hidden = [
221 * The attributes that should be cast.
227 'username' => 'string',
228 'discriminator' => 'string',
230 'avatar' => 'string',
231 'avatar_cached' => 'datetime',
232 'verified' => 'boolean',
233 'global_roles' => 'array',
234 'locale' => 'string',
235 'mfa_enabled' => 'boolean',
236 'refresh_token' => 'encrypted',