From 6d1807af111802f240a7cb9802bc2d49bf6386e5 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Fri, 17 Jul 2020 20:18:55 +0200 Subject: [PATCH] Add get_public_model stub --- main.py | 18 ++++++++++-------- model.py | 31 ++++++++++++++++++++++++++++++- tehsession.json | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index c7fc51e..193dcf3 100644 --- a/main.py +++ b/main.py @@ -44,26 +44,29 @@ async def handler(request: aiohttp.web.Request): return aiohttp.web.FileResponse('ui.html') -@routes.post(CLIENT_REGEX + '/api/{method}') +@routes.post('/api/{method}') async def handler(request: aiohttp.web.Request): - client = get_client(request) + #client = get_client(request) method = request.match_info.get('method', None) model = request.app['model'] data = await request.json() - print(f'{method=} {client=} {data=}') + print(f'{method=} {data=}') try: assert method in model.ApiMethod.dict - await model.ApiMethod.dict[method](model, client, **data) - return aiohttp.web.Response(status=200) + value = await model.ApiMethod.dict[method](model, **data) + if value: + return aiohttp.web.json_response(value) + else: + return aiohttp.web.Response(status=200) except Exception as e: del e # unused? traceback.print_exc() return aiohttp.web.Response(status=400) - finally: - await model.send_state(client) + #finally: + # await model.send_state(client) @routes.get(CLIENT_REGEX + '/ws') async def _(request: aiohttp.web.Request): @@ -88,7 +91,6 @@ async def _(request: aiohttp.web.Request): @routes.get(f"/api/{admintoken}") async def handler(request: aiohttp.web.Request): model = request.app['model'] - return aiohttp.web.json_response(model.model) diff --git a/model.py b/model.py index bb7fd85..a3ab6f8 100644 --- a/model.py +++ b/model.py @@ -37,12 +37,41 @@ class Model(object): def to_json(self): model = { - "sessions": {session: self.sessions[session].to_json() for session in self.sessions }, + "users": {session: self.sessions[session].to_json() for session in self.sessions }, "clients": {client: self.clients[client].to_json() for client in self.clients }, "items": {item: self.items[item].to_json() for item in self.items }, } return model + @ApiMethod + async def get_public_model(self): + publicmodel = { + "users" : [ + { + "score": 50, + "maxscore": 75, + "timeout": 0, + "uuid": "wasisseneuuid?", + "name": "Knödelkind" + }, + { + "score": 58, + "maxscore": 75, + "timeout": 1595019787, + "uuid": "neuuidsolleindeutigsein", + "name": "Dominickque" + }, + { + "score": 69, + "maxscore": 100, + "timeout": 1595024761, + "uuid": "moneyboyswag420", + "name": "Andi" + }, + ] + } + return publicmodel + @ApiMethod async def test_api(self, clientid): print(f'test_api {clientid=}') diff --git a/tehsession.json b/tehsession.json index 121ff74..a1ee7bc 100644 --- a/tehsession.json +++ b/tehsession.json @@ -1 +1 @@ -{"sessions": {"2b7jrklcn6eulwbw": {"id": "2b7jrklcn6eulwbw", "name": "Die coole session von Player 1", "clients": ["hmrcjnvo2ngs7265", "bkrqopf5j6q3tpta", "cho2o2ntflm4mq3u"], "owner": "bkrqopf5j6q3tpta", "inventories": {"cho2o2ntflm4mq3u": ["hr3gqfed4zelzdcz"], "bkrqopf5j6q3tpta": ["tuqjjll2cltlw6i6"]}}}, "clients": {"bkrqopf5j6q3tpta": {"id": "bkrqopf5j6q3tpta", "name": "Player 1", "session": "2b7jrklcn6eulwbw"}, "cho2o2ntflm4mq3u": {"id": "cho2o2ntflm4mq3u", "name": "Player 2", "session": "2b7jrklcn6eulwbw"}, "hmrcjnvo2ngs7265": {"id": "hmrcjnvo2ngs7265", "name": "Joe", "session": "2b7jrklcn6eulwbw"}}, "items": {"hr3gqfed4zelzdcz": {"id": "hr3gqfed4zelzdcz", "name": "Revive", "description": "Revives a KO pokemon and brings it to 50% health", "image": "https://1.bp.blogspot.com/-Hz8sIf0g7cY/VsyVIWiLsnI/AAAAAAAAnII/DjW20xLr0R4/s1600/max_revive_by_peetzaahhh2010-d8oki1o.png", "tags": {}}, "7htnswtslkla6rqh": {"id": "7htnswtslkla6rqh", "name": "Potion", "description": "Heals your Pokey-Man by 20 hp", "image": "https://cdn.bulbagarden.net/upload/thumb/4/45/PotionBaseSet94.jpg/200px-PotionBaseSet94.jpg", "tags": {}}, "tuqjjll2cltlw6i6": {"id": "tuqjjll2cltlw6i6", "name": "Revive\u2122", "description": "When mom tells you that there is Max-Revive at home", "image": "https://www.pokewiki.de/images/5/58/Vitalkraut_Traumwelt.png", "tags": {}}}} \ No newline at end of file +{"users": {}, "clients": {}, "items": {}} \ No newline at end of file