1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Tournament implements WithSkipSetters, WithTeams |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $name = ''; |
12
|
|
|
private $categories = []; |
13
|
|
|
private $rounds = []; |
14
|
|
|
private $teams = []; |
15
|
|
|
|
16
|
|
|
private $expectedPlay = 0; |
17
|
|
|
private $expectedGameWait = 0; |
18
|
|
|
private $expectedRoundWait = 0; |
19
|
|
|
private $expectedCategoryWait = 0; |
20
|
|
|
|
21
|
|
|
private $allowSkip = false; |
22
|
|
|
|
23
|
28 |
|
function __construct(string $name = ''){ |
24
|
28 |
|
$this->name = $name; |
25
|
28 |
|
} |
26
|
1 |
|
public function __toString() { |
27
|
1 |
|
return $this->name; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function setName(string $name) { |
31
|
1 |
|
$this->name = $name; |
32
|
1 |
|
return $this; |
33
|
|
|
} |
34
|
1 |
|
public function getName() { |
35
|
1 |
|
return $this->name; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
public function setPlay(int $play) { |
39
|
7 |
|
$this->expectedPlay = $play; |
40
|
7 |
|
return $this; |
41
|
|
|
} |
42
|
1 |
|
public function getPlay() { |
43
|
1 |
|
return $this->expectedPlay; |
44
|
|
|
} |
45
|
7 |
|
public function setGameWait(int $wait) { |
46
|
7 |
|
$this->expectedGameWait = $wait; |
47
|
7 |
|
return $this; |
48
|
|
|
} |
49
|
1 |
|
public function getGameWait() { |
50
|
1 |
|
return $this->expectedGameWait; |
51
|
|
|
} |
52
|
7 |
|
public function setRoundWait(int $wait) { |
53
|
7 |
|
$this->expectedRoundWait = $wait; |
54
|
7 |
|
return $this; |
55
|
|
|
} |
56
|
1 |
|
public function getRoundWait() { |
57
|
1 |
|
return $this->expectedRoundWait; |
58
|
|
|
} |
59
|
1 |
|
public function setCategoryWait(int $wait) { |
60
|
1 |
|
$this->expectedCategoryWait = $wait; |
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
1 |
|
public function getCategoryWait() { |
64
|
1 |
|
return $this->expectedCategoryWait; |
65
|
|
|
} |
66
|
3 |
|
public function getTournamentTime(){ |
67
|
3 |
|
$games = count($this->getGames()); |
68
|
3 |
|
return $games*$this->expectedPlay+$games*$this->expectedGameWait+count($this->getRounds())*$this->expectedRoundWait+count($this->getCategories())*$this->expectedCategoryWait; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public function allowSkip(){ |
72
|
3 |
|
$this->allowSkip = true; |
73
|
3 |
|
return $this; |
74
|
|
|
} |
75
|
1 |
|
public function disallowSkip(){ |
76
|
1 |
|
$this->allowSkip = false; |
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
1 |
|
public function setSkip(bool $skip) { |
80
|
1 |
|
$this->allowSkip = $skip; |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
1 |
|
public function getSkip() { |
84
|
1 |
|
return $this->allowSkip; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function addCategory(Category ...$categories){ |
88
|
1 |
|
foreach ($categories as $category) { |
89
|
1 |
|
$this->categories[] = $category; |
90
|
|
|
} |
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
4 |
|
public function category(string $name = '') { |
94
|
4 |
|
$c = new Category($name); |
95
|
4 |
|
$this->categories[] = $c->setSkip($this->allowSkip); |
96
|
4 |
|
return $c; |
97
|
|
|
} |
98
|
8 |
|
public function getCategories() { |
99
|
8 |
|
return $this->categories; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function addRound(Round ...$rounds) { |
103
|
1 |
|
foreach ($rounds as $round) { |
104
|
1 |
|
$this->rounds[] = $round; |
105
|
|
|
} |
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
16 |
|
public function round(string $name = '', $id = null) { |
109
|
16 |
|
$r = new Round($name, $id); |
110
|
16 |
|
$this->rounds[] = $r->setSkip($this->allowSkip); |
111
|
16 |
|
return $r; |
112
|
|
|
} |
113
|
18 |
|
public function getRounds() { |
114
|
18 |
|
if (count($this->categories) > 0) { |
115
|
2 |
|
$rounds = []; |
116
|
2 |
|
foreach ($this->categories as $category) { |
117
|
2 |
|
$rounds = array_merge($rounds, $category->getRounds()); |
118
|
|
|
} |
119
|
2 |
|
return array_merge($rounds, $this->rounds); |
120
|
|
|
} |
121
|
16 |
|
return $this->rounds; |
122
|
|
|
} |
123
|
|
|
|
124
|
15 |
|
public function getGroups() { |
125
|
15 |
|
$groups = []; |
126
|
15 |
|
foreach ($this->getRounds() as $round) { |
127
|
13 |
|
$groups = array_merge($groups, $round->getGroups()); |
128
|
|
|
} |
129
|
15 |
|
return $groups; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
public function addTeam(Team ...$teams) { |
133
|
2 |
|
foreach ($teams as $team) { |
134
|
2 |
|
$this->teams[] = $team; |
135
|
|
|
} |
136
|
2 |
|
return $this; |
137
|
|
|
} |
138
|
12 |
|
public function team(string $name = '', $id = null) { |
139
|
12 |
|
$t = new Team($name, $id); |
140
|
12 |
|
$this->teams[] = $t; |
141
|
12 |
|
return $t; |
142
|
|
|
} |
143
|
15 |
|
public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) { |
144
|
15 |
|
$teams = $this->teams; |
145
|
15 |
|
foreach ($this->categories as $category) { |
146
|
1 |
|
$teams = array_merge($teams, $category->getTeams()); |
147
|
|
|
} |
148
|
15 |
|
foreach ($this->rounds as $round) { |
149
|
13 |
|
$teams = array_merge($teams, $round->getTeams()); |
150
|
|
|
} |
151
|
15 |
|
$teams = \array_unique($teams); |
152
|
15 |
|
$this->teams = $teams; |
153
|
15 |
|
if ($ordered) $teams = $this->sortTeams($ordering); |
154
|
|
|
|
155
|
|
|
// APPLY FILTERS |
156
|
15 |
|
$filter = new Filter($this->getGroups(), $filters); |
157
|
15 |
|
$filter->filter($teams); |
158
|
|
|
|
159
|
15 |
|
return $teams; |
160
|
|
|
} |
161
|
3 |
|
public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) { |
162
|
3 |
|
$teams = []; |
163
|
3 |
|
for ($i = count($this->rounds)-1; $i >= 0; $i--) { |
164
|
|
|
$rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); }); |
165
|
3 |
|
$teams = array_merge($teams, $rTeams); |
166
|
|
|
} |
167
|
3 |
|
$this->teams = $teams; |
168
|
|
|
|
169
|
|
|
// APPLY FILTERS |
170
|
3 |
|
$filter = new Filter($this->getGroups(), $filters); |
171
|
3 |
|
$filter->filter($teams); |
172
|
|
|
|
173
|
3 |
|
return $teams; |
174
|
|
|
} |
175
|
|
|
|
176
|
4 |
|
public function getGames() { |
177
|
4 |
|
$games = []; |
178
|
4 |
|
foreach ($this->getRounds() as $round) { |
179
|
4 |
|
$games = array_merge($games, $round->getGames()); |
180
|
|
|
} |
181
|
4 |
|
return $games; |
182
|
|
|
} |
183
|
|
|
|
184
|
8 |
|
public function splitTeams(Round ...$wheres) { |
185
|
|
|
|
186
|
8 |
|
if (count($wheres) === 0) $wheres = $this->getRounds(); |
187
|
|
|
|
188
|
8 |
|
$teams = $this->getTeams(); |
189
|
8 |
|
shuffle($teams); |
190
|
|
|
|
191
|
8 |
|
while (count($teams) > 0) { |
192
|
8 |
|
foreach ($wheres as $where) { |
193
|
8 |
|
if (count($teams) > 0) $where->addTeam(array_shift($teams)); |
194
|
|
|
} |
195
|
|
|
} |
196
|
8 |
|
foreach ($wheres as $where) { |
197
|
8 |
|
$where->splitTeams(); |
198
|
|
|
} |
199
|
8 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
4 |
|
public function genGamesSimulate(bool $returnTime = false) { |
203
|
4 |
|
$games = Utilis\Simulator::simulateTournament($this); |
204
|
|
|
|
205
|
4 |
|
if ($returnTime) return $this->getTournamentTime(); |
206
|
3 |
|
return $games; |
207
|
|
|
} |
208
|
2 |
|
public function genGamesSimulateReal(bool $returnTime = false) { |
209
|
2 |
|
$games = Utilis\Simulator::simulateTournamentReal($this); |
210
|
|
|
|
211
|
2 |
|
if ($returnTime) return $this->getTournamentTime(); |
212
|
1 |
|
return $games; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|