]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Result.php
track twitch category where chats were sent in
[alttp.git] / app / Models / Result.php
index 3ced393cc64b1d13ebe3d2970e399e48550a9c13..229ac4b9c8e418c5d52cf9de99c33e07122eaf26 100644 (file)
@@ -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,64 @@ class Result extends Model
 {
        use HasFactory;
 
+
+       public static function compareResult(Result $a, Result $b) {
+               $a_time = !$a->forfeit ? $a->time : 0;
+               $b_time = !$b->forfeit ? $b->time : 0;
+               if ($a_time) {
+                       if ($b_time) {
+                               if ($a_time < $b_time) return -1;
+                               if ($b_time < $a_time) return 1;
+                               return static::compareUsername($a, $b);
+                       }
+                       return -1;
+               }
+               if ($b_time) {
+                       return 1;
+               }
+               if ($a->forfeit) {
+                       if ($b->forfeit) {
+                               return static::compareUsername($a, $b);
+                       }
+                       return -1;
+               }
+               if ($b->forfeit) {
+                       return 1;
+               }
+               return static::compareUsername($a, $b);
+       }
+
+       public static function compareUsername(Result $a, Result $b) {
+               return strcasecmp($a->user->username, $b->user->username);
+       }
+
+
+       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,10 +76,21 @@ class Result extends Model
                return $this->belongsTo(Participant::class);
        }
 
+       public function user() {
+               return $this->belongsTo(User::class);
+       }
+
        public function getHasFinishedAttribute() {
                return $this->time > 0 || $this->forfeit;
        }
 
+
+       protected $casts = [
+               'forfeit' => 'boolean',
+               'time' => 'double',
+               'user_id' => 'string',
+       ];
+
        protected $appends = [
                'has_finished',
        ];