Completed
Push — master ( 5d9f44...4b85e8 )
by Tomáš
03:31
created

SingleElimination::generate()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 12
nop 0
dl 0
loc 39
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
	public function generate() {
12
13
		$this->allowSkip();
14
15
		$countTeams = count($this->getTeams());
16
17
		// CALCULATE BYES
18
		$byes = 0;
19
		if ( !\TournamentGenerator\isPowerOf2($countTeams) ) {
20
			$nextPow = bindec(str_pad(1, strlen(decbin($countTeams))+1, 0, STR_PAD_RIGHT));
21
			$byes = $nextPow-$countTeams;
22
		}
23
24
		$roundsNum = log($countTeams+$byes, 2); // NUMBER OF ROUNDS
25
26
		$startRound = $this->round('Start');
27
28
		$previousGroups = [];
29
30
		for ($i=1; $i <= (($countTeams+$byes)/2); $i++) {
31
			$g = $startRound->group('Round 1 '.$i)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO);
32
			$previousGroups[] = $g;
33
		}
34
35
		$this->splitTeams();
36
37
		for ($r=2; $r <= $roundsNum; $r++) {
38
			$groups = [];
39
			$round = $this->round('Round '.$r);
40
			for ($g=1; $g <= (($countTeams+$byes)/pow(2, $r)); $g++) {
41
				$group = $round->group('Round '.$r.' - '.$g)->setInGame(2)->setType(\TournamentGenerator\Constants::ROUND_TWO);
42
				$groups[] = $group;
43
				array_shift($previousGroups)->progression($group, 0, 1); // PROGRESS FROM GROUP BEFORE
44
				array_shift($previousGroups)->progression($group, 0, 1); // PROGREESS FROM GROUP BEFORE
45
			}
46
			$previousGroups = $groups;
47
		}
48
49
		return $this;
50
51
	}
52
53
}
54