X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FParticipant.php;h=91b946d887fe726b2e066faf77d40991b8682485;hb=d32516335ea2534e15256c948e9c38d3de40794b;hp=8aac88ce54a04877ecefe43c6f67950ec0d09c63;hpb=2b3d5b3d98705cda41a7218d7133234b227e87b6;p=alttp.git diff --git a/app/Models/Participant.php b/app/Models/Participant.php index 8aac88c..91b946d 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,79 @@ 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 tournament() { return $this->belongsTo(Tournament::class); } @@ -17,6 +91,7 @@ class Participant extends Model return $this->belongsTo(User::class); } + protected $casts = [ 'roles' => 'array', ];