1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Team extends Base |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public $games = []; |
12
|
|
|
public $gamesWith = []; |
13
|
|
|
public $sumPoints = 0; |
14
|
|
|
public $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
|
85 |
|
function __construct(string $name = 'team', $id = null) { |
34
|
85 |
|
$this->setName($name); |
35
|
85 |
|
$this->setId(isset($id) ? $id : uniqid()); |
36
|
85 |
|
} |
37
|
|
|
public function getGamesInfo($groupId) { |
38
|
|
|
return array_filter($this->groupResults[$groupId], function($k) { return $k !== 'group'; }, ARRAY_FILTER_USE_KEY); |
39
|
|
|
} |
40
|
|
|
|
41
|
67 |
|
public function addGroupResults(Group $group) { |
42
|
67 |
|
$this->groupResults[$group->getId()] = [ |
43
|
67 |
|
'group' => $group, |
44
|
67 |
|
'points' => 0, |
45
|
67 |
|
'score' => 0, |
46
|
67 |
|
'wins' => 0, |
47
|
67 |
|
'draws' => 0, |
48
|
67 |
|
'losses' => 0, |
49
|
67 |
|
'second' => 0, |
50
|
67 |
|
'third' => 0 |
51
|
|
|
]; |
52
|
67 |
|
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
|
67 |
|
public function addGameWith(Team $team, Group $group) { |
63
|
67 |
|
if (!isset($this->gamesWith[$group->getId()][$team->getId()])) $this->gamesWith[$group->getId()][$team->getId()] = 0; |
64
|
67 |
|
$this->gamesWith[$group->getId()][$team->getId()]++; |
65
|
67 |
|
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
|
67 |
|
public function addGame(Game $game) { |
89
|
67 |
|
$group = $game->getGroup(); |
90
|
67 |
|
if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = []; |
91
|
67 |
|
$this->games[$group->getId()][] = $game; |
92
|
67 |
|
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
|
4 |
|
public function getSumPoints() { |
101
|
4 |
|
return $this->sumPoints; |
102
|
|
|
} |
103
|
5 |
|
public function getSumScore() { |
104
|
5 |
|
return $this->sumScore; |
105
|
|
|
} |
106
|
|
|
|
107
|
49 |
|
public function addWin(string $groupId = ''){ |
108
|
49 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
109
|
49 |
|
$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints; |
110
|
49 |
|
$this->sumPoints += $this->groupResults[$groupId]['group']->winPoints; |
111
|
49 |
|
$this->groupResults[$groupId]['wins']++; |
112
|
49 |
|
return $this; |
113
|
|
|
} |
114
|
9 |
|
public function removeWin(string $groupId = ''){ |
115
|
9 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
116
|
9 |
|
$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints; |
117
|
9 |
|
$this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints; |
118
|
9 |
|
$this->groupResults[$groupId]['wins']--; |
119
|
9 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
12 |
|
public function addDraw(string $groupId = ''){ |
123
|
12 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
124
|
12 |
|
$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints; |
125
|
12 |
|
$this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints; |
126
|
12 |
|
$this->groupResults[$groupId]['draws']++; |
127
|
12 |
|
return $this; |
128
|
|
|
} |
129
|
1 |
|
public function removeDraw(string $groupId = ''){ |
130
|
1 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
131
|
1 |
|
$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->drawPoints; |
132
|
1 |
|
$this->sumPoints -= $this->groupResults[$groupId]['group']->drawPoints; |
133
|
1 |
|
$this->groupResults[$groupId]['draws']--; |
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
49 |
|
public function addLoss(string $groupId = ''){ |
138
|
49 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
139
|
49 |
|
$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints; |
140
|
49 |
|
$this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints; |
141
|
49 |
|
$this->groupResults[$groupId]['losses']++; |
142
|
49 |
|
return $this; |
143
|
|
|
} |
144
|
9 |
|
public function removeLoss(string $groupId = ''){ |
145
|
9 |
|
if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
|
|
|
|
146
|
9 |
|
$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints; |
147
|
9 |
|
$this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints; |
148
|
9 |
|
$this->groupResults[$groupId]['losses']--; |
149
|
9 |
|
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']->secondPoints; |
155
|
10 |
|
$this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints; |
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']->secondPoints; |
162
|
2 |
|
$this->sumPoints -= $this->groupResults[$groupId]['group']->secondPoints; |
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']->thirdPoints; |
170
|
9 |
|
$this->sumPoints += $this->groupResults[$groupId]['group']->thirdPoints; |
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']->thirdPoints; |
177
|
1 |
|
$this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints; |
178
|
1 |
|
$this->groupResults[$groupId]['third']--; |
179
|
1 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
22 |
|
public function sumPoints(array $groupIds = []) { |
183
|
22 |
|
if (count($groupIds) === 0) return $this->sumPoints; |
184
|
22 |
|
$sum = 0; |
185
|
22 |
|
foreach ($groupIds as $gid) { |
186
|
22 |
|
$sum += $this->groupResults[$gid]['points'] ?? 0; |
187
|
|
|
} |
188
|
22 |
|
return $sum; |
189
|
|
|
} |
190
|
11 |
|
public function sumScore(array $groupIds = []) { |
191
|
11 |
|
if (count($groupIds) === 0) return $this->sumScore; |
192
|
11 |
|
$sum = 0; |
193
|
11 |
|
foreach ($groupIds as $gid) { |
194
|
11 |
|
$sum += $this->groupResults[$gid]['score'] ?? 0; |
195
|
|
|
} |
196
|
11 |
|
return $sum; |
197
|
|
|
} |
198
|
|
|
|
199
|
51 |
|
public function addScore(int $score) { |
200
|
51 |
|
$this->sumScore += $score; |
201
|
51 |
|
} |
202
|
1 |
|
public function removeScore(int $score) { |
203
|
1 |
|
$this->sumScore -= $score; |
204
|
1 |
|
} |
205
|
|
|
} |
206
|
|
|
|