summaryrefslogtreecommitdiff
path: root/swiftstory/SwiftStory.py
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-05-27 20:50:16 +0200
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-05-28 01:00:57 +0200
commit05235c8df61b0cd6bf588ee15a924576cb10aa0a (patch)
tree820706b25287b1c877d47fb3e7dad18731d988c9 /swiftstory/SwiftStory.py
parent40d54a78a6cfb510bfca30d9ab5cdb01f7956548 (diff)
Don't use coroutines for functions sending notifications
Functions which generate notifications for clients were all making use of await and async. This is not great because if we add a notification somewhere, we need to change the function to a coroutine and update all invocations (recursively changing all functions to coroutines). Instead, we now add a task to the event loop whenever a notification needs to be generated. This allows to drop the await and async specifiers from mostly everywhere. On the downside, it means that if we send a notification to n clients, we have to register n tasks. Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory/SwiftStory.py')
-rw-r--r--swiftstory/SwiftStory.py82
1 files changed, 40 insertions, 42 deletions
diff --git a/swiftstory/SwiftStory.py b/swiftstory/SwiftStory.py
index b252040..2aa1444 100644
--- a/swiftstory/SwiftStory.py
+++ b/swiftstory/SwiftStory.py
@@ -14,50 +14,48 @@ from swiftstory.Status import error
game_manager = swiftstory.GameManager.GameManager()
-async def message_received_handler(client, message):
+def message_received_handler(client, message):
try:
json_msg = json.loads(message)
except JSONDecodeError:
- res = error('badly formatted json')
- else:
- op = json_msg['op']
- if op == 'join_game':
- try:
- game_name = json_msg['game_name']
- except KeyError:
- res = error('field `game_name\' is required')
- else:
- lang = json_msg.get('lang')
- res = await client.join_game(game_name, lang)
- elif op == 'view_player_cards':
- res = client.view_player_cards()
- elif op == 'view_black_card':
- res = client.view_black_card()
- elif op == 'view_played_cards':
- res = client.view_played_cards()
- elif op == 'pick_black_card':
- res = await client.pick_black_card()
- elif op == 'designate_card':
- card_id = None
- try:
- card_id = int(json_msg['card_id'])
- except (KeyError, TypeError):
- pass
- finally:
- res = await client.designate_card(card_id)
- elif op == 'play_white_card':
- try:
- card_id = int(json_msg['card_id'])
- except KeyError:
- res = error('field `card_id\' is required')
- else:
- res = await client.play_white_card(card_id)
- elif op == 'collect_cards':
- res = await client.collect_cards()
+ return error('badly formatted json')
+
+ op = json_msg['op']
+ if op == 'join_game':
+ try:
+ game_name = json_msg['game_name']
+ except KeyError:
+ return error('field `game_name\' is required')
else:
- res = error('invalid command')
-
- await client.socket.send(res)
+ lang = json_msg.get('lang')
+ return client.join_game(game_name, lang)
+ elif op == 'view_player_cards':
+ return client.view_player_cards()
+ elif op == 'view_black_card':
+ return client.view_black_card()
+ elif op == 'view_played_cards':
+ return client.view_played_cards()
+ elif op == 'pick_black_card':
+ return client.pick_black_card()
+ elif op == 'designate_card':
+ card_id = None
+ try:
+ card_id = int(json_msg['card_id'])
+ except (KeyError, TypeError):
+ pass
+ finally:
+ return client.designate_card(card_id)
+ elif op == 'play_white_card':
+ try:
+ card_id = int(json_msg['card_id'])
+ except KeyError:
+ return error('field `card_id\' is required')
+ else:
+ return client.play_white_card(card_id)
+ elif op == 'collect_cards':
+ return client.collect_cards()
+ else:
+ return error('invalid command')
async def swiftstory(websocket, path):
@@ -65,9 +63,9 @@ async def swiftstory(websocket, path):
with contextlib.suppress(websockets.exceptions.ConnectionClosed):
async for message in client.socket:
- await message_received_handler(client, message)
+ await client.socket.send(message_received_handler(client, message))
- await client.disconnect()
+ client.disconnect()
def main():