Passed
Push — master ( b9aa7a...67756f )
by Tomáš
02:47
created

Group::disallowSkip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Group
9
{
10
11
	private $generator = null;
12
	private $teams = []; // ARRAY OF TEAMS
13
	private $progressed = []; // ARRAY OF TEAMS ALREADY PROGRESSED FROM THIS GROUP
14
	public $name = ''; // DISPLAYABLE NAME
15
	private $ordering = \POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS
16
	private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS
17
	private $games = []; // ARRAY OF GAME OBJECTS
18
	public $id = ''; // UNIQID OF GROUP FOR IDENTIFICATIONT
19
	public $winPoints = 3; // POINTS AQUIRED FROM WINNING
20
	public $drawPoints = 1; // POINTS AQUIRED FROM DRAW
21
	public $lostPoints = 0; // POINTS AQUIRED FROM LOOSING
22
	public $secondPoints = 2; // POINTS AQUIRED FROM BEING SECOND (APPLIES ONLY FOR 3 OR 4 INGAME VALUE)
23
	public $thirdPoints = 1; // POINTS AQUIRED FROM BEING THIRD (APPLIES ONLY FOR 4 INGAME VALUE)
24
	public $progressPoints = 50; // POINTS AQUIRED FROM PROGRESSING TO THE NEXT ROUND
25
	public $order = 0; // ORDER OF GROUPS IN ROUND
26
27
	function __construct(array $settings = []) {
28
		$this->id = uniqid();
29
		$this->generator = new Utilis\Generator($this);
30
		foreach ($settings as $key => $value) {
31
			switch ($key) {
32
				case 'name':
33
					if (gettype($value) !== 'string') throw new \Exception('Expected string as group name '.gettype($value).' given');
34
					$this->name = $value;
35
					break;
36
				case 'type':
37
					$this->generator->setType($value);
38
					break;
39
				case 'ordering':
40
					if (!in_array($value, orderingTypes)) throw new \Exception('Unknown group ordering: '.$value);
41
					$this->ordering = $value;
42
					break;
43
				case 'inGame':
44
					$this->generator->setInGame($value);
45
					break;
46
				case 'maxSize':
47
					$value = (int) $value;
48
					if ($value > 1) $this->generator->setMaxSize($value);
49
					break;
50
				case 'order':
51
					$this->order = (int) $value;
52
					break;
53
			}
54
		}
55
	}
56
	public function __toString() {
57
		return 'Group '.$this->name;
58
	}
59
60
	public function allowSkip(){
61
		$this->generator->allowSkip();
62
		return $this;
63
	}
64
	public function disallowSkip(){
65
		$this->generator->disallowSkip();
66
		return $this;
67
	}
68
	public function setSkip(bool $skip) {
69
		$this->generator->setSkip($skip);
70
		return $this;
71
	}
72
	public function getSkip() {
73
		return $this->generator->getSkip();
74
	}
75
76
	public function addTeam(...$teams) {
77
		foreach ($teams as $team) {
78
			if ($team instanceof Team)  {
79
				$this->teams[] = $team;
80
				$team->groupResults[$this->id] = [
81
					'group' => $this,
82
					'points' => 0,
83
					'score'  => 0,
84
					'wins'   => 0,
85
					'draws'  => 0,
86
					'losses' => 0,
87
					'second' => 0,
88
					'third'  => 0
89
				];
90
			}
91
			elseif (gettype($team) === 'array') {
92
				foreach ($team as $team2) {
93
					if ($team2 instanceof Team) $this->teams[] = $team2;
94
					$team2->groupResults[$this->id] = [
95
						'group' => $this,
96
						'points' => 0,
97
						'score'  => 0,
98
						'wins'   => 0,
99
						'draws'  => 0,
100
						'losses' => 0,
101
						'second' => 0,
102
						'third'  => 0
103
					];
104
				}
105
			}
106
			else throw new \Exception('Trying to add team which is not an instance of Team class');
107
		}
108
		return $this;
109
	}
110
	public function getTeams($filters = []) {
111
		$teams = $this->teams;
112
113
		if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) $filters = [$filters];
114
		elseif (gettype($filters) !== 'array') $filters = [];
115
116
		// APPLY FILTERS
117
		$filter = new Filter($this, $filters);
118
		$filter->filter($teams);
119
120
		return $teams;
121
	}
122
123
	public function team(string $name = '') {
124
		$t = new Team($name);
125
		$this->teams[] = $t;
126
		$t->groupResults[$this->id] = [
127
			'group' => $this,
128
			'points' => 0,
129
			'score'  => 0,
130
			'wins'   => 0,
131
			'draws'  => 0,
132
			'losses' => 0,
133
			'second' => 0,
134
			'third'  => 0
135
		];
136
		return $t;
137
	}
138
	public function sortTeams($filters = [], $ordering = null) {
139
		if (!isset($ordering)) $ordering = $this->ordering;
140
		Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering);
141
		return $this->getTeams($filters);
142
	}
143
144
	public function setType(string $type = \R_R) {
145
		$this->generator->setType($type);
146
		return $this;
147
	}
148
	public function getType() {
149
		return $this->generator->getType();
150
	}
151
152
	public function setOrdering(string $ordering = \POINTS) {
153
		if (in_array($ordering, orderingTypes)) $this->ordering = $ordering;
154
		else throw new \Exception('Unknown group ordering: '.$ordering);
155
		return $this;
156
	}
157
	public function getOrdering() {
158
		return $this->ordering;
159
	}
160
161
	public function setInGame(int $inGame) {
162
		$this->generator->setInGame($inGame);
163
		return $this;
164
	}
165
	public function getInGame() {
166
		return $this->generator->getInGame();
167
	}
168
169
	public function addProgression(Progression $progression) {
170
		$this->progressions[] = $progression;
171
		return $this;
172
	}
173
	public function progression(Group $to, int $start = 0, int $len = null) {
174
		$p = new Progression($this, $to, $start, $len);
175
		$this->progressions[] = $p;
176
		return $p;
177
	}
178
	public function progress(bool $blank = false) {
179
		foreach ($this->progressions as $progression) {
180
			$progression->progress($blank);
181
		}
182
	}
183
	public function addProgressed(...$teams) {
184
		foreach ($teams as $team) {
185
			if ($team instanceOf Team) {
186
				$this->progressed[] = $team->id;
187
			}
188
			elseif (gettype($team) === 'string' || gettype($team) === 'integer') {
189
				$this->progressed[] = $team;
190
			}
191
			elseif (gettype($team) === 'array') {
192
				foreach ($team as $teamInner) {
193
					if ($teamInner instanceOf Team) {
194
						$this->progressed[] = $teamInner->id;
195
					}
196
					elseif (gettype($teamInner) === 'string' || gettype($teamInner) === 'integer') {
197
						$this->progressed[] = $teamInner;
198
					}
199
				}
200
			}
201
		}
202
		return $this;
203
	}
204
	public function isProgressed(Team $team) {
205
		if (in_array($team->id, $this->progressed)) {
206
			return true;
207
		}
208
		return false;
209
	}
210
211
	public function genGames() {
212
		$this->generator->genGames();
213
		return $this->games;
214
	}
215
216
	public function game(array $teams = []) {
217
		$g = new Game($teams, $this);
218
		$this->games[] = $g;
219
		return $g;
220
	}
221
	public function addGame(...$games){
222
		foreach ($games as $key => $game) {
223
			if (gettype($game) === 'array') {
224
				unset($games[$key]);
225
				$games = array_merge($games, $game);
226
			}
227
		}
228
		foreach ($games as $game) {
229
			if ($game instanceof Game) $this->games[] = $game;
230
			else throw new \Exception('Trying to add game which is not instance of Game object.');
231
		}
232
		return $this;
233
	}
234
	public function getGames() {
235
		return $this->games;
236
	}
237
	public function orderGames() {
238
		if (count($this->games) <= 4) return $this->games;
239
		$this->games = $this->generator->orderGames();
240
		return $this->games;
241
	}
242
243
	public function simulate($filters = [], bool $reset = true) {
244
		foreach ($this->getGames() as $game) {
245
			$teams = $game->getTeams();
246
			$results = [];
247
			foreach ($teams as $team) {
248
				$results[$team->id] = floor(rand(-1000, 5000));
249
			}
250
			$game->setResults($results);
251
		}
252
		$return = $this->sortTeams($filters);
253
		if (!$reset) return $return;
254
		foreach ($this->getGames() as $game) {
255
			$game->resetResults();
256
		}
257
		return $return;
258
	}
259
	public function resetGames() {
260
		foreach ($this->getGames() as $game) {
261
			if (isset($game)) $game->resetResults();
262
		}
263
		return $this;
264
	}
265
	public function isPlayed(){
266
		foreach ($this->games as $game) {
267
			if ((isset($game) || !$this->getSkip()) && !$game->isPlayed()) return false;
268
		}
269
		return true;
270
	}
271
272
}
273