|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TournamentGenerator\Utilis\Sorter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
*Tournament generator sorter for games |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
// WORKS BY COMPARING AVAILABLE GAMES BY THEIR TEAMS |
|
10
|
|
|
// TEAMS FROM LAST 3 GAMES |
|
11
|
|
|
// 1 = PLAYED ONLY 3RD GAME FROM END |
|
12
|
|
|
// 2 = PLAYED ONLY 2ND GAME FROM END |
|
13
|
|
|
// 3 = PLAYED 3RD AND 2ND GAME FROM END |
|
14
|
|
|
// 4 = PLAYED ONLY THE LAST GAME |
|
15
|
|
|
// 5 = PLAYED 3RD AND 1ST GAME FROM END |
|
16
|
|
|
// 6 = PLAYED 2ND ANS 1ST GAME FROM END |
|
17
|
|
|
// 7 = PLAYED ALL 3 LAST GAMES |
|
18
|
|
|
class Games |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
private $group; |
|
22
|
|
|
private $games = []; |
|
23
|
|
|
/** |
|
24
|
|
|
* Orderes games from group |
|
25
|
|
|
* @param TournamentGenerator\Group $group |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
function __construct(\TournamentGenerator\Group $group) { |
|
28
|
|
|
$this->group = $group; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Orderes games from group |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function orderGames() { |
|
36
|
|
|
|
|
37
|
|
|
$games = $this->group->getGames(); |
|
38
|
|
|
|
|
39
|
|
|
if (count($games) <= 4) return $games; |
|
40
|
|
|
|
|
41
|
|
|
$this->games = []; |
|
42
|
|
|
|
|
43
|
|
|
$teams = []; |
|
44
|
|
|
foreach ($this->group->getTeams() as $team) { $teams[$team->id] = 0; } |
|
45
|
|
|
|
|
46
|
|
|
$this->moveCalculatedGames(array_shift($games), $teams); |
|
47
|
|
|
|
|
48
|
|
|
while (count($games) > 0) { |
|
49
|
|
|
// CYCLE 1 |
|
50
|
|
|
// TEAM WHICH DIDN'T PLAY IN LAST GAME (< 4) |
|
51
|
|
|
if ($this->cycle1($games, $teams)) continue; |
|
52
|
|
|
|
|
53
|
|
|
// CYCLE 2 |
|
54
|
|
|
// NOT TEAM WHICH PLAYED IN LAST TWO GAMES (NOT 6 or 7) |
|
55
|
|
|
if ($this->cycle2($games, $teams)) continue; |
|
56
|
|
|
|
|
57
|
|
|
// CYCLE 3 |
|
58
|
|
|
// NOT TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7) |
|
59
|
|
|
// TEAMS THAT DIDN'T PLAY IN LAST GAME WILL PLAY THIS GAME (< 4) |
|
60
|
|
|
if ($this->cycle3($games, $teams)) continue; |
|
61
|
|
|
|
|
62
|
|
|
// CYCLE 4 |
|
63
|
|
|
// NOT TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7) |
|
64
|
|
|
if ($this->cycle4($games, $teams)) continue; |
|
65
|
|
|
|
|
66
|
|
|
// CYCLE 5 |
|
67
|
|
|
// TEAMS THAT DIDN'T PLAY IN LAST GAME WILL PLAY THIS GAME (< 4) |
|
68
|
|
|
if ($this->cycle5($games, $teams)) continue; |
|
69
|
|
|
|
|
70
|
|
|
// CYCLE 6 |
|
71
|
|
|
// FIRST AVAILABLE GAME |
|
72
|
|
|
$this->moveCalculatedGames(array_shift($games),$teams); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->games; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// TEAM WHICH DIDN'T PLAY IN LAST GAME (< 4) |
|
79
|
|
|
private function cycle1(array &$games, array &$teams) { |
|
80
|
|
|
$found = false; |
|
81
|
|
|
foreach ($games as $key => $game) { |
|
82
|
|
|
if ($this->orderCheckTeamsVal($game, $teams, [4,5,6,7])) { |
|
83
|
|
|
$this->moveCalculatedGames($game,$teams); |
|
84
|
|
|
unset($games[$key]); |
|
85
|
|
|
$found = true; |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return $found; |
|
90
|
|
|
} |
|
91
|
|
|
// NOT TEAM WHICH PLAYED IN LAST TWO GAMES (NOT 6 or 7) |
|
92
|
|
|
private function cycle2(array &$games, array &$teams) { |
|
93
|
|
|
$found = false; |
|
94
|
|
|
foreach ($games as $key => $game) { |
|
95
|
|
|
if ($this->orderCheckTeamsVal($game, $teams, [6,7])) { |
|
96
|
|
|
$this->moveCalculatedGames($game,$teams); |
|
97
|
|
|
unset($games[$key]); |
|
98
|
|
|
$found = true; |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
return $found; |
|
103
|
|
|
} |
|
104
|
|
|
// NOT TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7) |
|
105
|
|
|
// TEAMS THAT DIDN'T PLAY IN LAST GAME WILL PLAY THIS GAME (< 4) |
|
106
|
|
|
private function cycle3(array &$games, array &$teams) { |
|
107
|
|
|
$found = false; |
|
108
|
|
|
foreach ($games as $key => $game) { |
|
109
|
|
|
if ($this->orderCheckTeamsVal($game, $teams, [7], [1,2,3])) { |
|
110
|
|
|
$this->moveCalculatedGames($game,$teams); |
|
111
|
|
|
unset($games[$key]); |
|
112
|
|
|
$found = true; |
|
113
|
|
|
break; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return $found; |
|
117
|
|
|
} |
|
118
|
|
|
// NOT TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7) |
|
119
|
|
|
private function cycle4(array &$games, array &$teams) { |
|
120
|
|
|
$found = false; |
|
121
|
|
|
foreach ($games as $key => $game) { |
|
122
|
|
|
if ($this->orderCheckTeamsVal($game, $teams, [7])) { |
|
123
|
|
|
$this->moveCalculatedGames($game,$teams); |
|
124
|
|
|
unset($games[$key]); |
|
125
|
|
|
$found = true; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
return $found; |
|
130
|
|
|
} |
|
131
|
|
|
// TEAMS THAT DIDN'T PLAY IN LAST GAME WILL PLAY THIS GAME (< 4) |
|
132
|
|
|
private function cycle5(array &$games, array &$teams) { |
|
133
|
|
|
$found = false; |
|
134
|
|
|
foreach ($games as $key => $game) { |
|
135
|
|
|
if ($this->orderCheckTeamsVal($game, $teams, [], [1,2,3])) { |
|
136
|
|
|
$this->moveCalculatedGames($game,$teams); |
|
137
|
|
|
unset($games[$key]); |
|
138
|
|
|
$found = true; |
|
139
|
|
|
break; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return $found; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private function moveCalculatedGames(Game $game, array &$teams) { |
|
146
|
|
|
|
|
147
|
|
|
$this->games[] = $game; |
|
148
|
|
|
|
|
149
|
|
|
foreach (end($this->games)->getTeamsIds() as $tid) { |
|
150
|
|
|
$teams[$tid] += 4; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (count($this->games) > 1) { |
|
154
|
|
|
foreach (prev($this->games)->getTeamsIds() as $tid) { |
|
155
|
|
|
$teams[$tid] -= 2; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
if (count($this->games) > 2) { |
|
159
|
|
|
foreach (prev($this->games)->getTeamsIds() as $tid) { |
|
160
|
|
|
$teams[$tid] -= 1; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
if (count($this->games) > 3) { |
|
164
|
|
|
foreach (prev($this->games)->getTeamsIds() as $tid) { |
|
165
|
|
|
$teams[$tid] -= 1; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $teams; |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
private function orderCheckTeamsVal(Game $game, array &$teams, array $checkVals, array $required = []) { |
|
173
|
|
|
|
|
174
|
|
|
$requiredTeams = array_filter($teams, function($a) use ($required) { return in_array($a, $required); }); |
|
175
|
|
|
|
|
176
|
|
|
foreach ($game->getTeamsIds() as $tid) { |
|
177
|
|
|
if (in_array($teams[$tid], $checkVals)) return false; |
|
178
|
|
|
if (isset($requiredTeams[$tid])) unset($requiredTeams[$tid]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (count($requiredTeams) > 0) return false; |
|
182
|
|
|
|
|
183
|
|
|
return true; |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|