-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaderboard.php
123 lines (115 loc) · 4.31 KB
/
leaderboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
require_once("./api_base.php");
class LeaderboardEndPoint extends APIClass {
public function getRank($game, $points) {
$cmd = [ 'count' => 'Players', 'query' => [ "rating" => [ '$gt' => $points ] ] ];
$query = new MongoDB\Driver\Command($cmd);
$cursor = $this->getMongo()->executeCommand($game, $query);
$result = $cursor->toArray()[0];
return $result->n + 1;
}
public function runPlayerCenter($game, $p) {
$slp = $this->getGamePlayerById($game, $p->uuid);
if ($slp == null) {
$this->dieError('Player not found');
}
$rank = $this->getRank($game, $slp->rating);
$limit = 100;
$low = $rank - 50;
if ($low < 0) { $limit-= $low; $low = 0; }
$filter = [];
$options = [
'projection' => [
'_id' => false,
'username' => true,
'uuid' => true,
'rating' => true
],
'skip' => $low,
'limit' => $limit,
'sort' => [ "rating" => -1 ]
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $this->getMongo()->executeQuery($game.".Players", $query);
$output = [];
$curRank = $low;
foreach ($cursor as $doc) {
$curRank++;
$output["players"][] = [
"username" => $doc->username,
"uuid" => $doc->uuid,
"rating" => $doc->rating,
"rank" => $curRank
];
}
$outputJson = json_encode($output, JSON_PRETTY_PRINT);
echo $outputJson;
}
public function run() {
if (!isset($_GET['path'])) {
$this->dieError("no argument specified");
}
$requestUrl = explode('/', $_GET['path']);
$game = null;
if ($this->isValidGameName($requestUrl[0])) {
$game = $this->fixGameName($requestUrl[0]);
}
if (count($requestUrl) > 2) {
if (strtolower($requestUrl[1]) == 'player') {
$player = $requestUrl[2];
$p = $this->getPlayerByName($player);
if ($p == null) {
$p = $this->getPlayerById($player);
}
if ($p == null) {
$this->dieError('player not found');
}
$this->runPlayerCenter($game, $p);
return;
}
}
$page = 1;
if (count($requestUrl) > 1) {
if (!is_numeric($requestUrl[1]) && !empty($requestUrl[1])) {
$this->dieError('Invalid argument for page');
}
if (!empty($requestUrl[1])) {
$page = intval($requestUrl[1]);
}
}
$count = 100;
$filter = [];
$skip = $page;
$skip--;
$skip = $skip * $count;
$options = [
'projection' => [
'_id' => false,
'username' => true,
'uuid' => true,
'rating' => true
],
'skip' => $skip,
'limit' => $count,
'sort' => [ "rating" => -1 ]
];
$query = new MongoDB\Driver\Query($filter, $options);
$output = [ "page" => $page, 'age' => 0, "players" => [] ];
$cursor = $this->getMongo()->executeQuery($game.".Players", $query);
$curRank = $count * ($page-1);
foreach ($cursor as $doc) {
$curRank++;
$output["players"][] = [
"username" => $doc->username,
"uuid" => $doc->uuid,
"rating" => $doc->rating,
"rank" => $curRank
];
}
$outputJson = json_encode($output, JSON_PRETTY_PRINT);
echo $outputJson;
}
}
$page = new LeaderboardEndPoint();
$page->run();
?>