-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygsm.py
451 lines (350 loc) · 14.9 KB
/
pygsm.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
pygsm - A RESTful API that acts as a game server tracker
Copyright (C) 2016 GoInto, LLC
This file is part of pygsm.
pygsm is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 2 of the License, or (at your option)
any later version.
pygsm is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. You should have
received a copy of the GNU General Public License along with pygsm. If
not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "GoInto, LLC"
__version__ = "0.1.1"
import hug
from urllib import parse
from datetime import datetime, timedelta
from psycopg2 import IntegrityError, ProgrammingError
from psycopg2.extras import Json
from marshmallow import fields
from core import log, zero_uuid
from core.db import db_connection, db_cursor
from core.config import settings
from core.auth import authenticate, optional_api_key
from core.decorators import rollback_on_failure
from utilities import response, response_positive, response_error
# setup auth
psk_authentication = hug.authentication.api_key(authenticate)
psk_optional = optional_api_key(authenticate)
@hug.directive()
def auth_context(default=None, request=None, *args, **kwargs):
""" Returns the current logged in user """
return request.context.get('user')
@hug.http('/auth-test', accept=('GET', 'POST'), requires=psk_authentication)
def auth_test():
""" Simple authentication test """
return response_positive("Success")
@hug.get('/game', examples='game_uuid=777ab9da-bc9a-4fe5-88da-b925e44909b3', requires=psk_optional)
def game(game_uuid: hug.types.uuid = None, dev: hug.types.boolean = False, auth: auth_context = None):
""" Returns basic game information """
if (not auth or auth.anonymous) and dev:
return response_error("Permission denied", code=403)
elif dev is None and auth:
dev = auth.development
if game_uuid:
db_cursor.execute("SELECT game_uuid, stamp FROM game WHERE game_uuid=%s AND dev=%s", (game_uuid, dev))
else:
db_cursor.execute("SELECT game_uuid, stamp FROM game WHERE stamp > now() - interval '%s days' AND dev = %s", (settings['GAME_MAX_AGE'], dev, ))
if db_cursor.rowcount > 0:
results = []
for row in db_cursor.fetchall():
results.append({
'game_uuid': str(row['game_uuid']),
'stamp': row['stamp'].isoformat(),
})
return response(results)
else:
return response_error("No games found", 404)
@hug.get('/server', requires=psk_optional)
def server(auth: auth_context = None, dev: hug.types.boolean = False):
""" Get active servers """
if (not auth or auth.anonymous) and dev:
return response_error("Permission denied", code=403)
elif dev is None and auth:
dev = auth.development
db_cursor.execute("""SELECT ping_id, hostname, port, name, ping,
active, max, dev, game_uuid
FROM ping
WHERE ping > now() - interval '5 minutes'
AND down = false
AND dev = %s
ORDER BY RANDOM()""", (dev, ))
if db_cursor.rowcount > 0:
results = []
for row in db_cursor.fetchall():
results.append({
'hostname': row['hostname'],
'port': row['port'],
'name': row['name'],
'ping': row['ping'],
'activePlayers': row['active'],
'maxPlayers': row['max'],
'game_uuid': str(row['game_uuid']),
})
return response(results)
else:
return response_error("No servers found", 404)
@rollback_on_failure
@hug.post('/server', requires=psk_authentication)
def ping(hostname: hug.types.text, port: hug.types.number,
name: hug.types.text, activePlayers: hug.types.number,
game_uuid: hug.types.uuid = None, maxPlayers: hug.types.number = 8,
auth: auth_context = None):
""" Add/update new server """
dev = auth.development
log.info("ping(hostname: %s, port: %s, name: %s, activePlayers: %s, game_uuid: %s, maxPlayers: %s, dev: %s)" % (hostname, port, name, activePlayers, game_uuid, maxPlayers, dev))
# things we'll populate later, maybe
latest = None
new_game = False
# create a new game entry
try:
if game_uuid:
db_cursor.execute("INSERT INTO game (game_uuid, stamp, dev) VALUES (%s, now(), %s) ON CONFLICT DO NOTHING RETURNING game_uuid", (game_uuid, dev, ))
else:
db_cursor.execute("INSERT INTO game (stamp, dev) VALUES (now(), %s) RETURNING game_uuid", (dev, ))
except Exception as e:
log.error(str(e))
return response_error("Could not create new game.", code=500)
finally:
if db_cursor.rowcount > 1:
db_connection.commit()
game_uuid = db_cursor.fetchone()['game_uuid']
new_game = True
# add or update ping
try:
db_cursor.execute("""INSERT INTO ping
(hostname, port, name, ping, active, max, dev, game_uuid)
VALUES (%s, %s, %s, now(), %s, %s, %s, %s)
ON CONFLICT ON CONSTRAINT ping_hostname_port_key DO UPDATE
SET name = %s, ping = now(), active = %s, max = %s, dev = %s,
game_uuid = %s, down = false""",
(hostname, port, name, activePlayers, maxPlayers, dev, game_uuid,
name, activePlayers, maxPlayers, dev, game_uuid))
except Exception as e:
log.error(str(e))
return response_error("Internal pygsm error. See logs for more details.")
if db_cursor.rowcount < 1:
log.error("Ping failed!")
db_connection.rollback()
return response_error("Ping failed for unknown reasons!")
else:
db_connection.commit()
return response_positive("Ping successful!")
@rollback_on_failure
@hug.delete('/server', requires=psk_authentication)
def server(hostname: hug.types.text, port: hug.types.number):
""" Remove an active server """
try:
db_cursor.execute("""UPDATE ping SET down = true
WHERE hostname = %s AND port = %s""", (hostname, port))
except Exception as e:
log.error(str(e))
return response_error("Internal pygsm error. See logs for more details.")
if db_cursor.rowcount > 0:
db_connection.commit()
return response_positive("Shutdown successful!")
else:
db_connection.rollback()
return response_error("No servers found", 404)
@hug.get('/game-player', examples="game_player_id=1", requires=psk_optional)
def game_player(game_player_id: hug.types.number = None, game_uuid: hug.types.uuid = None,
dev: hug.types.boolean = False, auth: auth_context = None):
""" Show player(s) and their data """
if (not auth or auth.anonymous) and dev:
return response_error("Permission denied", code=403)
elif dev is None and auth:
dev = auth.development
if game_player_id:
db_cursor.execute("""SELECT game_player_id, game_uuid, meta
FROM game_player gp
WHERE game_player_id = %s""", [game_player_id])
elif game_uuid:
db_cursor.execute("""SELECT game_player_id, game_uuid, meta
FROM game_player gp
WHERE game_uuid = %s""", [game_uuid])
else:
db_cursor.execute("""SELECT game_player_id, game_uuid
FROM game_player gp
JOIN game g USING (game_uuid)
WHERE g.stamp > now() - interval '%s days'
AND g.dev = %s""", (settings['GAME_MAX_AGE'], dev, ))
if db_cursor.rowcount > 0:
players = []
for row in db_cursor.fetchall():
players.append({
'game_player_id': row['game_player_id'],
'game_uuid': str(row['game_uuid']),
})
# Only include the in-depth data if a game_player_id was
# specified
if game_player_id:
players[-1]['meta'] = row['meta']
return response(players)
else:
return response_error("No players found.", 404)
@rollback_on_failure
@hug.post('/game-player', requires=psk_authentication)
def add_player(game_uuid: hug.types.uuid, meta: hug.types.json):
""" Add a player to the game """
# sanity check
if game_uuid == zero_uuid:
return response_error("Invalid UUID")
try:
db_cursor.execute("""INSERT INTO game_player (game_uuid, meta)
VALUES (%s, %s) RETURNING game_player_id""", [game_uuid, Json(meta)])
except IntegrityError as e:
log.warning(str(e))
return response_error("Invalid game_uuid provided")
except Exception as e:
log.error(str(e))
return response_error("Internal pygsm error. See logs for more details.")
if db_cursor.rowcount < 1:
errmsg = "Player insert failed!"
log.error(errmsg)
db_connection.rollback()
return response_error(errmsg)
else:
db_connection.commit()
try:
new_player = db_cursor.fetchone()
except ProgrammingError:
new_player = None
return response([{
"game_player_id": new_player["game_player_id"]
}, ])
@hug.get('/leaderboard', requires=psk_optional)
def leaderboard(game_player_id: hug.types.number = None,
game_uuid: hug.types.uuid = None, leaderboard_id: hug.types.number = None,
dev: hug.types.boolean = False, auth: auth_context = None):
""" Show game stats of user(s) """
if (not auth or auth.anonymous) and dev:
return response_error("Permission denied", code=403)
elif dev is None and auth:
dev = auth.development
if game_player_id:
db_cursor.execute("""SELECT gp.game_player_id, gp.game_uuid, gp.meta,
COALESCE(SUM(kills), 0) AS kills, COALESCE(SUM(deaths), 0) AS deaths
FROM game_player gp
LEFT JOIN leaderboard l USING (game_player_id)
WHERE gp.game_player_id = %s
GROUP BY game_player_id""", [game_player_id])
elif game_uuid:
db_cursor.execute("""SELECT game_player_id, gp.game_uuid, gp.meta,
COALESCE(SUM(kills), 0) AS kills, COALESCE(SUM(deaths), 0) AS deaths
FROM game_player gp
LEFT JOIN leaderboard l USING (game_player_id)
WHERE game_uuid = %s
GROUP BY game_player_id""", [game_uuid])
elif leaderboard_id:
db_cursor.execute("""SELECT gp.game_player_id, gp.game_uuid, gp.meta,
SUM(kills) AS kills, SUM(deaths) AS deaths
FROM leaderboard l
JOIN game_player gp USING (game_player_id)
WHERE leaderboard_id = %s
GROUP BY gp.game_player_id""", [leaderboard_id])
else:
# TODO: aggregate display
return response_error("Aggregate leaderboard not yet implemented. For now, at least one parameter must be specified.", 501)
if db_cursor.rowcount > 0:
results = []
for row in db_cursor.fetchall():
results.append({
'kills': row['kills'],
'deaths': row['deaths'],
'game_player': {
'game_player_id': row['game_player_id'],
'game_uuid': str(row['game_uuid']),
'meta': row['meta'],
}
})
return response(results)
else:
return response_error("No leaderboard entries found.", 404)
@rollback_on_failure
@hug.post('/game-player/stats', requires=psk_authentication)
def leaderboard_add(game_player_id: hug.types.number, kills: hug.types.number,
deaths: hug.types.number):
""" Add a leaderboard entry for a player """
try:
db_cursor.execute("""INSERT INTO leaderboard (game_player_id, kills, deaths)
VALUES (%s, %s, %s)""", [game_player_id, kills, deaths])
except Exception as e:
log.error(str(e))
return response_error("Internal pygsm error. See logs for more details.")
if db_cursor.rowcount < 1:
errmsg = "Player stats insert failed!"
log.error(errmsg)
db_connection.rollback()
return response_error(errmsg)
else:
db_connection.commit()
return response_positive("Successfully added player stats.")
@rollback_on_failure
@hug.post('/register-kill', requires=psk_authentication)
def leaderboard_register_kill(alive_game_player_id: hug.types.number,
dead_game_player_id: hug.types.number = None):
""" Register a kill for leaderboard update """
# sanity check
if not alive_game_player_id and not dead_game_player_id:
return response_error("Invalid parameters", code=400)
error = False
error_code = 500
error_messages = []
# First, let's register the kill for the alive player
if alive_game_player_id:
try:
db_cursor.execute("""INSERT INTO leaderboard (game_player_id, kills, deaths)
VALUES (%s, %s, %s)""", [alive_game_player_id, 1, 0])
if db_cursor.rowcount < 1:
errmsg = "Player kill increment failed!"
log.error(errmsg)
db_connection.rollback()
error = True
error_messages.append(errmsg)
else:
db_connection.commit()
except IntegrityError:
errmsg = "Player kill increment failed! Invalid game_player_id?"
log.error(errmsg)
db_connection.rollback()
error = True
error_code = 400
error_messages.append(errmsg)
except Exception as e:
log.error(str(e))
db_connection.rollback()
error = True
error_messages.append("Internal pygsm error. See logs for more details.")
# Now handle the death of the dead player
if dead_game_player_id:
try:
db_cursor.execute("""INSERT INTO leaderboard (game_player_id, kills, deaths)
VALUES (%s, %s, %s)""", [dead_game_player_id, 0, 1])
if db_cursor.rowcount < 1:
errmsg = "Player death increment failed!"
log.error(errmsg)
db_connection.rollback()
error = True
error_messages.append(errmsg)
else:
db_connection.commit()
except IntegrityError:
errmsg = "Player death increment failed! Invalid game_player_id?"
log.error(errmsg)
db_connection.rollback()
error = True
error_code = 400
error_messages.append(errmsg)
except Exception as e:
log.error(str(e))
db_connection.rollback()
error = True
error_messages.append("Internal pygsm error. See logs for more details.")
if error:
return response_error(' '.join(error_messages), code=error_code)
else:
return response_positive("Successfully updated player stats.")