| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Xoco70\KendoTournaments\TreeGen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Xoco70\KendoTournaments\Models\DirectEliminationFight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Xoco70\KendoTournaments\Models\PreliminaryFight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | abstract class DirectEliminationTreeGen extends TreeGen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Calculate the Byes need to fill the Championship Tree. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param $fighters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @return Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 17 |  | View Code Duplication |     protected function getByeGroup($fighters) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $fighterCount = $fighters->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $firstRoundGroupSize = $this->firstRoundGroupSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $treeSize = $this->getTreeSize($fighterCount, $firstRoundGroupSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $byeCount = $treeSize - $fighterCount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return $this->createByeGroup($byeCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Save Groups with their parent info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param integer $numRounds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param integer $numFighters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 32 |  | View Code Duplication |     protected function pushGroups($numRounds, $numFighters) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // TODO Here is where you should change when enable several winners for preliminary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         for ($roundNumber = 2; $roundNumber <= $numRounds + 1; $roundNumber++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             // From last match to first match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $maxMatches = ($numFighters / pow(2, $roundNumber)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $fighters = $this->createByeGroup(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $group = $this->saveGroup($matchNumber, $roundNumber, null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $this->syncGroup($group, $fighters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Create empty groups for direct Elimination Tree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param $numFighters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected function pushEmptyGroupsToTree($numFighters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if ($this->championship->hasPreliminary()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             // We calculate how much rounds we will have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return $this->pushGroups($numRounds, $numFightersElim); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // We calculate how much rounds we will have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $numRounds = $this->getNumRounds($numFighters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->pushGroups($numRounds, $numFighters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Chunk Fighters into groups for fighting, and optionnaly shuffle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param $fightersByEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return Collection|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function chunkAndShuffle(Collection $fightersByEntity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         //TODO Should Pull down to know if team or competitor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($this->championship->hasPreliminary()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return (new PlayOffCompetitorTreeGen($this->championship, null))->chunkAndShuffle($fightersByEntity); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $fightersGroup = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $fightersGroup = $fightersByEntity->chunk(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (!app()->runningUnitTests()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $fightersGroup = $fightersGroup->shuffle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $fightersGroup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Generate First Round Fights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     protected function generateFights() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         //  First Round Fights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $settings = $this->championship->getSettings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         parent::destroyPreviousFights(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $groups = $this->championship->groupsByRound(1)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $initialRound = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         // Very specific case to common case : Preliminary with 3 fighters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if ($this->championship->hasPreliminary() && $settings->preliminaryGroupSize == 3) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             // First we make all first fights of all groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             // Then we make all second fights of all groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             // Then we make all third fights of all groups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             for ($numFight = 1; $numFight <= $settings->preliminaryGroupSize; $numFight++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $fight = new PreliminaryFight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $fight->saveFights($groups, $numFight); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $initialRound++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         // Save Next rounds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $fight = new DirectEliminationFight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $fight->saveFights($this->championship, $initialRound); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Return number of rounds for the tree based on fighter count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param $numFighters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function getNumRounds($numFighters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return intval(log($numFighters / $this->firstRoundGroupSize() * 2, 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |     private function firstRoundGroupSize() | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         return $this->championship->hasPreliminary() | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |             ? $this->championship->getSettings()->preliminaryGroupSize | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |             : 2; | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  | 
            
                        
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.