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

SingleElimination::generate()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 12
nop 0
dl 0
loc 39
ccs 24
cts 24
cp 1
crap 5
rs 9.2408
c 0
b 0
f 0
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