diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-23 19:43:47 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-23 23:15:21 +0100 |
commit | 7d39216a3bbea50223ac4a6d7a9e6343b8247886 (patch) | |
tree | 86ac5ba1b9b429bc8dfbccf65d0a80abb86c2917 /swiftstory/player.py | |
parent | 143947ad03054b7297de4da4195548860e6541f1 (diff) |
Don't store a reference to a client from the player
The only reason why we had a reference was that we were able to send
notifications back to the client.
Instead, we now store the notifications at the player level.
At the client level, we now have a coroutine that waits for
notifications from the player and sends them when available.
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'swiftstory/player.py')
-rw-r--r-- | swiftstory/player.py | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/swiftstory/player.py b/swiftstory/player.py index 7193b64..011ae51 100644 --- a/swiftstory/player.py +++ b/swiftstory/player.py @@ -1,18 +1,19 @@ +import asyncio import json +from typing import Any class Player: - def __init__(self, client): + def __init__(self): self.cards = {} self.next_idx = 0 - self.client = client - self.score = 0 self.has_played = False self.name = 'default' + self.notifications: asyncio.Queue = asyncio.Queue() def pop_card(self, card_id): return self.cards.pop(card_id) @@ -29,10 +30,7 @@ class Player: self.next_idx += 1 return self.next_idx - 1 - def register_notification(self, obj): - if self.client is None: - return - + def register_notification(self, obj: Any): message = json.dumps({'type': 'notification', 'content': obj}) - self.client.register_notification(message) + self.notifications.put_nowait(message) |