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
|
|
|
public $name = ''; // DISPLAYABLE NAME |
15
|
|
|
private $ordering = \POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS |
16
|
|
|
private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS |
17
|
|
|
private $games = []; // ARRAY OF GAME OBJECTS |
18
|
|
|
public $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
|
|
|
public $order = 0; // ORDER OF GROUPS IN ROUND |
26
|
|
|
|
27
|
|
|
function __construct(array $settings = []) { |
28
|
|
|
$this->id = uniqid(); |
29
|
|
|
$this->generator = new Utilis\Generator($this); |
30
|
|
|
foreach ($settings as $key => $value) { |
31
|
|
|
switch ($key) { |
32
|
|
|
case 'name': |
33
|
|
|
$this->name = (string) $value; |
34
|
|
|
break; |
35
|
|
|
case 'type': |
36
|
|
|
$this->generator->setType($value); |
37
|
|
|
break; |
38
|
|
|
case 'ordering': |
39
|
|
|
$this->setOrdering($value); |
40
|
|
|
break; |
41
|
|
|
case 'inGame': |
42
|
|
|
$this->generator->setInGame((int) $value); |
43
|
|
|
break; |
44
|
|
|
case 'maxSize': |
45
|
|
|
$this->generator->setMaxSize((int) $value); |
46
|
|
|
break; |
47
|
|
|
case 'order': |
48
|
|
|
$this->order = (int) $value; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
public function __toString() { |
54
|
|
|
return 'Group '.$this->name; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function allowSkip(){ |
58
|
|
|
$this->generator->allowSkip(); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
public function disallowSkip(){ |
62
|
|
|
$this->generator->disallowSkip(); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
public function setSkip(bool $skip) { |
66
|
|
|
$this->generator->setSkip($skip); |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
public function getSkip() { |
70
|
|
|
return $this->generator->getSkip(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function addTeam(...$teams) { |
74
|
|
|
foreach ($teams as $team) { |
75
|
|
|
if (gettype($team) === 'array') { |
76
|
|
|
foreach ($team as $team2) { |
77
|
|
|
$this->setTeam($team); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
$this->setTeam($team); |
82
|
|
|
} |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
private function setTeam(Team $team) { |
86
|
|
|
$this->teams[] = $team; |
87
|
|
|
$team2->groupResults[$this->id] = [ |
|
|
|
|
88
|
|
|
'group' => $this, |
89
|
|
|
'points' => 0, |
90
|
|
|
'score' => 0, |
91
|
|
|
'wins' => 0, |
92
|
|
|
'draws' => 0, |
93
|
|
|
'losses' => 0, |
94
|
|
|
'second' => 0, |
95
|
|
|
'third' => 0 |
96
|
|
|
]; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
public function getTeams($filters = []) { |
100
|
|
|
$teams = $this->teams; |
101
|
|
|
|
102
|
|
|
if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) $filters = [$filters]; |
103
|
|
|
elseif (gettype($filters) !== 'array') $filters = []; |
104
|
|
|
|
105
|
|
|
// APPLY FILTERS |
106
|
|
|
$filter = new Filter($this, $filters); |
107
|
|
|
$filter->filter($teams); |
108
|
|
|
|
109
|
|
|
return $teams; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function team(string $name = '') { |
113
|
|
|
$t = new Team($name); |
114
|
|
|
$this->teams[] = $t; |
115
|
|
|
$t->groupResults[$this->id] = [ |
116
|
|
|
'group' => $this, |
117
|
|
|
'points' => 0, |
118
|
|
|
'score' => 0, |
119
|
|
|
'wins' => 0, |
120
|
|
|
'draws' => 0, |
121
|
|
|
'losses' => 0, |
122
|
|
|
'second' => 0, |
123
|
|
|
'third' => 0 |
124
|
|
|
]; |
125
|
|
|
return $t; |
126
|
|
|
} |
127
|
|
|
public function sortTeams($filters = [], $ordering = null) { |
128
|
|
|
if (!isset($ordering)) $ordering = $this->ordering; |
129
|
|
|
Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering); |
130
|
|
|
return $this->getTeams($filters); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function setType(string $type = \R_R) { |
134
|
|
|
$this->generator->setType($type); |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
public function getType() { |
138
|
|
|
return $this->generator->getType(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setOrdering(string $ordering = \POINTS) { |
142
|
|
|
if (!in_array($ordering, orderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering); |
143
|
|
|
$this->ordering = $ordering; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
public function getOrdering() { |
147
|
|
|
return $this->ordering; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setInGame(int $inGame) { |
151
|
|
|
$this->generator->setInGame($inGame); |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
public function getInGame() { |
155
|
|
|
return $this->generator->getInGame(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function addProgression(Progression $progression) { |
159
|
|
|
$this->progressions[] = $progression; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
public function progression(Group $to, int $start = 0, int $len = null) { |
163
|
|
|
$p = new Progression($this, $to, $start, $len); |
164
|
|
|
$this->progressions[] = $p; |
165
|
|
|
return $p; |
166
|
|
|
} |
167
|
|
|
public function progress(bool $blank = false) { |
168
|
|
|
foreach ($this->progressions as $progression) { |
169
|
|
|
$progression->progress($blank); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
public function addProgressed(...$teams) { |
173
|
|
|
foreach ($teams as $team) { |
174
|
|
|
if ($team instanceOf Team) $this->progressed[] = $team->id; |
175
|
|
|
elseif (gettype($team) === 'array') { |
176
|
|
|
$this->progressed = array_merge($this->progressed, array_filter($team, function($a) { |
177
|
|
|
return ($a instanceof Team); |
178
|
|
|
})); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
public function isProgressed(Team $team) { |
184
|
|
|
return in_array($team->id, $this->progressed); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function genGames() { |
188
|
|
|
$this->generator->genGames(); |
189
|
|
|
return $this->games; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function game(array $teams = []) { |
193
|
|
|
$g = new Game($teams, $this); |
194
|
|
|
$this->games[] = $g; |
195
|
|
|
return $g; |
196
|
|
|
} |
197
|
|
|
public function addGame(...$games){ |
198
|
|
|
foreach ($games as $key => $game) { |
199
|
|
|
if (gettype($game) === 'array') { |
200
|
|
|
unset($games[$key]); |
201
|
|
|
$games = array_merge($games, array_filter($game, function($a){ return ($a instanceof Game); })); |
202
|
|
|
continue; |
203
|
|
|
} |
204
|
|
|
if (!$game instanceof Game) throw new \Exception('Trying to add game which is not instance of Game object.'); |
205
|
|
|
$this->games[] = $game; |
206
|
|
|
} |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
public function getGames() { |
210
|
|
|
return $this->games; |
211
|
|
|
} |
212
|
|
|
public function orderGames() { |
213
|
|
|
if (count($this->games) <= 4) return $this->games; |
214
|
|
|
$this->games = $this->generator->orderGames(); |
215
|
|
|
return $this->games; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function simulate(array $filters = [], bool $reset = true) { |
219
|
|
|
return Utilis\Simulator::simulateGroup($this, $filters, $reset); |
220
|
|
|
} |
221
|
|
|
public function resetGames() { |
222
|
|
|
foreach ($this->getGames() as $game) { |
223
|
|
|
$game->resetResults(); |
224
|
|
|
} |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
public function isPlayed(){ |
228
|
|
|
foreach ($this->games as $game) { |
229
|
|
|
if (!$game->isPlayed()) return false; |
230
|
|
|
} |
231
|
|
|
return true; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|