|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
class Rankings { |
|
4
|
|
|
private function __construct() {} |
|
5
|
|
|
|
|
6
|
|
|
public static function collectRaceRankings(MySqlDatabase $db, AbstractSmrPlayer $player) { |
|
7
|
|
|
$rankings = []; |
|
8
|
|
|
$rank = 0; |
|
9
|
|
|
while ($db->nextRecord()) { |
|
10
|
|
|
// increase rank counter |
|
11
|
|
|
$rank++; |
|
12
|
|
|
|
|
13
|
|
|
$race_id = $db->getInt('race_id'); |
|
14
|
|
|
if ($player->getRaceID() == $race_id) { |
|
15
|
|
|
$style = ' class="bold"'; |
|
16
|
|
|
} else { |
|
17
|
|
|
$style = ''; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$rankings[$rank] = [ |
|
21
|
|
|
'style' => $style, |
|
22
|
|
|
'race_id' => $db->getInt('race_id'), |
|
23
|
|
|
'amount' => $db->getInt('amount'), |
|
24
|
|
|
'amount_avg' => IRound($db->getInt('amount') / $db->getInt('num_players')), |
|
25
|
|
|
'num_players' => $db->getInt('num_players'), |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
return $rankings; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function collectAllianceRankings(MySqlDatabase $db, AbstractSmrPlayer $player, $rank) { |
|
32
|
|
|
$rankings = array(); |
|
33
|
|
|
while ($db->nextRecord()) { |
|
34
|
|
|
// increase rank counter |
|
35
|
|
|
$rank++; |
|
36
|
|
|
$currentAlliance = SmrAlliance::getAlliance($db->getInt('alliance_id'), $player->getGameID()); |
|
37
|
|
|
|
|
38
|
|
|
$class = ''; |
|
39
|
|
|
if ($player->getAllianceID() == $currentAlliance->getAllianceID()) { |
|
40
|
|
|
$class = ' class="bold"'; |
|
41
|
|
|
} elseif ($currentAlliance->hasDisbanded()) { |
|
42
|
|
|
$class = ' class="red"'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$rankings[$rank] = array( |
|
46
|
|
|
'Rank' => $rank, |
|
47
|
|
|
'Alliance' => $currentAlliance, |
|
48
|
|
|
'Class' => $class, |
|
49
|
|
|
'Value' => $db->getInt('amount') |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
return $rankings; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function collectRankings(MySqlDatabase $db, AbstractSmrPlayer $player, $rank) { |
|
56
|
|
|
$rankings = array(); |
|
57
|
|
|
while ($db->nextRecord()) { |
|
58
|
|
|
// increase rank counter |
|
59
|
|
|
$rank++; |
|
60
|
|
|
$currentPlayer = SmrPlayer::getPlayer($db->getInt('account_id'), $player->getGameID(), false, $db); |
|
61
|
|
|
|
|
62
|
|
|
$class = ''; |
|
63
|
|
|
if ($player->equals($currentPlayer)) { |
|
64
|
|
|
$class .= 'bold'; |
|
65
|
|
|
} |
|
66
|
|
|
if ($currentPlayer->hasNewbieStatus()) { |
|
67
|
|
|
$class .= ' newbie'; |
|
68
|
|
|
} |
|
69
|
|
|
if ($class != '') { |
|
70
|
|
|
$class = ' class="' . trim($class) . '"'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$rankings[$rank] = array( |
|
74
|
|
|
'Rank' => $rank, |
|
75
|
|
|
'Player' => $currentPlayer, |
|
76
|
|
|
'Class' => $class, |
|
77
|
|
|
'Value' => $db->getInt('amount') |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
return $rankings; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get a subset of rankings from the player table sorted by $stat. |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function playerRanks(string $stat, int $minRank = 1, int $maxRank = 10) : array { |
|
87
|
|
|
global $player, $db; |
|
88
|
|
|
$offset = $minRank - 1; |
|
89
|
|
|
$limit = $maxRank - $offset; |
|
90
|
|
|
$db->query('SELECT *, ' . $stat . ' AS amount FROM player WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' ORDER BY amount DESC, player_name LIMIT ' . $offset . ', ' . $limit); |
|
91
|
|
|
return self::collectRankings($db, $player, $offset); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get a subset of rankings from the alliance table sorted by $stat. |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function allianceRanks(string $stat, int $minRank = 1, int $maxRank = 10) : array { |
|
98
|
|
|
global $player, $db; |
|
99
|
|
|
$offset = $minRank - 1; |
|
100
|
|
|
$limit = $maxRank - $offset; |
|
101
|
|
|
$db->query('SELECT alliance_id, alliance_' . $stat . ' AS amount FROM alliance WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' ORDER BY amount DESC, alliance_name LIMIT ' . $offset. ', ' . $limit); |
|
102
|
|
|
return self::collectAllianceRankings($db, $player, $offset); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function calculateMinMaxRanks($ourRank, $totalRanks) { |
|
106
|
|
|
global $var, $template; |
|
107
|
|
|
$minRank = SmrSession::getRequestVarInt('min_rank', $ourRank - 5); |
|
108
|
|
|
$maxRank = SmrSession::getRequestVarInt('max_rank', $ourRank + 5); |
|
109
|
|
|
|
|
110
|
|
|
if ($minRank <= 0 || $minRank > $totalRanks) { |
|
111
|
|
|
$minRank = 1; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$maxRank = min($maxRank, $totalRanks); |
|
115
|
|
|
|
|
116
|
|
|
$template->assign('MinRank', $minRank); |
|
117
|
|
|
$template->assign('MaxRank', $maxRank); |
|
118
|
|
|
$template->assign('TotalRanks', $totalRanks); |
|
119
|
|
|
|
|
120
|
|
|
return [$minRank, $maxRank]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|