5 use App\Events\ResultChanged;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
9 class Result extends Model
14 public static function compareResult(Result $a, Result $b) {
15 $a_time = !$a->forfeit ? $a->time : 0;
16 $b_time = !$b->forfeit ? $b->time : 0;
19 if ($a_time < $b_time) return -1;
20 if ($b_time < $a_time) return 1;
21 return static::compareUsername($a, $b);
30 return static::compareUsername($a, $b);
37 return static::compareUsername($a, $b);
40 public static function compareUsername(Result $a, Result $b) {
41 return strcasecmp($a->user->username, $b->user->username);
45 public function formatTime() {
46 $hours = floor($this->time / 60 / 60);
47 $minutes = floor(($this->time / 60) % 60);
48 $seconds = floor($this->time % 60);
49 return sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
52 public function updateResult($time, $forfeit) {
54 $this->forfeit = $forfeit;
56 if ($this->wasChanged()) {
57 ResultChanged::dispatch($this);
61 public function updatePlacement($score, $placement) {
62 $this->score = $score;
63 $this->placement = $placement;
65 if ($this->wasChanged()) {
66 ResultChanged::dispatch($this);
71 public function round() {
72 return $this->belongsTo(Round::class);
75 public function participant() {
76 return $this->belongsTo(Participant::class);
79 public function user() {
80 return $this->belongsTo(User::class);
83 public function getHasFinishedAttribute() {
84 return $this->time > 0 || $this->forfeit;
89 'forfeit' => 'boolean',
91 'user_id' => 'string',
94 protected $appends = [
98 protected $fillable = [