diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-06-04 01:26:00 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-06-04 01:34:45 +0100 |
commit | 22f12bdd64bc9417bc9af29844213e7735080144 (patch) | |
tree | d004d93f80311e83fe85acc9c5054eba5ff6915d /CAO_Game.py | |
parent | 0d87016f206b9dab6c7a0d694db160dd07156499 (diff) |
generate proper json to send to the clients
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'CAO_Game.py')
-rw-r--r-- | CAO_Game.py | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/CAO_Game.py b/CAO_Game.py index 0502b88..ed446ee 100644 --- a/CAO_Game.py +++ b/CAO_Game.py @@ -1,6 +1,8 @@ from CAO_Player import CAO_Player from CAO_Board import CAO_Board +from CAO_Status import cao_error, cao_success + import json class CAO_Game(): @@ -26,7 +28,7 @@ class CAO_Game(): def try_join(self, client): if len(self.players) >= 10: - return ('ERR', 'too many players in this game') + return cao_error('too many players in this game') cards = [] @@ -34,74 +36,74 @@ class CAO_Game(): for i in range(10): cards.append(self.board.pick_white_card()) except IndexError: - return ('ERR', 'no enough white cards for player') + return cao_error('no enough white cards for player') player = CAO_Player(client, cards) client.set_player(player) self.players.append(player) - return ('OK', '') + return cao_success(None) def try_become_judge(self, player): if self.state is not self.WAITING_NEW_JUDGE: # TODO what if the judge has quit ? - return ('ERR', 'Someone is judge already') + return cao_error('Someone is judge already') self.judge = player self.board.reveal_black_card() self.state = self.WAITING_COLLECTION - return ('OK', '') + return cao_success(None) def try_play_card(self, player, card_id): if self.state is not self.WAITING_COLLECTION: - return ('ERR', 'Who asked you to play now ?!') + return cao_error('Who asked you to play now ?!') if self.judge is player: - return ('ERR', 'You\'re the judge, you silly') + return cao_error('You\'re the judge, you silly') elif player.get_has_played(): - return ('ERR', 'You already played, you dumb ass') + return cao_error('You already played, you dumb ass') try: card = player.pop_card(card_id) except IndexError: - return ('ERR', 'Invalid card id') + return cao_error('Invalid card id') player.set_has_played() self.board.play_card(player, card) - return ('OK', '') + return cao_success(None) def try_collect_cards(self, player): if self.state is not self.WAITING_COLLECTION: - return ('ERR', 'Do you think it\'s the moment for colletion !?') + return cao_error('Do you think it\'s the moment for colletion !?') if self.judge is not player: - return ('ERR', 'You\'re not the judge, you fool!') + return cao_error('You\'re not the judge, you fool!') self.board.shuffle_played_cards() # we prevent the others to play self.state = self.WAITING_DESIGNATION - return ('OK', '') + return cao_success(None) def try_designate_card(self, player, card_id): if self.state is not self.WAITING_DESIGNATION: - return ('ERR', 'Not now, moron !') + return cao_error('Not now, moron !') if self.judge is not player: - return ('ERR', 'Who do you think you are !?') + return cao_error('Who do you think you are !?') if card_id is None and len(self.board.played_cards) > 0: - return ('ERR', 'There are cards on the board, pick one !') + return cao_error('There are cards on the board, pick one !') if card_id is not None or len(self.board.played_cards) > 0: # if there are cards on the board @@ -109,7 +111,7 @@ class CAO_Game(): try: card, winner = self.board.played_cards[card_id] except IndexError: - return ('ERR', 'Invalid card') + return cao_error('Invalid card') winner.inc_score() @@ -127,7 +129,7 @@ class CAO_Game(): self.state = self.WAITING_NEW_JUDGE - return ('OK', '') + return cao_success(None) def try_view_player_cards(self, player): cards = [] @@ -135,15 +137,15 @@ class CAO_Game(): for card in player.cards: cards.append(self.white_desc[card]) - return ('OK', json.dumps(cards)) + return cao_success(cards) def try_view_played_cards(self, player): if self.state is not self.WAITING_DESIGNATION: - return ('ERR', 'Not now, moron !') + return cao_error('Not now, moron !') cards = [] for card, unused in self.board.played_cards: cards.append(self.white_desc[card]) - return ('OK', json.dumps(cards)) + return cao_success(cards) |