-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
executable file
·285 lines (250 loc) · 7.73 KB
/
api.php
File metadata and controls
executable file
·285 lines (250 loc) · 7.73 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Pluto Attacks High Score API
* @author Doug Park
* @version 2018-07-22
* Based On https://github.com/memaker/webservice-php-json/blob/master/api.php
*/
session_start();
class api
{
private $db;
private $type;
/**
* Constructor - open DB connection
*
* @param none
* @return database
*/
function __construct()
{
$conf = json_decode(file_get_contents('db.json'), TRUE);
$this->db = new mysqli($conf["host"], $conf["user"], $conf["password"], $conf["database"]);
$this->type = $conf["type"];
}
/**
* Destructor - close DB connection
*
* @param none
* @return none
*/
function __destruct()
{
$this->db->close();
}
/**
* Set High Score
*
* @param array of params
* @return result of db insert
*
*/
function set($params)
{
$query = "INSERT INTO `highscore` ("
. "`id`, `Type`, `Game`, `Date`, `WinWidth`, "
. "`WinHeight`, `GameMode`, `GameLevel`,`EndLevel`, "
. "`PerfectLevels`, `AliensEscaped`, "
. "`Score`, `UserName`, `SessionID`, `IpAddress`, `Browser`)"
. " VALUES (NULL, "
. "'" . $this->type . "',"
. "'" . $params["Game"] . "',"
. "'" . $params["Date"] . "',"
. "'" . $params["WinWidth"] . "',"
. "'" . $params["WinHeight"] . "',"
. "'" . $params["GameMode"] . "',"
. "'" . $params["GameLevel"] . "',"
. "'" . $params["EndLevel"] . "',"
. "'" . $params["PerfectLevels"] . "',"
. "'" . $params["AliensEscaped"] . "',"
. "'" . $params["Score"] . "',"
. "'" . $params["UserName"] . "',"
. "'" . session_id() . "',"
. "'" . $_SERVER["REMOTE_ADDR"] . "',"
. "'" . $params["Browser"] . "')";
$result = $this->db->query($query);
return $result;
}
/**
* Get game high score list
*
* @param none
* @return list array
*/
function gameHighScoresList($params)
{
$query = 'SELECT Score, PerfectLevels, AliensEscaped '
. ' FROM highscore'
. ' where GameLevel > 0 and type = "prod" '
. ' ORDER BY Score DESC, PerfectLevels DESC, AliensEscaped Limit 10';
$list = array();
$result = $this->db->query($query);
while ($row = $result->fetch_assoc()) {
$list[] = $row;
}
return $list;
}
/**
* Get game score rank
*
* @param none
* @return result set
*/
function Rankings($params)
{
$s = '';
$query = "SELECT Score, UserName, FIND_IN_SET( score, ( "
. " SELECT GROUP_CONCAT(DISTINCT score "
. " ORDER BY score DESC ) "
. ' FROM highscore where GameLevel > 0 and type = "prod") '
. " ) AS Rank "
. " FROM highscore "
. ' where GameLevel > 0 and type = "prod" ' . $s
. " ORDER BY Score DESC ";
//. "WHERE name = 'Boo'";
$result = $this->db->query($query);
return $result;
}
/**
* Get game score my rank
*
* @param none
* @return result set
*/
function gameScoreMyRank($params)
{
$s = ' and Score > ' . $params;
//$s = '';
$query = "SELECT Score, UserName, FIND_IN_SET( score, ( "
. " SELECT GROUP_CONCAT(DISTINCT score "
. " ORDER BY score DESC ) "
. ' FROM highscore where GameLevel > 0 and type = "prod") '
. " ) AS Rank "
. " FROM highscore "
. ' where GameLevel > 0 and type = "prod" ' . $s
. " ORDER BY Score LIMIT 1 ";
$result = $this->db->query($query);
while ($row = $result->fetch_assoc()) {
$list[] = $row;
}
return $list;
//return $result;
}
/**
* Get game high score
*
* @param none
* @return result set
*/
function gameHighScores($params)
{
$query = 'SELECT Score, PerfectLevels, AliensEscaped '
. ' FROM highscore'
. ' where GameLevel > 0 and type = "prod" '
. ' ORDER BY Score DESC, PerfectLevels DESC, AliensEscaped Limit 10';
$result = $this->db->query($query);
return $result;
}
/**
* Get game high score today
*
* @param none
* @return result set
*/
function gameHighScoresToday($params)
{
$query = 'SELECT Score, PerfectLevels, AliensEscaped '
. ' FROM highscore'
. ' where GameLevel > 0 and type = "prod" '
. ' and Date(Time) = CURDATE() '
. ' ORDER BY Score DESC, PerfectLevels DESC, AliensEscaped Limit 10';
$result = $this->db->query($query);
return $result;
}
/**
* Get high score today
*
* @param none
* @return result set
*/
function highScoresToday($params)
{
$query = 'SELECT Time, Score, PerfectLevels, AliensEscaped, GameMode, (EndLevel - GameLevel) as LevelsCompleted,'
. ' GameLevel, EndLevel, '
. ' (PerfectLevels / (EndLevel-GameLevel))*100 as Percent,'
. ' SessionID, IpAddress'
. ' FROM highscore'
. ' where GameLevel > 0 and type = "prod" '
. ' and Date(Time) = CURDATE() '
. ' ORDER BY Score DESC, PerfectLevels DESC, AliensEscaped Limit 10';
$result = $this->db->query($query);
return $result;
}
/**
* Get count of games played today
*
* @param none
* @return result set
*/
function gamesPlayedToday($params)
{
$query = 'SELECT count(id) as GameCount'
. ' from highscore '
. ' where GameLevel >0 and type = "prod" '
. ' and Date(Time) = CURDATE() ';
$result = $this->db->query($query);
return $result;
}
/**
* Get count of players (by SessionID) played today
*
* @param none
* @return result set
*/
function playersToday($params)
{
$query = "SELECT count(DISTINCT(SessionID)) as PlayerCount\n"
. " from highscore \n"
. " where GameLevel >0 and type = \"prod\" \n"
. " and Date(Time) = CURDATE()";
$result = $this->db->query($query);
return $result;
}
/**
* Get the high scores
*
* @param none or user id
* @return result set
*/
function highScores($params)
{
$query = 'SELECT Time, Score, PerfectLevels, AliensEscaped, GameMode, (EndLevel - GameLevel) as LevelsCompleted,'
. ' GameLevel, EndLevel, '
. ' (PerfectLevels / (EndLevel-GameLevel))*100 as Percent,'
. ' SessionID, IpAddress'
. ' FROM highscore'
. ' where GameLevel > 0 and type = "prod" '
. ' ORDER BY Score DESC, PerfectLevels DESC, Percent DESC Limit 10';
$result = $this->db->query($query);
return $result;
}
/**
* Get the recent scores
*
* @param none or user id
* @return result set
*/
function recentScores($params)
{
$query = 'SELECT Time, Score, PerfectLevels, AliensEscaped, GameMode, (EndLevel - GameLevel) as LevelsCompleted,'
. ' GameLevel, EndLevel, '
. ' (PerfectLevels / (EndLevel-GameLevel))*100 as Percent,'
. ' SessionID, IpAddress'
. ' FROM highscore'
. ' where UserName != "" '
. ' and type = "prod" '
. ' ORDER BY Date DESC, Score DESC, PerfectLevels DESC, Percent DESC Limit 10';
$result = $this->db->query($query);
return $result;
}
}