Passed
Push — master ( 0e4a87...bf2039 )
by Tomáš
01:54
created

Generator::getSkip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator\Utilis;
4
5
/**
6
 *
7
 */
8
class Generator
9
{
10
11
	private $group = null;
12
	private $type = \TournamentGenerator\R_R; // TYPE OF ROUND TO CREATE A LAYOUT
13
	private $inGame = 2; // NUMBER OF TEAMS IN ONE GAME - 2/3/4
14
	private $maxSize = 4; // MAX SIZE OF GROUP BEFORE SPLIT
15
	private $allowSkip = false; // IF IS NUMBER OF TEAMS LESS THAN $this->inGame THEN SKIP PLAYING THIS GROUP
16
	private $games = []; // ARRAY OF GAME OBJECTS
0 ignored issues
show
introduced by
The private property $games is not used, and could be removed.
Loading history...
17
18
	function __construct(Group $group) {
0 ignored issues
show
Bug introduced by
The type TournamentGenerator\Utilis\Group was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
		$this->group = $group;
20
	}
21
22
	public function allowSkip(){
23
		$this->allowSkip = true;
24
		return $this;
25
	}
26
	public function disallowSkip(){
27
		$this->allowSkip = false;
28
		return $this;
29
	}
30
	public function setSkip(bool $skip) {
31
		$this->allowSkip = $skip;
32
		return $this;
33
	}
34
	public function getSkip() {
35
		return $this->allowSkip;
36
	}
37
38
39
	public function setType(/** @scrutinizer ignore-all */ string $type = \TournamentGenerator\R_R) {
40
		if (in_array($type, \TournamentGenerator\groupTypes)) $this->type = $type;
41
		else throw new \Exception('Unknown group type: '.$type);
42
		return $this;
43
	}
44
	public function getType() {
45
		return $this->type;
46
	}
47
48
	public function setInGame(int $inGame) {
49
		if ($inGame < 2 ||  $inGame > 4) throw new \Exception('Expected 2,3 or 4 as inGame '.$inGame.' given');
50
		$this->inGame = $inGame;
51
		return $this;
52
	}
53
	public function getInGame() {
54
		return $this->inGame;
55
	}
56
57
	public function setMaxSize(int $maxSize) {
58
		if ($maxSize < 2) throw new \Exception('Max group size has to be at least 2, '.$maxSize.' given');
59
		$this->maxSize = $maxSize;
60
		return $this;
61
	}
62
	public function getMaxSize() {
63
		return $this->maxSize;
64
	}
65
66
	public function genGames() {
67
		switch ($this->type) {
68
			case \TournamentGenerator\R_R:{
69
					$this->group->addGame($this->r_rGames());
70
				break;}
71
			case \TournamentGenerator\TWO_TWO:
72
				$teams = $this->group->getTeams();
73
				$discard = [];
74
				shuffle($teams);
75
				$count = count($teams);
76
				while (count($teams) % $this->inGame !== 0) { $discard[] = array_shift($teams); }
77
78
				while (count($teams) > 0) {
79
					$tInGame = [];
80
					for ($i=0; $i < $this->inGame; $i++) { $tInGame[] = array_shift($teams); }
81
					$this->group->game($tInGame);
82
				}
83
84
				if (count($discard) > 0 && !$this->allowSkip) throw new \Exception('Couldn\'t make games with all teams. Expected k*'.$this->inGame.' teams '.$count.' teams given - discarting '.count($discard).' teams ('.implode(', ', $discard).') in group '.$this->group.' - allow skip '.($this->allowSkip ? 'True' : 'False'));
85
				break;
86
			case \TournamentGenerator\COND_SPLIT:
87
				$games = [];
88
				$teams = $this->group->getTeams();
89
				if (count($teams) > $this->maxSize) {
90
					$groups = array_chunk($teams, /** @scrutinizer ignore-type */ ceil(count($teams)/ceil(count($teams)/$this->maxSize))); // SPLIT TEAMS INTO GROUP OF MAXIMUM SIZE OF $this->maxSize
91
					foreach ($groups as $group) { $games[] = $this->r_rGames($group); }
92
					$g = 0;
93
					foreach ($games as $group) {
94
						$g += count($group);
95
					}
96
					while ($g > 0) {
97
						foreach ($games as $key => $group) {
98
							$this->group->addGame(array_shift($games[$key]));
99
							if (count($games[$key]) === 0) unset($games[$key]);
100
							$g--;
101
						}
102
					}
103
				}
104
				else $this->group->addGame($this->r_rGames());
105
				break;
106
		}
107
		return $this->group->getGames();
108
	}
109
	public function r_rGames(array $teams = []) {
110
		$games = [];
111
		if (count($teams) === 0) $teams = $this->group->getTeams();
112
		switch ($this->inGame) {
113
			case 2:
114
				$games = Generator::circle_genGames2($teams, $this->group);
115
				break;
116
			case 3:{
117
				$teamsB = $teams;
118
				while (count($teamsB) >= 3) {
119
					$lockedTeam = array_shift($teamsB);
120
					$gamesTemp = Generator::circle_genGames2($teamsB, $this->group);
121
					foreach ($gamesTemp as $game) {
122
						$game->addTeam($lockedTeam);
123
					}
124
					$games = array_merge($games, $gamesTemp);
125
				}
126
				break;}
127
			case 4:{
128
				$teamsB = $teams;
129
				$lockedTeam1 = array_shift($teamsB);
130
				while (count($teamsB) >= 4) {
131
					$teamsB2 = $teamsB;
132
					while (count($teamsB2) >= 3) {
133
						$lockedTeam2 = array_shift($teamsB2);
134
						$gamesTemp = Generator::circle_genGames2($teamsB2, $this->group);
135
						foreach ($gamesTemp as $game) {
136
							$game->addTeam($lockedTeam1, $lockedTeam2);
137
						}
138
						$games = array_merge($games, $gamesTemp);
139
					}
140
					$lockedTeam1 = array_shift($teamsB);
141
				}
142
				$games[] = new \TournamentGenerator\Game(array_merge([$lockedTeam1], $teamsB), $this->group);
143
				break;}
144
		}
145
		return $games;
146
	}
147
148
	public function orderGames() {
149
		$sorter = new \Sorter\Games($this, $this->group);
0 ignored issues
show
Bug introduced by
The type Sorter\Games was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
151
		return $sorter->orderGames();
152
	}
153
154
	// GENERATES A ROBIN-ROBIN BRACKET
155
	public static function circle_genGames2(array $teams = [], Group $group) {
156
		$bracket = []; // ARRAY OF GAMES
157
158
		if (count($teams) % 2 != 0) $teams[] = \TournamentGenerator\DUMMY_TEAM; // IF NOT EVEN NUMBER OF TEAMS, ADD DUMMY
159
160
		shuffle($teams); // SHUFFLE TEAMS FOR MORE RANDOMNESS
161
162
		for ($i=0; $i < count($teams)-1; $i++) {
163
			$bracket = array_merge($bracket, Generator::circle_saveBracket($teams, $group)); // SAVE CURRENT ROUND
164
165
			$teams = Generator::circle_rotateBracket($teams); // ROTATE TEAMS IN BRACKET
166
		}
167
168
		return $bracket;
169
170
	}
171
	// CREATE GAMES FROM BRACKET
172
	public static function circle_saveBracket(array $teams, Group $group) {
173
174
		$bracket = [];
175
176
		for ($i=0; $i < count($teams)/2; $i++) { // GO THROUGH HALF OF THE TEAMS
177
178
			$home = $teams[$i];
179
			$reverse = array_reverse($teams);
180
			$away = $reverse[$i];
181
182
			if (($home == \TournamentGenerator\DUMMY_TEAM || $away == \TournamentGenerator\DUMMY_TEAM)) continue; // SKIP WHEN DUMMY_TEAM IS PRESENT
183
184
			$bracket[] = new \TournamentGenerator\Game([$home, $away], $group);
185
186
		}
187
188
		return $bracket;
189
190
	}
191
	// ROTATE TEAMS IN BRACKET
192
	public static function circle_rotateBracket(array $teams) {
193
194
		$first = array_shift($teams); // THE FIRST TEAM REMAINS FIRST
195
		$last = array_shift($teams); // THE SECOND TEAM MOVES TO LAST PLACE
196
197
		$teams = array_merge([$first], $teams, [$last]); // MERGE BACK TOGETHER
198
199
		return $teams;
200
201
	}
202
203
}
204