Completed
Push — master ( 4f1a31...63e51c )
by Tomáš
01:39
created

Tournament::getTeams()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 21
nc 8
nop 2
dl 0
loc 29
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Tournament
9
{
10
11
	public $name = '';
12
	private $categories = [];
13
	private $rounds = [];
14
	private $teams = [];
15
16
	private $expectedPlay = 0;
17
	private $expectedGameWait = 0;
18
	private $expectedRoundWait = 0;
19
	private $expectedCategoryWait = 0;
20
21
	private $allowSkip = false;
22
23
	function __construct(string $name = ''){
24
		$this->name = $name;
25
	}
26
	public function __toString() {
27
		return $this->name;
28
	}
29
30
	public function setPlay(int $play) {
31
		$this->expectedPlay = $play;
32
		return $this;
33
	}
34
	public function getPlay() {
35
		return $this->expectedPlay;
36
	}
37
	public function setGameWait(int $wait) {
38
		$this->expectedGameWait = $wait;
39
		return $this;
40
	}
41
	public function getGameWait() {
42
		return $this->expectedGameWait;
43
	}
44
	public function setRoundWait(int $wait) {
45
		$this->expectedRoundWait = $wait;
46
		return $this;
47
	}
48
	public function getRoundWait() {
49
		return $this->expectedRoundWait;
50
	}
51
	public function setCategoryWait(int $wait) {
52
		$this->expectedCategoryWait = $wait;
53
		return $this;
54
	}
55
	public function getCategoryWait() {
56
		return $this->expectedCategoryWait;
57
	}
58
	public function getTournamentTime(){
59
		$games = count($this->getGames());
60
		return $games*$this->expectedPlay+$games*$this->expectedGameWait+count($this->getRounds())*$this->expectedRoundWait+count($this->getCategories())*$this->expectedCategoryWait;
61
	}
62
63
	public function allowSkip(){
64
		$this->allowSkip = true;
65
		return $this;
66
	}
67
	public function disallowSkip(){
68
		$this->allowSkip = false;
69
		return $this;
70
	}
71
	public function setSkip(bool $skip) {
72
		$this->allowSkip = $skip;
73
		return $this;
74
	}
75
	public function getSkip() {
76
		return $this->allowSkip;
77
	}
78
79
	public function addCategory(Category ...$categories){
80
		foreach ($categories as $category) {
81
			if ($category instanceof Category) $this->categories[] = $category;
82
			else throw new \Exception('Trying to add category which is not an instance of the Category class.');
83
		}
84
		return $this;
85
	}
86
	public function category(string $name = '') {
87
		$c = new Category($name);
88
		$this->categories[] = $c->setSkip($this->allowSkip);
89
		return $c;
90
	}
91
	public function getCategories() {
92
		return $this->categories;
93
	}
94
95
	public function addRound(Round ...$rounds) {
96
		foreach ($rounds as $round) {
97
			if ($round instanceof Round) $this->rounds[] = $round;
98
			else throw new \Exception('Trying to add round which is not an instance of the Round class.');
99
		}
100
		return $this;
101
	}
102
	public function round(string $name = '') {
103
		$r = new Round($name);
104
		$this->rounds[] = $r->setSkip($this->allowSkip);
105
		return $r;
106
	}
107
	public function getRounds() {
108
		if (count($this->categories) > 0) {
109
			$rounds = [];
110
			foreach ($this->categories as $category) {
111
				$rounds = array_merge($rounds, $category->getRounds());
112
			}
113
			return $rounds;
114
		}
115
		return $this->rounds;
116
	}
117
118
	public function addTeam(...$teams) {
119
		foreach ($teams as $team) {
120
			if ($team instanceof Team)  {
121
				$this->teams[] = $team;
122
			}
123
			elseif (gettype($team) === 'array') {
124
				foreach ($team as $team2) {
125
					if ($team2 instanceof Team) $this->teams[] = $team2;
126
				}
127
			}
128
			else throw new \Exception('Trying to add team which is not an instance of Team class');
129
		}
130
		return $this;
131
	}
132
	public function team(string $name = '') {
133
		$t = new Team($name);
134
		$this->teams[] = $t;
135
		return $t;
136
	}
137
	public function getTeams(bool $ordered = false, $ordering = POINTS) {
1 ignored issue
show
Bug introduced by
The constant TournamentGenerator\POINTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
138
		if (count($this->teams) === 0) {
139
			$teams = [];
140
			foreach ($this->categories as $category) {
141
				$teams = array_merge($teams, $category->getTeams());
142
			}
143
			foreach ($this->rounds as $round) {
144
				$teams = array_merge($teams, $round->getTeams());
145
			}
146
			$this->teams = $teams;
147
		}
148
		if ($ordered) {
149
			switch ($ordering) {
150
				case POINTS:{
151
					uasort($this->teams, function($a, $b) {
152
						if ($a->sumPoints === $b->sumPoints && $a->sumScore === $b->sumScore) return 0;
153
						if ($a->sumPoints === $b->sumPoints) return ($a->sumScore > $b->sumScore ? -1 : 1);
154
						return ($a->sumPoints > $b->sumPoints ? -1 : 1);
155
					});
156
					break;}
157
				case SCORE:{
1 ignored issue
show
Bug introduced by
The constant TournamentGenerator\SCORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
158
					uasort($this->teams, function($a, $b) {
159
						if ($a->sumScore === $b->sumScore) return 0;
160
						return ($a->sumScore > $b->sumScore ? -1 : 1);
161
					});
162
					break;}
163
			}
164
		}
165
		return $this->teams;
166
	}
167
168
	public function getGames() {
169
		$games = [];
170
		foreach ($this->getRounds() as $round) {
171
			$games = array_merge($games, $round->getGames());
172
		}
173
		return $games;
174
	}
175
176
	public function splitTeams(...$wheres) {
177
178
		if (count($wheres) === 0) $wheres = array_merge($this->getRounds(), $this->getCategories());
179
180
		foreach ($wheres as $key => $value) {
181
			if (gettype($value) === 'array') {
182
				unset($wheres[$key]);
183
				$wheres = array_merge($wheres, $value);
184
			}
185
		}
186
187
		$teams = $this->getTeams();
188
		shuffle($teams);
189
190
		while (count($teams) > 0) {
191
			foreach ($wheres as $where) {
192
				if ($where instanceof Round) {
193
					if (count($teams) > 0) $where->addTeam(array_shift($teams));
194
				}
195
				elseif ($where instanceof Category) {
196
					if (count($teams) > 0) $where->addTeam(array_shift($teams));
197
				}
198
			}
199
		}
200
		foreach ($wheres as $where) {
201
			$where->splitTeams();
202
		}
203
		return $this;
204
	}
205
206
	public function genGamesSimulate(bool $returnTime = false) {
207
		$games = [];
208
		if (count($this->categories) > 0) {
209
			foreach ($this->categories as $category) {
210
				$games = array_merge($games, $category->genGamesSimulate());
211
			}
212
		}
213
		elseif (count($this->rounds) > 0) {
214
			foreach ($this->rounds as $round) {
215
				$games = array_merge($games, $round->genGames());
216
				$round->simulate()->progressBlank();
217
			}
218
			foreach ($this->rounds as $round) {
219
				$round->resetGames();
220
			}
221
		}
222
		else {
223
			throw new \Exception('There are no rounds or categories to simulate games from.');
224
		}
225
		if ($returnTime) return $this->getTournamentTime();
226
		return $games;
227
	}
228
	public function genGamesSimulateReal(bool $returnTime = false) {
229
		$games = [];
230
		if (count($this->categories) > 0) {
231
			foreach ($this->categories as $category) {
232
				$games = array_merge($games, $category->genGamesSimulate());
233
			}
234
		}
235
		elseif (count($this->rounds) > 0) {
236
			foreach ($this->rounds as $round) {
237
				$games = array_merge($games, $round->genGames());
238
				$round->simulate();
239
				$round->progress();
240
			}
241
		}
242
		else {
243
			throw new \Exception('There are no rounds or categories to simulate games from.');
244
		}
245
		if ($returnTime) return $this->getTournamentTime();
246
		return $games;
247
	}
248
249
}
250