-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
78 lines (71 loc) · 2.3 KB
/
routes.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
from flask_restful import Resource, marshal_with, reqparse, abort
from models import Player, Game
from controllers import PlayerController as pc
from controllers import GameController as gc
class RestPlayer(Resource):
@marshal_with(Player.serialize)
def get(self, id):
""" Get a player - example
{
'name': 'mario mario',
'id': 123
}
"""
return pc.get(id) or abort(404)
@marshal_with(Player.serialize)
def post(self):
""" Create a player, POST format:
{
'name': 'mario mario'
}
"""
parser = reqparse.RequestParser()
parser.add_argument('name', type=str, location='json', required=True)
args = parser.parse_args()
return pc.create(args['name'])
class RestGame(Resource):
@marshal_with(Game.serialize)
def get(self, id):
""" Get a game - example
{
u'complete': False,
u'current_frame': 1,
u'current_player':
{u'id': 62621, u'name': u'luigi'},
u'frames': [
{u'frames': [{u'score': 9, u'shots': [4, 5]}], u'pid': 61617},
{u'frames': [], u'pid': 62621}
],
u'id': 73360,
u'players': [
{u'id': 61617, u'name': u'mario'},
{u'id': 62621, u'name': u'luigi'}
],
u'started': True,
u'totals': {u'61617': 9, u'62621': 0}
}
"""
return gc.get(id) or abort(404)
@marshal_with(Game.serialize)
def post(self):
""" Create a game, POSTdata format:
{
'players': [123, 456, 789]
}
"""
parser = reqparse.RequestParser()
parser.add_argument('players', type=list, location='json', required=True)
args = parser.parse_args()
return gc.create(args['players'])
class RestFrameRecorder(Resource):
""" This REST resource is purpouse-built for recording frames.
{
"shots": [7, "/"]
}
"""
@marshal_with(Game.serialize)
def post(self, gid, pid):
parser = reqparse.RequestParser()
parser.add_argument('shots', type=list, location='json', required=True)
args = parser.parse_args()
return gc.frame_for_player(gid, pid, args['shots'])