SingleElimination   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 48
ccs 24
cts 24
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 39 5
1
<?php
2
3
namespace TournamentGenerator\Preset;
4
5
use Exception;
6
use TournamentGenerator\Constants;
7
use TournamentGenerator\Helpers\Functions;
8
use TournamentGenerator\Tournament;
9
10
/**
11
 * Single elimination generator
12
 *
13
 * @author  Tomáš Vojík <[email protected]>
14
 * @package TournamentGenerator\Preset
15
 * @since   0.1
16
 */
17
class SingleElimination extends Tournament implements Preset
18
{
19
20
	/**
21
	 * Generate all games
22
	 *
23
	 * @return $this
24
	 * @throws Exception
25
	 */
26 11
	public function generate() : SingleElimination {
27
28 11
		$this->allowSkip();
29
30 11
		$countTeams = count($this->getTeams());
31
32
		// CALCULATE BYES
33 11
		$byes = 0;
34 11
		if (!Functions::isPowerOf2($countTeams)) {
35 6
			$nextPow = Functions::nextPowerOf2($countTeams);
36 6
			$byes = $nextPow - $countTeams;
37
		}
38
39 11
		$roundsNum = log($countTeams + $byes, 2); // NUMBER OF ROUNDS
40
41 11
		$startRound = $this->round('Start');
42
43 11
		$previousGroups = [];
44
45 11
		for ($i = 1; $i <= (($countTeams + $byes) / 2); $i++) {
46 11
			$g = $startRound->group('Round 1 '.$i)->setInGame(2)->setType(Constants::ROUND_TWO);
47 11
			$previousGroups[] = $g;
48
		}
49
50 11
		$this->splitTeams();
51
52 11
		for ($r = 2; $r <= $roundsNum; $r++) {
53 11
			$groups = [];
54 11
			$round = $this->round('Round '.$r);
55 11
			for ($g = 1; $g <= (($countTeams + $byes) / (2 ** $r)); $g++) {
56 11
				$group = $round->group('Round '.$r.' - '.$g)->setInGame(2)->setType(Constants::ROUND_TWO);
57 11
				$groups[] = $group;
58 11
				array_shift($previousGroups)->progression($group, 0, 1); // PROGRESS FROM GROUP BEFORE
59 11
				array_shift($previousGroups)->progression($group, 0, 1); // PROGRESS FROM GROUP BEFORE
60
			}
61 11
			$previousGroups = $groups;
62
		}
63
64 11
		return $this;
65
66
	}
67
68
}
69