|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Progression is a class that takes care of moving teams between groups. |
|
9
|
|
|
* |
|
10
|
|
|
* Progressions are used in order to move teams from one group to another. This can be used to progress the winning teams to semi-finals and finals round, but you can also progress teams between groups in different categories and even tournaments if you ever needed to. |
|
11
|
|
|
* Progressions use a similar syntax to php's array_slice() function or it can use defined filters. |
|
12
|
|
|
* |
|
13
|
|
|
* @package TournamentGenerator |
|
14
|
|
|
* @author Tomáš Vojík <[email protected]> |
|
15
|
|
|
* @since 0.1 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Progression |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var Group What group to progress from */ |
|
21
|
|
|
protected Group $from; |
|
22
|
|
|
/** @var Group What group to progress to */ |
|
23
|
|
|
protected Group $to; |
|
24
|
|
|
/** @var int Offset to start picking teams */ |
|
25
|
|
|
protected int $start; |
|
26
|
|
|
/** @var int|null Maximum number of teams to progress */ |
|
27
|
|
|
protected ?int $len; |
|
28
|
|
|
/** @var TeamFilter[] Filters to use */ |
|
29
|
|
|
protected array $filters = []; |
|
30
|
|
|
/** @var bool If the progression was already called */ |
|
31
|
|
|
protected bool $progressed = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Progression constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Group $from What group to progress from |
|
37
|
|
|
* @param Group $to What group to progress to |
|
38
|
|
|
* @param int $start Offset to start picking teams |
|
39
|
|
|
* @param int|null $len Maximum number of teams to progress |
|
40
|
|
|
*/ |
|
41
|
35 |
|
public function __construct(Group $from, Group $to, int $start = 0, ?int $len = null) { |
|
42
|
35 |
|
$this->from = $from; |
|
43
|
35 |
|
$this->to = $to; |
|
44
|
35 |
|
$this->start = $start; |
|
45
|
35 |
|
$this->len = $len; |
|
46
|
35 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets a description |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
17 |
|
public function __toString() { |
|
54
|
17 |
|
return 'Team from '.$this->from; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Adds progression's filters |
|
59
|
|
|
* |
|
60
|
|
|
* @param TeamFilter[] $filters |
|
61
|
|
|
* |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function addFilter(TeamFilter ...$filters) : Progression { |
|
65
|
8 |
|
foreach ($filters as $filter) { |
|
66
|
8 |
|
$this->filters[] = $filter; |
|
67
|
|
|
} |
|
68
|
8 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Progress the teams using set rules |
|
73
|
|
|
* |
|
74
|
|
|
* @param bool $blank If true -> do not move the real team objects, but create new dummy teams |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
* @throws Exception |
|
78
|
|
|
*/ |
|
79
|
33 |
|
public function progress(bool $blank = false) : Progression { |
|
80
|
33 |
|
if ($this->progressed) { |
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
33 |
|
if ($blank) { |
|
85
|
17 |
|
$teams = $this->from->isPlayed() ? $this->from->sortTeams(null, $this->filters) : $this->from->simulate($this->filters); |
|
86
|
|
|
} |
|
87
|
|
|
else { |
|
88
|
16 |
|
$teams = $this->from->sortTeams(null, $this->filters); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
33 |
|
if ($this->start !== 0 || $this->len !== null) { |
|
92
|
29 |
|
$next = array_splice($teams, $this->start, ($this->len ?? count($teams))); |
|
93
|
|
|
} |
|
94
|
|
|
else { |
|
95
|
5 |
|
$next = $teams; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
33 |
|
$i = 1; |
|
99
|
|
|
|
|
100
|
33 |
|
foreach ($next as $team) { |
|
101
|
33 |
|
if ($blank) { |
|
102
|
17 |
|
$this->to->addTeam(new BlankTeam($this.' - '.$i++, $team, $this->from, $this)); |
|
103
|
|
|
} |
|
104
|
|
|
else { |
|
105
|
16 |
|
$team->addPoints($this->from->getProgressPoints()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
33 |
|
$this->from->addProgressed(...$next); |
|
110
|
33 |
|
if (!$blank) { |
|
111
|
16 |
|
$this->to->addTeam(...$next); |
|
112
|
|
|
} |
|
113
|
33 |
|
$this->progressed = true; |
|
114
|
33 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Reset progression |
|
119
|
|
|
* |
|
120
|
|
|
* @warning This does not remove the teams from the progressed groups! |
|
121
|
|
|
* |
|
122
|
|
|
* @return $this |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function reset() : Progression { |
|
125
|
1 |
|
$this->progressed = false; |
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return Group |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function getFrom() : Group { |
|
133
|
3 |
|
return $this->from; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Group |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function getTo() : Group { |
|
140
|
3 |
|
return $this->to; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return int |
|
145
|
|
|
*/ |
|
146
|
3 |
|
public function getStart() : int { |
|
147
|
3 |
|
return $this->start; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return int|null |
|
152
|
|
|
*/ |
|
153
|
3 |
|
public function getLen() : ?int { |
|
154
|
3 |
|
return $this->len; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return TeamFilter[] |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function getFilters() : array { |
|
161
|
3 |
|
return $this->filters; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sets progression's filters |
|
166
|
|
|
* |
|
167
|
|
|
* @param TeamFilter[] $filters |
|
168
|
|
|
* |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function setFilters(array $filters) : Progression { |
|
172
|
1 |
|
$this->filters = $filters; |
|
173
|
1 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
3 |
|
public function isProgressed() : bool { |
|
180
|
3 |
|
return $this->progressed; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param bool $progressed |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setProgressed(bool $progressed) : void { |
|
187
|
|
|
$this->progressed = $progressed; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|