From 0cae8115a93589e37943590db45967beec841a07 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 4 Jun 2015 05:54:16 +0100 Subject: added a system of notification what the client will receive will look like: {'type': TYPE, 'content': CONTENT} TYPE will be either 'notification' or 'response' CONTENT will be the content of the notification or the response Signed-off-by: Olivier Gayot --- CAO_Client.py | 7 ++++++- CAO_Game.py | 18 ++++++++++++++++++ CAO_Player.py | 11 ++++++++++- CAO_Status.py | 4 ++-- server.py | 2 +- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CAO_Client.py b/CAO_Client.py index 6eccc2d..b9da42a 100644 --- a/CAO_Client.py +++ b/CAO_Client.py @@ -2,10 +2,12 @@ from CAO_Status import cao_error from CAO_Game import CAO_Game class CAO_Client(): - def __init__(self, game_manager): + def __init__(self, socket, handler, game_manager): self.game = None self.game_manager = game_manager + self.handler = handler + self.socket = socket self.player = None def join_game(self, game_name): @@ -56,3 +58,6 @@ class CAO_Client(): if self.game is None: return cao_error('You have to join a game first') return self.game.try_view_black_card(self.player) + + def send_notification(self, message): + self.socket.send_message(self.handler, message) diff --git a/CAO_Game.py b/CAO_Game.py index 7275f56..470eb71 100644 --- a/CAO_Game.py +++ b/CAO_Game.py @@ -48,6 +48,10 @@ class CAO_Game(): self.players.append(player) + for p in self.players: + if p is not player: + p.send_notification('somebody has joined the game') + return self.try_view_player_cards(player) @@ -61,6 +65,10 @@ class CAO_Game(): self.state = self.WAITING_COLLECTION + for p in self.players: + if p is not player: + p.send_notification('a judge has been designed') + return self.try_view_black_card(player) @@ -82,6 +90,8 @@ class CAO_Game(): self.board.play_card(player, card) + self.judge.send_notification('somebody played a card') + return cao_success({'card_id': card_id}) @@ -97,6 +107,10 @@ class CAO_Game(): # we prevent the others to play self.state = self.WAITING_DESIGNATION + for p in self.players: + if p is not player: + p.send_notification('somebody collected the cards') + return self.try_view_played_cards(player) @@ -132,6 +146,10 @@ class CAO_Game(): self.board.recycle_black_card() self.judge = None # useful or not ... + for p in self.players: + if p is not player: + p.send_notification('we need a new judge') + self.state = self.WAITING_NEW_JUDGE return cao_success(None) diff --git a/CAO_Player.py b/CAO_Player.py index 94ff2cf..a4dc326 100644 --- a/CAO_Player.py +++ b/CAO_Player.py @@ -1,3 +1,5 @@ +import json + class CAO_Player(): def __init__(self, client): self.cards = {} @@ -14,7 +16,6 @@ class CAO_Player(): def pop_card(self, card_id): return self.cards.pop(card_id) - def get_has_played(self): return self.has_played @@ -27,3 +28,11 @@ class CAO_Player(): def receive_card(self, card): self.cards[self.next_idx] = card self.next_idx += 1 + + def send_notification(self, obj): + if self.client is None: + return + + message = json.dumps({'type': 'notification', 'content': obj}) + + self.client.send_notification(message) diff --git a/CAO_Status.py b/CAO_Status.py index 813fcf0..782a0b4 100644 --- a/CAO_Status.py +++ b/CAO_Status.py @@ -1,7 +1,7 @@ import json def cao_error(msg, code=255): - return json.dumps({'status': code, 'info': msg}) + return json.dumps({'type': 'response', 'content': {'status': code, 'info': msg}}) def cao_success(obj): - return json.dumps({'status': 0, 'result': obj}) + return json.dumps({'type': 'response', 'content': {'status': 0, 'result': obj}}) diff --git a/server.py b/server.py index 77d8cab..cdd8abf 100755 --- a/server.py +++ b/server.py @@ -10,7 +10,7 @@ import json game_manager = CAO_GameManager.CAO_GameManager() def new_client_handler(client, server): - client['cao_client'] = CAO_Client.CAO_Client(game_manager) + client['cao_client'] = CAO_Client.CAO_Client(server, client, game_manager) def client_left_handler(client, server): pass -- cgit v1.2.3