Passed
Push — master ( 4b85e8...da6ead )
by Tomáš
01:57
created

Game::getResults()   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 Game
9
{
10
11
	private $teams = [];
12
	private $results = [];
13
	private $group = null;
14
	private $winId = null;
15
	private $lossId = null;
16
	private $secondId = null;
17
	private $thirdId = null;
18
	private $drawIds = [];
19
20
	function __construct(array $teams, Group $group) {
21
		$this->group = $group;
22
		$error = [];
23
		$tids = [];
24
		foreach ($teams as $key => $team) {
25
			if (!$team instanceof Team) {
26
				$error[] = $team;
27
				unset($teams[$key]);
28
				continue;
29
			}
30
			$team->addGame($this);
31
			$tids[] = $team->getId();
32
		}
33
		$this->teams = $teams;
34
		foreach ($this->teams as $team) {
35
			foreach ($this->teams as $team2) {
36
				if ($team === $team2) continue;
37
				$team->addGameWith($team2, $group);
38
			}
39
		}
40
		if (count($error) > 0) throw new \Exception('Trying to add teams ('.count($error).') that are not instance of Team class'.PHP_EOL.print_r($error, true));
41
	}
42
43
	public function getGroup() {
44
		return $this->group;
45
	}
46
47
	public function addTeam(...$teams) {
48
		$error = [];
49
		foreach ($teams as $key => $team) {
50
			if (!$team instanceof Team) {
51
				$error[] = $team;
52
				unset($teams[$key]);
53
				continue;
54
			}
55
			$this->teams[] = $team;
56
			$team->addGame($this);
57
58
			foreach ($this->teams as $team2) {
59
				if ($team === $team2) continue;
60
				if ($team instanceof Team) {
61
					$team->addGameWith($team2, $this->group);
62
					$team2->addGameWith($team, $this->group);
63
				}
64
			}
65
		}
66
		if (count($error) > 0) throw new \Exception('Trying to add teams ('.count($error).') that are not instance of Team class'.PHP_EOL.print_r($error, true));
67
		return $this;
68
	}
69
	public function getTeams(){
70
		return $this->teams;
71
	}
72
	public function getTeamsIds(){
73
		return array_map(function($a){ return $a->getId(); }, $this->teams);
74
	}
75
	public function getTeam(string $id) {
76
		foreach ($this->teams as $team) {
77
			if ($team->getId() === $id) return $team;
78
		}
79
		return false;
80
	}
81
82
	/**
83
	* $results = array (
84
	* * team->getId() => team->score
85
	* )
86
	*/
87
	public function setResults(array $results = []) {
88
		if (count($this->results) === 0) $this->resetResults();
89
		arsort($results);
90
		$inGame = /** @scrutinizer ignore-call */ $this->group->getInGame();
91
		$i = 1;
92
		foreach ($results as $id => $score) {
93
			$team = $this->getTeam($id);
94
			if ($team === false) throw new \Exception('Couldn\'t find team with id of "'.$id.'"');
95
			$this->results[$team->getId()] = ['score' => $score];
96
			$team->sumScore += $score;
97
			$prev = prev($results);
98
			next($results);
99
			$next = next($results);
100
			switch ($inGame) {
101
				case 2:
102
					$this->setResults2($i, $score, $prev, $next, $team);
103
					break;
104
				case 3:
105
					$this->setResults3($i, $team);
106
					break;
107
				case 4:
108
					$this->setResults4($i, $team);
109
					break;
110
			}
111
			$team->groupResults[$this->group->getId()]['score'] += $score;
112
			$i++;
113
		}
114
		return $this;
115
	}
116
	private function setResults2($i, $score, $prev, $next, $team) {
117
		if ($score === $prev || $score === $next) {
118
			$this->drawIds[] = $team->getId();
119
			$team->addDraw($this->group->getId());
120
			$this->results[$team->getId()] += ['points' => $this->group->drawPoints, 'type' => 'draw'];
121
		}
122
		elseif ($i === 1) {
123
			$this->winId = $team->getId();
124
			$team->addWin($this->group->getId());
125
			$this->results[$team->getId()] += ['points' => $this->group->winPoints, 'type' => 'win'];
126
		}
127
		else {
128
			$this->lossId = $team->getId();
129
			$team->addLoss($this->group->getId());
130
			$this->results[$team->getId()] += ['points' => $this->group->lostPoints, 'type' => 'loss'];
131
		}
132
		return $this;
133
	}
134
	private function setResults3($i, $team) {
135
		switch ($i) {
136
			case 1:
137
				$this->winId = $team->getId();
138
				$team->addWin($this->group->getId());
139
				$this->results[$team->getId()] += ['points' => $this->group->winPoints, 'type' => 'win'];
140
				break;
141
			case 2:
142
				$this->secondId = $team->getId();
143
				$team->addSecond($this->group->getId());
144
				$this->results[$team->getId()] += ['points' => $this->group->secondPoints, 'type' => 'second'];
145
				break;
146
			case 3:
147
				$this->lossId = $team->getId();
148
				$team->addLoss($this->group->getId());
149
				$this->results[$team->getId()] += ['points' => $this->group->lostPoints, 'type' => 'loss'];
150
				break;
151
		}
152
		return $this;
153
	}
154
	private function setResults4($i, $team) {
155
		switch ($i) {
156
			case 1:
157
				$this->winId = $team->getId();
158
				$team->addWin($this->group->getId());
159
				$this->results[$team->getId()] += ['points' => $this->group->winPoints, 'type' => 'win'];
160
				break;
161
			case 2:
162
				$this->secondId = $team->getId();
163
				$team->addSecond($this->group->getId());
164
				$this->results[$team->getId()] += ['points' => $this->group->secondPoints, 'type' => 'second'];
165
				break;
166
			case 3:
167
				$this->thirdId = $team->getId();
168
				$team->addThird($this->group->getId());
169
				$this->results[$team->getId()] += ['points' => $this->group->thirdPoints, 'type' => 'third'];
170
				break;
171
			case 4:
172
				$this->lossId = $team->getId();
173
				$team->addLoss($this->group->getId());
174
				$this->results[$team->getId()] += ['points' => $this->group->lostPoints, 'type' => 'loss'];
175
				break;
176
		}
177
		return $this;
178
	}
179
	public function getResults() {
180
		return $this->results;
181
	}
182
183
	public function resetResults() {
184
		foreach ($this->results as $teamId => $score) {
185
			$team = $this->getTeam($teamId);
186
			$team->groupResults[$this->group->getId()]['score'] -= $score['score'];
187
			$team->sumScore -= $score['score'];
188
			switch ($score['type']) {
189
				case 'win':
190
					$team->removeWin($this->group->getId());
191
					break;
192
				case 'draw':
193
					$team->removeDraw($this->group->getId());
194
					break;
195
				case 'loss':
196
					$team->removeLoss($this->group->getId());
197
					break;
198
				case 'second':
199
					$team->removeSecond($this->group->getId());
200
					break;
201
				case 'third':
202
					$team->removeThird($this->group->getId());
203
					break;
204
			}
205
		}
206
		$this->results = [];
207
		return $this;
208
	}
209
	public function getWin() {
210
		return $this->winId;
211
	}
212
	public function getLoss() {
213
		return $this->lossId;
214
	}
215
	public function getSecond() {
216
		return $this->secondId;
217
	}
218
	public function getThird() {
219
		return $this->thirdId;
220
	}
221
	public function getDraw() {
222
		return $this->drawIds;
223
	}
224
225
	public function isPlayed() {
226
		if (count($this->results) > 0) return true;
227
		return false;
228
	}
229
}
230