]> git.localhorst.tv Git - alttp.git/blob - app/Models/Round.php
show placements on user profile
[alttp.git] / app / Models / Round.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7
8 class Round extends Model
9 {
10         use HasFactory;
11
12
13         public function updatePlacement() {
14                 $runners = [];
15                 foreach ($this->tournament->participants as $p) {
16                         if ($p->isRunner()) {
17                                 $runners[] = $p;
18                         } else {
19                                 $result = $p->findResult($this);
20                                 if ($result) {
21                                         $result->updatePlacement(null, null);
22                                 }
23                         }
24                 }
25
26                 usort($runners, Participant::compareResult($this));
27                 $mapped = array_map(function ($p) {
28                         return ['participant' => $p, 'result' => $p->findResult($this)];
29                 }, $runners);
30                 $filtered = array_filter($mapped, function($r) {
31                         return $r['result'] && ($r['result']->time || $r['result']->forfeit);
32                 });
33                 $reversed = array_reverse($filtered);
34
35                 $running = 0;
36                 $bonus = 1;
37                 $lastResult = null;
38                 foreach ($reversed as $r) {
39                         $betterThanLast = is_null($lastResult) || $r['result']->time < $lastResult;
40                         if (!$r['result']->forfeit && $betterThanLast) {
41                                 $running += $bonus;
42                                 $lastResult = $r['result']->time;
43                                 $bonus = 1;
44                         } else {
45                                 ++$bonus;
46                         }
47                         if (!$r['result']->forfeit) {
48                                 $r['result']->updatePlacement($running, count($filtered) - $running + 1);
49                         } else {
50                                 $r['result']->updatePlacement(0, count($filtered));
51                         }
52                 }
53         }
54
55
56         public function results() {
57                 return $this->hasMany(Result::class);
58         }
59
60         public function tournament() {
61                 return $this->belongsTo(Tournament::class);
62         }
63
64
65         protected $casts = [
66                 'code' => 'array',
67                 'locked' => 'boolean',
68         ];
69
70         protected $fillable = [
71                 'number',
72                 'tournament_id',
73         ];
74
75 }