working on players
authorBen Doumenc <bdoumenc@parkeon.com>
Sun, 26 Jul 2015 16:05:21 +0000 (18:05 +0200)
committerBen Doumenc <bdoumenc@parkeon.com>
Sun, 26 Jul 2015 16:05:21 +0000 (18:05 +0200)
r15k/engine/game.py
r15k/engine/user.py [new file with mode: 0644]
r15k/server/app/api.py
r15k/server/server.py
r15k/server/user.py [deleted file]

index 1bc9c38..dafc994 100644 (file)
@@ -42,9 +42,10 @@ class Game:
         self.uid = data["uid"]
 
 class Player:
         self.uid = data["uid"]
 
 class Player:
-    def __init__(self, name, user):
+    def __init__(self, name, user, corp):
         self.name = name
         self.user = user
         self.name = name
         self.user = user
+        self.corp = corp
         self.uid = new_id()
 
     def dump(self):
         self.uid = new_id()
 
     def dump(self):
diff --git a/r15k/engine/user.py b/r15k/engine/user.py
new file mode 100644 (file)
index 0000000..51f3a30
--- /dev/null
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+import logging
+logger = logging.getLogger('r15k.user')
+
+from r15k.engine.ids import new_id
+
+class User:
+    def __init__(self, login, name, corp):
+        self.login = login
+        self.name = name
+        self.corp = corp
+        self.uid = new_id()
+
+    def dump(self):
+        data = {}
+        data["login"] = self.login
+        data["name"] = self.name
+        data["corp"] = self.corp
+        data["uid"] = self.uid
+        return data
+
+    def restore(self, data):
+        self.uid = data["uid"]
+        self.login = data["login"]
+        self.name = data["name"]
+        self.corp = data["corp"]
+        return data
+
+
index 9dff071..f54725d 100644 (file)
@@ -8,6 +8,7 @@ import tornado.web as web
 from r15k.engine.world import World
 from r15k.engine.game import Game
 from r15k.engine.game import Player
 from r15k.engine.world import World
 from r15k.engine.game import Game
 from r15k.engine.game import Player
+from r15k.engine.user import User
 from r15k.server.datastore import Datastore
 
 logger = logging.getLogger('r15k.api')
 from r15k.server.datastore import Datastore
 
 logger = logging.getLogger('r15k.api')
@@ -16,6 +17,47 @@ API_ROOT = r"/api"
 PATTERN_ID = r"([\w\d]+)"
 
 
 PATTERN_ID = r"([\w\d]+)"
 
 
+class UsersHandler(web.RequestHandler):
+    API_PATTERN_ITEMS = API_ROOT + r"/users"
+    API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
+    API_ROUTE_ITEMS = API_PATTERN_ITEMS
+    API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID)
+
+    def get(self, id=None):
+        store = Datastore("/tmp/users")
+        user = store.get(str(id))
+        if user:
+            self.write(user.dump())
+            return
+        data = []
+        for k in store.keys():
+            data.append(UsersHandler.API_PATTERN_ITEM % store.get(k).uid)
+        self.write({"users": data})
+
+    def post(self):
+        store = Datastore("/tmp/users")
+        logger.info("GAMES:Got %s" % (self.request.body))
+        data = json.loads(self.request.body)
+        if (data and data.has_key("login") and data.has_key("name") and data.has_key("corp")):
+            U = User(data.get("login"), data.get("name"), data.get("corp"))
+            store.set(U.uid, U)
+            self.write(UsersHandler.API_PATTERN_ITEM % U.uid)
+
+
+class GameBackend:
+    def __init__(self):
+        self.store = Datastore("/tmp/games")
+
+    def getGame(self, gameId):
+        return self.store.get(str(gameId))
+
+    def getPlayer(self, gameId, playerId):
+        game = self.getGame(str(gameId))
+        if game:
+            if game.players.has_key(str(playerId)):
+                return game.players.get(str(playerId))
+
+
 class GamesHandler(web.RequestHandler):
     API_PATTERN_ITEMS = API_ROOT + r"/games"
     API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
 class GamesHandler(web.RequestHandler):
     API_PATTERN_ITEMS = API_ROOT + r"/games"
     API_PATTERN_ITEM = API_PATTERN_ITEMS + r"/%s"
@@ -23,26 +65,25 @@ class GamesHandler(web.RequestHandler):
     API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID)
 
     def get(self, id=None):
     API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID)
 
     def get(self, id=None):
-        games = Datastore("/tmp/games")
-        if id:
-            if games.has_key(str(id)):
-                self.write(games.get(str(id)).dump())
-                return
+        backend = GameBackend()
+        game = backend.getGame(id)
+        if game:
+            self.write(game.dump())
+            return
         data = []
         data = []
-        for k in games.keys():
-            data.append(GamesHandler.API_PATTERN_ITEM % games.get(k).uid)
+        for k in backend.store.keys():
+            data.append(GamesHandler.API_PATTERN_ITEM % backend.getGame(k).uid)
         self.write({"games": data})
 
     def post(self):
         self.write({"games": data})
 
     def post(self):
-        games = Datastore("/tmp/games")
+        backend = GameBackend()
         logger.info("GAMES:Got %s" % (self.request.body))
         data = json.loads(self.request.body)
         if (data and data.has_key("size") and data.has_key("seed")):
             W = World()
             W.create(data)
             game = Game(W)
         logger.info("GAMES:Got %s" % (self.request.body))
         data = json.loads(self.request.body)
         if (data and data.has_key("size") and data.has_key("seed")):
             W = World()
             W.create(data)
             game = Game(W)
-            games.set(game.uid, game)
-            games.commit()
+            backend.store.set(game.uid, game)
             self.write(GamesHandler.API_PATTERN_ITEM % game.uid)
 
 class PlayersHandler(web.RequestHandler):
             self.write(GamesHandler.API_PATTERN_ITEM % game.uid)
 
 class PlayersHandler(web.RequestHandler):
@@ -52,35 +93,31 @@ class PlayersHandler(web.RequestHandler):
     API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID, PATTERN_ID)
 
     def get(self, gameId, playerId=None):
     API_ROUTE_ITEM = API_PATTERN_ITEM % (PATTERN_ID, PATTERN_ID)
 
     def get(self, gameId, playerId=None):
-        games = Datastore("/tmp/games")
-        if gameId:
-            if games.has_key(str(gameId)):
-                if playerId:
-                    game = games.get(str(gameId))
-                    if game.players.has_key(str(playerId)):
-                        self.write(game.players[str(playerId)].dump())
-                        return
-                else:
-                    dump = games.get(str(gameId)).dump()
-                    data = []
-                    for k, v in dump["players"].iteritems():
-                        data.append(PlayersHandler.API_PATTERN_ITEM % v.uid)
-                    self.write({"players": data})
-                    return
+        backend = GameBackend()
+        game = backend.getGame(gameId)
+        player = backend.getPlayer(gameId, playerId)
+        if game and player:
+            self.write(game.players[str(playerId)].dump())
+            return
+        elif game:
+            data = []
+            for k, v in game.players.iteritems():
+                data.append(PlayersHandler.API_PATTERN_ITEM % v.uid)
+            self.write({"players": data})
+            return
         raise web.HTTPError(400)
 
     def post(self, gameId):
         raise web.HTTPError(400)
 
     def post(self, gameId):
-        games = Datastore("/tmp/games")
-        if gameId:
-            if games.has_key(str(gameId)):
-                game = games.get(str(gameId))
-                data = json.loads(self.request.body)
-                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.set(game.uid, game)
-                    self.write(PlayersHandler.API_PATTERN_ITEM % (gameId, p.uid))
-                    return
+        backend = GameBackend()
+        game = backend.getGame(gameId)
+        if game:
+            data = json.loads(self.request.body)
+            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
+                backend.store.set(game.uid, game)
+                self.write(PlayersHandler.API_PATTERN_ITEM % (gameId, p.uid))
+                return
         raise web.HTTPError(400)
 
 
         raise web.HTTPError(400)
 
 
index 55b0b7e..986c395 100644 (file)
@@ -13,7 +13,7 @@ sys.path.append(os.path.join(rootPath, ".."))
 import tornado.ioloop as loop
 import tornado.web as web
 
 import tornado.ioloop as loop
 import tornado.web as web
 
-from app.api import GamesHandler, PlayersHandler, OrdersHandler
+from app.api import GamesHandler, PlayersHandler, OrdersHandler, UsersHandler
 
 # configuration
 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(name)-25s %(message)s')
 
 # configuration
 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(name)-25s %(message)s')
@@ -28,6 +28,8 @@ class NoCacheStaticFileHandler(web.StaticFileHandler):
 def make_app():
     import app
     routes = [
 def make_app():
     import app
     routes = [
+        (UsersHandler.API_ROUTE_ITEMS, UsersHandler),
+        (UsersHandler.API_ROUTE_ITEM, UsersHandler),
         (OrdersHandler.API_ROUTE_ITEMS, OrdersHandler),
         (OrdersHandler.API_ROUTE_ITEM, OrdersHandler),
         (PlayersHandler.API_ROUTE_ITEMS, PlayersHandler),
         (OrdersHandler.API_ROUTE_ITEMS, OrdersHandler),
         (OrdersHandler.API_ROUTE_ITEM, OrdersHandler),
         (PlayersHandler.API_ROUTE_ITEMS, PlayersHandler),
diff --git a/r15k/server/user.py b/r15k/server/user.py
deleted file mode 100644 (file)
index f7a3cca..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-# -*- coding: utf-8 -*-
-
-class User:
-    def __init__(self, login):
-        self.login = login
-
-