forked from alialo/Shoreditch-Gamerunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
80 lines (61 loc) · 1.76 KB
/
util.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
from uuid import uuid4
from copy import copy
from bottle import request
from bottle import abort
def require_json(*fields):
def require_json_inner(f):
def inner_func(*args, **kwargs):
if not 'json' in kwargs:
if not request.json:
abort(400, 'Must use Content-type of application/json')
kwargs['json'] = request.json
for field in fields:
if not field in kwargs['json']:
abort(400, 'Must pass "%s"' % field)
return f(*args, **kwargs)
return inner_func
return require_json_inner
def use_game(f):
def inner_func(db, game_id, *args, **kwargs):
game = db.get(game_id)
if not game:
abort(400, "Invalid game id")
return f(db, game, *args, **kwargs)
return inner_func
def require_player(f):
def inner_func(db, game, *args, **kwargs):
if not 'json' in kwargs:
if not request.json:
abort(400, 'Must use Content-type of application/json')
kwargs['json'] = request.json
if not 'player_id' in kwargs['json']:
abort(400, "Must pass player_id")
player_id = kwargs['json']['player_id']
if not player_id in game['players']:
abort(400, "Player not part of game")
player = game['players'][player_id]
if not 'secret' in kwargs['json']:
abort(400, "Must pass secret")
if not kwargs['json']['secret'] == player['secret']:
abort(400, "Incorrect secret")
del kwargs['json']
return f(db, game, player, *args, **kwargs)
return inner_func
# Remove sensitive data from a document
def sanitise(doc):
doc = copy(doc)
del doc['secret']
return doc
def add_player(db, name, endpoint, player_id=None):
if not player_id:
player_id = uuid4().hex
player_secret = uuid4().hex
player = {
"id": player_id,
"type": "player",
"secret": player_secret,
"name": name,
"endpoint": endpoint
}
db.save(player)
return player