1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\TreeGen; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Xoco70\LaravelTournaments\Contracts\TreeGenerable; |
7
|
|
|
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException; |
8
|
|
|
use Xoco70\LaravelTournaments\Models\Championship; |
9
|
|
|
use Xoco70\LaravelTournaments\Models\Fight; |
10
|
|
|
use Xoco70\LaravelTournaments\Models\FightersGroup; |
11
|
|
|
|
12
|
|
|
abstract class TreeGen implements TreeGenerable |
13
|
|
|
{ |
14
|
|
|
protected $groupBy; |
15
|
|
|
protected $tree; |
16
|
|
|
public $championship; |
17
|
|
|
public $settings; |
18
|
|
|
protected $numFighters; |
19
|
|
|
|
20
|
|
|
abstract protected function generateAllTrees(); |
21
|
|
|
|
22
|
|
|
abstract protected function generateFights(); |
23
|
|
|
|
24
|
|
|
abstract protected function addFighterToGroup(FightersGroup $group, $fighter, $fighterToUpdate); |
25
|
|
|
|
26
|
|
|
abstract protected function getByeGroup($fighters); |
27
|
|
|
|
28
|
|
|
abstract protected function getNumRounds($fightersCount); |
29
|
|
|
|
30
|
|
|
abstract protected function chunk(Collection $fightersByEntity); |
31
|
|
|
|
32
|
|
|
abstract protected function syncGroup(FightersGroup $group, $fighters); |
33
|
|
|
|
34
|
|
|
abstract protected function generateGroupsForRound(Collection $fightersByArea, $round); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Championship $championship |
38
|
|
|
* @param $groupBy |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Championship $championship, $groupBy) |
41
|
|
|
{ |
42
|
|
|
$this->championship = $championship; |
43
|
|
|
$this->groupBy = $groupBy; |
44
|
|
|
$this->settings = $championship->getSettings(); |
45
|
|
|
$this->tree = new Collection(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generate tree groups for a championship. |
50
|
|
|
* |
51
|
|
|
* @throws TreeGenerationException |
52
|
|
|
*/ |
53
|
|
|
public function run() |
54
|
|
|
{ |
55
|
|
|
$this->championship->fightersGroups()->delete(); |
56
|
|
|
$this->generateAllTrees(); |
57
|
|
|
$this->generateAllFights(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get Competitor's list ordered by entities |
64
|
|
|
* Countries for Internation Tournament, State for a National Tournament, etc. |
65
|
|
|
* |
66
|
|
|
* @param $fighters |
67
|
|
|
* |
68
|
|
|
* @return Collection |
69
|
|
|
*/ |
70
|
|
|
private function getFightersByEntity($fighters): Collection |
71
|
|
|
{ |
72
|
|
|
// Right now, we are treating users and teams as equals. |
73
|
|
|
// It doesn't matter right now, because we only need name attribute which is common to both models |
74
|
|
|
|
75
|
|
|
// $this->groupBy contains federation_id, association_id, club_id, etc. |
76
|
|
|
if (($this->groupBy) != null) { |
77
|
|
|
return $fighters->groupBy($this->groupBy); // Collection of Collection |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $fighters->chunk(1); // Collection of Collection |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the size the first round will have. |
85
|
|
|
* |
86
|
|
|
* @param $fighterCount |
87
|
|
|
* @param $groupSize |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
protected function getTreeSize($fighterCount, $groupSize) |
92
|
|
|
{ |
93
|
|
|
$squareMultiplied = collect([1, 2, 4, 8, 16, 32, 64]) |
94
|
|
|
->map(function ($item) use ($groupSize) { |
95
|
|
|
return $item * $groupSize; |
96
|
|
|
}); // [4, 8, 16, 32, 64,...] |
|
|
|
|
97
|
|
|
|
98
|
|
|
foreach ($squareMultiplied as $limit) { |
99
|
|
|
if ($fighterCount <= $limit) { |
100
|
|
|
$treeSize = $limit; |
101
|
|
|
$numAreas = $this->settings->fightingAreas; |
102
|
|
|
$fighterCountPerArea = $treeSize / $numAreas; |
103
|
|
|
if ($fighterCountPerArea < $groupSize) { |
104
|
|
|
$treeSize = $treeSize * $numAreas; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $treeSize; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return 64 * $groupSize; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $order |
118
|
|
|
* @param $round |
119
|
|
|
* @param $parent |
120
|
|
|
* |
121
|
|
|
* @return FightersGroup |
122
|
|
|
*/ |
123
|
|
|
protected function saveGroup($order, $round, $parent): FightersGroup |
124
|
|
|
{ |
125
|
|
|
$group = new FightersGroup(); |
126
|
|
|
$this->championship->isSingleEliminationType() |
127
|
|
|
? $group->area = $this->getNumArea($round, $order) |
|
|
|
|
128
|
|
|
: $group->area = 1; // Area limited to 1 in playoff |
|
|
|
|
129
|
|
|
|
130
|
|
|
$group->order = $order; |
|
|
|
|
131
|
|
|
$group->round = $round; |
|
|
|
|
132
|
|
|
$group->championship_id = $this->championship->id; |
|
|
|
|
133
|
|
|
if ($parent != null) { |
134
|
|
|
$group->parent_id = $parent->id; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
$group->save(); |
137
|
|
|
|
138
|
|
|
return $group; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $groupSize |
143
|
|
|
* |
144
|
|
|
* @return Collection |
145
|
|
|
*/ |
146
|
|
|
public function createByeGroup($groupSize): Collection |
147
|
|
|
{ |
148
|
|
|
$byeFighter = $this->createByeFighter(); |
|
|
|
|
149
|
|
|
$group = new Collection(); |
150
|
|
|
for ($i = 0; $i < $groupSize; $i++) { |
151
|
|
|
$group->push($byeFighter); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $group; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get All Groups on previous round. |
161
|
|
|
* |
162
|
|
|
* @param $currentRound |
163
|
|
|
* |
164
|
|
|
* @return Collection |
165
|
|
|
*/ |
166
|
|
|
private function getPreviousRound($currentRound) |
167
|
|
|
{ |
168
|
|
|
$previousRound = $this->championship->groupsByRound($currentRound + 1)->get(); |
169
|
|
|
|
170
|
|
|
return $previousRound; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the next group on the right ( parent ), final round being the ancestor. |
175
|
|
|
* |
176
|
|
|
* @param $matchNumber |
177
|
|
|
* @param Collection $previousRound |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
private function getParentGroup($matchNumber, $previousRound) |
182
|
|
|
{ |
183
|
|
|
$parentIndex = intval(($matchNumber + 1) / 2); |
184
|
|
|
$parent = $previousRound->get($parentIndex - 1); |
185
|
|
|
|
186
|
|
|
return $parent; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Group Fighters by area. |
191
|
|
|
* Here is where we fill with empty fighters |
192
|
|
|
* |
193
|
|
|
* @throws TreeGenerationException |
194
|
|
|
* |
195
|
|
|
* @return Collection |
196
|
|
|
*/ |
197
|
|
|
protected function getFightersByArea() |
198
|
|
|
{ |
199
|
|
|
$areas = $this->settings->fightingAreas; |
200
|
|
|
$fighters = $this->getFighters(); // Get Competitor or Team Objects |
|
|
|
|
201
|
|
|
$fighterByEntity = $this->getFightersByEntity($fighters); // Chunk it by entities (Fede, Assoc, Club,...) |
202
|
|
|
$fightersWithBye = $this->adjustFightersGroupWithByes($fighters, $fighterByEntity); // Fill with Byes |
|
|
|
|
203
|
|
|
return $fightersWithBye->chunk(count($fightersWithBye) / $areas); // Chunk user by areas |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Logically build the tree ( attach a parent to every child for nestedSet Navigation ) |
208
|
|
|
* |
209
|
|
|
* @param $numFighters |
210
|
|
|
*/ |
211
|
|
|
protected function addParentToChildren($numFighters) |
212
|
|
|
{ |
213
|
|
|
$numRounds = $this->getNumRounds($numFighters); |
214
|
|
|
$groupsDesc = $this->championship |
215
|
|
|
->fightersGroups() |
216
|
|
|
->where('round', '<', $numRounds) |
217
|
|
|
->orderByDesc('id')->get(); |
218
|
|
|
|
219
|
|
|
$groupsDescByRound = $groupsDesc->groupBy('round'); |
220
|
|
|
|
221
|
|
|
foreach ($groupsDescByRound as $round => $groups) { |
222
|
|
|
$previousRound = $this->getPreviousRound($round); |
223
|
|
|
foreach ($groups->reverse()->values() as $matchNumber => $group) { |
224
|
|
|
$parent = $this->getParentGroup($matchNumber + 1, $previousRound); |
225
|
|
|
$group->parent_id = $parent->id; |
226
|
|
|
$group->save(); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Destroy Previous Fights for demo. |
234
|
|
|
*/ |
235
|
|
|
protected function destroyPreviousFights() |
236
|
|
|
{ |
237
|
|
|
// Delete previous fight for this championship |
238
|
|
|
$arrGroupsId = $this->championship->fightersGroups()->get()->pluck('id'); |
239
|
|
|
if (count($arrGroupsId) > 0) { |
240
|
|
|
Fight::destroy($arrGroupsId); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generate Fights for next rounds. |
246
|
|
|
*/ |
247
|
|
|
public function generateNextRoundsFights() |
248
|
|
|
{ |
249
|
|
|
$fightersCount = $this->championship->competitors->count() + $this->championship->teams->count(); |
|
|
|
|
250
|
|
|
$maxRounds = $this->getNumRounds($fightersCount); |
251
|
|
|
for ($numRound = 1; $numRound < $maxRounds; $numRound++) { |
252
|
|
|
$groupsByRound = $this->championship->fightersGroups()->where('round', $numRound)->with('parent', 'children')->get(); |
253
|
|
|
$this->updateParentFight($groupsByRound); // should be groupsByRound |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param $groupsByRound |
259
|
|
|
*/ |
260
|
|
|
protected function updateParentFight($groupsByRound) |
261
|
|
|
{ |
262
|
|
|
foreach ($groupsByRound as $keyGroup => $group) { |
263
|
|
|
$parentGroup = $group->parent; |
264
|
|
|
if ($parentGroup == null) { |
265
|
|
|
break; |
266
|
|
|
} |
267
|
|
|
$parentFight = $parentGroup->fights->get(0); |
268
|
|
|
|
269
|
|
|
// determine whether c1 or c2 must be updated |
270
|
|
|
$this->chooseAndUpdateParentFight($keyGroup, $group, $parentFight); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param $group |
276
|
|
|
* @param $parentFight |
277
|
|
|
*/ |
278
|
|
|
protected function chooseAndUpdateParentFight($keyGroup, FightersGroup $group, Fight $parentFight) |
279
|
|
|
{ |
280
|
|
|
// we need to know if the child has empty fighters, is this BYE or undetermined |
281
|
|
|
if ($group->hasDeterminedParent()) { |
282
|
|
|
$valueToUpdate = $group->getValueToUpdate(); // This should be OK |
283
|
|
|
if ($valueToUpdate != null) { |
284
|
|
|
$fighterToUpdate = $group->getParentFighterToUpdate($keyGroup); |
285
|
|
|
$parentFight->$fighterToUpdate = $valueToUpdate; |
286
|
|
|
$parentFight->save(); |
287
|
|
|
// Add fighter to pivot table |
288
|
|
|
$parentGroup = $parentFight->group; |
|
|
|
|
289
|
|
|
|
290
|
|
|
$fighter = $this->getFighter($valueToUpdate); |
|
|
|
|
291
|
|
|
$this->addFighterToGroup($parentGroup, $fighter, $fighterToUpdate); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Calculate the area of the group ( group is still not created ). |
298
|
|
|
* |
299
|
|
|
* @param $round |
300
|
|
|
* @param $order |
301
|
|
|
* |
302
|
|
|
* @return int |
303
|
|
|
*/ |
304
|
|
|
protected function getNumArea($round, $order) |
305
|
|
|
{ |
306
|
|
|
$totalAreas = $this->settings->fightingAreas; |
307
|
|
|
$numFighters = $this->championship->fighters->count(); // 4 |
|
|
|
|
308
|
|
|
$numGroups = $this->getTreeSize($numFighters, $this->championship->getGroupSize()) / $this->championship->getGroupSize(); // 1 -> 1 |
|
|
|
|
309
|
|
|
|
310
|
|
|
$areaSize = $numGroups / ($totalAreas * pow(2, $round - 1)); |
311
|
|
|
|
312
|
|
|
$numArea = intval(ceil($order / $areaSize)); // if round == 4, and second match 2/2 = 1 BAD |
313
|
|
|
return $numArea; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
protected function generateAllFights() |
317
|
|
|
{ |
318
|
|
|
$this->generateFights(); // Abstract |
|
|
|
|
319
|
|
|
$this->generateNextRoundsFights(); |
320
|
|
|
Fight::generateFightsId($this->championship); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.