Introduction

Tournament class is a main class, you will be working with most of the time. It's used to generate brackets, create new games and store all information about the tournament.


Creating a new tournament class

include_once 'vendor/autoload.php';

$tournament = new TournamentGenerator\Tournament('Tournament name');

Setting up a basic tournament

See Examples


Properties

Scope Name Type Default Description
protected $name string '' Name of the tournament
private $categories array [] List of tournament categories
private $rounds array [] List of tournament rounds
private $teams array [] List of teams
private $expectedPlay int 0 Time to play one game in minutes
private $expectedGameWait int 0 Pause between 2 games in minutes
private $expectedRoundWait int 0 Pause between 2 rounds in minutes
private $allowSkip bool false If true, generator will skip unplayed games without throwing exception

Methods

Method list

Scope Name Return Description
public __construct $this Construct method
public __toString string Returns the name of the tournament
public setName $this Sets name of the tournament.
public getName string Gets name of the tournament.
public setPlay $this Sets time to play one game in minutes.
public getPlay int Gets time to play one game in minutes.
public setGameWait $this Sets time to wait between games in minutes.
public getGameWait int Gets time to wait between games in minutes.
public setRoundWait $this Sets time to wait between rounds in minutes.
public getRoundWait int Gets time to wait between rounds in minutes.
public setCategoryWait $this Sets time to wait between category in minutes.
public getCategoryWait int Gets time to wait between category in minutes.
public getTournamentTime int Gets time to play the whole tournament in minutes.
public allowSkip $this Allow skipping of unplayed games while progressing.
public disallowSkip $this Disllow skipping of unplayed games while progressing.
public setSkip $this Sets whether to skip unplayed games while progressing or not.
public getSkip bool Gets whether to skip unplayed games while progressing or not.
public addCategory $this Adds created Category to Tournament.
public category TournamentGenerator\Category Creates and adds new Category to Tournament.
public getCategories array Gets an array of all Categories from Tournament.
public addRound $this Adds created Round to Tournament.
public round TournamentGenerator\Round Creates and adds new Round to Tournament.
public getRounds array Gets an array of all Rounds from Tournament.
public getGroups array Gets an array of all Groups from Tournament.
public addTeam $this Adds created Team to Tournament.
public team new TournamentGenerator\Team Creates and adds new Team to Tournament.
public getTeams array Gets an array of all Teams from Tournament.
public sortTeams array Sorts all Teams from Tournament and returns them.
public getGames array Gets an array of all Games from Tournament.
public splitTeams $this Splits all Teams from Tournament to given Rounds (or all Rounds from a Tournament).
public genGamesSimulate array/int Generate and simulate all Games from Tournament without real teams (just to export) and returns array of all Games or caculated tournament time.
public genGamesSimulateReal array/int Generate and simulate all Games from Tournament with real teams (just as it was played for real) and returns array of all Games or caculated tournament time.

Tournament __construct(string $name)

Creates a new Tournament class

Parameters
Name Type Default Description
$name string '' Name of the tournament
Return value
new TournamentGenerator\Tournament();

Code

function __construct(string $name = ''){
    $this->name = $name;
}

public string __toString()

Returns tournament name

Return value
string $this->name;

Code

public function __toString() {
    return $this->name;
}

public Tournament setName(string $name)

Sets the name of the tournament.

Parameters
Name Type Default Description
$name string Name of the tournament
Return value
TournamentGenerator\Tournament $this

Code

public function setName(string $name) {
    $this->name = $name;
    return $this;
}

public string getName()

Gets the name of the tournament.

Return value
string $this->name;

Code

public function getName() {
    return $this->name;
}

public Tournament setPlay(int $play)

Sets time to play one game in minutes.

Parameters
Name Type Default Description
$play int Time of one game in minutes
Return value
TournamentGenerator\Tournament $this

Code

public function setPlay(int $play) {
    $this->expectedPlay = $play;
    return $this;
}

public int getPlay()

Gets time to play one game in minutes.

Return value
int $this->expectedPlay;

Code

public function getPlay() {
    return $this->expectedPlay;
}

public Tournament setGameWait(int $wait)

Sets time to wait between games in minutes.

Parameters
Name Type Default Description
$wait int Pause between two games in minutes
Return value
TournamentGenerator\Tournament $this

Code

public function setGameWait(int $wait) {
    $this->expectedGameWait = $wait;
    return $this;
}

public int getGameWait()

Sets time to play one game in minutes.

Return value
int $this->expectedGameWait;

Code

public function getGameWait() {
    return $this->expectedGameWait;
}

public Tournament setRoundWait(int $wait)

Sets time to wait between rounds in minutes.

Parameters
Name Type Default Description
$wait int Pause between rounds in minutes
Return value
TournamentGenerator\Tournament $this

Code

public function setRoundWait(int $wait) {
    $this->expectedRoundWait = $wait;
    return $this;
}

public int getRoundWait()

Gets time between rounds in minutes.

Return value
int $this->expectedRoundWait

Code

public function getRoundWait() {
    return $this->expectedRoundWait;
}

public Tournament setCategoryWait(int $wait)

Sets time to wait between categories in minutes.

Parameters
Name Type Default Description
$wait int Pause between categories in minutes
Return value
TournamentGenerator\Tournament $this

Code

public function setCategoryWait(int $wait) {
    $this->expectedCategoryWait = $wait;
    return $this;
}

public int getCategoryWait()

Gets time between categories in minutes.

Return value
int $this->expectedCategoryWait

Code

public function getCategoryWait() {
    return $this->expectedCategoryWait;
}

public int getTournamentTime()

Gets expected time to play the whole tournament.

Return value
int

Code

public function getTournamentTime(){
    $games = count($this->getGames());
    return $games*$this->expectedPlay+$games*$this->expectedGameWait+count($this->getRounds())*$this->expectedRoundWait+count($this->getCategories())*$this->expectedCategoryWait;
    }

public Tournament allowSkip()

Allow skipping of unplayed games while progressing.

Return value
TournamentGenerator\Tournament $this

Code

public function allowSkip(){
    $this->allowSkip = true;
    return $this;
}

public Tournament disallowSkip()

Disallow skipping of unplayed games while progressing.

Return value
TournamentGenerator\Tournament $this

Code

public function disallowSkip(){
    $this->allowSkip = false;
    return $this;
}

public Tournament setSkip(bool $skip)

Sets whether an unplayed games should be skipped while progressing or not.

Parameters
Name Type Default Description
$skip bool Skip or not
Return value
TournamentGenerator\Tournament $this

Code

public function setSkip(bool $skip) {
    $this->allowSkip = $skip;
    return $this;
}

public bool getSkip()

Gets whether an unplayed games should be skipped while progressing or not.

Return value
bool $this->allowSkip

Code

public function getSkip(bool $skip) {
    return $this->allowSkip;
}

public Tournament addCategory(TournamentGenerator\Category ...$categories)

Adds created Categories to Tournament.

Parameters
Name Type Default Description
$categories TournamentGenerator\Category [] One or more instances of Category class
Return value
TournamentGenerator\Tournament $this

Code

public function addCategory(Category ...$categories){
    foreach ($categories as $category) {
        if ($category instanceof Category) $this->categories[] = $category;
        else throw new \Exception('Trying to add category which is not an instance of the Category class.');
    }
    return $this;
}

public Category category(string $name)

Creates and adds new Category to Tournament.

Parameters
Name Type Default Description
$name string '' Name of the new Category.
$id string|int null Id of the new Category.
Return value
new TournamentGenerator\Category

Code

public function category(string $name = '', $id = null) {
    $c = new Category($name, $id);
    $this->categories[] = $c->setSkip($this->allowSkip);
    return $c;
}

public array getCategories()

Gets an array of all Categories from Tournament.

Return value
array of TournamentGenerator\Category

Code

public function getCategories() {
    return $this->categories;
}

public Tournament addRound(TournamentGenerator\Round ...$rounds)

Adds created Round to Tournament.

Parameters
Name Type Default Description
$rounds TournamentGenerator\Round [] One or more instances of Round class.
Return value
$this

Code

public function addRound(Round ...$rounds) {
    foreach ($rounds as $round) {
        if ($round instanceof Round) $this->rounds[] = $round;
        else throw new \Exception('Trying to add round which is not an instance of the Round class.');
    }
    return $this;
}

public Round round(string $name)

Creates and adds new Round to Tournament.

Parameters
Name Type Default Description
$name string '' Name of the new Round.
$id string|int null Id of the new Round.
Return value
new TournamentGenerator\Round

Code

public function round(string $name = '', $id = null) {
    $r = new Round($name, $id);
    $this->rounds[] = $r->setSkip($this->allowSkip);
    return $r;
}

public array getRounds()

Gets an array of all Rounds from Tournament.

Return value
array of TournamentGenerator\Round

Code

public function getRounds() {
    if (count($this->categories) > 0) {
        $rounds = [];
        foreach ($this->categories as $category) {
            $rounds = array_merge($rounds, $category->getRounds());
        }
        return $rounds;
    }
    return $this->rounds;
}

public array getGroups()

Gets an array of all Groups from Tournament.

Return value
array of TournamentGenerator\Group

Code

public function getGroups() {
    $groups = [];
    foreach ($this->getRounds() as $round) {
        $groups = array_merge($groups, $round->getGroups());
    }
    return $groups;
}

public Tournament addTeam(TournamentGenerator\Team ...$teams)

Adds created Team to Tournament.

Parameters
Name Type Default Description
$teams TournamentGenerator\Team [] One or more instances of Team class.
Return value
$this

Code

public function addTeam(Team ...$teams) {
    foreach ($teams as $team) {
        $this->teams[] = $team;
    }
    return $this;
}

public Team team(string $name)

Creates and adds new Team to Tournament.

Parameters
Name Type Default Description
$name string '' Name of the new Team.
$id string|int null Id of the new Team.
Return value
new TournamentGenerator\Team

Code

public function team(string $name = '', $id = null) {
    $t = new Team($name, $id);
    $this->teams[] = $t;
    return $t;
}

public array getTeams(bool $ordered = false, $ordering = POINTS)

Gets an array of all Teams from Tournament. If passed true as the first argument, teams will be ordered.

Parameters
Name Type Default Description
$ordered bool false If teams should be ordered.
$ordering \TournamentGenerator\Constants::POINTS | \TournamentGenerator\Constants::SCORE \TournamentGenerator\Constants::POINTS What to order the teams by.
$filters array [] Filters to filter the teams.
Return value
array of TournamentGenerator\Team

Code

public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
    $teams = $this->teams;
    foreach ($this->categories as $category) {
        $teams = array_merge($teams, $category->getTeams());
    }
    foreach ($this->rounds as $round) {
        $teams = array_merge($teams, $round->getTeams());
    }
    $teams = \array_unique($teams);
    $this->teams = $teams;
    if ($ordered) $teams = $this->sortTeams($ordering);

    // APPLY FILTERS
    $filter = new Filter($this->getGroups(), $filters);
    $filter->filter($teams);

    return $teams;
}

public array sortTeams($ordering = POINTS)

Sorts all Teams from Tournament and returns them.

Parameters
Name Type Default Description
$ordering \TournamentGenerator\Constants::POINTS | \TournamentGenerator\Constants::SCORE \TournamentGenerator\Constants::POINTS What to order the teams by.
$filters array [] Filters to filter the teams.
Return value
array of TournamentGenerator\Team

Code

public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
    $teams = [];
    for ($i = count($this->rounds)-1; $i >= 0; $i--) {
        $rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); });
        $teams = array_merge($teams, $rTeams);
    }
    $this->teams = $teams;

    // APPLY FILTERS
    $filter = new Filter($this->getGroups(), $filters);
    $filter->filter($teams);

    return $teams;
}

public array getGames()

Gets an array of all Games from Tournament.

Return value
array of TournamentGenerator\Game

Code

public function getGames() {
    $games = [];
    foreach ($this->getRounds() as $round) {
        $games = array_merge($games, $round->getGames());
    }
    return $games;
}

public Tournament splitTeams(TournamentGenerator\Round ...$wheres)

Split all Teams from Tournament into given Rounds. If no argument is given, method will split into all available Rounds in Tournament.

Parameters
Name Type Default Description
$wheres TournamentGenerator\Round [] One or more instances of Round class to split the teams into.
Return value
$this

Code

public function splitTeams(Round ...$wheres) {

    if (count($wheres) === 0) $wheres = $this->getRounds();

    $teams = $this->getTeams();
    shuffle($teams);

    while (count($teams) > 0) {
        foreach ($wheres as $where) {
            if (count($teams) > 0) $where->addTeam(array_shift($teams));
        }
    }
    foreach ($wheres as $where) {
        $where->splitTeams();
    }
    return $this;
}

public array|int genGamesSimulate(bool $returnTime)

Generate and simulate all Games from Tournament without real teams (just to export) and returns array of all Games or caculated tournament time. It uses BlankTeam class for progressing.

Parameters
Name Type Default Description
$returnTime bool false If true, returns calculated tournament time in minutes.
Return value

If $returnTime parameter empty or false

array of TournamentGenerator\Games

If $returnTime parameter true

int $this->getTournamentTime()

Code

public function genGamesSimulate(bool $returnTime = false) {
    $games = Utilis\Simulator::simulateTournament($this);

    if ($returnTime) return $this->getTournamentTime();
    return $games;
}

public array|int genGamesSimulateReal(bool $returnTime)

Generate and simulate all Games from Tournament with real teams (just as it was played for real) and returns array of all Games or caculated tournament time.
Could be used for testing and demonstration purposes.

Parameters
Name Type Default Description
$returnTime bool false If true, returns calculated tournament time in minutes.
Return value

If $returnTime parameter empty or false

array of TournamentGenerator\Games

If $returnTime parameter true

int $this->getTournamentTime()

Code

public function genGamesSimulateReal(bool $returnTime = false) {
    $games = Utilis\Simulator::simulateTournamentReal($this);

    if ($returnTime) return $this->getTournamentTime();
    return $games;
}