working on players
authorBen Doumenc <bdoumenc@parkeon.com>
Sun, 26 Jul 2015 14:51:40 +0000 (16:51 +0200)
committerBen Doumenc <bdoumenc@parkeon.com>
Sun, 26 Jul 2015 14:51:40 +0000 (16:51 +0200)
r15k/server/app/api.py
r15k/server/datastore.py
r15k/server/server.py

index a1f8d48..9b2ec85 100644 (file)
@@ -12,8 +12,13 @@ from r15k.server.datastore import Datastore
 
 logger = logging.getLogger('r15k.api')
 
+API_ROOT = r"/api"
+PATTERN_ID = r"([\w\d]+)"
+
 
 class GamesHandler(web.RequestHandler):
+    API_ROUTE_ITEMS = API_ROOT + r"/games" 
+    API_ROUTE_ITEM = API_ROUTE_ITEMS + r"/%s" % (PATTERN_ID)
 
     def get(self, id=None):
         games = Datastore("/tmp/games")
@@ -21,7 +26,10 @@ class GamesHandler(web.RequestHandler):
             if games.has_key(str(id)):
                 self.write(games.get(str(id)).dump())
                 return
-        self.write({"games": games.keys()})
+        data = []
+        for k in games.keys():
+            data.append(API_ROUTE_ITEMS % games.get(k).uid)
+        self.write({"games": data})
 
     def post(self):
         games = Datastore("/tmp/games")
@@ -31,10 +39,14 @@ class GamesHandler(web.RequestHandler):
             W = World()
             W.create(data)
             game = Game(W)
-            games.save(game.uid, game)
+            games.set(game.uid, game)
+            games.commit()
             self.write({"uid": game.uid})
 
 class PlayersHandler(web.RequestHandler):
+    API_ROUTE_ITEMS = API_ROOT + r"/games/%s/players" % (PATTERN_ID)
+    API_ROUTE_ITEM = API_ROUTE_ITEMS + r"/%s" % (PATTERN_ID)
+
     def get(self, gameId, playerId=None):
         games = Datastore("/tmp/games")
         if gameId:
@@ -57,10 +69,13 @@ class PlayersHandler(web.RequestHandler):
                 if (data and data.has_key("user") and data.has_key("name")):
                     p = Player(data.get("name"), data.get("user"))
                     game.players[p.uid] = p
-                    games.save()
+                    games.commit()
                     self.write(p.dump())
 
 class OrdersHandler(web.RequestHandler):
+    API_ROUTE_ITEMS = API_ROOT + r"/games/%s/players/%s/orders" % (PATTERN_ID, PATTERN_ID)
+    API_ROUTE_ITEM = API_ROUTE_ITEMS + r"/%s" % (PATTERN_ID)
+
     def get(self, gameId, playerId, ordersId):
         pass
 
index 6a32f5e..9091056 100644 (file)
@@ -10,16 +10,18 @@ class Datastore:
         self.filename = filename
         self.shelve = shelve.open(self.filename)
         logger.info("Opened shelve '%s'" % self.filename)
+        self.cache = {}
 
     def __del__(self):
         self.shelve.close()
         logger.info("Closed shelve '%s'" % self.filename)
 
-    def save(self, uid, data):
-        self.shelve[uid] = data
+    def set(self, uid, data):
+        self.cache[uid] = data
 
     def get(self, uid):
         if self.shelve.has_key(uid):
+            self.cache[uid] = self.shelve[uid]
             return self.shelve[uid]
         return None
 
@@ -29,4 +31,9 @@ class Datastore:
     def keys(self):
         return self.shelve.keys()
 
+    def commit(self):
+        for k, v in self.cache.iteritems():
+            self.shelve[k] = v
+
+
 
index 72d9b63..55b0b7e 100644 (file)
@@ -25,16 +25,15 @@ class NoCacheStaticFileHandler(web.StaticFileHandler):
         # Disable cache
         self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
 
-
 def make_app():
     import app
     routes = [
-        (r"/api/games$", GamesHandler),
-        (r"/api/games/([\w\d]+$)", GamesHandler),
-        (r"/api/games/([\w\d]+)/players", PlayersHandler),
-        (r"/api/games/([\w\d]+)/players/([\w\d]+$)", PlayersHandler),
-        (r"/api/games/([\w\d]+)/players/([\w\d]+$)/orders", OrdersHandler),
-        (r"/api/games/([\w\d]+)/players/([\w\d]+$)/orders/([\w\d]+$)", OrdersHandler),
+        (OrdersHandler.API_ROUTE_ITEMS, OrdersHandler),
+        (OrdersHandler.API_ROUTE_ITEM, OrdersHandler),
+        (PlayersHandler.API_ROUTE_ITEMS, PlayersHandler),
+        (PlayersHandler.API_ROUTE_ITEM, PlayersHandler),
+        (GamesHandler.API_ROUTE_ITEMS, GamesHandler),
+        (GamesHandler.API_ROUTE_ITEM, GamesHandler),
     ]
     print("Routes\n======\n\n" + json.dumps(
         [(url, repr(rh)) for url, rh in routes],