]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/Participant.php
server calculated scoring
[alttp.git] / app / Models / Participant.php
index 8aac88ce54a04877ecefe43c6f67950ec0d09c63..91b946d887fe726b2e066faf77d40991b8682485 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\Events\ParticipantChanged;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
@@ -9,6 +10,79 @@ class Participant extends Model
 {
        use HasFactory;
 
+
+       public static function compareResult(Round $round) {
+               return function (Participant $a, Participant $b) use ($round) {
+                       $a_result = $a->findResult($round);
+                       $b_result = $b->findResult($round);
+                       $a_time = $a_result && !$a_result->forfeit ? $a_result->time : 0;
+                       $b_time = $b_result && !$b_result->forfeit ? $b_result->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;
+                       }
+                       $a_forfeit = $a_result ? $a_result->forfeit : false;
+                       $b_forfeit = $b_result ? $b_result->forfeit : false;
+                       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 compareScore(Participant $a, Participant $b) {
+               $a_score = $a->isRunner() ? ($a->score ? $a->score : 0) : -1;
+               $b_score = $b->isRunner() ? ($b->score ? $b->score : 0) : -1;
+               if ($a_score < $b_score) return -1;
+               if ($b_score < $a_score) return 1;
+               return static::compareUsername($a, $b);
+       }
+
+       public static function compareUsername(Participant $a, Participant $b) {
+               return strcasecmp($a->user->username, $b->user->username);
+       }
+
+
+       public function updatePlacement($score, $placement) {
+               $this->score = $score;
+               $this->placement = $placement;
+               $this->save();
+               if ($this->wasChanged()) {
+                       ParticipantChanged::dispatch($this);
+               }
+       }
+
+       public function findResult(Round $round) {
+               foreach ($round->results as $result) {
+                       if ($this->user_id == $result->user_id) {
+                               return $result;
+                       }
+               }
+               return null;
+       }
+
+       public function isRunner() {
+               return in_array('runner', $this->roles);
+       }
+
+       public function isTournamentAdmin() {
+               return in_array('admin', $this->roles);
+       }
+
+
        public function tournament() {
                return $this->belongsTo(Tournament::class);
        }
@@ -17,6 +91,7 @@ class Participant extends Model
                return $this->belongsTo(User::class);
        }
 
+
        protected $casts = [
                'roles' => 'array',
        ];