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
|
61 |
|
public function __construct(Group $from, Group $to, int $start = 0, ?int $len = null) { |
42
|
61 |
|
$this->from = $from; |
43
|
61 |
|
$this->to = $to; |
44
|
61 |
|
$this->start = $start; |
45
|
61 |
|
$this->len = $len; |
46
|
61 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets a description |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
23 |
|
public function __toString() { |
54
|
23 |
|
return 'Team from '.$this->from; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Adds progression's filters |
59
|
|
|
* |
60
|
|
|
* @param TeamFilter[] $filters |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
22 |
|
public function addFilter(TeamFilter ...$filters) : Progression { |
65
|
22 |
|
foreach ($filters as $filter) { |
66
|
22 |
|
$this->filters[] = $filter; |
67
|
|
|
} |
68
|
22 |
|
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
|
53 |
|
public function progress(bool $blank = false) : Progression { |
80
|
53 |
|
if ($this->progressed) { |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
53 |
|
if ($blank) { |
85
|
23 |
|
$teams = $this->from->isPlayed() ? $this->from->sortTeams(null, $this->filters) : $this->from->simulate($this->filters); |
86
|
|
|
} |
87
|
|
|
else { |
88
|
30 |
|
$teams = $this->from->sortTeams(null, $this->filters); |
89
|
|
|
} |
90
|
|
|
|
91
|
53 |
|
if ($this->start !== 0 || $this->len !== null) { |
92
|
49 |
|
$next = array_splice($teams, $this->start, ($this->len ?? count($teams))); |
93
|
|
|
} |
94
|
|
|
else { |
95
|
5 |
|
$next = $teams; |
96
|
|
|
} |
97
|
|
|
|
98
|
53 |
|
$i = 1; |
99
|
|
|
|
100
|
53 |
|
foreach ($next as $team) { |
101
|
53 |
|
if ($blank) { |
102
|
23 |
|
$this->to->addTeam(new BlankTeam($this.' - '.$i++, $team, $this->from, $this)); |
103
|
|
|
} |
104
|
|
|
else { |
105
|
30 |
|
$team->addPoints($this->from->getProgressPoints()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
53 |
|
$this->from->addProgressed(...$next); |
110
|
53 |
|
if (!$blank) { |
111
|
30 |
|
$this->to->addTeam(...$next); |
112
|
|
|
} |
113
|
53 |
|
$this->progressed = true; |
114
|
53 |
|
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
|
5 |
|
public function getFrom() : Group { |
133
|
5 |
|
return $this->from; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Group |
138
|
|
|
*/ |
139
|
7 |
|
public function getTo() : Group { |
140
|
7 |
|
return $this->to; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
7 |
|
public function getStart() : int { |
147
|
7 |
|
return $this->start; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return int|null |
152
|
|
|
*/ |
153
|
7 |
|
public function getLen() : ?int { |
154
|
7 |
|
return $this->len; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return TeamFilter[] |
159
|
|
|
*/ |
160
|
7 |
|
public function getFilters() : array { |
161
|
7 |
|
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
|
8 |
|
public function isProgressed() : bool { |
180
|
8 |
|
return $this->progressed; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param bool $progressed |
185
|
|
|
*/ |
186
|
5 |
|
public function setProgressed(bool $progressed) : void { |
187
|
5 |
|
$this->progressed = $progressed; |
188
|
5 |
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|