namespace App\Models;
+use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
- use HasApiTokens, HasFactory, Notifiable;
-
- /**
- * The attributes that are mass assignable.
- *
- * @var string[]
- */
- protected $fillable = [
- 'id',
- 'username',
- 'discriminator',
- 'email',
- 'avatar',
- 'verified',
- 'locale',
- 'mfa_enabled',
- 'refresh_token'
- ];
-
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var array
- */
- protected $hidden = [
- 'refresh_token',
- 'remember_token',
- ];
-
- /**
- * The attributes that should be cast.
- *
- * @var array
- */
- protected $casts = [
- 'id' => 'string',
- 'username' => 'string',
- 'discriminator' => 'string',
- 'email' => 'string',
- 'avatar' => 'string',
- 'verified' => 'boolean',
- 'locale' => 'string',
- 'mfa_enabled' => 'boolean',
- 'refresh_token' => 'encrypted',
- ];
+ use HasApiTokens, HasFactory, Notifiable;
+
+ public function findResult(Round $round) {
+ foreach ($round->results as $result) {
+ if ($this->id == $result->user_id) {
+ return $result;
+ }
+ }
+ return null;
+ }
+
+ public function getName() {
+ if (!empty($this->nickname)) {
+ return $this->nickname;
+ }
+ return $this->username;
+ }
+
+ public function getRandomQuoteAttribute() {
+ return $this->results()
+ ->where('comment', '!=', '')
+ ->whereHas('round', function(Builder $query) {
+ $query->where('locked', true);
+ })
+ ->inRandomOrder()
+ ->first();
+ }
+
+
+ public function hasGlobalRole($name) {
+ return !empty($this->global_roles) && in_array($name, $this->global_roles);
+ }
+
+ public function isAdmin() {
+ return $this->role === 'admin';
+ }
+
+ public function isPrivileged() {
+ return $this->role === 'special' || $this->isAdmin();
+ }
+
+ public function isApplicant(Tournament $tournament) {
+ foreach ($tournament->applications as $applicant) {
+ if ($applicant->user_id == $this->id) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public function isDeniedApplicant(Tournament $tournament) {
+ foreach ($tournament->applications as $applicant) {
+ if ($applicant->user_id == $this->id) {
+ return $applicant->denied;
+ }
+ }
+ return false;
+ }
+
+ public function isParticipant(Tournament $tournament) {
+ foreach ($tournament->participants as $participant) {
+ if ($participant->user_id == $this->id) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public function isRunner(Tournament $tournament) {
+ foreach ($tournament->participants as $participant) {
+ if ($participant->user_id == $this->id) {
+ return in_array('runner', $participant->roles);
+ }
+ }
+ return false;
+ }
+
+ public function isTournamentAdmin(Tournament $tournament) {
+ foreach ($tournament->participants as $participant) {
+ if ($participant->user_id == $this->id) {
+ return in_array('admin', $participant->roles);
+ }
+ }
+ return false;
+ }
+
+ public function isTournamentCrew(Tournament $tournament) {
+ return $this->isTournamentAdmin($tournament) || $this->isTournamentMonitor($tournament);
+ }
+
+ public function isTournamentMonitor(Tournament $tournament) {
+ foreach ($tournament->participants as $participant) {
+ if ($participant->user_id == $this->id) {
+ return in_array('monitor', $participant->roles);
+ }
+ }
+ return false;
+ }
+
+ public function hasFinished(Round $round) {
+ foreach ($round->results as $result) {
+ if ($result->user_id != $this->id) continue;
+ return $result->has_finished;
+ }
+ return false;
+ }
+
+
+ public function channel_crews() {
+ return $this->hasMany(ChannelCrew::class);
+ }
+
+ public function participation() {
+ return $this->hasMany(Participant::class);
+ }
+
+ public function results() {
+ return $this->hasMany(Result::class);
+ }
+
+ public function rounds() {
+ return $this->belongsToMany(Round::class, 'results');
+ }
+
+ public function tournaments() {
+ return $this->belongsToMany(Tournament::class, 'participants');
+ }
+
+
+ public function round_first() {
+ return $this->rounds()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('forfeit', false)
+ ->wherePivot('placement', 1);
+ }
+
+ public function round_second() {
+ return $this->rounds()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('forfeit', false)
+ ->wherePivot('placement', 2);
+ }
+
+ public function round_third() {
+ return $this->rounds()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('forfeit', false)
+ ->wherePivot('placement', 3);
+ }
+
+ public function tournament_first() {
+ return $this->tournaments()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('placement', 1);
+ }
+
+ public function tournament_second() {
+ return $this->tournaments()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('placement', 2);
+ }
+
+ public function tournament_third() {
+ return $this->tournaments()
+ ->where('locked', true)
+ ->where('no_record', false)
+ ->wherePivot('placement', 3);
+ }
+
+
+ /**
+ * The attributes that are mass assignable.
+ *
+ * @var string[]
+ */
+ protected $fillable = [
+ 'id',
+ 'username',
+ 'discord_nickname',
+ 'discriminator',
+ 'email',
+ 'avatar',
+ 'verified',
+ 'locale',
+ 'mfa_enabled',
+ 'refresh_token',
+ 'role',
+ ];
+
+ /**
+ * The attributes that should be hidden for serialization.
+ *
+ * @var array
+ */
+ protected $hidden = [
+ 'email',
+ 'mfa_enabled',
+ 'refresh_token',
+ 'remember_token',
+ ];
+
+ /**
+ * The attributes that should be cast.
+ *
+ * @var array
+ */
+ protected $casts = [
+ 'id' => 'string',
+ 'username' => 'string',
+ 'discriminator' => 'string',
+ 'email' => 'string',
+ 'avatar' => 'string',
+ 'avatar_cached' => 'datetime',
+ 'verified' => 'boolean',
+ 'global_roles' => 'array',
+ 'locale' => 'string',
+ 'mfa_enabled' => 'boolean',
+ 'refresh_token' => 'encrypted',
+ ];
}