Files
shell-leaderboard/src/app/Models/Participant.php

66 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Participant extends Model
{
use HasFactory;
protected $fillable = [
'name',
'number',
'best_time_ms',
'status',
];
protected $casts = [
'number' => 'integer',
'best_time_ms' => 'integer',
];
const STATUS_READY = 'ready';
const STATUS_RUNNING = 'running';
const STATUS_FINISHED = 'finished';
const STATUS_PIT = 'pit';
const STATUS_DNF = 'dnf';
public static function statuses(): array
{
return [
self::STATUS_READY => 'READY',
self::STATUS_RUNNING => 'RUNNING',
self::STATUS_FINISHED => 'FINISHED',
self::STATUS_PIT => 'IN PIT',
self::STATUS_DNF => 'DNF',
];
}
public function getFormattedTimeAttribute(): ?string
{
if ($this->best_time_ms === null) {
return null;
}
$totalMs = $this->best_time_ms;
$minutes = floor($totalMs / 60000);
$seconds = floor(($totalMs % 60000) / 1000);
$milliseconds = $totalMs % 1000;
return sprintf('%02d:%02d.%03d', $minutes, $seconds, $milliseconds);
}
public function getStatusLabelAttribute(): string
{
return self::statuses()[$this->status] ?? strtoupper($this->status);
}
public function scopeRanked($query)
{
return $query->orderByRaw('CASE WHEN best_time_ms IS NULL THEN 1 ELSE 0 END')
->orderBy('best_time_ms', 'asc');
}
}