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