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