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