1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TournamentGenerator\Helpers\Sorter; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use TournamentGenerator\Constants; |
7
|
|
|
use TournamentGenerator\Group; |
8
|
|
|
use TournamentGenerator\Round; |
9
|
|
|
use TournamentGenerator\Team; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* TournamentGenerator sorter for teams |
13
|
|
|
* |
14
|
|
|
* @author Tomáš Vojík <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @package TournamentGenerator\Helpers\Sorter |
17
|
|
|
* @since 0.3 |
18
|
|
|
*/ |
19
|
|
|
class Teams |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** @var int|string[] $ids Stores ids of groups get scores and points from */ |
23
|
|
|
protected static array $ids = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sort teams in group by defined ordering type |
27
|
|
|
* |
28
|
|
|
* @param Team[] & $teams Array of teams to be sorted |
|
|
|
|
29
|
|
|
* @param Group $group Group to get the results from |
30
|
|
|
* @param string $ordering What to order by (\TournamentGenerator\Constants::POINTS / \TournamentGenerator\Constants::SCORE) |
31
|
|
|
* |
32
|
|
|
* @return Team[] Sorted array of teams |
33
|
|
|
* @throws Exception |
34
|
|
|
*/ |
35
|
48 |
|
public static function sortGroup(array &$teams, Group $group, string $ordering = Constants::POINTS) : array { |
36
|
48 |
|
if (!in_array($ordering, Constants::OrderingTypes, true)) { |
37
|
|
|
throw new Exception('Unknown ordering type `'.$ordering.'`'); |
38
|
|
|
} |
39
|
|
|
|
40
|
48 |
|
self::$ids = [$group->getId()]; |
41
|
|
|
|
42
|
48 |
|
switch ($ordering) { |
43
|
|
|
case Constants::POINTS: |
44
|
47 |
|
usort($teams, [__CLASS__, 'sortTeamsByPoints']); |
45
|
47 |
|
break; |
46
|
|
|
case Constants::SCORE: |
47
|
1 |
|
usort($teams, [__CLASS__, 'sortTeamsByScore']); |
48
|
1 |
|
break; |
49
|
|
|
} |
50
|
|
|
|
51
|
48 |
|
return $teams; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sort teams in round by defined ordering type |
56
|
|
|
* |
57
|
|
|
* @param Team[] & $teams Array of teams to be sorted |
|
|
|
|
58
|
|
|
* @param Round $round Round to get the results from |
59
|
|
|
* @param string $ordering What to order by (\TournamentGenerator\Constants::POINTS / \TournamentGenerator\Constants::SCORE) |
60
|
|
|
* |
61
|
|
|
* @return Team[] Sorted array of teams |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
9 |
|
public static function sortRound(array &$teams, Round $round, string $ordering = Constants::POINTS) : array { |
65
|
9 |
|
if (!in_array($ordering, Constants::OrderingTypes, true)) { |
66
|
|
|
throw new Exception('Unknown ordering type `'.$ordering.'`'); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
self::$ids = $round->getGroupsIds(); |
70
|
|
|
|
71
|
9 |
|
switch ($ordering) { |
72
|
|
|
case Constants::POINTS: |
73
|
6 |
|
usort($teams, [__CLASS__, 'sortTeamsByPoints']); |
74
|
6 |
|
break; |
75
|
|
|
case Constants::SCORE: |
76
|
3 |
|
usort($teams, [__CLASS__, 'sortTeamsByScore']); |
77
|
3 |
|
break; |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
return $teams; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sorter function for usort by points |
85
|
|
|
* |
86
|
|
|
* @param Team $a First team |
87
|
|
|
* @param Team $b Second team |
88
|
|
|
*/ |
89
|
47 |
|
private static function sortTeamsByPoints(Team $a, Team $b) : int { |
90
|
47 |
|
$groupsIds = self::$ids; |
91
|
47 |
|
if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds) && $a->sumScore($groupsIds) === $b->sumScore($groupsIds)) { |
92
|
3 |
|
return 0; |
93
|
|
|
} |
94
|
44 |
|
if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds)) { |
95
|
21 |
|
return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1); |
96
|
|
|
} |
97
|
44 |
|
return ($a->sumPoints($groupsIds) > $b->sumPoints($groupsIds) ? -1 : 1); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sorter function for usort by score |
102
|
|
|
* |
103
|
|
|
* @param Team $a First team |
104
|
|
|
* @param Team $b Second team |
105
|
|
|
*/ |
106
|
4 |
|
private static function sortTeamsByScore(Team $a, Team $b) : int { |
107
|
4 |
|
$groupsIds = self::$ids; |
108
|
4 |
|
if ($a->sumScore($groupsIds) === $b->sumScore($groupsIds)) { |
109
|
1 |
|
return 0; |
110
|
|
|
} |
111
|
4 |
|
return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|