|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class Category extends Base implements WithSkipSetters, WithTeams, WithRounds |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
private $rounds = []; |
|
12
|
|
|
private $teams = []; |
|
13
|
|
|
private $allowSkip = false; |
|
14
|
|
|
|
|
15
|
25 |
|
public function __construct(string $name = '', $id = null) { |
|
16
|
25 |
|
$this->setName($name); |
|
17
|
25 |
|
$this->setId(isset($id) ? $id : uniqid()); |
|
18
|
25 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function addRound(Round ...$rounds){ |
|
21
|
1 |
|
foreach ($rounds as $round) { |
|
22
|
1 |
|
$this->rounds[] = $round; |
|
23
|
|
|
} |
|
24
|
1 |
|
return $this; |
|
25
|
|
|
} |
|
26
|
15 |
|
public function round(string $name = '') { |
|
27
|
15 |
|
$r = new Round($name); |
|
28
|
15 |
|
$this->rounds[] = $r->setSkip($this->allowSkip); |
|
29
|
15 |
|
return $r; |
|
30
|
|
|
} |
|
31
|
18 |
|
public function getRounds(){ |
|
32
|
18 |
|
return $this->rounds; |
|
33
|
|
|
} |
|
34
|
15 |
|
public function getGroups() { |
|
35
|
15 |
|
$groups = []; |
|
36
|
15 |
|
foreach ($this->getRounds() as $round) { |
|
37
|
12 |
|
$groups = array_merge($groups, $round->getGroups()); |
|
38
|
|
|
} |
|
39
|
15 |
|
return $groups; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function allowSkip(){ |
|
43
|
2 |
|
$this->allowSkip = true; |
|
44
|
2 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
1 |
|
public function disallowSkip(){ |
|
47
|
1 |
|
$this->allowSkip = false; |
|
48
|
1 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
9 |
|
public function setSkip(bool $skip) { |
|
51
|
9 |
|
$this->allowSkip = $skip; |
|
52
|
9 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
2 |
|
public function getSkip() { |
|
55
|
2 |
|
return $this->allowSkip; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
public function addTeam(Team ...$teams) { |
|
59
|
3 |
|
foreach ($teams as $team) { |
|
60
|
3 |
|
$this->teams[] = $team; |
|
61
|
|
|
} |
|
62
|
3 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
8 |
|
public function team(string $name = '', $id = null) { |
|
65
|
8 |
|
$t = new Team($name, $id); |
|
66
|
8 |
|
$this->teams[] = $t; |
|
67
|
8 |
|
return $t; |
|
68
|
|
|
} |
|
69
|
15 |
|
public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) { |
|
70
|
15 |
|
$teams = $this->teams; |
|
71
|
15 |
|
foreach ($this->rounds as $round) { |
|
72
|
12 |
|
$teams = \array_merge($teams, $round->getTeams()); |
|
73
|
|
|
} |
|
74
|
15 |
|
$teams = \array_unique($teams); |
|
75
|
15 |
|
$this->teams = $teams; |
|
76
|
15 |
|
if ($ordered) $teams = $this->sortTeams($ordering); |
|
77
|
|
|
|
|
78
|
|
|
// APPLY FILTERS |
|
79
|
15 |
|
$filter = new Filter($this->getGroups(), $filters); |
|
80
|
15 |
|
$filter->filter($teams); |
|
81
|
|
|
|
|
82
|
15 |
|
return $teams; |
|
83
|
|
|
} |
|
84
|
3 |
|
public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) { |
|
85
|
3 |
|
$teams = []; |
|
86
|
3 |
|
for ($i = count($this->rounds)-1; $i >= 0; $i--) { |
|
87
|
|
|
$rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); }); |
|
88
|
3 |
|
$teams = array_merge($teams, $rTeams); |
|
89
|
|
|
} |
|
90
|
3 |
|
$this->teams = $teams; |
|
91
|
|
|
|
|
92
|
|
|
// APPLY FILTERS |
|
93
|
3 |
|
$filter = new Filter($this->getGroups(), $filters); |
|
94
|
3 |
|
$filter->filter($teams); |
|
95
|
|
|
|
|
96
|
3 |
|
return $teams; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function getGames() { |
|
100
|
2 |
|
$games = []; |
|
101
|
2 |
|
foreach ($this->getRounds() as $round) { |
|
102
|
2 |
|
$games = array_merge($games, $round->getGames()); |
|
103
|
|
|
} |
|
104
|
2 |
|
return $games; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
public function splitTeams(Round ...$wheres) { |
|
108
|
|
|
|
|
109
|
4 |
|
if (count($wheres) === 0) $wheres = $this->getRounds(); |
|
110
|
|
|
|
|
111
|
4 |
|
$teams = $this->getTeams(); |
|
112
|
4 |
|
shuffle($teams); |
|
113
|
|
|
|
|
114
|
4 |
|
while (count($teams) > 0) { |
|
115
|
4 |
|
foreach ($wheres as $where) { |
|
116
|
4 |
|
if (count($teams) > 0) $where->addTeam(array_shift($teams)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
4 |
|
foreach ($wheres as $where) { |
|
120
|
4 |
|
$where->splitTeams(); |
|
121
|
|
|
} |
|
122
|
4 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
public function genGamesSimulate() { |
|
126
|
1 |
|
$games = Utilis\Simulator::simulateCategory($this); |
|
127
|
1 |
|
return $games; |
|
128
|
|
|
} |
|
129
|
1 |
|
public function genGamesSimulateReal() { |
|
130
|
1 |
|
$games = Utilis\Simulator::simulateCategoryReal($this); |
|
131
|
1 |
|
return $games; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|