1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\TreeGen; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException; |
7
|
|
|
use Xoco70\LaravelTournaments\Models\SingleEliminationFight; |
8
|
|
|
|
9
|
|
|
abstract class PlayOffTreeGen extends TreeGen |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Calculate the Byes need to fill the Championship Tree. |
13
|
|
|
* |
14
|
|
|
* @param $fighters |
15
|
|
|
* |
16
|
|
|
* @return Collection |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
protected function getByeGroup($fighters) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$fighterCount = $fighters->count(); |
21
|
|
|
$treeSize = $this->getTreeSize($fighterCount, 2); |
22
|
|
|
$byeCount = $treeSize - $fighterCount; |
23
|
|
|
|
24
|
|
|
return $this->createByeGroup($byeCount); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Chunk Fighters into groups for fighting, and optionnaly shuffle. |
29
|
|
|
* |
30
|
|
|
* @param $fightersByEntity |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
protected function chunk(Collection $fightersByEntity) |
35
|
|
|
{ |
36
|
|
|
if ($this->championship->hasPreliminary()) { |
37
|
|
|
$fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize); |
38
|
|
|
return $fightersGroup; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $fightersByEntity->chunk($fightersByEntity->count()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generate First Round Fights. |
46
|
|
|
*/ |
47
|
|
|
public function generateFights() |
48
|
|
|
{ |
49
|
|
|
parent::destroyPreviousFights($this->championship); |
|
|
|
|
50
|
|
|
SingleEliminationFight::saveFights($this->championship); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Save Groups with their parent info. |
55
|
|
|
* |
56
|
|
|
* @param int $numRounds |
57
|
|
|
* @param $numFightersElim |
58
|
|
|
*/ |
59
|
|
|
protected function pushGroups($numRounds, $numFightersElim) |
60
|
|
|
{ |
61
|
|
View Code Duplication |
for ($roundNumber = 2; $roundNumber <= $numRounds; $roundNumber++) { |
|
|
|
|
62
|
|
|
// From last match to first match |
63
|
|
|
$maxMatches = ($numFightersElim / pow(2, $roundNumber)); |
64
|
|
|
for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { |
65
|
|
|
$fighters = $this->createByeGroup(2); |
66
|
|
|
$group = $this->saveGroup($matchNumber, $roundNumber, null); |
67
|
|
|
$this->syncGroup($group, $fighters); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return number of rounds for the tree based on fighter count. |
74
|
|
|
* |
75
|
|
|
* @param $numFighters |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
protected function getNumRounds($numFighters) |
80
|
|
|
{ |
81
|
|
|
return intval(log($numFighters, 2)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws TreeGenerationException |
86
|
|
|
*/ |
87
|
|
|
protected function generateAllTrees() |
88
|
|
|
{ |
89
|
|
|
// TODO This is limiting Playoff only have 1 area |
90
|
|
|
$fighters = $this->championship->fighters; |
|
|
|
|
91
|
|
|
// This means that when playoff, we only generate 1 group |
92
|
|
|
// Could be better, for now it is ok |
93
|
|
|
$fighterType = $this->settings->isTeam |
94
|
|
|
? trans_choice('laravel-tournaments::core.team', 2) |
95
|
|
|
: trans_choice('laravel-tournaments::core.competitor', 2); |
96
|
|
|
|
97
|
|
|
if (count($fighters) <= 1) { |
98
|
|
|
throw new TreeGenerationException(trans('laravel-tournaments::core.min_competitor_required', [ |
99
|
|
|
'number' => $this->settings->preliminaryGroupSize, |
100
|
|
|
'fighter_type' => $fighterType, |
101
|
|
|
]), 422); |
102
|
|
|
} |
103
|
|
|
$this->generateGroupsForRound($fighters, 1); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function generateGroupsForRound(Collection $fightersByArea, $round) |
107
|
|
|
{ |
108
|
|
|
$fightersId = $fightersByArea->pluck('id'); |
109
|
|
|
$group = $this->saveGroup($round, $round, null); |
110
|
|
|
$this->syncGroup($group, $fightersId); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.