diff options
| -rw-r--r-- | CAO_Client.py | 15 | ||||
| -rw-r--r-- | CAO_Game.py | 44 | ||||
| -rw-r--r-- | CAO_Status.py | 7 | 
3 files changed, 38 insertions, 28 deletions
| diff --git a/CAO_Client.py b/CAO_Client.py index daf6f7e..3132e57 100644 --- a/CAO_Client.py +++ b/CAO_Client.py @@ -1,3 +1,4 @@ +from CAO_Status import cao_error  from CAO_Game import CAO_Game  class CAO_Client(): @@ -9,7 +10,7 @@ class CAO_Client():      def join_game(self, game_name):          if self.game is not None: -            return ('ERR', 'You are already in a game') +            return cao_error('You are already in a game')          self.game = self.game_manager.join_game(game_name)          return self.game.try_join(self) @@ -19,30 +20,30 @@ class CAO_Client():      def play_white_card(self, card_id):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            return cao_error('You have to join a game first')          return self.game.try_play_card(self.player, card_id)      def pick_black_card(self):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            return cao_error('You have to join a game first')          return self.game.try_become_judge(self.player)      def collect_cards(self):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            cao_error('You have to join a game first')          return self.game.try_collect_cards(self.player)      def designate_card(self, card_id):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            return cao_error('You have to join a game first')          return self.game.try_designate_card(self.player, card_id)      def view_player_cards(self):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            return cao_error('You have to join a game first')          return self.game.try_view_player_cards(self.player)      def view_played_cards(self):          if self.game is None: -            return ('ERR', 'You have to join a game first') +            return cao_error('You have to join a game first')          return self.game.try_view_played_cards(self.player) 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) diff --git a/CAO_Status.py b/CAO_Status.py new file mode 100644 index 0000000..813fcf0 --- /dev/null +++ b/CAO_Status.py @@ -0,0 +1,7 @@ +import json + +def cao_error(msg, code=255): +    return json.dumps({'status': code, 'info': msg}) + +def cao_success(obj): +    return json.dumps({'status': 0, 'result': obj}) | 
