Completed
Push — master ( da6ead...647271 )
by Tomáš
02:03
created

Group::setMaxSize()   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 1
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
	private $name = ''; // DISPLAYABLE NAME
15
	private $ordering = \TournamentGenerator\Constants::POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS
16
	private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS
17
	private $games = []; // ARRAY OF GAME OBJECTS
18
	private $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
	private $order = 0; // ORDER OF GROUPS IN ROUND
26
27
	function __construct(string $name, $id = null) {
28
		$this->setName($name);
29
		$this->generator = new Utilis\Generator($this);
30
		$this->setId(isset($id) ? $id : uniqid());
31
	}
32
	public function __toString() {
33
		return $this->name;
34
	}
35
36
	public function setName(string $name) {
37
		$this->name = $name;
38
	}
39
	public function getName() {
40
		return $this->name;
41
	}
42
	public function setId($id) {
43
		if (!is_string($id) && !is_int($id)) {
44
			$this->id = uniqid();
45
			throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int');
46
		}
47
		$this->id = $id;
48
	}
49
	public function getId() {
50
		return $this->id;
51
	}
52
53
	public function allowSkip(){
54
		$this->generator->allowSkip();
55
		return $this;
56
	}
57
	public function disallowSkip(){
58
		$this->generator->disallowSkip();
59
		return $this;
60
	}
61
	public function setSkip(bool $skip) {
62
		$this->generator->setSkip($skip);
63
		return $this;
64
	}
65
	public function getSkip() {
66
		return $this->generator->getSkip();
67
	}
68
69
	public function addTeam(...$teams) {
70
		foreach ($teams as $team) {
71
			if (is_array($team)) {
72
				foreach ($team as $team2) {
73
					$this->setTeam($team2);
74
				}
75
				continue;
76
			}
77
			$this->setTeam($team);
78
		}
79
		return $this;
80
	}
81
	private function setTeam(Team $team) {
82
		$this->teams[] = $team;
83
		$team->addGroupResults($this);
84
		return $this;
85
	}
86
	public function getTeams(array $filters = []) {
87
		$teams = $this->teams;
88
89
		// APPLY FILTERS
90
		$filter = new Filter($this, $filters);
91
		$filter->filter($teams);
92
93
		return $teams;
94
	}
95
96
	public function team(string $name = '') {
97
		$t = new Team($name);
98
		$this->teams[] = $t;
99
		$t->addGroupResults($this);
100
		return $t;
101
	}
102
	public function sortTeams(array $filters = [], $ordering = null) {
103
		if (!isset($ordering)) $ordering = $this->ordering;
104
		Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering);
105
		return $this->getTeams($filters);
106
	}
107
108
	public function setMaxSize(int $size) {
109
		$this->generator->setMaxSize($size);
110
		return $this;
111
	}
112
	public function getMaxSize() {
113
		return $this->generator->getMaxSize();
114
	}
115
116
	public function setType(string $type = \TournamentGenerator\Constants::ROUND_ROBIN) {
117
		$this->generator->setType($type);
118
		return $this;
119
	}
120
	public function getType() {
121
		return $this->generator->getType();
122
	}
123
124
	public function setOrder(int $order) {
125
		$this->order = $order;
126
		return $this;
127
	}
128
	public function getOrder() {
129
		return $this->order;
130
	}
131
132
	public function setOrdering(string $ordering = \TournamentGenerator\Constants::POINTS) {
133
		if (!in_array($ordering, \TournamentGenerator\Constants::OrderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering);
134
		$this->ordering = $ordering;
135
		return $this;
136
	}
137
	public function getOrdering() {
138
		return $this->ordering;
139
	}
140
141
	public function setInGame(int $inGame) {
142
		$this->generator->setInGame($inGame);
143
		return $this;
144
	}
145
	public function getInGame() {
146
		return $this->generator->getInGame();
147
	}
148
149
	public function addProgression(Progression $progression) {
150
		$this->progressions[] = $progression;
151
		return $this;
152
	}
153
	public function progression(Group $to, int $start = 0, int $len = null) {
154
		$p = new Progression($this, $to, $start, $len);
155
		$this->progressions[] = $p;
156
		return $p;
157
	}
158
	public function progress(bool $blank = false) {
159
		foreach ($this->progressions as $progression) {
160
			$progression->progress($blank);
161
		}
162
	}
163
	public function addProgressed(...$teams) {
164
		foreach ($teams as $team) {
165
			if ($team instanceOf Team) $this->progressed[] = $team;
166
			elseif (gettype($team) === 'array') {
167
				$this->progressed = array_merge($this->progressed, array_filter($team, function($a) {
168
					return ($a instanceof Team);
169
				}));
170
			}
171
		}
172
		return $this;
173
	}
174
	public function isProgressed(Team $team) {
175
		return in_array($team, $this->progressed);
176
	}
177
178
	public function genGames() {
179
		$this->generator->genGames();
180
		return $this->games;
181
	}
182
183
	public function game(array $teams = []) {
184
		$g = new Game($teams, $this);
185
		$this->games[] = $g;
186
		return $g;
187
	}
188
	public function addGame(...$games){
189
		foreach ($games as $key => $game) {
190
			if (gettype($game) === 'array') {
191
				unset($games[$key]);
192
				$this->games = array_merge($this->games, array_filter($game, function($a){ return ($a instanceof Game); }));
193
				continue;
194
			}
195
			if (!$game instanceof Game) throw new \Exception('Trying to add game which is not instance of Game object.');
196
			$this->games[] = $game;
197
		}
198
		return $this;
199
	}
200
	public function getGames() {
201
		return $this->games;
202
	}
203
	public function orderGames() {
204
		if (count($this->games) <= 4) return $this->games;
205
		$this->games = $this->generator->orderGames();
206
		return $this->games;
207
	}
208
209
	public function simulate(array $filters = [], bool $reset = true) {
210
		return Utilis\Simulator::simulateGroup($this, $filters, $reset);
211
	}
212
	public function resetGames() {
213
		foreach ($this->getGames() as $game) {
214
			$game->resetResults();
215
		}
216
		return $this;
217
	}
218
	public function isPlayed(){
219
		if (count($this->games) === 0) return false;
220
		foreach ($this->games as $game) {
221
			if (!$game->isPlayed()) return false;
222
		}
223
		return true;
224
	}
225
226
}
227