working on players
[r15k.git] / r15k / server / app / api.py
1 # -*- coding: utf-8 -*-
2
3 import logging
4 import json
5
6 import tornado.web as web
7
8 from r15k.engine.world import World
9 from r15k.engine.game import Game
10 from r15k.engine.game import Player
11 from r15k.engine.user import User
12 from r15k.server.datastore import Datastore
13
14 logger = logging.getLogger('r15k.api')
15
16 API_ROOT = r"/api"
17 PATTERN_ID = r"([\w\d]+)"
18
19
20 class UsersHandler(web.RequestHandler):
21 API_PATTERN_ITEMS = API_ROOT + r"/users"
22 API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
23 API_ROUTE_ITEMS = API_PATTERN_ITEMS
24 API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID)
25
26 def get(self, id=None):
27 store = Datastore("/tmp/users")
28 user = store.get(str(id))
29 if user:
30 self.write(user.dump())
31 return
32 data = []
33 for k in store.keys():
34 data.append(UsersHandler.API_PATTERN_ITEM % store.get(k).uid)
35 self.write({"users": data})
36
37 def post(self):
38 store = Datastore("/tmp/users")
39 logger.info("GAMES:Got %s" % (self.request.body))
40 data = json.loads(self.request.body)
41 if (data and data.has_key("login") and data.has_key("name") and data.has_key("corp")):
42 U = User(data.get("login"), data.get("name"), data.get("corp"))
43 store.set(U.uid, U)
44 self.write(UsersHandler.API_PATTERN_ITEM % U.uid)
45
46
47 class GameBackend:
48 def __init__(self):
49 self.store = Datastore("/tmp/games")
50
51 def getGame(self, gameId):
52 return self.store.get(str(gameId))
53
54 def getPlayer(self, gameId, playerId):
55 game = self.getGame(str(gameId))
56 if game:
57 if game.players.has_key(str(playerId)):
58 return game.players.get(str(playerId))
59
60
61 class GamesHandler(web.RequestHandler):
62 API_PATTERN_ITEMS = API_ROOT + r"/games"
63 API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
64 API_ROUTE_ITEMS = API_PATTERN_ITEMS
65 API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID)
66
67 def get(self, id=None):
68 backend = GameBackend()
69 game = backend.getGame(id)
70 if game:
71 self.write(game.dump())
72 return
73 data = []
74 for k in backend.store.keys():
75 data.append(GamesHandler.API_PATTERN_ITEM % backend.getGame(k).uid)
76 self.write({"games": data})
77
78 def post(self):
79 backend = GameBackend()
80 logger.info("GAMES:Got %s" % (self.request.body))
81 data = json.loads(self.request.body)
82 if (data and data.has_key("size") and data.has_key("seed")):
83 W = World()
84 W.create(data)
85 game = Game(W)
86 backend.store.set(game.uid, game)
87 self.write(GamesHandler.API_PATTERN_ITEM % game.uid)
88
89 class PlayersHandler(web.RequestHandler):
90 API_PATTERN_ITEMS = API_ROOT + r"/games/%s/players"
91 API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
92 API_ROUTE_ITEMS = API_PATTERN_ITEMS % (PATTERN_ID)
93 API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID, PATTERN_ID)
94
95 def get(self, gameId, playerId=None):
96 backend = GameBackend()
97 game = backend.getGame(gameId)
98 player = backend.getPlayer(gameId, playerId)
99 if game and player:
100 self.write(game.players[str(playerId)].dump())
101 return
102 elif game:
103 data = []
104 for k, v in game.players.iteritems():
105 data.append(PlayersHandler.API_PATTERN_ITEM % v.uid)
106 self.write({"players": data})
107 return
108 raise web.HTTPError(400)
109
110 def post(self, gameId):
111 backend = GameBackend()
112 game = backend.getGame(gameId)
113 if game:
114 data = json.loads(self.request.body)
115 if (data and data.has_key("user") and data.has_key("name")):
116 p = Player(data.get("name"), data.get("user"))
117 game.players[p.uid] = p
118 backend.store.set(game.uid, game)
119 self.write(PlayersHandler.API_PATTERN_ITEM % (gameId, p.uid))
120 return
121 raise web.HTTPError(400)
122
123
124 class OrdersHandler(web.RequestHandler):
125 API_PATTERN_ITEMS = API_ROOT + r"/games/%s/players/%s/orders"
126 API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
127 API_ROUTE_ITEMS = API_PATTERN_ITEMS % (PATTERN_ID, PATTERN_ID)
128 API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID, PATTERN_ID, PATTERN_ID)
129
130 def get(self, gameId, playerId, ordersId):
131 pass
132
133