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