X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FModels%2FResult.php;h=99f3791eacfc951acabb8d69d22053f592adbc80;hb=47033726a69270d991a496b433163b176ba58e71;hp=e5c9f7e77f8b756380275ebdf09854fe64df0555;hpb=d748feb96453d74aeffec648d6f5f68d9ef3b520;p=alttp.git diff --git a/app/Models/Result.php b/app/Models/Result.php index e5c9f7e..99f3791 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\ResultChanged; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -9,6 +10,33 @@ class Result extends Model { use HasFactory; + + 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; + $this->save(); + if ($this->wasChanged()) { + ResultChanged::dispatch($this); + } + } + + public function updatePlacement($score, $placement) { + $this->score = $score; + $this->placement = $placement; + $this->save(); + if ($this->wasChanged()) { + ResultChanged::dispatch($this); + } + } + + public function round() { return $this->belongsTo(Round::class); } @@ -17,7 +45,22 @@ class Result extends Model return $this->belongsTo(Participant::class); } + public function getHasFinishedAttribute() { + return $this->time > 0 || $this->forfeit; + } + + + protected $casts = [ + 'forfeit' => 'boolean', + 'time' => 'double', + ]; + + protected $appends = [ + 'has_finished', + ]; + protected $fillable = [ + 'forfeit', 'round_id', 'time', 'user_id',