1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Round |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public $name = ''; |
12
|
|
|
public $id = ''; |
13
|
|
|
private $groups = []; |
14
|
|
|
private $games = []; |
15
|
|
|
private $teams = []; |
16
|
|
|
private $allowSkip = false; |
17
|
|
|
|
18
|
|
|
function __construct(string $name = '') { |
19
|
|
|
$this->id = uniqid(); |
20
|
|
|
$this->name = $name; |
21
|
|
|
} |
22
|
|
|
public function __toString() { |
23
|
|
|
return $this->name; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function addGroup(Group ...$groups){ |
27
|
|
|
foreach ($groups as $group) { |
28
|
|
|
if ($group instanceof Group) $this->groups[] = $group; |
29
|
|
|
else throw new \Exception('Trying to add group which is not an instance of Group class.'); |
30
|
|
|
} |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
public function group(array $settings = []) { |
34
|
|
|
$g = new Group($settings); |
35
|
|
|
$this->groups[] = $g->setSkip($this->allowSkip); |
36
|
|
|
return $g; |
37
|
|
|
} |
38
|
|
|
public function getGroups(){ |
39
|
|
|
$this->orderGroups(); |
40
|
|
|
return $this->groups; |
41
|
|
|
} |
42
|
|
|
public function getGroupsIds() { |
43
|
|
|
$this->orderGroups(); |
44
|
|
|
return array_map(function($a) { return $a->id; }, $this->groups); |
45
|
|
|
} |
46
|
|
|
public function orderGroups() { |
47
|
|
|
usort($this->groups, function($a, $b){ |
48
|
|
|
return $a->order - $b->order; |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function allowSkip(){ |
53
|
|
|
$this->allowSkip = true; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
public function disallowSkip(){ |
57
|
|
|
$this->allowSkip = false; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
public function setSkip(bool $skip = false) { |
61
|
|
|
$this->allowSkip = $skip; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
public function getSkip() { |
65
|
|
|
return $this->allowSkip; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function genGames(){ |
69
|
|
|
$g = 0; |
|
|
|
|
70
|
|
|
foreach ($this->groups as $group) { |
71
|
|
|
$group->genGames(); |
72
|
|
|
$this->games = array_merge($this->games, $group->orderGames()); |
73
|
|
|
} |
74
|
|
|
return $this->games; |
75
|
|
|
} |
76
|
|
|
public function getGames() { |
77
|
|
|
return $this->games; |
78
|
|
|
} |
79
|
|
|
public function isPlayed(){ |
80
|
|
|
foreach ($this->groups as $group) { |
81
|
|
|
if (!$group->isPlayed()) return false; |
82
|
|
|
} |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addTeam(...$teams) { |
87
|
|
|
foreach ($teams as $team) { |
88
|
|
|
if ($team instanceof Team) { |
89
|
|
|
$this->teams[] = $team; |
90
|
|
|
} |
91
|
|
|
elseif (gettype($team) === 'array') { |
92
|
|
|
foreach ($team as $team2) { |
93
|
|
|
if ($team2 instanceof Team) $this->teams[] = $team2; |
94
|
|
|
$team2->groupResults[$this->id] = [ |
95
|
|
|
'group' => $this, |
96
|
|
|
'points' => 0, |
97
|
|
|
'score' => 0, |
98
|
|
|
'wins' => 0, |
99
|
|
|
'draws' => 0, |
100
|
|
|
'losses' => 0, |
101
|
|
|
'second' => 0, |
102
|
|
|
'third' => 0 |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
else throw new \Exception('Trying to add team which is not an instance of Team class'); |
107
|
|
|
} |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
public function team(string $name = '') { |
111
|
|
|
$t = new Team($name); |
112
|
|
|
$this->teams[] = $t; |
113
|
|
|
return $t; |
114
|
|
|
} |
115
|
|
|
public function getTeams(bool $ordered = false, $ordering = POINTS) { |
|
|
|
|
116
|
|
|
if (count($this->teams) == 0) { |
117
|
|
|
$teams = []; |
118
|
|
|
foreach ($this->groups as $group) { |
119
|
|
|
$teams = array_merge($teams, $group->getTeams()); |
120
|
|
|
} |
121
|
|
|
$this->teams = $teams; |
122
|
|
|
} |
123
|
|
|
if ($ordered) { |
124
|
|
|
$this->sortTeams($ordering); |
125
|
|
|
} |
126
|
|
|
return $this->teams; |
127
|
|
|
} |
128
|
|
|
public function sortTeams($ordering = POINTS) { |
|
|
|
|
129
|
|
|
$teams = []; |
|
|
|
|
130
|
|
|
$groupsIds = $this->getGroupsIds(); |
131
|
|
|
switch ($ordering) { |
132
|
|
|
case POINTS:{ |
133
|
|
|
uasort($this->teams, function($a, $b) use ($groupsIds) { |
134
|
|
|
if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds) && $a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0; |
135
|
|
|
if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds)) return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1); |
136
|
|
|
return ($a->sumPoints($groupsIds) > $b->sumPoints($groupsIds) ? -1 : 1); |
137
|
|
|
}); |
138
|
|
|
break;} |
139
|
|
|
case SCORE:{ |
|
|
|
|
140
|
|
|
uasort($this->teams, function($a, $b) use ($groupsIds) { |
141
|
|
|
if ($a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0; |
142
|
|
|
return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1); |
143
|
|
|
}); |
144
|
|
|
break;} |
145
|
|
|
} |
146
|
|
|
return $this->teams; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function splitTeams(...$groups) { |
150
|
|
|
|
151
|
|
|
if (count($groups) === 0) $groups = $this->getGroups(); |
152
|
|
|
|
153
|
|
|
foreach ($groups as $key => $value) { |
154
|
|
|
if (gettype($value) === 'array') { |
155
|
|
|
unset($groups[$key]); |
156
|
|
|
$groups = array_merge($groups, $value); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$teams = $this->getTeams(); |
161
|
|
|
shuffle($teams); |
162
|
|
|
|
163
|
|
|
while (count($teams) > 0) { |
164
|
|
|
foreach ($groups as $group) { |
165
|
|
|
if ($group instanceof Group) { |
166
|
|
|
if (count($teams) > 0) $group->addTeam(array_shift($teams)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function progress(bool $blank = false){ |
174
|
|
|
foreach ($this->groups as $group) { |
175
|
|
|
$group->progress($blank); |
176
|
|
|
} |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function simulate() { |
181
|
|
|
foreach ($this->groups as $group) { |
182
|
|
|
if ($group->isPlayed()) continue; |
183
|
|
|
$group->simulate([], false); |
184
|
|
|
} |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
public function resetGames() { |
188
|
|
|
foreach ($this->groups as $group) { |
189
|
|
|
$group->resetGames(); |
190
|
|
|
} |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|