X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FResult.php;h=c672a6a780856b1222b5e4e5ac6f7cb7ef93aa6b;hb=537b998e8059c56e3a20ee2a89d42c3bbfbb80b8;hp=99f3791eacfc951acabb8d69d22053f592adbc80;hpb=3e970c52e6a46cf9db4f69c5bec9102da53c0744;p=alttp.git diff --git a/app/Models/Result.php b/app/Models/Result.php index 99f3791..c672a6a 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -11,6 +11,37 @@ class Result extends Model use HasFactory; + public static function compareResult(Result $a, Result $b) { + $a_time = !$a->forfeit ? $a->time : 0; + $b_time = !$b->forfeit ? $b->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; + } + 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 compareUsername(Participant $a, Participant $b) { + return strcasecmp($a->user->username, $b->user->username); + } + + public function formatTime() { $hours = floor($this->time / 60 / 60); $minutes = floor(($this->time / 60) % 60); @@ -45,6 +76,10 @@ class Result extends Model return $this->belongsTo(Participant::class); } + public function user() { + return $this->belongsTo(User::class); + } + public function getHasFinishedAttribute() { return $this->time > 0 || $this->forfeit; }