1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Round |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $name = ''; |
12
|
|
|
private $id = ''; |
13
|
|
|
private $groups = []; |
14
|
|
|
private $games = []; |
15
|
|
|
private $teams = []; |
16
|
|
|
private $allowSkip = false; |
17
|
|
|
|
18
|
|
|
function __construct(string $name = '', $id = null) { |
19
|
|
|
$this->setName($name); |
20
|
|
|
$this->setId(isset($id) ? $id : uniqid()); |
21
|
|
|
} |
22
|
|
|
public function __toString() { |
23
|
|
|
return $this->name; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setName(string $name) { |
27
|
|
|
$this->name = $name; |
28
|
|
|
} |
29
|
|
|
public function getName() { |
30
|
|
|
return $this->name; |
31
|
|
|
} |
32
|
|
|
public function setId($id) { |
33
|
|
|
if (!is_string($id) && !is_int($id)) { |
34
|
|
|
$this->id = uniqid(); |
35
|
|
|
throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int'); |
36
|
|
|
} |
37
|
|
|
$this->id = $id; |
38
|
|
|
} |
39
|
|
|
public function getId() { |
40
|
|
|
return $this->id; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addGroup(Group ...$groups){ |
44
|
|
|
foreach ($groups as $group) { |
45
|
|
|
$this->groups[] = $group; |
46
|
|
|
} |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
public function group(string $name, $id = null) { |
50
|
|
|
$g = new Group($name, $id); |
51
|
|
|
$this->groups[] = $g->setSkip($this->allowSkip); |
52
|
|
|
return $g; |
53
|
|
|
} |
54
|
|
|
public function getGroups(){ |
55
|
|
|
$this->orderGroups(); |
56
|
|
|
return $this->groups; |
57
|
|
|
} |
58
|
|
|
public function getGroupsIds() { |
59
|
|
|
$this->orderGroups(); |
60
|
|
|
return array_map(function($a) { return $a->getId(); }, $this->groups); |
61
|
|
|
} |
62
|
|
|
public function orderGroups() { |
63
|
|
|
usort($this->groups, function($a, $b){ |
64
|
|
|
return $a->getOrder() - $b->getOrder(); |
65
|
|
|
}); |
66
|
|
|
return $this->groups; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function allowSkip(){ |
70
|
|
|
$this->allowSkip = true; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
public function disallowSkip(){ |
74
|
|
|
$this->allowSkip = false; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
public function setSkip(bool $skip = false) { |
78
|
|
|
$this->allowSkip = $skip; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
public function getSkip() { |
82
|
|
|
return $this->allowSkip; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function genGames(){ |
86
|
|
|
foreach ($this->groups as $group) { |
87
|
|
|
$group->genGames(); |
88
|
|
|
$this->games = array_merge($this->games, $group->orderGames()); |
89
|
|
|
} |
90
|
|
|
return $this->games; |
91
|
|
|
} |
92
|
|
|
public function getGames() { |
93
|
|
|
return $this->games; |
94
|
|
|
} |
95
|
|
|
public function isPlayed(){ |
96
|
|
|
if (count($this->games) === 0) return false; |
97
|
|
|
foreach ($this->groups as $group) { |
98
|
|
|
if (!$group->isPlayed()) return false; |
99
|
|
|
} |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addTeam(Team ...$teams) { |
104
|
|
|
foreach ($teams as $team) { |
105
|
|
|
$this->teams[] = $team; |
106
|
|
|
} |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
public function team(string $name = '', $id = null) { |
110
|
|
|
$t = new Team($name, $id); |
111
|
|
|
$this->teams[] = $t; |
112
|
|
|
return $t; |
113
|
|
|
} |
114
|
|
|
public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS) { |
115
|
|
|
if (count($this->teams) == 0) { |
116
|
|
|
$teams = []; |
117
|
|
|
foreach ($this->groups as $group) { |
118
|
|
|
$teams = array_merge($teams, $group->getTeams()); |
119
|
|
|
} |
120
|
|
|
$this->teams = $teams; |
121
|
|
|
} |
122
|
|
|
if ($ordered) { |
123
|
|
|
$this->sortTeams($ordering); |
124
|
|
|
} |
125
|
|
|
return $this->teams; |
126
|
|
|
} |
127
|
|
|
public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS) { |
128
|
|
|
Utilis\Sorter\Teams::sortRound($this->teams, $this, $ordering); |
129
|
|
|
return $this->teams; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function splitTeams(Group ...$groups) { |
133
|
|
|
|
134
|
|
|
if (count($groups) === 0) $groups = $this->getGroups(); |
135
|
|
|
|
136
|
|
|
$teams = $this->getTeams(); |
137
|
|
|
shuffle($teams); |
138
|
|
|
|
139
|
|
|
while (count($teams) > 0) { |
140
|
|
|
foreach ($groups as $group) { |
141
|
|
|
if (count($teams) > 0) $group->addTeam(array_shift($teams)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function progress(bool $blank = false){ |
148
|
|
|
foreach ($this->groups as $group) { |
149
|
|
|
$group->progress($blank); |
150
|
|
|
} |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function simulate() { |
155
|
|
|
Utilis\Simulator::simulateRound($this); |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
public function resetGames() { |
159
|
|
|
foreach ($this->groups as $group) { |
160
|
|
|
$group->resetGames(); |
161
|
|
|
} |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|