1 | <?php |
||
14 | class TreeGen implements TreeGenerable |
||
15 | { |
||
16 | protected $groupBy; |
||
17 | public $championship; |
||
18 | public $settings; |
||
19 | |||
20 | /** |
||
21 | * @param \Xoco70\KendoTournaments\Models\ChampionshipSettings $settings |
||
22 | */ |
||
23 | public function __construct(Championship $championship, $groupBy, $settings) |
||
29 | |||
30 | /** |
||
31 | * Generate tree groups for a championship. |
||
32 | * |
||
33 | * @throws TreeGenerationException |
||
34 | * |
||
35 | * @return Collection |
||
36 | */ |
||
37 | public function run() |
||
38 | { |
||
39 | // If previous trees already exist, delete all |
||
40 | $this->championship->rounds()->delete(); |
||
41 | $areas = $this->settings->fightingAreas; |
||
42 | $fighters = $this->getFighters(); |
||
43 | |||
44 | if ($fighters->count() / $areas < ChampionshipSettings::MIN_COMPETITORS_BY_AREA) { |
||
45 | throw new TreeGenerationException(); |
||
46 | } |
||
47 | // Get Competitor's / Team list ordered by entities ( Federation, Assoc, Club, etc...) |
||
48 | $fightersByEntity = $this->getFightersByEntity($fighters); |
||
49 | |||
50 | // Chunk user by areas |
||
51 | |||
52 | $usersByArea = $fightersByEntity->chunk(count($fightersByEntity) / $areas); |
||
53 | |||
54 | $area = 1; |
||
55 | |||
56 | // loop on areas |
||
57 | $tree = $this->generateAllRounds($usersByArea, $area); |
||
58 | return $tree; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param $userGroups |
||
63 | * |
||
64 | * @return int |
||
65 | */ |
||
66 | private function getMaxCompetitorByEntity($userGroups): int |
||
78 | |||
79 | /** |
||
80 | * Get Competitor's list ordered by entities |
||
81 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
82 | * |
||
83 | * @return Collection |
||
84 | */ |
||
85 | private function getFightersByEntity($fighters): Collection |
||
86 | { |
||
87 | // Right now, we are treating users and teams as equals. |
||
88 | // It doesn't matter right now, because we only need name attribute which is common to both models |
||
89 | |||
90 | // $this->groupBy contains federation_id, association_id, club_id, etc. |
||
91 | if (($this->groupBy) != null) { |
||
92 | $fighterGroups = $fighters->groupBy($this->groupBy); // Collection of Collection |
||
93 | } else { |
||
94 | $fighterGroups = $fighters->chunk(1); // Collection of Collection |
||
95 | } |
||
96 | |||
97 | $tmpFighterGroups = clone $fighterGroups; |
||
98 | |||
99 | $byeGroup = $this->getByeGroup($this->championship, $fighters); |
||
100 | |||
101 | // Get biggest competitor's group |
||
102 | $max = $this->getMaxCompetitorByEntity($tmpFighterGroups); |
||
103 | |||
104 | // We reacommodate them so that we can mix them up and they don't fight with another competitor of his entity. |
||
105 | |||
106 | $competitors = $this->repart($fighterGroups, $max); |
||
107 | |||
108 | $competitors = $this->insertByes($competitors, $byeGroup); |
||
109 | |||
110 | return $competitors; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Calculate the Byes need to fill the Championship Tree. |
||
115 | * |
||
116 | * @param Championship $championship |
||
117 | * |
||
118 | * @return Collection |
||
119 | */ |
||
120 | private function getByeGroup(Championship $championship, $fighters) |
||
143 | |||
144 | /** |
||
145 | * @param $fighterCount |
||
146 | * |
||
147 | * @return int |
||
148 | */ |
||
149 | private function getTreeSize($fighterCount, $groupSize) |
||
164 | |||
165 | /** |
||
166 | * @param $byeCount |
||
167 | * |
||
168 | * @return Collection |
||
169 | */ |
||
170 | private function createNullsGroup($byeCount, $isTeam): Collection |
||
183 | |||
184 | /** |
||
185 | * @param $fighterGroups |
||
186 | * @param int $max |
||
187 | * |
||
188 | * @return Collection |
||
189 | */ |
||
190 | private function repart($fighterGroups, $max) |
||
204 | |||
205 | /** |
||
206 | * Insert byes in an homogen way. |
||
207 | * |
||
208 | * @param Collection $fighters |
||
209 | * @param Collection $byeGroup |
||
210 | * |
||
211 | * @return Collection |
||
212 | */ |
||
213 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
238 | |||
239 | private function getFighters() |
||
247 | |||
248 | /** |
||
249 | * @param $usersByArea |
||
250 | * @param $area |
||
251 | * |
||
252 | * @return Collection |
||
253 | */ |
||
254 | public function generateAllRounds($usersByArea, $area) |
||
280 | |||
281 | /** |
||
282 | * @param $area |
||
283 | * @param $fighters |
||
284 | * @param $order |
||
285 | * |
||
286 | * @return Round |
||
287 | */ |
||
288 | public function saveRound($area, $fighters, $order) |
||
308 | } |
||
309 |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.