From 05235c8df61b0cd6bf588ee15a924576cb10aa0a Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 27 May 2020 20:50:16 +0200 Subject: 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 --- swiftstory/Game.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'swiftstory/Game.py') diff --git a/swiftstory/Game.py b/swiftstory/Game.py index 1282c0e..93502bd 100644 --- a/swiftstory/Game.py +++ b/swiftstory/Game.py @@ -28,7 +28,7 @@ class Game: random.shuffle(self.board.white_pick) random.shuffle(self.board.black_pick) - async def try_join(self, client): + def try_join(self, client): if len(self.players) >= 10: return error('too many players in this game') @@ -52,7 +52,7 @@ class Game: for p in self.players: if p is not player: - await p.send_notification({'op': 'player_joined_game'}) + p.register_notification({'op': 'player_joined_game'}) cards = [(idx, desc) for idx, (_, desc) in player.cards.items()] @@ -66,7 +66,7 @@ class Game: return success({'cards': cards, 'game_state': state}) - async def try_become_judge(self, player): + def try_become_judge(self, player): if self.state is not self.WAITING_NEW_JUDGE: # TODO what if the judge has quit ? return error('Someone is judge already') @@ -78,12 +78,12 @@ class Game: for p in self.players: if p is not player: - await p.send_notification({'op': 'judge_designed'}) + p.register_notification({'op': 'judge_designed'}) return self.try_view_black_card(player) - async def try_play_card(self, player, card_id): + def try_play_card(self, player, card_id): if self.state is not self.WAITING_COLLECTION: return error('Who asked you to play now ?!') @@ -101,12 +101,12 @@ class Game: self.board.play_card(player, card) - await self.judge.send_notification({'op': 'card_played'}) + self.judge.register_notification({'op': 'card_played'}) return success({'card_id': card_id}) - async def try_collect_cards(self, player): + def try_collect_cards(self, player): if self.state is not self.WAITING_COLLECTION: return error('Do you think it\'s the moment for colletion !?') @@ -120,12 +120,12 @@ class Game: for p in self.players: if p is not player: - await p.send_notification({'op': 'cards_collected'}) + p.register_notification({'op': 'cards_collected'}) return self.try_view_played_cards(player) - async def try_designate_card(self, player, card_id): + def try_designate_card(self, player, card_id): if self.state is not self.WAITING_DESIGNATION: return error('Not now, moron !') @@ -143,7 +143,7 @@ class Game: except IndexError: return error('Invalid card') - await winner.inc_score() + winner.inc_score() # put the cards back in the deck self.board.recycle_played_cards() @@ -153,7 +153,7 @@ class Game: if p.has_played: idx = p.receive_card(self.board.pick_white_card()) - await p.send_notification({ + p.register_notification({ 'op': 'received_card', 'content': { 'card': { @@ -168,7 +168,7 @@ class Game: for p in self.players: if p is not player: - await p.send_notification({'op': 'judge_needed'}) + p.register_notification({'op': 'judge_needed'}) self.state = self.WAITING_NEW_JUDGE @@ -191,19 +191,19 @@ class Game: return error('The black card has not been revealed yet') - async def disconnect(self, player): + def disconnect(self, player): player.client = None if self.judge is player: self.judge = None for p in self.players: - await p.send_notification({'op': 'judge_needed'}) + p.register_notification({'op': 'judge_needed'}) for card, p in self.board.played_cards: p.receive_card(card) - await p.send_notification({ + p.register_notification({ 'op': 'received_card', 'content': { 'card': { -- cgit v1.2.3