Add get_public_model stub

This commit is contained in:
Dominic Zimmer 2020-07-17 20:18:55 +02:00
parent cb934e8220
commit 6d1807af11
3 changed files with 41 additions and 10 deletions

18
main.py
View File

@ -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)

View File

@ -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=}')

View File

@ -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": {}}}}
{"users": {}, "clients": {}, "items": {}}