summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CAO_Client.py7
-rw-r--r--CAO_Game.py18
-rw-r--r--CAO_Player.py11
-rw-r--r--CAO_Status.py4
-rwxr-xr-xserver.py2
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