-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_base.php
179 lines (155 loc) · 5.81 KB
/
api_base.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
require_once('./config.php');
class Player {
public $username;
public $uuid;
public $rank;
public $hiddenStaff;
public function __construct($username, $uuid, $rank = "DEFAULT") {
$this->username = $username;
$this->uuid = $uuid;
$this->rank = $rank;
$this->hiddenStaff = false;
}
public function isHiddenStaff() {
return $this->hiddenStaff;
}
public static function constructPlayer($data) {
$rank = property_exists($data, 'rank') ? $data->rank : "DEFAULT";
$p = new Player($data->username, $data->uuid, $rank);
if (property_exists($data, "hiddenStaff")) {
$p->hiddenStaff = $data->hiddenStaff;
}
return $p;
}
}
class GamePlayer extends Player {
public $rating;
public function __construct($username, $uuid, $rating, $rank = "DEFAULT") {
parent::__construct($username, $uuid, $rank);
$this->rating = $rating;
}
public static function constructPlayer($data) {
$rank = property_exists($data, 'rank') ? $data->rank : "DEFAULT";
$p = new GamePlayer($data->username, $data->uuid, $data->rating, $rank);
return $p;
}
}
abstract class APIClass {
private $database = array(
'MAIN' => 'SpleefLeague',
'SPLEEF' => 'SuperSpleef',
'SUPERJUMP' => 'SuperJump'
);
private $collection = array(
'PLAYERS' => 'Players',
'ARENAS' => 'Arenas',
'SETTINGS' => 'Settings',
'RANKS' => 'Ranks'
);
private $playerProjection = array('projection' => array(
'_id' => false,
'username' => true,
'rank' => true,
'uuid' => true,
'hiddenStaff' => true,
'rating' => true
)
);
private $mongoConnection;
public function getCollection($db, $col) {
$db = $this->database[$db];
$col = $this->collection[$col];
return $db.".".$col;
}
public function getMongo() {
if ($this->mongoConnection == null) {
$this->mongoConnection = new MongoDB\Driver\Manager(Config::get()['dbCon']);
}
return $this->mongoConnection;
}
public function dieError($message) {
die(json_encode(['error' => $message], JSON_PRETTY_PRINT));
}
public function getPlayerById($uuid) {
$query = new MongoDB\Driver\Query(['uuid' => $uuid], $this->playerProjection);
$res = $this->getMongo()->executeQuery('SpleefLeague.Players', $query)->toArray();
if (count($res) > 0) {
return Player::constructPlayer($res[0]);
}
return null;
}
public function getPlayerByName($username) {
$query = new MongoDB\Driver\Query(['lookupUsername' => strtolower($username)], $this->playerProjection);
$res = $this->getMongo()->executeQuery('SpleefLeague.Players', $query)->toArray();
if (count($res) > 0) {
return Player::constructPlayer($res[0]);
}
return null;
}
public function getGamePlayerById($game, $uuid) {
$query = new MongoDB\Driver\Query(['uuid' => $uuid], $this->playerProjection);
$res = $this->getMongo()->executeQuery($game.'.Players', $query)->toArray();
if (count($res) > 0) {
return GamePlayer::constructPlayer($res[0]);
}
return null;
}
public function getGamePlayerByName($game, $username) {
$query = new MongoDB\Driver\Query(['lookupUsername' => strtolower($username)], $this->playerProjection);
$res = $this->getMongo()->executeQuery($game.'.Players', $query)->toArray();
if (count($res) > 0) {
return GamePlayer::constructPlayer($res[0]);
}
return null;
}
public function isValidGameName($game) {
switch (strtolower($game)) {
case "spleef":
case "snowspleef":
case "ss":
case "superspleef":
case "jump":
case "superjump":
case "sj":
return true;
default:
return false;
}
}
public function fixGameName($game) {
switch (strtolower($game)) {
case "spleef":
case "snowspleef":
case "ss":
case "superspleef":
return "SuperSpleef";
case "jump":
case "superjump":
case "sj":
return "SuperJump";
default: return null;
}
}
public function disguiseRank($rank) {
switch ($rank) {
case "SENIOR_MODERATOR_BUILDER":
return "SENIOR_MODERATOR";
case "HIDDEN_DEVELOPER":
return "DEFAULT";
case "HIDDEN_COUNCIL":
return "DEFAULT";
case "VIP_DEV":
return "VIP";
case "TOPKEK":
return "VIP";
case "TEMP_MOD":
return "MODERATOR";
default:
return $rank;
}
}
}
?>