Passed
Branch master (7949ab)
by Tomáš
02:07
created

SingleElimination   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 39 5
1
<?php
2
3
namespace TournamentGenerator\Preset;
4
5
/**
6
 *
7
 */
8
class SingleElimination extends \TournamentGenerator\Tournament
9
{
10
11 2
	public function generate() {
12
13 2
		$this->allowSkip();
14
15 2
		$countTeams = count($this->getTeams());
16
17
		// CALCULATE BYES
18 2
		$byes = 0;
19 2
		if ( !\TournamentGenerator\isPowerOf2($countTeams) ) {
20 1
			$nextPow = bindec(str_pad(1, strlen(decbin($countTeams))+1, 0, STR_PAD_RIGHT));
21 1
			$byes = $nextPow-$countTeams;
22
		}
23
24 2
		$roundsNum = log($countTeams+$byes, 2); // NUMBER OF ROUNDS
25
26 2
		$startRound = $this->round('Start');
27
28 2
		$previousGroups = [];
29
30 2
		for ($i=1; $i <= (($countTeams+$byes)/2); $i++) {
31 2
			$g = $startRound->group('Round 1 '.$i)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO);
32 2
			$previousGroups[] = $g;
33
		}
34
35 2
		$this->splitTeams();
36
37 2
		for ($r=2; $r <= $roundsNum; $r++) {
38 2
			$groups = [];
39 2
			$round = $this->round('Round '.$r);
40 2
			for ($g=1; $g <= (($countTeams+$byes)/pow(2, $r)); $g++) {
41 2
				$group = $round->group('Round '.$r.' - '.$g)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO);
42 2
				$groups[] = $group;
43 2
				array_shift($previousGroups)->progression($group, 0, 1); // PROGRESS FROM GROUP BEFORE
44 2
				array_shift($previousGroups)->progression($group, 0, 1); // PROGREESS FROM GROUP BEFORE
45
			}
46 2
			$previousGroups = $groups;
47
		}
48
49 2
		return $this;
50
51
	}
52
53
}
54