Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

DirectEliminationTreeGen::generateFights()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 0
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace Xoco70\LaravelTournaments\TreeGen;
4
5
use Illuminate\Support\Collection;
6
use Xoco70\LaravelTournaments\Models\DirectEliminationFight;
7
use Xoco70\LaravelTournaments\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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method chunkAndShuffle() cannot be called from this context as it is declared protected in class Xoco70\LaravelTournaments\TreeGen\PlayOffTreeGen.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
75
        }
76
        $fightersGroup = null;
0 ignored issues
show
Unused Code introduced by
$fightersGroup is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
//        dd("ok");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
//        parent::destroyPreviousFights();
95
        $groups = $this->championship->groupsByRound(1)->get();
96
        $initialRound = 1;
97
        // Very specific case to common case : Preliminary with 3 fighters
98
        if ($this->championship->hasPreliminary() && $settings->preliminaryGroupSize == 3) {
99
            // First we make all first fights of all groups
100
            // Then we make all second fights of all groups
101
            // Then we make all third fights of all groups
102
            for ($numFight = 1; $numFight <= $settings->preliminaryGroupSize; $numFight++) {
103
                $fight = new PreliminaryFight;
104
                $fight->saveFights($groups, $numFight);
105
            }
106
            $initialRound++;
107
        }
108
        // Save Next rounds
109
        $fight = new DirectEliminationFight;
110
        $fight->saveFights($this->championship, $initialRound);
111
    }
112
113
114
    /**
115
     * Return number of rounds for the tree based on fighter count
116
     * @param $numFighters
117
     * @return int
118
     */
119
    protected function getNumRounds($numFighters)
120
    {
121
        return intval(log($numFighters / $this->firstRoundGroupSize() * 2, 2));
122
    }
123
124
    private function firstRoundGroupSize()
125
    {
126
        return $this->championship->hasPreliminary()
127
            ? $this->championship->getSettings()->preliminaryGroupSize
128
            : 2;
129
    }
130
}
131