+
+ 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);
+ }
+
+