<commit_before><?php

namespace Xoco70\KendoTournaments\TreeGen;

use Xoco70\KendoTournaments\Contracts\TreeGenerable;

class DirectEliminationTreeGen implements TreeGenerable
{
    public $names;
    public $brackets = array();
    public $noTeams;
    public $noRounds;
    public $playerWrapperHeight = 30;
    public $matchWrapperWidth = 150;
    public $roundSpacing = 40;
    public $matchSpacing = 42;
    public $borderWidth = 3;

    public function __construct($names)
    {

        $this->names = $names;

        $this->run();

    }

    public function run()
    {
        $teams = [];
        //If no names have been entered, then use numbers

        if ($this->names != '') {
            $teams = $this->names;
            $this->noTeams = count($teams);
        }


        //Calculate the size of the first full round - for example if you have 5 teams, then the first full round will consist of 4 teams
        $minimumFirstRoundSize = pow(2, ceil(log($this->noTeams) / log(2)));
        $this->noRounds = log($minimumFirstRoundSize, 2);


        //Order the teams in a seeded order - this is required regardless of whether it is a seeded tournament or not, as it prevents BYEs playing eachother
        $teams = $this->orderTeamsInSeededOrder($teams);

        $roundNumber = 1;

        //Group 2 teams into a match
        $matches = array_chunk($teams, 2);


        //If there's already a match in the match array, then that means the next round is round 2, so increase the round number
        if (count($this->brackets)) $roundNumber++;
        $countMatches = count($matches);
        //Create the first full round of teams, some may be blank if waiting on the results of a previous round
        for ($i = 0; $i < $countMatches; $i++) {
            $this->brackets[$roundNumber][$i + 1] = $matches[$i];
        }

        $this->assignPositions();

    }

    private function assignPositions()
    {

        //Variables required for figuring outing the height of the vertical connectors

        $matchSpacingMultiplier = 0.5;
        $playerWrapperHeightMultiplier = 1;

        foreach ($this->brackets as $roundNumber => &$round) {

            foreach ($round as $matchNumber => &$match) {

                //Give teams a nicer index

                $match['playerA'] = $match[0];
                $match['playerB'] = $match[1];

                unset($match[0]);
                unset($match[1]);

                //Figure out the bracket positions

                $match['matchWrapperTop'] = (((2 * $matchNumber) - 1) * (pow(2, ($roundNumber) - 1)) - 1) * (($this->matchSpacing / 2) + $this->playerWrapperHeight);
                $match['matchWrapperLeft'] = ($roundNumber - 1) * ($this->matchWrapperWidth + $this->roundSpacing - 1);
                $match['vConnectorLeft'] = floor($match['matchWrapperLeft'] + $this->matchWrapperWidth + ($this->roundSpacing / 2) - ($this->borderWidth / 2));
                $match['vConnectorHeight'] = ($matchSpacingMultiplier * $this->matchSpacing) + ($playerWrapperHeightMultiplier * $this->playerWrapperHeight) + $this->borderWidth;
                $match['vConnectorTop'] = $match['hConnectorTop'] = $match['matchWrapperTop'] + $this->playerWrapperHeight;
                $match['hConnectorLeft'] = ($match['vConnectorLeft'] - ($this->roundSpacing / 2)) + 2;
                $match['hConnector2Left'] = $match['matchWrapperLeft'] + $this->matchWrapperWidth + ($this->roundSpacing / 2);

                //Adjust the positions depending on the match number

                if (!($matchNumber % 2)) {
                    $match['hConnector2Top'] = $match['vConnectorTop'] -= ($match['vConnectorHeight'] - $this->borderWidth);
                } else {
                    $match['hConnector2Top'] = $match['vConnectorTop'] + ($match['vConnectorHeight'] - $this->borderWidth);
                }

            }

            //Update the spacing variables

            $matchSpacingMultiplier *= 2;
            $playerWrapperHeightMultiplier *= 2;

        }

    }

    public function printBrackets()
    {

        $this->printRoundTitles();

        echo '<div id="brackets-wrapper">';

        foreach ($this->brackets as $roundNumber => $round) {

            foreach ($round as $matchNumber => $match) {

                echo '<div class="match-wrapper" style="top: ' . $match['matchWrapperTop'] . 'px; left: ' . $match['matchWrapperLeft'] . 'px; width: ' . $this->matchWrapperWidth . 'px;">
                        <input type="text" class="score">'
                    . $this->getPlayerList($match['playerA']) .
                    '<div class="match-divider">
                        </div>
                        <input type="text" class="score">'
                    . $this->getPlayerList($match['playerB']) .
                    '</div>';

                if ($roundNumber != $this->noRounds) {

                    echo '<div class="vertical-connector" style="top: ' . $match['vConnectorTop'] . 'px; left: ' . $match['vConnectorLeft'] . 'px; height: ' . $match['vConnectorHeight'] . 'px;"></div>
                          <div class="horizontal-connector" style="top: ' . $match['hConnectorTop'] . 'px; left: ' . $match['hConnectorLeft'] . 'px;"></div>
                          <div class="horizontal-connector" style="top: ' . $match['hConnector2Top'] . 'px; left: ' . $match['hConnector2Left'] . 'px;"></div>';

                }

            }

        }

        echo '</div>';

    }

    /**
     * Print Round Titles
     */
    public function printRoundTitles()
    {

        if ($this->noTeams == 2) {

            $roundTitles = array('Final');

        } elseif ($this->noTeams <= 4) {

            $roundTitles = array('Semi-Finals', 'Final');

        } elseif ($this->noTeams <= 8) {

            $roundTitles = array('Quarter-Finals', 'Semi-Finals', 'Final');

        } else {

            $roundTitles = array('Quarter-Finals', 'Semi-Finals', 'Final');
            $noRounds = ceil(log($this->noTeams, 2));
            $noTeamsInFirstRound = pow(2, ceil(log($this->noTeams) / log(2)));
            $tempRounds = array();

            //The minus 3 is to ignore the final, semi final and quarter final rounds

            for ($i = 0; $i < $noRounds - 3; $i++) {
                $tempRounds[] = 'Last ' . $noTeamsInFirstRound;
                $noTeamsInFirstRound /= 2;
            }

            $roundTitles = array_merge($tempRounds, $roundTitles);

        }

        echo '<div id="round-titles-wrapper">';

        foreach ($roundTitles as $key => $roundTitle) {

            $left = $key * ($this->matchWrapperWidth + $this->roundSpacing - 1);

            echo '<div class="round-title" style="left: ' . $left . 'px;">' . $roundTitle . '</div>';

        }

        echo '</div>';

    }

    /**
     * @param $selected
     * @return string
     */
    private function getPlayerList($selected)
    {

        $html = '<select>
                <option' . ($selected == '' ? ' selected' : '') . '></option>';
        foreach (array_merge($this->brackets[1]) as $bracket) { // Bug Fix 24-02-2017 , $this->brackets[2]
            if ($bracket['playerA'] != '') {
                $html .= '<option' . ($selected == $bracket['playerA'] ? ' selected' : '') . '>' . $bracket['playerA'] . '</option>';
            }

            if ($bracket['playerB'] != '') {
                $html .= '<option' . ($selected == $bracket['playerB'] ? ' selected' : '') . '>' . $bracket['playerB'] . '</option>';
            }
        }

        $html .= '</select>';

        return $html;

    }

    /**
     * @param $teams
     * @return array
     */
    private function orderTeamsInSeededOrder($teams): array
    {
        $logNoTeam = log($this->noTeams / 2, 2);

        for ($i = 0; $i < $logNoTeam; $i++) {

            $out = [];

            foreach ($teams as $player) {
                $splice = pow(2, $i);
                $out = array_merge($out, array_splice($teams, 0, $splice));
                $out = array_merge($out, array_splice($teams, -$splice));
            }

            $teams = $out;

        }
        return $teams;
    }
}
<commit_msg>public to private<commit_after><?php

namespace Xoco70\KendoTournaments\TreeGen;

use Xoco70\KendoTournaments\Contracts\TreeGenerable;

class DirectEliminationTreeGen implements TreeGenerable
{
    public $names;
    public $brackets = array();
    public $noTeams;
    public $noRounds;
    public $playerWrapperHeight = 30;
    public $matchWrapperWidth = 150;
    public $roundSpacing = 40;
    public $matchSpacing = 42;
    public $borderWidth = 3;

    public function __construct($names)
    {

        $this->names = $names;

        $this->run();

    }

    public function run()
    {
        $teams = [];
        //If no names have been entered, then use numbers

        if ($this->names != '') {
            $teams = $this->names;
            $this->noTeams = count($teams);
        }


        //Calculate the size of the first full round - for example if you have 5 teams, then the first full round will consist of 4 teams
        $minimumFirstRoundSize = pow(2, ceil(log($this->noTeams) / log(2)));
        $this->noRounds = log($minimumFirstRoundSize, 2);


        //Order the teams in a seeded order - this is required regardless of whether it is a seeded tournament or not, as it prevents BYEs playing eachother
        $teams = $this->orderTeamsInSeededOrder($teams);

        $roundNumber = 1;

        //Group 2 teams into a match
        $matches = array_chunk($teams, 2);


        //If there's already a match in the match array, then that means the next round is round 2, so increase the round number
        if (count($this->brackets)) $roundNumber++;
        $countMatches = count($matches);
        //Create the first full round of teams, some may be blank if waiting on the results of a previous round
        for ($i = 0; $i < $countMatches; $i++) {
            $this->brackets[$roundNumber][$i + 1] = $matches[$i];
        }

        $this->assignPositions();

    }

    private function assignPositions()
    {

        //Variables required for figuring outing the height of the vertical connectors

        $matchSpacingMultiplier = 0.5;
        $playerWrapperHeightMultiplier = 1;

        foreach ($this->brackets as $roundNumber => &$round) {

            foreach ($round as $matchNumber => &$match) {

                //Give teams a nicer index

                $match['playerA'] = $match[0];
                $match['playerB'] = $match[1];

                unset($match[0]);
                unset($match[1]);

                //Figure out the bracket positions

                $match['matchWrapperTop'] = (((2 * $matchNumber) - 1) * (pow(2, ($roundNumber) - 1)) - 1) * (($this->matchSpacing / 2) + $this->playerWrapperHeight);
                $match['matchWrapperLeft'] = ($roundNumber - 1) * ($this->matchWrapperWidth + $this->roundSpacing - 1);
                $match['vConnectorLeft'] = floor($match['matchWrapperLeft'] + $this->matchWrapperWidth + ($this->roundSpacing / 2) - ($this->borderWidth / 2));
                $match['vConnectorHeight'] = ($matchSpacingMultiplier * $this->matchSpacing) + ($playerWrapperHeightMultiplier * $this->playerWrapperHeight) + $this->borderWidth;
                $match['vConnectorTop'] = $match['hConnectorTop'] = $match['matchWrapperTop'] + $this->playerWrapperHeight;
                $match['hConnectorLeft'] = ($match['vConnectorLeft'] - ($this->roundSpacing / 2)) + 2;
                $match['hConnector2Left'] = $match['matchWrapperLeft'] + $this->matchWrapperWidth + ($this->roundSpacing / 2);

                //Adjust the positions depending on the match number

                if (!($matchNumber % 2)) {
                    $match['hConnector2Top'] = $match['vConnectorTop'] -= ($match['vConnectorHeight'] - $this->borderWidth);
                } else {
                    $match['hConnector2Top'] = $match['vConnectorTop'] + ($match['vConnectorHeight'] - $this->borderWidth);
                }

            }

            //Update the spacing variables

            $matchSpacingMultiplier *= 2;
            $playerWrapperHeightMultiplier *= 2;

        }

    }

    public function printBrackets()
    {

        $this->printRoundTitles();

        echo '<div id="brackets-wrapper">';

        foreach ($this->brackets as $roundNumber => $round) {

            foreach ($round as $matchNumber => $match) {

                echo '<div class="match-wrapper" style="top: ' . $match['matchWrapperTop'] . 'px; left: ' . $match['matchWrapperLeft'] . 'px; width: ' . $this->matchWrapperWidth . 'px;">
                        <input type="text" class="score">'
                    . $this->getPlayerList($match['playerA']) .
                    '<div class="match-divider">
                        </div>
                        <input type="text" class="score">'
                    . $this->getPlayerList($match['playerB']) .
                    '</div>';

                if ($roundNumber != $this->noRounds) {

                    echo '<div class="vertical-connector" style="top: ' . $match['vConnectorTop'] . 'px; left: ' . $match['vConnectorLeft'] . 'px; height: ' . $match['vConnectorHeight'] . 'px;"></div>
                          <div class="horizontal-connector" style="top: ' . $match['hConnectorTop'] . 'px; left: ' . $match['hConnectorLeft'] . 'px;"></div>
                          <div class="horizontal-connector" style="top: ' . $match['hConnector2Top'] . 'px; left: ' . $match['hConnector2Left'] . 'px;"></div>';

                }

            }

        }

        echo '</div>';

    }

    /**
     * Print Round Titles
     */
    private function printRoundTitles()
    {

        if ($this->noTeams == 2) {

            $roundTitles = array('Final');

        } elseif ($this->noTeams <= 4) {

            $roundTitles = array('Semi-Finals', 'Final');

        } elseif ($this->noTeams <= 8) {

            $roundTitles = array('Quarter-Finals', 'Semi-Finals', 'Final');

        } else {

            $roundTitles = array('Quarter-Finals', 'Semi-Finals', 'Final');
            $noRounds = ceil(log($this->noTeams, 2));
            $noTeamsInFirstRound = pow(2, ceil(log($this->noTeams) / log(2)));
            $tempRounds = array();

            //The minus 3 is to ignore the final, semi final and quarter final rounds

            for ($i = 0; $i < $noRounds - 3; $i++) {
                $tempRounds[] = 'Last ' . $noTeamsInFirstRound;
                $noTeamsInFirstRound /= 2;
            }

            $roundTitles = array_merge($tempRounds, $roundTitles);

        }

        echo '<div id="round-titles-wrapper">';

        foreach ($roundTitles as $key => $roundTitle) {

            $left = $key * ($this->matchWrapperWidth + $this->roundSpacing - 1);

            echo '<div class="round-title" style="left: ' . $left . 'px;">' . $roundTitle . '</div>';

        }

        echo '</div>';

    }

    /**
     * @param $selected
     * @return string
     */
    private function getPlayerList($selected)
    {

        $html = '<select>
                <option' . ($selected == '' ? ' selected' : '') . '></option>';
        foreach (array_merge($this->brackets[1]) as $bracket) { // Bug Fix 24-02-2017 , $this->brackets[2]
            if ($bracket['playerA'] != '') {
                $html .= '<option' . ($selected == $bracket['playerA'] ? ' selected' : '') . '>' . $bracket['playerA'] . '</option>';
            }

            if ($bracket['playerB'] != '') {
                $html .= '<option' . ($selected == $bracket['playerB'] ? ' selected' : '') . '>' . $bracket['playerB'] . '</option>';
            }
        }

        $html .= '</select>';

        return $html;

    }

    /**
     * @param $teams
     * @return array
     */
    private function orderTeamsInSeededOrder($teams): array
    {
        $logNoTeam = log($this->noTeams / 2, 2);

        for ($i = 0; $i < $logNoTeam; $i++) {

            $out = [];

            foreach ($teams as $player) {
                $splice = pow(2, $i);
                $out = array_merge($out, array_splice($teams, 0, $splice));
                $out = array_merge($out, array_splice($teams, -$splice));
            }

            $teams = $out;

        }
        return $teams;
    }
}

{'fraction_non_alphanumeric': 0.16000489685988858, 'fraction_numerical': 0.009671298279977964, 'mean_word_length': 2.0105030403537865, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5437809', 'n_tokens_mistral': 5017, 'n_tokens_neox': 4658, 'n_words': 1100}