X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FResult.php;h=229ac4b9c8e418c5d52cf9de99c33e07122eaf26;hb=8645b77ea2dc402f0265e1c8022ba18302506ca1;hp=308b9e9fcfd2e8935b25a46df87a39e4d991f8b5;hpb=a4260a00251cef4ad806c9d5c44d4c444d6ab831;p=alttp.git diff --git a/app/Models/Result.php b/app/Models/Result.php index 308b9e9..229ac4b 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -11,6 +11,44 @@ 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(Result $a, Result $b) { + return strcasecmp($a->user->username, $b->user->username); + } + + + public function formatTime() { + $hours = floor($this->time / 60 / 60); + $minutes = floor(($this->time / 60) % 60); + $seconds = floor($this->time % 60); + return sprintf('%d:%02d:%02d', $hours, $minutes, $seconds); + } + public function updateResult($time, $forfeit) { $this->time = $time; $this->forfeit = $forfeit; @@ -38,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; } @@ -46,6 +88,7 @@ class Result extends Model protected $casts = [ 'forfeit' => 'boolean', 'time' => 'double', + 'user_id' => 'string', ]; protected $appends = [