|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments\TreeGen; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\Facades\App; |
|
7
|
|
|
use Xoco70\KendoTournaments\Models\Championship; |
|
8
|
|
|
use Xoco70\KendoTournaments\Models\DirectEliminationFight; |
|
9
|
|
|
use Xoco70\KendoTournaments\Models\Fight; |
|
10
|
|
|
|
|
11
|
|
|
class DirectEliminationTreeGen extends TreeGen |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Calculate the Byes need to fill the Championship Tree. |
|
16
|
|
|
* @param $fighters |
|
17
|
|
|
* @return Collection |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function getByeGroup($fighters) |
|
20
|
|
|
{ |
|
21
|
|
|
$fighterCount = $fighters->count(); |
|
22
|
|
|
$treeSize = $this->getTreeSize($fighterCount, 2); |
|
23
|
|
|
$byeCount = $treeSize - $fighterCount; |
|
24
|
|
|
|
|
25
|
|
|
return $this->createNullsGroup($byeCount); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create empty groups for direct Elimination Tree |
|
31
|
|
|
* @param $numFightersElim |
|
32
|
|
|
*/ |
|
33
|
|
|
public function pushEmptyGroupsToTree($numFightersElim) |
|
34
|
|
|
{ |
|
35
|
|
|
// We calculate how much rounds we will have |
|
36
|
|
|
$numRounds = intval(log($numFightersElim, 2)); |
|
37
|
|
|
$this->pushGroups($numRounds, $numFightersElim); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Chunk Fighters into groups for fighting, and optionnaly shuffle |
|
42
|
|
|
* @param $fightersByEntity |
|
43
|
|
|
* @return Collection|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function chunkAndShuffle($round = null, $fightersByEntity) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$fightersGroup = null; |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$fightersGroup = $fightersByEntity->chunk(2); |
|
50
|
|
|
if (!App::runningUnitTests()) { |
|
51
|
|
|
$fightersGroup = $fightersGroup->shuffle(); |
|
52
|
|
|
} |
|
53
|
|
|
return $fightersGroup; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Generate First Round Fights |
|
59
|
|
|
*/ |
|
60
|
|
|
public function generateFights() |
|
61
|
|
|
{ |
|
62
|
|
|
parent::destroyPreviousFights($this->championship); |
|
|
|
|
|
|
63
|
|
|
DirectEliminationFight::saveFights($this->championship); |
|
64
|
|
|
} |
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function generateNextRoundsFights() |
|
69
|
|
|
{ |
|
70
|
|
|
$fightersCount = $this->championship->competitors->count() + $this->championship->teams->count(); |
|
71
|
|
|
$maxRounds = intval(ceil(log($fightersCount, 2))); |
|
72
|
|
|
for ($numRound = 1; $numRound < $maxRounds; $numRound++) { |
|
73
|
|
|
$fightsByRound = $this->championship->fightsByRound($numRound)->with('group.parent', 'group.children')->get(); |
|
74
|
|
|
$this->updateParentFight($this->championship, $fightsByRound); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Championship $championship |
|
81
|
|
|
* @param $fightsByRound |
|
82
|
|
|
*/ |
|
83
|
|
|
private function updateParentFight(Championship $championship, $fightsByRound) |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($fightsByRound as $fight) { |
|
86
|
|
|
$parentGroup = $fight->group->parent; |
|
87
|
|
|
if ($parentGroup == null) break; |
|
88
|
|
|
$parentFight = $parentGroup->fights->get(0); //TODO This Might change when extending to Preliminary |
|
89
|
|
|
|
|
90
|
|
|
// IN this $fight, is c1 or c2 has the info? |
|
91
|
|
|
if ($championship->isDirectEliminationType()) { |
|
92
|
|
|
// determine whether c1 or c2 must be updated |
|
93
|
|
|
$this->chooseAndUpdateParentFight($fight, $parentFight); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $fight |
|
100
|
|
|
* @param $parentFight |
|
101
|
|
|
*/ |
|
102
|
|
|
private function chooseAndUpdateParentFight($fight, $parentFight) |
|
103
|
|
|
{ |
|
104
|
|
|
$fighterToUpdate = $fight->getParentFighterToUpdate(); |
|
105
|
|
|
$valueToUpdate = $fight->getValueToUpdate(); |
|
106
|
|
|
// we need to know if the child has empty fighters, is this BYE or undetermined |
|
107
|
|
|
if ($fight->hasDeterminedParent() && $valueToUpdate != null) { |
|
108
|
|
|
$parentFight->$fighterToUpdate = $fight->$valueToUpdate; |
|
109
|
|
|
$parentFight->save(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the parent field that need to be updated |
|
116
|
|
|
* @return null|string |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function getParentFighterToUpdate() |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
$childrenGroup = $this->group->parent->children; |
|
|
|
|
|
|
121
|
|
|
foreach ($childrenGroup as $key => $children) { |
|
122
|
|
|
$childFight = $children->fights->get(0); |
|
123
|
|
|
if ($childFight->id == $this->id) { |
|
|
|
|
|
|
124
|
|
|
if ($key % 2 == 0) { |
|
125
|
|
|
return "c1"; |
|
126
|
|
|
} |
|
127
|
|
|
if ($key % 2 == 1) { |
|
128
|
|
|
return "c2"; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* In the original fight ( child ) return the field that contains data to copy to parent |
|
137
|
|
|
* @return null|string |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
public function getValueToUpdate() |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
if ($this->c1 != null && $this->c2 != null) { |
|
|
|
|
|
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
if ($this->c1 != null) { |
|
145
|
|
|
return "c1"; |
|
146
|
|
|
} |
|
147
|
|
|
if ($this->c2 != null) { |
|
148
|
|
|
return "c2"; |
|
149
|
|
|
} |
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Check if we are able to fill the parent fight or not |
|
155
|
|
|
* If one of the children has c1 x c2, then we must wait to fill parent |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function hasDeterminedParent() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
if ($this->group->has2Fighters()) return true; |
|
162
|
|
|
foreach ($this->group->children as $child) { |
|
163
|
|
|
$fight = $child->fights->get(0); |
|
164
|
|
|
if ($fight->has2Fighters()) return false; |
|
165
|
|
|
} |
|
166
|
|
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Save Groups with their parent info |
|
171
|
|
|
* @param integer $numRounds |
|
172
|
|
|
* @param $numFightersElim |
|
173
|
|
|
*/ |
|
174
|
|
View Code Duplication |
protected function pushGroups($numRounds, $numFightersElim) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) { |
|
177
|
|
|
// From last match to first match |
|
178
|
|
|
$maxMatches = ($numFightersElim / pow(2, $roundNumber)); |
|
179
|
|
|
|
|
180
|
|
|
for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { |
|
181
|
|
|
$fighters = $this->createByeGroup(2); |
|
182
|
|
|
$group = $this->saveGroup(1, $matchNumber, $roundNumber, null); |
|
183
|
|
|
$this->syncGroup($group, $fighters); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Return number of rounds for the tree based on fighter count |
|
190
|
|
|
* @param $numFighters |
|
191
|
|
|
* @return int |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getNumRounds($numFighters){ |
|
194
|
|
|
return intval(log($numFighters, 2)); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.