1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Traits; |
5
|
|
|
|
6
|
|
|
use TournamentGenerator\Base; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait HasScore |
10
|
|
|
* |
11
|
|
|
* A trait for a Team object that adds the ability to calculate scores / points. |
12
|
|
|
* |
13
|
|
|
* @package TournamentGenerator\Traits |
14
|
|
|
* @author Tomáš Vojík <[email protected]> |
15
|
|
|
* @since 0.4 |
16
|
|
|
*/ |
17
|
|
|
trait HasScore |
18
|
|
|
{ |
19
|
|
|
/** @var int $sumPoints Sum of all points acquired through the whole tournament */ |
20
|
|
|
protected int $sumPoints = 0; |
21
|
|
|
/** @var int $sumScore Sum of all score acquired through the whole tournament */ |
22
|
|
|
protected int $sumScore = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Gets all points that the team has acquired through the tournament |
26
|
|
|
* |
27
|
|
|
* @return int Sum of the points acquired |
28
|
|
|
*/ |
29
|
5 |
|
public function getSumPoints() : int { |
30
|
5 |
|
return $this->sumPoints; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets all score that the team has acquired through the tournament |
35
|
|
|
* |
36
|
|
|
* @return int Sum of the score acquired |
37
|
|
|
*/ |
38
|
5 |
|
public function getSumScore() : int { |
39
|
5 |
|
return $this->sumScore; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Calculate all the points acquired from given group ids |
44
|
|
|
* |
45
|
|
|
* @param array $groupIds Array of group ids |
46
|
|
|
* |
47
|
|
|
* @return int Sum of the points or sum of all points if argument is empty |
48
|
|
|
*/ |
49
|
45 |
|
public function sumPoints(array $groupIds = []) : int { |
50
|
45 |
|
if (count($groupIds) === 0) { |
51
|
1 |
|
return $this->sumPoints; |
52
|
|
|
} |
53
|
44 |
|
$sum = 0; |
54
|
44 |
|
foreach ($groupIds as $gid) { |
55
|
44 |
|
$sum += $this->groupResults[$gid]['points'] ?? 0; |
56
|
|
|
} |
57
|
44 |
|
return $sum; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Calculate all score acquired from given group ids |
62
|
|
|
* |
63
|
|
|
* @param array $groupIds Array of group ids |
64
|
|
|
* |
65
|
|
|
* @return int Sum of score or sum of all score if argument is empty |
66
|
|
|
*/ |
67
|
25 |
|
public function sumScore(array $groupIds = []) : int { |
68
|
25 |
|
if (count($groupIds) === 0) { |
69
|
1 |
|
return $this->sumScore; |
70
|
|
|
} |
71
|
24 |
|
$sum = 0; |
72
|
24 |
|
foreach ($groupIds as $gid) { |
73
|
24 |
|
$sum += $this->groupResults[$gid]['score'] ?? 0; |
74
|
|
|
} |
75
|
24 |
|
return $sum; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Adds score to the total sum |
80
|
|
|
* |
81
|
|
|
* @param int $score Score to add |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
75 |
|
public function addScore(int $score) : Base { |
86
|
75 |
|
$this->sumScore += $score; |
87
|
75 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Removes score to the total sum |
92
|
|
|
* |
93
|
|
|
* @param int $score Score to add |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
21 |
|
public function removeScore(int $score) : Base { |
98
|
21 |
|
$this->sumScore -= $score; |
99
|
21 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Adds points to the total sum |
104
|
|
|
* |
105
|
|
|
* @param int $points Points to add |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
78 |
|
public function addPoints(int $points) : Base { |
110
|
78 |
|
$this->sumPoints += $points; |
111
|
78 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Removes points to the total sum |
116
|
|
|
* |
117
|
|
|
* @param int $points Points to remove |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
21 |
|
public function removePoints(int $points) : Base { |
122
|
21 |
|
$this->sumPoints -= $points; |
123
|
21 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |