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/client.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/client.py')
-rw-r--r-- | swiftstory/client.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/swiftstory/client.py b/swiftstory/client.py index 527c9c6..c3a2aff 100644 --- a/swiftstory/client.py +++ b/swiftstory/client.py @@ -66,12 +66,16 @@ class Client: raise WrongAction('You have to join a game first') return self.game.try_view_black_card(self.player) - def register_notification(self, message): + def monitor_player(self) -> None: + """ Start monitoring the player for notifications and send them. """ async def f(): - try: - await self.socket.send(message) - except websockets.exceptions.ConnectionClosed: - logging.warning("Recipient has disconnected.") + assert self.player is not None + while True: + try: + notif = await self.player.notifications.get() + await self.socket.send(notif) + except websockets.exceptions.ConnectionClosed: + logging.warning("Recipient has disconnected.") asyncio.create_task(f()) |