]> git.localhorst.tv Git - alttp.git/blob - app/Models/Result.php
discord result command
[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 function formatTime() {
15                 $hours = floor($this->time / 60 / 60);
16                 $minutes = floor(($this->time / 60) % 60);
17                 $seconds = floor($this->time % 60);
18                 return sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
19         }
20
21         public function updateResult($time, $forfeit) {
22                 $this->time = $time;
23                 $this->forfeit = $forfeit;
24                 $this->save();
25                 if ($this->wasChanged()) {
26                         ResultChanged::dispatch($this);
27                 }
28         }
29
30         public function updatePlacement($score, $placement) {
31                 $this->score = $score;
32                 $this->placement = $placement;
33                 $this->save();
34                 if ($this->wasChanged()) {
35                         ResultChanged::dispatch($this);
36                 }
37         }
38
39
40         public function round() {
41                 return $this->belongsTo(Round::class);
42         }
43
44         public function participant() {
45                 return $this->belongsTo(Participant::class);
46         }
47
48         public function getHasFinishedAttribute() {
49                 return $this->time > 0 || $this->forfeit;
50         }
51
52
53         protected $casts = [
54                 'forfeit' => 'boolean',
55                 'time' => 'double',
56         ];
57
58         protected $appends = [
59                 'has_finished',
60         ];
61
62         protected $fillable = [
63                 'forfeit',
64                 'round_id',
65                 'time',
66                 'user_id',
67         ];
68
69 }