예제 #1
0
    def listen_for_user(self, s):
        """Listen for user requests

        Arguments:
        s -- the socket on which to listen for user requests

        """
        PM = PokerMessage
        MT = MessageTypes
        PCF = PlayerControlFields
        PCT = PlayerControlTypes
        ET = ErrorTypes
        conn, addr = s.accept()
        messages = NetMessage.get_message(conn)
        responses = []
        for message in messages:
            if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL:
                player_control = message[MT.PLAYER_CONTROL]
                if player_control[PCF.CONTROL_TYPE] == PCT.JOIN:
                    response = self.handle_join_request(player_control)
                    responses.append(response)
                elif player_control[PCF.CONTROL_TYPE] == PCT.QUIT:
                    response = self.handle_quit_request(player_control)
                    responses.append(response)
                elif player_control[PCF.CONTROL_TYPE] == PCT.STATUS:
                    response = self.handle_player_status_request(player_control)
                    responses.append(response)
                else:
                    bcc_id = player_control[PCF.BCC_ID]
                    text = "Got unexpected control type: "
                    text = text + player_control[PCF.CONTROL_TYPE]
                    error_type = ET.UNEXPECTED_CONTROL_TYPE
                    error = PlayerError(bcc_id=bcc_id, text=text, error_type=error_type)
                    response = error.make_message()
                    responses.append(response)
            elif message[PM.MESSAGE_TYPE] == MT.TABLE_INFO_REQUEST:
                info_request = message[MT.TABLE_INFO_REQUEST]
                response = self.handle_table_info_request(info_request)
                responses.append(response)
            else:
                text = "Got an unexpected message type: "
                text = text + message[PM.MESSAGE_TYPE]
                error_type = ET.UNEXPECTED_MESSAGE
                error = PlayerError(text=text, error_type=error_type)
                response = error.make_message()
                responses.append(response)
        NetMessage.send_message(conn, responses)
        conn.close()
예제 #2
0
    def handle_player_action(self, player_action):
        """return messages in response to a Client's game action

        Arguments:
        player_action -- a PlayerAction type message

        """
        AF = ActionFields
        ET = ErrorTypes
        bcc_id = player_action[AF.BCC_ID]
        user = self.table.get_user_by_id(bcc_id)
        user.update_latest_contact()
        if (user is not None):
            action_type = player_action[AF.ACTION_TYPE]
            action_amount = None
            if AF.ACTION_AMOUNT in player_action:
                action_amount = player_action[AF.ACTION_AMOUNT]
            action = Action(action_type, action_amount, bcc_id)
            user.perform_action(action)
            messages = [self.table.updater.get_update_message(user)]
            return messages
        else:
            text = 'Player' + bcc_id
            text = text + 'not recognized in player action message.'
            error_type = ET.UNRECOGNIZED_PLAYER
            error = PlayerError(bcc_id=bcc_id,
                                error_type=error_type,
                                text=text,
                                stacktrace_text=traceback.format_exc())
            messages = [error.make_message()]
            return messages
예제 #3
0
 def send_action_out_of_turn_message(self):
     """Add a message to the user's queue indicating an action sent out of
     turn"""
     ET = ErrorTypes
     text = "You played an error out of turn."
     error_type = ET.UNEXPECTED_ACTION
     error = PlayerError(bcc_id=self.bcc_id,
                         text=text,
                         error_type=error_type)
     response = error.make_message()
     self.put_error_on_queue(response)
예제 #4
0
    def send_bad_action_response(self, new_action):
        """Add a 'bad action response' message to the user's message queue

        Arguments:
        new_action -- the bad action

        """
        ET = ErrorTypes
        text = "Invalid action. Please try again."
        error_type = ET.INVALID_ACTION
        error = PlayerError(bcc_id=self.bcc_id,
                            text=text,
                            error_type=error_type)
        response = error.make_message()
        self.put_error_on_queue(response)
예제 #5
0
 def listen(self):
     PM = PokerMessage
     MT = MessageTypes
     PCF = PlayerControlFields
     PCT = PlayerControlTypes
     PCRF = PlayerControlResponseFields
     ET = ErrorTypes
     self.controller1.start()
     HOST = ''
     PORT = 2423
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.bind((HOST, PORT))
     s.listen(50)
     while len(self.users) < 5:
         conn, addr = s.accept()
         messages = NetMessage.get_message(conn)
         responses = []
         for message in messages:
             if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL:
                 player_control = message[MT.PLAYER_CONTROL]
                 if player_control[PCF.CONTROL_TYPE] == PCT.JOIN:
                     chips = player_control[PCF.CHIPS]
                     player_id = player_control[PCF.PLAYER_ID]
                     newUser = NetUser(player_id, chips)
                     self.users.append(newUser)
                     response = {}
                     response[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL_RESPONSE
                     join_success = {}
                     join_success[PCRF.CONTROL_TYPE] = PCT.JOIN
                     join_success[PCRF.PLAYER_ID] = player_id
                     join_success[PCRF.SUCCESS] = True
                     join_success[PCRF.PORT] = 2424
                     response[MT.PLAYER_CONTROL_RESPONSE] = join_success
                     responses.append(response)
                 else:
                     player_id = player_control[PCF.PLAYER_ID]
                     text = "Expected JOIN message, got "
                     text = text + player_control[PCF.CONTROL_TYPE]
                     error_type = ET.UNEXPECTED_CONTROL_TYPE
                     error = PlayerError(player_id=player_id,
                                         text=text,
                                         error_type=error_type)
                     response = error.make_message()
                     responses.append(response)
             else:
                 text = "Expected PLAYER_CONTROL message, got "
                 text = text + message[PM.MESSAGE_TYPE]
                 error_type = ET.UNEXPECTED_MESSAGE
                 error = PlayerError(text=text, error_type=error_type)
                 response = error.make_message()
                 responses.append(response)
         NetMessage.send_message(conn, responses)
         conn.close()
     players = []
     for user in self.users:
         players.append(Player(user, user.chips))
     self.round1.players = players
     self.roundthread = RoundThread(self.round1)
     self.roundthread.start()
     print('started the roundthread')
     self.roundthread.join()
     print('joined with roundthread')
     time.sleep(30)
     self.controller1.stop_listener()
     print('waiting for the listener thread to end')
     self.controller1.join()
     print('joined with the controller')