X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FParticipant.php;h=6e8abbe2ea40d5b99ea5c1d91723e68417f28ef1;hb=abdc2ea1ade1fb12ceacc28660890750e69ae36f;hp=36ab3cf305e18ff1d2992ae106b6bb4117c3810b;hpb=55f2d7cd6c290a0d26db177d54d20c393f890bbb;p=alttp.git diff --git a/app/Models/Participant.php b/app/Models/Participant.php index 36ab3cf..6e8abbe 100644 --- a/app/Models/Participant.php +++ b/app/Models/Participant.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\ParticipantChanged; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -9,6 +10,91 @@ class Participant extends Model { use HasFactory; + + public static function compareResult(Round $round) { + return function (Participant $a, Participant $b) use ($round) { + $a_result = $a->findResult($round); + $b_result = $b->findResult($round); + $a_time = $a_result && !$a_result->forfeit ? $a_result->time : 0; + $b_time = $b_result && !$b_result->forfeit ? $b_result->time : 0; + if ($a_time) { + if ($b_time) { + if ($a_time < $b_time) return -1; + if ($b_time < $a_time) return 1; + return static::compareUsername($a, $b); + } + return -1; + } + if ($b_time) { + return 1; + } + $a_forfeit = $a_result ? $a_result->forfeit : false; + $b_forfeit = $b_result ? $b_result->forfeit : false; + if ($a_forfeit) { + if ($b_forfeit) { + return static::compareUsername($a, $b); + } + return -1; + } + if ($b_forfeit) { + return 1; + } + return static::compareUsername($a, $b); + }; + } + + public static function compareScore(Participant $a, Participant $b) { + $a_score = $a->isRunner() ? ($a->score ? $a->score : 0) : -1; + $b_score = $b->isRunner() ? ($b->score ? $b->score : 0) : -1; + if ($a_score < $b_score) return -1; + if ($b_score < $a_score) return 1; + return static::compareUsername($a, $b); + } + + public static function compareUsername(Participant $a, Participant $b) { + return strcasecmp($a->user->username, $b->user->username); + } + + + public function updatePlacement($score, $placement) { + $this->score = $score; + $this->placement = $placement; + $this->save(); + if ($this->wasChanged()) { + ParticipantChanged::dispatch($this); + } + } + + public function findResult(Round $round) { + foreach ($round->results as $result) { + if ($this->user_id == $result->user_id) { + return $result; + } + } + return null; + } + + public function isRunner() { + return in_array('runner', $this->roles); + } + + public function isTournamentAdmin() { + return in_array('admin', $this->roles); + } + + public function makeRunner() { + if (!is_array($this->roles)) { + $this->roles = ['runner']; + } else if (!in_array('runner', $this->roles)) { + $newRoles = array_values($this->roles); + $newRoles[] = 'runner'; + $this->roles = $newRoles; + } + $this->save(); + ParticipantChanged::dispatch($this); + } + + public function tournament() { return $this->belongsTo(Tournament::class); } @@ -17,4 +103,19 @@ class Participant extends Model return $this->belongsTo(User::class); } + + protected $casts = [ + 'roles' => 'array', + 'user_id' => 'string', + ]; + + protected $fillable = [ + 'tournament_id', + 'user_id', + ]; + + protected $with = [ + 'user', + ]; + }