1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Group extends Base implements WithGeneratorSetters, WithSkipSetters, WithTeams |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $generator = null; |
12
|
|
|
private $teams = []; // ARRAY OF TEAMS |
13
|
|
|
private $progressed = []; // ARRAY OF TEAMS ALREADY PROGRESSED FROM THIS GROUP |
14
|
|
|
private $ordering = \TournamentGenerator\Constants::POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS |
15
|
|
|
private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS |
16
|
|
|
private $games = []; // ARRAY OF GAME OBJECTS |
17
|
|
|
private $winPoints = 3; // POINTS AQUIRED FROM WINNING |
18
|
|
|
private $drawPoints = 1; // POINTS AQUIRED FROM DRAW |
19
|
|
|
private $lostPoints = 0; // POINTS AQUIRED FROM LOOSING |
20
|
|
|
private $secondPoints = 2; // POINTS AQUIRED FROM BEING SECOND (APPLIES ONLY FOR 3 OR 4 INGAME VALUE) |
21
|
|
|
private $thirdPoints = 1; // POINTS AQUIRED FROM BEING THIRD (APPLIES ONLY FOR 4 INGAME VALUE) |
22
|
|
|
private $progressPoints = 50; // POINTS AQUIRED FROM PROGRESSING TO THE NEXT ROUND |
23
|
|
|
private $order = 0; // ORDER OF GROUPS IN ROUND |
24
|
|
|
|
25
|
136 |
|
function __construct(string $name, $id = null) { |
26
|
136 |
|
$this->setName($name); |
27
|
136 |
|
$this->generator = new Utilis\Generator($this); |
28
|
136 |
|
$this->setId(isset($id) ? $id : uniqid()); |
29
|
136 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function allowSkip(){ |
32
|
1 |
|
$this->generator->allowSkip(); |
33
|
1 |
|
return $this; |
34
|
|
|
} |
35
|
1 |
|
public function disallowSkip(){ |
36
|
1 |
|
$this->generator->disallowSkip(); |
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
51 |
|
public function setSkip(bool $skip) { |
40
|
51 |
|
$this->generator->setSkip($skip); |
41
|
51 |
|
return $this; |
42
|
|
|
} |
43
|
2 |
|
public function getSkip() { |
44
|
2 |
|
return $this->generator->getSkip(); |
45
|
|
|
} |
46
|
|
|
|
47
|
61 |
|
public function addTeam(...$teams) { |
48
|
61 |
|
foreach ($teams as $team) { |
49
|
61 |
|
if (is_array($team)) { |
50
|
25 |
|
foreach ($team as $team2) { |
51
|
25 |
|
$this->setTeam($team2); |
52
|
|
|
} |
53
|
25 |
|
continue; |
54
|
|
|
} |
55
|
46 |
|
$this->setTeam($team); |
56
|
|
|
} |
57
|
61 |
|
return $this; |
58
|
|
|
} |
59
|
61 |
|
private function setTeam(Team $team) { |
60
|
61 |
|
$this->teams[] = $team; |
61
|
61 |
|
$team->addGroupResults($this); |
62
|
61 |
|
return $this; |
63
|
|
|
} |
64
|
89 |
|
public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) { |
65
|
89 |
|
$teams = $this->teams; |
66
|
|
|
|
67
|
89 |
|
if ($ordered) return $this->sortTeams($ordering, $filters); |
68
|
|
|
|
69
|
|
|
// APPLY FILTERS |
70
|
89 |
|
$filter = new Filter([$this], $filters); |
71
|
89 |
|
$filter->filter($teams); |
72
|
|
|
|
73
|
83 |
|
return $teams; |
74
|
|
|
} |
75
|
51 |
|
public function team(string $name = '', $id = null) { |
76
|
51 |
|
$t = new Team($name, $id); |
77
|
51 |
|
$this->teams[] = $t; |
78
|
51 |
|
$t->addGroupResults($this); |
79
|
51 |
|
return $t; |
80
|
|
|
} |
81
|
39 |
|
public function sortTeams($ordering = null, array $filters = []) { |
82
|
39 |
|
if (!isset($ordering)) $ordering = $this->ordering; |
83
|
39 |
|
$this->teams = Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering); |
84
|
39 |
|
return $this->getTeams(false, null, $filters); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function setWinPoints(int $points) { |
88
|
1 |
|
$this->winPoints = $points; |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
73 |
|
public function getWinPoints() { |
92
|
73 |
|
return $this->winPoints; |
93
|
|
|
} |
94
|
1 |
|
public function setDrawPoints(int $points) { |
95
|
1 |
|
$this->drawPoints = $points; |
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
17 |
|
public function getDrawPoints() { |
99
|
17 |
|
return $this->drawPoints; |
100
|
|
|
} |
101
|
1 |
|
public function setLostPoints(int $points) { |
102
|
1 |
|
$this->lostPoints = $points; |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
73 |
|
public function getLostPoints() { |
106
|
73 |
|
return $this->lostPoints; |
107
|
|
|
} |
108
|
1 |
|
public function setSecondPoints(int $points) { |
109
|
1 |
|
$this->secondPoints = $points; |
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
11 |
|
public function getSecondPoints() { |
113
|
11 |
|
return $this->secondPoints; |
114
|
|
|
} |
115
|
1 |
|
public function setThirdPoints(int $points) { |
116
|
1 |
|
$this->thirdPoints = $points; |
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
10 |
|
public function getThirdPoints() { |
120
|
10 |
|
return $this->thirdPoints; |
121
|
|
|
} |
122
|
1 |
|
public function setProgressPoints(int $points) { |
123
|
1 |
|
$this->progressPoints = $points; |
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
17 |
|
public function getProgressPoints() { |
127
|
17 |
|
return $this->progressPoints; |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
public function setMaxSize(int $size) { |
131
|
7 |
|
$this->generator->setMaxSize($size); |
132
|
7 |
|
return $this; |
133
|
|
|
} |
134
|
1 |
|
public function getMaxSize() { |
135
|
1 |
|
return $this->generator->getMaxSize(); |
136
|
|
|
} |
137
|
|
|
|
138
|
41 |
|
public function setType(string $type = \TournamentGenerator\Constants::ROUND_ROBIN) { |
139
|
41 |
|
$this->generator->setType($type); |
140
|
41 |
|
return $this; |
141
|
|
|
} |
142
|
1 |
|
public function getType() { |
143
|
1 |
|
return $this->generator->getType(); |
144
|
|
|
} |
145
|
|
|
|
146
|
4 |
|
public function setOrder(int $order) { |
147
|
4 |
|
$this->order = $order; |
148
|
4 |
|
return $this; |
149
|
|
|
} |
150
|
41 |
|
public function getOrder() { |
151
|
41 |
|
return $this->order; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
public function setOrdering(string $ordering = \TournamentGenerator\Constants::POINTS) { |
155
|
1 |
|
if (!in_array($ordering, \TournamentGenerator\Constants::OrderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering); |
156
|
1 |
|
$this->ordering = $ordering; |
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
1 |
|
public function getOrdering() { |
160
|
1 |
|
return $this->ordering; |
161
|
|
|
} |
162
|
|
|
|
163
|
53 |
|
public function setInGame(int $inGame) { |
164
|
53 |
|
$this->generator->setInGame($inGame); |
165
|
53 |
|
return $this; |
166
|
|
|
} |
167
|
76 |
|
public function getInGame() { |
168
|
76 |
|
return $this->generator->getInGame(); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
public function addProgression(Progression $progression) { |
172
|
1 |
|
$this->progressions[] = $progression; |
173
|
1 |
|
return $this; |
174
|
|
|
} |
175
|
33 |
|
public function progression(Group $to, int $start = 0, int $len = null) { |
176
|
33 |
|
$p = new Progression($this, $to, $start, $len); |
177
|
33 |
|
$this->progressions[] = $p; |
178
|
33 |
|
return $p; |
179
|
|
|
} |
180
|
33 |
|
public function progress(bool $blank = false) { |
181
|
33 |
|
foreach ($this->progressions as $progression) { |
182
|
33 |
|
$progression->progress($blank); |
183
|
|
|
} |
184
|
33 |
|
return $this; |
185
|
|
|
} |
186
|
34 |
|
public function addProgressed(...$teams) { |
187
|
|
|
$this->progressed = array_merge($this->progressed, array_map(function ($a) {return $a->getId();}, array_filter($teams, function($a){return $a instanceof Team;}))); |
188
|
|
|
foreach (array_filter($teams, function($a){return is_array($a);}) as $team) { |
189
|
|
|
$this->progressed = array_merge($this->progressed, array_map(function ($a) {return $a->getId();}, array_filter($team, function($a) { |
190
|
33 |
|
return ($a instanceof Team); |
191
|
33 |
|
}))); |
192
|
|
|
} |
193
|
34 |
|
return $this; |
194
|
|
|
} |
195
|
7 |
|
public function isProgressed(Team $team) { |
196
|
7 |
|
return in_array($team->getId(), $this->progressed); |
197
|
|
|
} |
198
|
|
|
|
199
|
42 |
|
public function genGames() { |
200
|
42 |
|
$this->generator->genGames(); |
201
|
41 |
|
return $this->games; |
202
|
|
|
} |
203
|
|
|
|
204
|
48 |
|
public function game(array $teams = []) { |
205
|
48 |
|
$g = new Game($teams, $this); |
206
|
48 |
|
$this->games[] = $g; |
207
|
48 |
|
return $g; |
208
|
|
|
} |
209
|
36 |
|
public function addGame(...$games){ |
210
|
|
|
$this->games = array_merge($this->games, array_filter($games, function($a){ return ($a instanceof Game); })); |
211
|
|
|
|
212
|
|
|
foreach (array_filter($games, function($a){return is_array($a);}) as $key => $game) { |
213
|
|
|
$this->games = array_merge($this->games, array_filter($game, function($a){ return ($a instanceof Game); })); |
214
|
|
|
} |
215
|
36 |
|
return $this; |
216
|
|
|
} |
217
|
46 |
|
public function getGames() { |
218
|
46 |
|
return $this->games; |
219
|
|
|
} |
220
|
29 |
|
public function orderGames() { |
221
|
29 |
|
if (count($this->games) <= 4) return $this->games; |
222
|
23 |
|
$this->games = $this->generator->orderGames(); |
223
|
23 |
|
return $this->games; |
224
|
|
|
} |
225
|
|
|
|
226
|
31 |
|
public function simulate(array $filters = [], bool $reset = true) { |
227
|
31 |
|
return Utilis\Simulator::simulateGroup($this, $filters, $reset); |
228
|
|
|
} |
229
|
15 |
|
public function resetGames() { |
230
|
15 |
|
foreach ($this->getGames() as $game) { |
231
|
15 |
|
$game->resetResults(); |
232
|
|
|
} |
233
|
15 |
|
return $this; |
234
|
|
|
} |
235
|
31 |
|
public function isPlayed(){ |
236
|
31 |
|
if (count($this->games) === 0) return false; |
237
|
28 |
|
foreach ($this->games as $game) { |
238
|
28 |
|
if (!$game->isPlayed()) return false; |
239
|
|
|
} |
240
|
19 |
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|