1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace OSS\CoreBundle\FixtureGenerator;
|
4
|
|
|
|
5
|
|
|
class Generator
|
6
|
|
|
{
|
7
|
|
|
/**
|
8
|
|
|
* @var bool
|
9
|
|
|
*/
|
10
|
|
|
private $hasGhostTeam = false;
|
11
|
|
|
|
12
|
|
|
/**
|
13
|
|
|
* @var int
|
14
|
|
|
*/
|
15
|
|
|
private $numberOfTeams;
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* @var int
|
19
|
|
|
*/
|
20
|
|
|
private $currentHomeTeam;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* @var int
|
24
|
|
|
*/
|
25
|
|
|
private $currentAwayTeam;
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* @param int $numberOfTeams
|
29
|
|
|
*/
|
30
|
4 |
|
public function __construct($numberOfTeams)
|
31
|
|
|
{
|
32
|
4 |
|
$this->hasGhostTeam = $numberOfTeams % 2 > 0;
|
33
|
4 |
|
if ($numberOfTeams % 2 > 0) {
|
34
|
2 |
|
$numberOfTeams++;
|
35
|
2 |
|
}
|
36
|
4 |
|
$this->numberOfTeams = $numberOfTeams;
|
37
|
4 |
|
}
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* @return Fixture[]
|
41
|
|
|
*
|
42
|
|
|
* @throws \Exception
|
43
|
|
|
*/
|
44
|
2 |
|
public function createFixtures()
|
45
|
|
|
{
|
46
|
2 |
|
$fixtures = $this->createInitialFixtures();
|
47
|
2 |
|
$returnFixtures = $this->createReturnFixtures($fixtures);
|
48
|
|
|
|
49
|
2 |
|
return array_merge($fixtures, $returnFixtures);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* @param int $home
|
54
|
|
|
* @param int $away
|
55
|
|
|
* @param int $week
|
56
|
|
|
*
|
57
|
|
|
* @return Fixture
|
58
|
|
|
*/
|
59
|
2 |
|
private function createFixture($home, $away, $week)
|
60
|
|
|
{
|
61
|
2 |
|
$fixture = new Fixture();
|
62
|
2 |
|
$fixture->setTeamHome($home);
|
63
|
2 |
|
$fixture->setTeamAway($away);
|
64
|
2 |
|
$fixture->setWeek($week);
|
65
|
|
|
|
66
|
2 |
|
return $fixture;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* @return Fixture[]
|
71
|
|
|
*/
|
72
|
2 |
|
private function createInitialFixtures()
|
73
|
|
|
{
|
74
|
2 |
|
$fixtures = array();
|
75
|
2 |
|
for ($currentWeek = 1; $currentWeek <= $this->numberOfTeams - 1; $currentWeek++) {
|
76
|
2 |
|
$fixtures = array_merge($fixtures, $this->createFixturesForWeek($currentWeek));
|
77
|
2 |
|
}
|
78
|
|
|
|
79
|
2 |
|
return $fixtures;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @param int $currentWeek
|
84
|
|
|
*
|
85
|
|
|
* @return Fixture[]
|
86
|
|
|
*/
|
87
|
2 |
|
private function createFixturesForWeek($currentWeek)
|
88
|
|
|
{
|
89
|
2 |
|
$fixtures = array();
|
90
|
2 |
|
$this->currentHomeTeam = $this->numberOfTeams;
|
91
|
2 |
|
$this->currentAwayTeam = $currentWeek;
|
92
|
|
|
|
93
|
2 |
|
if (($currentWeek % 2) != 0) {
|
94
|
2 |
|
$this->toggleHomeAndAwayTeam();
|
95
|
2 |
|
}
|
96
|
|
|
|
97
|
2 |
|
if ($this->shallFixtureBeGenerated()) {
|
98
|
1 |
|
$fixtures[] = $this->createFixture($this->currentHomeTeam, $this->currentAwayTeam, $currentWeek);
|
99
|
1 |
|
}
|
100
|
|
|
|
101
|
2 |
|
for ($i = 1; $i <= (($this->numberOfTeams / 2) - 1); $i++) {
|
102
|
1 |
|
$fixtures = array_merge($fixtures, $this->createFixturesForMatchIndex($currentWeek, $i));
|
103
|
1 |
|
}
|
104
|
|
|
|
105
|
2 |
|
return $fixtures;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* @param int $week
|
110
|
|
|
* @param int $index
|
111
|
|
|
*
|
112
|
|
|
* @return Fixture[]
|
113
|
|
|
*/
|
114
|
1 |
|
private function createFixturesForMatchIndex($week, $index)
|
115
|
|
|
{
|
116
|
1 |
|
$fixtures = array();
|
117
|
1 |
|
$this->currentAwayTeam = $this->findCurrentAwayTeam($this->numberOfTeams, $week, $index);
|
118
|
1 |
|
$this->currentHomeTeam = $this->findCurrentHomeTeam($this->numberOfTeams, $week, $index);
|
119
|
|
|
|
120
|
1 |
|
if ($index % 2 == 0) {
|
121
|
|
|
$this->toggleHomeAndAwayTeam();
|
122
|
|
|
}
|
123
|
|
|
|
124
|
1 |
|
if ($this->shallFixtureBeGenerated()) {
|
125
|
1 |
|
$fixtures[] = $this->createFixture($this->currentHomeTeam, $this->currentAwayTeam, $week);
|
126
|
1 |
|
}
|
127
|
|
|
|
128
|
1 |
|
return $fixtures;
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* @param int $numberOfTeams
|
133
|
|
|
* @param int $week
|
134
|
|
|
* @param int $index
|
135
|
|
|
*
|
136
|
|
|
* @return int
|
137
|
|
|
*/
|
138
|
1 |
|
private function findCurrentHomeTeam($numberOfTeams, $week, $index)
|
139
|
|
|
{
|
140
|
1 |
|
return $this->wrapTeam(($week + $index) % ($numberOfTeams - 1));
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
/**
|
144
|
|
|
* @param int $numberOfTeams
|
145
|
|
|
* @param int $week
|
146
|
|
|
* @param int $index
|
147
|
|
|
*
|
148
|
|
|
* @return int
|
149
|
|
|
*/
|
150
|
1 |
|
private function findCurrentAwayTeam($numberOfTeams, $week, $index)
|
151
|
|
|
{
|
152
|
1 |
|
if ($week - $index < 0) {
|
153
|
|
|
$team = $numberOfTeams - 1 + $week - $index;
|
154
|
|
|
} else {
|
155
|
1 |
|
$team = $this->wrapTeam(($week - $index) % ($numberOfTeams - 1));
|
156
|
|
|
}
|
157
|
|
|
|
158
|
1 |
|
return $team;
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
/**
|
162
|
|
|
* @param int $team
|
163
|
|
|
*
|
164
|
|
|
* @return int
|
165
|
|
|
*/
|
166
|
2 |
|
public function wrapTeam($team)
|
167
|
|
|
{
|
168
|
2 |
|
if ($team == 0) {
|
169
|
2 |
|
$team = $this->numberOfTeams - 1;
|
170
|
2 |
|
}
|
171
|
|
|
|
172
|
2 |
|
return $team;
|
173
|
|
|
}
|
174
|
|
|
|
175
|
3 |
|
public function toggleHomeAndAwayTeam()
|
176
|
|
|
{
|
177
|
3 |
|
$temp = $this->currentAwayTeam;
|
178
|
3 |
|
$this->currentAwayTeam = $this->currentHomeTeam;
|
179
|
3 |
|
$this->currentHomeTeam = $temp;
|
180
|
3 |
|
}
|
181
|
|
|
|
182
|
|
|
/**
|
183
|
|
|
* @return bool
|
184
|
|
|
*/
|
185
|
1 |
|
private function isLastTeamCurrent()
|
186
|
|
|
{
|
187
|
1 |
|
return $this->numberOfTeams == $this->currentHomeTeam || $this->numberOfTeams == $this->currentAwayTeam;
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* @return bool
|
192
|
|
|
*/
|
193
|
2 |
|
private function shallFixtureBeGenerated()
|
194
|
|
|
{
|
195
|
2 |
|
return !$this->hasGhostTeam || !$this->isLastTeamCurrent();
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
/**
|
199
|
|
|
* @param Fixture[] $homeFixtures
|
200
|
|
|
*
|
201
|
|
|
* @return Fixture[]
|
202
|
|
|
*/
|
203
|
2 |
|
private function createReturnFixtures($homeFixtures)
|
204
|
|
|
{
|
205
|
2 |
|
$fixtures = array();
|
206
|
2 |
|
foreach ($homeFixtures as $homeFixture) {
|
207
|
2 |
|
if (!$this->hasGhostTeam || ($this->numberOfTeams != $homeFixture->getTeamAway() && $this->numberOfTeams != $homeFixture->getTeamHome())) {
|
208
|
2 |
|
$fixtures[] = $this->createFixture($homeFixture->getTeamAway(), $homeFixture->getTeamHome(), $homeFixture->getWeek() + $this->numberOfTeams - 1);
|
209
|
2 |
|
}
|
210
|
2 |
|
}
|
211
|
|
|
|
212
|
2 |
|
return $fixtures;
|
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* @return int
|
217
|
|
|
*/
|
218
|
1 |
|
public function getCurrentHomeTeam()
|
219
|
|
|
{
|
220
|
1 |
|
return $this->currentHomeTeam;
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/**
|
224
|
|
|
* @param int $currentHomeTeam
|
225
|
|
|
*/
|
226
|
1 |
|
public function setCurrentHomeTeam($currentHomeTeam)
|
227
|
|
|
{
|
228
|
1 |
|
$this->currentHomeTeam = $currentHomeTeam;
|
229
|
1 |
|
}
|
230
|
|
|
|
231
|
|
|
/**
|
232
|
|
|
* @return int
|
233
|
|
|
*/
|
234
|
1 |
|
public function getCurrentAwayTeam()
|
235
|
|
|
{
|
236
|
1 |
|
return $this->currentAwayTeam;
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* @param int $currentAwayTeam
|
241
|
|
|
*/
|
242
|
1 |
|
public function setCurrentAwayTeam($currentAwayTeam)
|
243
|
|
|
{
|
244
|
1 |
|
$this->currentAwayTeam = $currentAwayTeam;
|
245
|
1 |
|
}
|
246
|
|
|
}
|
247
|
|
|
|