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

Team::addSecond()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 6
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
	function __construct(string $name = 'team', $id = null) {
36
		$this->setName($name);
37
		$this->setId(isset($id) ? $id : uniqid());
38
	}
39
	public function __toString() {
40
		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
	public function setName(string $name) {
47
		$this->name = $name;
48
	}
49
	public function getName() {
50
		return $this->name;
51
	}
52
	public function setId($id) {
53
		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');
0 ignored issues
show
Bug introduced by
The type TournamentGenerator\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
56
		}
57
		$this->id = $id;
58
	}
59
	public function getId() {
60
		return $this->id;
61
	}
62
63
	public function addGroupResults(Group $group) {
64
		$this->groupResults[$group->getId()] = [
65
			'group' => $group,
66
			'points' => 0,
67
			'score'  => 0,
68
			'wins'   => 0,
69
			'draws'  => 0,
70
			'losses' => 0,
71
			'second' => 0,
72
			'third'  => 0
73
		];
74
		return $this;
75
	}
76
	public function getGroupResults($groupId = null) {
77
		if (isset($groupId)) {
78
			if (!isset($this->groupResults[$groupId])) throw new \Exception('Trying to get unexisting group results ('.$groupId.')');
79
			return $this->groupResults[$groupId];
80
		}
81
		return $this->groupResults;
82
	}
83
84
	public function addGameWith(Team $team, Group $group) {
85
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) $this->gamesWith[$group->getId()][$team->getId()] = 0;
86
		$this->gamesWith[$group->getId()][$team->getId()]++;
87
		return $this;
88
	}
89
	public function addGroup(Group $group) {
90
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
91
		return $this;
92
	}
93
	public function addGame(Game $game) {
94
		$group = $game->getGroup();
95
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
96
		$this->games[$group->getId()][] = $game;
97
		return $this;
98
	}
99
100
	public function getSumPoints() {
101
		return $this->sumPoints;
102
	}
103
	public function getSumScore() {
104
		return $this->sumScore;
105
	}
106
107
	public function addWin(string $groupId = ''){
108
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
109
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints;
110
		$this->sumPoints += $this->groupResults[$groupId]['group']->winPoints;
111
		$this->groupResults[$groupId]['wins']++;
112
		return $this;
113
	}
114
	public function removeWin(string $groupId = ''){
115
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
116
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints;
117
		$this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints;
118
		$this->groupResults[$groupId]['wins']--;
119
		return $this;
120
	}
121
	public function addDraw(string $groupId = ''){
122
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
123
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints;
124
		$this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints;
125
		$this->groupResults[$groupId]['draws']++;
126
		return $this;
127
	}
128
	public function removeDraw(string $groupId = ''){
129
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
130
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->drawPointsPoints;
131
		$this->sumPoints -= $this->groupResults[$groupId]['group']->drawPoints;
132
		$this->groupResults[$groupId]['draws']--;
133
		return $this;
134
	}
135
	public function addLoss(string $groupId = ''){
136
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
137
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints;
138
		$this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints;
139
		$this->groupResults[$groupId]['losses']++;
140
		return $this;
141
	}
142
	public function removeLoss(string $groupId = ''){
143
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
144
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints;
145
		$this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints;
146
		$this->groupResults[$groupId]['losses']--;
147
		return $this;
148
	}
149
	public function addSecond(string $groupId = ''){
150
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
151
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->secondPoints;
152
		$this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints;
153
		$this->groupResults[$groupId]['second']++;
154
		return $this;
155
	}
156
	public function removeSecond(string $groupId = ''){
157
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
158
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->secondPoints;
159
		$this->sumPoints -= $this->groupResults[$groupId]['group']->secondPoints;
160
		$this->groupResults[$groupId]['second']--;
161
		return $this;
162
	}
163
	public function addThird(string $groupId = ''){
164
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
165
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->thirdPoints;
166
		$this->sumPoints += $this->groupResults[$groupId]['group']->thirdPoints;
167
		$this->groupResults[$groupId]['third']++;
168
		return $this;
169
	}
170
	public function removeThird(string $groupId = ''){
171
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
172
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->thirdPoints;
173
		$this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints;
174
		$this->groupResults[$groupId]['third']--;
175
		return $this;
176
	}
177
	public function sumPoints(array $groupIds = []) {
178
		if (count($groupIds) === 0) return $this->sumPoints;
179
		$sum = 0;
180
		foreach ($groupIds as $gid) {
181
			if (isset($this->groupResults[$gid])) $sum += $this->groupResults[$gid]['points'];
182
		}
183
		return $sum;
184
	}
185
	public function sumScore(array $groupIds = []) {
186
		if (count($groupIds) === 0) return $this->sumScore;
187
		$sum = 0;
188
		foreach ($groupIds as $gid) {
189
			if (isset($this->groupResults[$gid])) $sum += $this->groupResults[$gid]['score'];
190
		}
191
		return $sum;
192
	}
193
}
194