|
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 = $this->getNumRounds($numFightersElim); |
|
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
|
|
|
* Save Groups with their parent info |
|
69
|
|
|
* @param integer $numRounds |
|
70
|
|
|
* @param $numFightersElim |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
protected function pushGroups($numRounds, $numFightersElim) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) { |
|
75
|
|
|
// From last match to first match |
|
76
|
|
|
$maxMatches = ($numFightersElim / pow(2, $roundNumber)); |
|
77
|
|
|
|
|
78
|
|
|
for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { |
|
79
|
|
|
$fighters = $this->createByeGroup(2); |
|
80
|
|
|
$group = $this->saveGroup(1, $matchNumber, $roundNumber, null); |
|
81
|
|
|
$this->syncGroup($group, $fighters); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return number of rounds for the tree based on fighter count |
|
88
|
|
|
* @param $numFighters |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getNumRounds($numFighters) |
|
92
|
|
|
{ |
|
93
|
|
|
return intval(log($numFighters, 2)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.