Completed
Push — master ( 417953...8b93c6 )
by Tomáš
02:10
created

Game::__construct()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

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