Passed
Push — master ( f2e9a4...5d9f44 )
by Tomáš
01:51
created

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