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

Team::addScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Team
9
{
10
11
	private $name = 'team';
12
	private $id = '';
13
	public $games = [];
14
	public $gamesWith = [];
15
	public $sumPoints = 0;
16
	public $sumScore = 0;
17
18
	/**
19
	* ARRAY WITH GROUPS AND IT'S RESULTS
20
	* array (
21
	* * groupId => array (
22
	* * * "group"  => Group, # GROUP OBJECT
23
	* * * "points" => int 0, # NUMBER OF POINTS AQUIRED
24
	* * * "score"  => int 0, # SUM OF SCORE AQUIRED
25
	* * * "wins"   => int 0, # NUMBER OF WINS
26
	* * * "draws"  => int 0, # NUMBER OF DRAWS
27
	* * * "losses" => int 0, # NUMBER OF LOSSES
28
	* * * "second" => int 0, # NUMBER OF TIMES BEING SECOND (ONLY FOR INGAME OPTION OF 3 OR 4)
29
	* * * "third"  => int 0  # NUMBER OF TIMES BEING THIRD  (ONLY FOR INGAME OPTION OF 4)
30
	* * )
31
	*)
32
	*/
33
	public $groupResults = [];
34
35 57
	function __construct(string $name = 'team', $id = null) {
36 57
		$this->setName($name);
37 57
		$this->setId(isset($id) ? $id : uniqid());
38 57
	}
39 21
	public function __toString() {
40 21
		return $this->name;
41
	}
42
	public function getGamesInfo($groupId) {
43
		return array_filter($this->groupResults[$groupId], function($k) { return $k !== 'group'; }, ARRAY_FILTER_USE_KEY);
44
	}
45
46 57
	public function setName(string $name) {
47 57
		$this->name = $name;
48 57
	}
49 10
	public function getName() {
50 10
		return $this->name;
51
	}
52 57
	public function setId($id) {
53 57
		if (!is_string($id) && !is_int($id)) {
54
			$this->id = uniqid();
55
			throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int');
56
		}
57 57
		$this->id = $id;
58 57
	}
59 45
	public function getId() {
60 45
		return $this->id;
61
	}
62
63 44
	public function addGroupResults(Group $group) {
64 44
		$this->groupResults[$group->getId()] = [
65 44
			'group' => $group,
66 44
			'points' => 0,
67 44
			'score'  => 0,
68 44
			'wins'   => 0,
69 44
			'draws'  => 0,
70 44
			'losses' => 0,
71 44
			'second' => 0,
72 44
			'third'  => 0
73
		];
74 44
		return $this;
75
	}
76 6
	public function getGroupResults($groupId = null) {
77 6
		if (isset($groupId)) {
78 4
			if (!isset($this->groupResults[$groupId])) throw new \Exception('Trying to get unexisting group results ('.$groupId.')');
79 4
			return $this->groupResults[$groupId];
80
		}
81 2
		return $this->groupResults;
82
	}
83
84 45
	public function addGameWith(Team $team, Group $group) {
85 45
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) $this->gamesWith[$group->getId()][$team->getId()] = 0;
86 45
		$this->gamesWith[$group->getId()][$team->getId()]++;
87 45
		return $this;
88
	}
89 2
	public function getGameWith(Team $team = null, Group $group = null) {
90 2
		if (isset($group)) {
91 2
			if (isset($team)) return $this->gamesWith[$group->getId()][$team->getId()];
92 2
			return $this->gamesWith[$group->getId()];
93
		}
94 1
		if (isset($team)) {
95 1
			$return = [];
96 1
			foreach ($this->gamesWith as $id => $games) {
97
				$filter = array_filter($games, function($key) use ($team){
98 1
					return $key === $team->getId();
99 1
				}, ARRAY_FILTER_USE_KEY);
100 1
				if (count($filter) > 0) $return[$id] = $filter;
101
			}
102 1
			return $return;
103
		}
104
		return $this->gamesWith;
105
	}
106
	public function addGroup(Group $group) {
107
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
108
		return $this;
109
	}
110 45
	public function addGame(Game $game) {
111 45
		$group = $game->getGroup();
112 45
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
113 45
		$this->games[$group->getId()][] = $game;
114 45
		return $this;
115
	}
116
117 4
	public function getSumPoints() {
118 4
		return $this->sumPoints;
119
	}
120 4
	public function getSumScore() {
121 4
		return $this->sumScore;
122
	}
123
124 27
	public function addWin(string $groupId = ''){
125 27
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
126 27
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints;
127 27
		$this->sumPoints += $this->groupResults[$groupId]['group']->winPoints;
128 27
		$this->groupResults[$groupId]['wins']++;
129 27
		return $this;
130
	}
131 9
	public function removeWin(string $groupId = ''){
132 9
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
133 9
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints;
134 9
		$this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints;
135 9
		$this->groupResults[$groupId]['wins']--;
136 9
		return $this;
137
	}
138
139 2
	public function addDraw(string $groupId = ''){
140 2
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
141 2
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints;
142 2
		$this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints;
143 2
		$this->groupResults[$groupId]['draws']++;
144 2
		return $this;
145
	}
146 1
	public function removeDraw(string $groupId = ''){
147 1
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
148 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->drawPoints;
149 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->drawPoints;
150 1
		$this->groupResults[$groupId]['draws']--;
151 1
		return $this;
152
	}
153
154 27
	public function addLoss(string $groupId = ''){
155 27
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
156 27
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints;
157 27
		$this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints;
158 27
		$this->groupResults[$groupId]['losses']++;
159 27
		return $this;
160
	}
161 9
	public function removeLoss(string $groupId = ''){
162 9
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
163 9
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints;
164 9
		$this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints;
165 9
		$this->groupResults[$groupId]['losses']--;
166 9
		return $this;
167
	}
168
169 3
	public function addSecond(string $groupId = ''){
170 3
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
171 3
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->secondPoints;
172 3
		$this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints;
173 3
		$this->groupResults[$groupId]['second']++;
174 3
		return $this;
175
	}
176 2
	public function removeSecond(string $groupId = ''){
177 2
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
178 2
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->secondPoints;
179 2
		$this->sumPoints -= $this->groupResults[$groupId]['group']->secondPoints;
180 2
		$this->groupResults[$groupId]['second']--;
181 2
		return $this;
182
	}
183
184 2
	public function addThird(string $groupId = ''){
185 2
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
186 2
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->thirdPoints;
187 2
		$this->sumPoints += $this->groupResults[$groupId]['group']->thirdPoints;
188 2
		$this->groupResults[$groupId]['third']++;
189 2
		return $this;
190
	}
191 1
	public function removeThird(string $groupId = ''){
192 1
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
193 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->thirdPoints;
194 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints;
195 1
		$this->groupResults[$groupId]['third']--;
196 1
		return $this;
197
	}
198
199 18
	public function sumPoints(array $groupIds = []) {
200 18
		if (count($groupIds) === 0) return $this->sumPoints;
201 18
		$sum = 0;
202 18
		foreach ($groupIds as $gid) {
203 18
			$sum += $this->groupResults[$gid]['points'] ?? 0;
204
		}
205 18
		return $sum;
206
	}
207 12
	public function sumScore(array $groupIds = []) {
208 12
		if (count($groupIds) === 0) return $this->sumScore;
209 12
		$sum = 0;
210 12
		foreach ($groupIds as $gid) {
211 12
			$sum += $this->groupResults[$gid]['score'] ?? 0;
212
		}
213 12
		return $sum;
214
	}
215
216 29
	public function addScore(int $score) {
217 29
		$this->sumScore += $score;
218 29
	}
219
	public function removeScore(int $score) {
220
		$this->sumScore -= $score;
221
	}
222
}
223