Completed
Push — master ( 4e860c...c8ac6b )
by Julien
01:58
created

PlayOffTreeGen::generateFights()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 24
rs 8.9713
c 3
b 0
f 1
cc 3
eloc 13
nc 2
nop 0
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 PlayOffTreeGen extends TreeGen
10
{
11
12
    /**
13
     * Calculate the Byes need to fill the Championship Tree.
14
     * @param $fighters
15
     * @return Collection
16
     */
17
    protected function getByeGroup($fighters)
18
    {
19
        $fighterCount = $fighters->count();
20
        $preliminaryGroupSize = $this->championship->getSettings()->preliminaryGroupSize;
21
        $treeSize = $this->getTreeSize($fighterCount, $preliminaryGroupSize);
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 $numFightersElim
31
     */
32
    protected function pushGroups($numRounds, $numFightersElim)
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 = ($numFightersElim / 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 Preliminary Round
49
     * @param $numFighters
50
     */
51
    protected function pushEmptyGroupsToTree($numFighters)
52
    {
53
        $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2;
54
        // We calculate how much rounds we will have
55
        $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4)
56
        $this->pushGroups($numRounds, $numFightersElim);
57
    }
58
59
    /**
60
     * Chunk Fighters into groups for fighting, and optionnaly shuffle
61
     * @param $round
62
     * @param $fightersByEntity
63
     * @return mixed
64
     */
65
    protected function chunkAndShuffle($round, Collection $fightersByEntity)
66
    {
67
        if ($this->championship->hasPreliminary()) {
68
            $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize);
69
            if (!app()->runningUnitTests()) {
70
                $fightersGroup = $fightersGroup->shuffle();
71
            }
72
            return $fightersGroup;
73
        }
74
        return $fightersByEntity->chunk($fightersByEntity->count());
75
    }
76
77
    /**
78
     * Generate First Round Fights
79
     */
80
    protected function generateFights()
81
    {
82
        //  First Round Fights
83
        $settings = $this->championship->getSettings();
84
        parent::destroyPreviousFights();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (destroyPreviousFights() instead of generateFights()). Are you sure this is correct? If so, you might want to change this to $this->destroyPreviousFights().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
85
        $groups = $this->championship->groupsByRound(1)->get();
86
        // Very specific case to common case : Preliminary with 3 fighters
87
        if ($settings->preliminaryGroupSize == 3) {
88
            // First we make all first fights of all groups
89
            // Then we make all second fights of all groups
90
            // Then we make all third fights of all groups
91
            for ($numFight = 1; $numFight <= $settings->preliminaryGroupSize; $numFight++) {
92
                $fight = new PreliminaryFight;
93
                $fight->saveFights($groups, $numFight);
94
            }
95
        } else {
96
            // Generate fights with PLayoff style
97
            $fight = new PreliminaryFight;
98
            $fight->saveRoundRobinFights($groups, $settings->preliminaryGroupSize);
99
        }
100
        // Save Next rounds
101
        $fight = new DirectEliminationFight;
102
        $fight->saveFights($this->championship, 2);
103
    }
104
105
106
    /**
107
     * Return number of rounds for the tree based on fighter count
108
     * @param $numFighters
109
     * @return int
110
     */
111
    protected function getNumRounds($numFighters)
112
    {
113
        return intval(log($numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2, 2));
114
    }
115
}
116