1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\TreeGen; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Xoco70\LaravelTournaments\Models\DirectEliminationFight; |
7
|
|
|
|
8
|
|
|
abstract class PlayOffTreeGen extends TreeGen |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Calculate the Byes need to fill the Championship Tree. |
12
|
|
|
* |
13
|
|
|
* @param $fighters |
14
|
|
|
* |
15
|
|
|
* @return Collection |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
protected function getByeGroup($fighters) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$fighterCount = $fighters->count(); |
20
|
|
|
$treeSize = $this->getTreeSize($fighterCount, 2); |
21
|
|
|
$byeCount = $treeSize - $fighterCount; |
22
|
|
|
|
23
|
|
|
return $this->createByeGroup($byeCount); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create empty groups for PlayOff Round. |
28
|
|
|
* |
29
|
|
|
* @param $numFighters |
30
|
|
|
*/ |
31
|
|
|
protected function pushEmptyGroupsToTree($numFighters) |
32
|
|
|
{ |
33
|
|
|
//TODO CHANGE HERE TOO |
34
|
|
|
$numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2; |
35
|
|
|
// We calculate how much rounds we will have |
36
|
|
|
$numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4) |
37
|
|
|
$this->pushGroups($numRounds, $numFightersElim); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Chunk Fighters into groups for fighting, and optionnaly shuffle. |
42
|
|
|
* |
43
|
|
|
* @param $fightersByEntity |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
protected function chunkAndShuffle(Collection $fightersByEntity) |
48
|
|
|
{ |
49
|
|
|
if ($this->championship->hasPreliminary()) { |
50
|
|
|
$fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize); |
51
|
|
|
if (!app()->runningUnitTests()) { |
52
|
|
|
$fightersGroup = $fightersGroup->shuffle(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $fightersGroup; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $fightersByEntity->chunk($fightersByEntity->count()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generate First Round Fights. |
63
|
|
|
*/ |
64
|
|
|
public function generateFights() |
65
|
|
|
{ |
66
|
|
|
parent::destroyPreviousFights($this->championship); |
|
|
|
|
67
|
|
|
DirectEliminationFight::saveFights($this->championship); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Save Groups with their parent info. |
72
|
|
|
* |
73
|
|
|
* @param int $numRounds |
74
|
|
|
* @param $numFightersElim |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
protected function pushGroups($numRounds, $numFightersElim) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) { |
79
|
|
|
// From last match to first match |
80
|
|
|
$maxMatches = ($numFightersElim / pow(2, $roundNumber)); |
81
|
|
|
for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { |
82
|
|
|
$fighters = $this->createByeGroup(2); |
83
|
|
|
$group = $this->saveGroup($matchNumber, $roundNumber, null); |
84
|
|
|
$this->syncGroup($group, $fighters); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return number of rounds for the tree based on fighter count. |
91
|
|
|
* |
92
|
|
|
* @param $numFighters |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
protected function getNumRounds($numFighters) |
97
|
|
|
{ |
98
|
|
|
return intval(log($numFighters, 2)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function generateAllTrees() |
102
|
|
|
{ |
103
|
|
|
// TODO This is limiting Playoff only have 1 area |
104
|
|
|
$fighters = $this->championship->fighters; |
105
|
|
|
// This means that when playoff, we only generate 1 group |
106
|
|
|
// Could be better, for now it is ok |
107
|
|
|
$this->generateGroupsForRound($fighters, 1); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.