]> git.localhorst.tv Git - alttp.git/blob - app/Models/Result.php
respond to whispers
[alttp.git] / app / Models / Result.php
1 <?php
2
3 namespace App\Models;
4
5 use App\Events\ResultChanged;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8
9 class Result extends Model
10 {
11         use HasFactory;
12
13
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;
17                 if ($a_time) {
18                         if ($b_time) {
19                                 if ($a_time < $b_time) return -1;
20                                 if ($b_time < $a_time) return 1;
21                                 return static::compareUsername($a, $b);
22                         }
23                         return -1;
24                 }
25                 if ($b_time) {
26                         return 1;
27                 }
28                 if ($a->forfeit) {
29                         if ($b->forfeit) {
30                                 return static::compareUsername($a, $b);
31                         }
32                         return -1;
33                 }
34                 if ($b->forfeit) {
35                         return 1;
36                 }
37                 return static::compareUsername($a, $b);
38         }
39
40         public static function compareUsername(Result $a, Result $b) {
41                 return strcasecmp($a->user->username, $b->user->username);
42         }
43
44
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);
50         }
51
52         public function updateResult($time, $forfeit) {
53                 $this->time = $time;
54                 $this->forfeit = $forfeit;
55                 $this->save();
56                 if ($this->wasChanged()) {
57                         ResultChanged::dispatch($this);
58                 }
59         }
60
61         public function updatePlacement($score, $placement) {
62                 $this->score = $score;
63                 $this->placement = $placement;
64                 $this->save();
65                 if ($this->wasChanged()) {
66                         ResultChanged::dispatch($this);
67                 }
68         }
69
70
71         public function round() {
72                 return $this->belongsTo(Round::class);
73         }
74
75         public function participant() {
76                 return $this->belongsTo(Participant::class);
77         }
78
79         public function user() {
80                 return $this->belongsTo(User::class);
81         }
82
83         public function getHasFinishedAttribute() {
84                 return $this->time > 0 || $this->forfeit;
85         }
86
87
88         protected $casts = [
89                 'forfeit' => 'boolean',
90                 'time' => 'double',
91                 'user_id' => 'string',
92         ];
93
94         protected $appends = [
95                 'has_finished',
96         ];
97
98         protected $fillable = [
99                 'forfeit',
100                 'round_id',
101                 'time',
102                 'user_id',
103         ];
104
105 }