Beispiel #1
0
 def status(self):
     """Return whether the host is up"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     HS = MM.HostStatus
     HSF = MM.HostStatusFields
     host = self.ip_address
     port = self.port
     status_request_message = {}
     status_request_message[MM.MESSAGE_TYPE] = MT.HOST_STATUS
     status_request = {}
     status_request[HS.ACTION] = HSF.CHECK_STATUS
     status_request_message[MT.HOST_STATUS] = status_request
     sock = None
     try:
         sock = NetMessage.open_socket(host, port)
         NetMessage.send_message(sock, [status_request_message])
         response = NetMessage.get_message(sock)
     except:
         response = []
     finally:
         if sock is not None:
             sock.close()
     if response:
         return "Alive"
     else:
         return "Dead"
Beispiel #2
0
    def send_action(self, action_type, action_amount=None):
        """Attempt to send a player's action

        Arguments:
        action_type -- the type of action to be sent
        action_amount -- the amount associated with the action [default: None]

        """
        PM = PokerMessage
        MT = MessageTypes
        AF = ActionFields
        message = {}
        message[PM.MESSAGE_TYPE] = MT.PLAYER_ACTION
        action = {}
        action[AF.BCC_ID] = self.bcc_id
        action[AF.ACTION_TYPE] = action_type
        if action_amount is not None:
            action[AF.ACTION_AMOUNT] = action_amount
        message[MT.PLAYER_ACTION] = action
        try:
            sock = NetMessage.open_socket(self.game_host, self.game_port)
            NetMessage.send_message(sock, [message])
            responses = NetMessage.get_message(sock)
            return responses[len(responses) - 1]
        except:
            logging.error('An error occurred due to an action request. %s',
                          traceback.format_exc())
            return {'errors':
                    'An error occurred making your action'}
        finally:
            sock.close()
Beispiel #3
0
 def add_table(self, table_id, table_size, big_blind, little_blind):
     """Instruct the remote table manager to create a new table"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TTC = MM.ToggleTableCreated
     TTCF = MM.ToggleTableCreatedFields
     TF = MM.TableFields
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_created_message = {}
     toggle_created_message[MM.MESSAGE_TYPE] = MT.TOGGLE_TABLE_CREATED
     toggle_created = {}
     toggle_created[TTC.ACTION] = TTCF.CREATE
     table_info = {}
     table_info[TF.ID] = table_id
     table_info[TF.SIZE] = table_size
     table_info[TF.BIG_BLIND] = big_blind
     table_info[TF.LITTLE_BLIND] = little_blind
     toggle_created[TTC.TABLE_INFO] = table_info
     toggle_created_message[MT.TOGGLE_TABLE_CREATED] = toggle_created
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_created_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_CREATED:
             toggle_response = message[MT.TOGGLE_TABLE_CREATED]
             if not toggle_response[TTC.SUCCESS]:
                 raise ValueError(toggle_response[TTC.ERROR])
Beispiel #4
0
 def status(self):
     """Return whether the manager is responding"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     host = self.host_handle.ip_address
     port = self.manager_port
     status_request_message = {}
     status_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     status_request = {}
     status_request[MI.REQUEST] = MIF.STATUS_CHECK
     status_request_message[MT.MANAGER_INFO] = status_request
     sock = None
     try:
         sock = NetMessage.open_socket(host, port)
         NetMessage.send_message(sock, [status_request_message])
         response = NetMessage.get_message(sock)
     except:
         response = []
     finally:
         if sock is not None:
             sock.close()
     if response:
         for message in response:
             if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
                 status_response = message[MT.MANAGER_INFO]
                 status = status_response[MIF.STATUS]
                 if status == MIF.RUNNING:
                     return "Running"
                 elif status == MIF.STOPPING:
                     return "Stopping"
                 elif status == MIF.STOPPED:
                     return "Created"
     return "Not Created"
Beispiel #5
0
 def toggle_running(self):
     """Create or destroy the Table Manager on the host"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TMR = MM.ToggleManagerRunning
     TMRF = MM.ToggleManagerRunningFields
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_running_message = {}
     toggle_running_message[MM.MESSAGE_TYPE] = MT.TOGGLE_MANAGER_RUNNING
     toggle_running = {}
     status = self.status()
     if status == "Running" or status == "Stopping":
         toggle_running[TMR.ACTION] = TMRF.STOP
     elif status == "Created":
         toggle_running[TMR.ACTION] = TMRF.START
     elif status == "Not Created":
         error = "Connect start or stop manager. It has not been created"
         raise ValueError(error)
     toggle_running_message[MT.TOGGLE_MANAGER_RUNNING] = toggle_running
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_running_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_RUNNING:
             toggle_response = message[MT.TOGGLE_MANAGER_RUNNING]
             if not toggle_response[TMR.SUCCESS]:
                 raise ValueError(toggle_response[TMR.ERROR])
Beispiel #6
0
    def quit_table(self):
        """Attempt to quit the table

        If successful, return True and None. Otherwise, return False and an
        error message

        """
        PM = PokerMessage
        MT = MessageTypes
        PCT = PlayerControlTypes
        PCF = PlayerControlFields
        PCRF = PlayerControlResponseFields
        success = False
        message = {}
        message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
        player_control = {}
        player_control[PCF.CONTROL_TYPE] = PCT.QUIT
#        player_control[PCF.PLAYER_ID] = self.player_id
        player_control[PCF.BCC_ID] = self.bcc_id
        player_control[PCF.TABLE_ID] = self.table_id
        message[MT.PLAYER_CONTROL] = player_control
        try:
            sock = NetMessage.open_socket(self.manager_host, self.manager_port)
            NetMessage.send_message(sock, [message])
            responses = NetMessage.get_message(sock)
            error_message = None
        except:
            success = False
            error_message = "Could not reach the table"
            responses = []
            logging.error('An error occurred communicating with the table ' +
                          'manager. Stacktrace: %s', traceback.format_exc())
        finally:
            sock.close()
        try:
            for message in responses:
                assert(message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE)
                leave_response = message[MT.PLAYER_CONTROL_RESPONSE]
                assert(leave_response[PCRF.CONTROL_TYPE] == PCT.QUIT)
                #if leave_response[PCRF.SUCCESS] is True:
                success = True
                #else:
                #    error_message = 'failed to leave table'
                #    success = False

        except:
            success = False
            error_message = "The table sent an expected response"
            logging.error('An error occurred processing the table manager\'s' +
                          'response to a quit request. Response: %s\n' +
                          'stacktrace: %s', str(responses),
                          traceback.format_exc())
        return success, error_message
Beispiel #7
0
 def shutdown(self):
     """Tell the host daemon to shut down"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     host = self.ip_address
     port = self.port
     shutdown_request_message = {}
     shutdown_request_message[MM.MESSAGE_TYPE] = MT.SHUT_DOWN
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [shutdown_request_message])
     NetMessage.get_message(sock)
     sock.close()
Beispiel #8
0
def get_all_poker_tables(big_blinds):
    """Return a list of the available poker tables

    Arguments:
    big_blinds -- a list of integers representing the acceptable big blinds

    """
    PM = PokerMessage
    MT = MessageTypes
    TIF = TableInfoFields
    manager_host = hosts.TABLE_MANAGER_HOST
    manager_port = hosts.TABLE_MANAGER_PORT
    info_request_message = {}
    info_request_message[PM.MESSAGE_TYPE] = MT.TABLE_INFO_REQUEST
    info_request = {}
    info_request[TIF.BIG_BLINDS] = big_blinds
    # TODO: pagination
    info_request_message[MT.TABLE_INFO_REQUEST] = info_request
    sock = None
    try:
        sock = NetMessage.open_socket(manager_host, manager_port)
        NetMessage.send_message(sock, [info_request_message])
        response = NetMessage.get_message(sock)
    except:
        logging.error('An error occurred communicating with the table ' +
                      'manager. Stacktrace: %s', traceback.format_exc())
        response = []
    finally:
        if sock is not None:
            sock.close()
    tables = []
    try:
        for message in response:
            if (message[PM.MESSAGE_TYPE] == MT.TABLE_INFO_REQUEST):
                info_response = message[MT.TABLE_INFO_REQUEST]
                tables_info = info_response[TIF.TABLES_INFO]
                for table_info in tables_info:
                    table_id = table_info[TIF.TABLE_ID]
                    table_desc = table_info[TIF.DESCRIPTION]
                    suggested_chips = table_info[TIF.SUGGESTED_CHIPS]
                    #big_blind = table_info[TIF.BIG_BLIND]
                    num_users = table_info[TIF.NUM_USERS]
                    table = (table_id, table_desc, suggested_chips, num_users)
                    tables.append(table)
    except:
        logging.error('An error occurred processing the table manager\'s ' +
                      'response to a table info request. Response: %s\n' +
                      'stacktrack: %s', str(response), traceback.format_exc())
    return tables
Beispiel #9
0
def check_for_valid_user_at_tables(bcc_id):
    """Find out if a user is already joined to a table in the backend

    Arguments:
    bcc_id -- the unique id of the player to search for

    """
    PM = PokerMessage
    MT = MessageTypes
    PCT = PlayerControlTypes
    PCF = PlayerControlFields
    PCRF = PlayerControlResponseFields
    table_id = None
    port = None
    manager_host = hosts.TABLE_MANAGER_HOST
    manager_port = hosts.TABLE_MANAGER_PORT
    check_message = {}
    check_message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
    status_check = {}
    status_check[PCF.CONTROL_TYPE] = PCT.STATUS
    status_check[PCF.BCC_ID] = bcc_id
    check_message[MT.PLAYER_CONTROL] = status_check
    sock = None
    try:
        sock = NetMessage.open_socket(manager_host, manager_port)
        NetMessage.send_message(sock, [check_message])
        response = NetMessage.get_message(sock)
    except:
        logging.error('An error occurred communicating with the table ' +
                      'manager. Stacktrace: %s', traceback.format_exc())
        response = []
    finally:
        if sock is not None:
            sock.close()
    try:
        for message in response:
            if (message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE):
                check_response = message[MT.PLAYER_CONTROL_RESPONSE]
                assert(check_response[PCRF.CONTROL_TYPE] == PCT.STATUS)
                if check_response[PCRF.VALID_USER] is True:
                    table_id = check_response[PCRF.TABLE_ID]
                    port = check_response[PCRF.PORT]
    except:
        logging.error('An error occurred processing the table manager\'s ' +
                      'response to a status check. Response: %s\n' +
                      'stacktrace: %s', str(response), traceback.format_exc())
    return table_id, port
Beispiel #10
0
 def get_tables(self):
     """Return a list of the tables and their information"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     TF = MM.TableFields
     host = self.host_handle.ip_address
     port = self.manager_port
     info_request_message = {}
     info_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     info_request = {}
     info_request[MI.REQUEST] = MIF.GET_TABLES
     info_request_message[MT.MANAGER_INFO] = info_request
     tables = []
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [info_request_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
             info_response = message[MT.MANAGER_INFO]
             for table_info in info_response[MIF.TABLES]:
                 table_id = table_info[TF.ID]
                 status = table_info[TF.STATUS]
                 big = table_info[TF.BIG_BLIND]
                 little = table_info[TF.LITTLE_BLIND]
                 size = table_info[TF.SIZE]
                 num_users = table_info[TF.NUM_USERS]
                 host = table_info[TF.HOST]
                 port = table_info[TF.PORT]
                 if status == TF.RUNNING or status == TF.WAITING:
                     toggle_action = "Stop"
                 elif status == TF.STOPPING:
                     toggle_action = "Cancel stop"
                 else:
                     toggle_action = "Start"
                 table = (table_id, status, big, little, size,
                          num_users, host, port, toggle_action)
                 tables.append(table)
     return tables
Beispiel #11
0
 def num_tables(self):
     """Return the number of tables managed by the manager"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     host = self.host_handle.ip_address
     port = self.manager_port
     info_request_message = {}
     info_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     info_request = {}
     info_request[MI.REQUEST] = MIF.NUM_TABLES
     info_request_message[MT.MANAGER_INFO] = info_request
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [info_request_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
             info_response = message[MT.MANAGER_INFO]
             return info_response[MIF.NUM_TABLES]
Beispiel #12
0
 def toggle_table_running(self, table_id):
     """Make table manager start or stop the specified table"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TTR = MM.ToggleTableRunning
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_running_message = {}
     toggle_running_message[MM.MESSAGE_TYPE] = MT.TOGGLE_TABLE_RUNNING
     toggle_running = {}
     toggle_running[TTR.ID] = table_id
     toggle_running_message[MT.TOGGLE_TABLE_RUNNING] = toggle_running
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_running_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_RUNNING:
             toggle_response = message[MT.TOGGLE_TABLE_RUNNING]
             if not toggle_response[TTR.SUCCESS]:
                 raise ValueError(toggle_response[TTR.ERROR])
Beispiel #13
0
 def send_update_request(self):
     """Send an update request to the game and return the response"""
     PM = PokerMessage
     MT = MessageTypes
     PURF = PlayerUpdateRequestFields
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_UPDATE_REQUEST
     update_request = {}
     update_request[PURF.BCC_ID] = self.bcc_id
     message[MT.PLAYER_UPDATE_REQUEST] = update_request
     try:
         sock = NetMessage.open_socket(self.game_host, self.game_port)
         NetMessage.send_message(sock, [message])
         responses = NetMessage.get_message(sock)
         return responses[len(responses) - 1]
     except:
         logging.error('An error occurred due to an update request. %s',
                       traceback.format_exc())
         return {'errors':
                 'An error occurred requesting an update of the table'}
     finally:
         sock.close()
Beispiel #14
0
 def toggle_created(self):
     """Create or destroy the Table Manager on the host"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TMC = MM.ToggleManagerCreated
     TMCF = MM.ToggleManagerCreatedFields
     host = self.host_handle.ip_address
     toggle_created_message = {}
     toggle_created_message[MM.MESSAGE_TYPE] = MT.TOGGLE_MANAGER_CREATED
     toggle_created = {}
     toggle_created[TMC.USER_PORT] = self.user_port
     toggle_created[TMC.MANAGER_PORT] = self.manager_port
     toggle_created[TMC.TYPE] = self.table_type
     status = self.status()
     if status == "Running":
         raise ValueError('Cannot remove a running manager')
     elif status == "Created":
         toggle_created[TMC.ACTION] = TMCF.REMOVE
         port = self.manager_port
     elif status == "Not Created":
         toggle_created[TMC.ACTION] = TMCF.CREATE
         port = self.host_handle.port
     toggle_created_message[MT.TOGGLE_MANAGER_CREATED] = toggle_created
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_created_message])
     response = NetMessage.get_message(sock)
     sock.close()
     if response is not None:
         for message in response:
             if message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_CREATED:
                 toggle_response = message[MT.TOGGLE_MANAGER_CREATED]
                 success = toggle_response[TMC.SUCCESS]
                 if not success:
                     raise ValueError(toggle_response[TMC.ERROR])
     else:
         raise ValueError("Table could not be contacted")
Beispiel #15
0
    def join_table(self):
        """Attempt to join the table

        If the join is successful, return True, None.
        Otherwise, return False and an error message

        """
        PM = PokerMessage
        MT = MessageTypes
        PCT = PlayerControlTypes
        PCF = PlayerControlFields
        PCRF = PlayerControlResponseFields
        success = False
        join_request = {}
        join_request[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
        join_message = {}
        join_message[PCF.CONTROL_TYPE] = PCT.JOIN
        join_message[PCF.CHIPS] = self.chips
        join_message[PCF.PLAYER_ID] = self.player_id
        join_message[PCF.BCC_ID] = self.bcc_id
        join_message[PCF.TABLE_ID] = self.table_id
        join_request[MT.PLAYER_CONTROL] = join_message
        try:
            sock = NetMessage.open_socket(self.manager_host, self.manager_port)
            NetMessage.send_message(sock, [join_request])
            response = NetMessage.get_message(sock)
            error_message = None
        except:
            success = False
            error_message = "Could not reach the table"
            response = []
            logging.error('An error occurred communicating with the table ' +
                          'manager. Stacktrace: %s', traceback.format_exc())
        finally:
            sock.close()
        try:
            for message in response:
                if (message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE):
                    join_response = message[MT.PLAYER_CONTROL_RESPONSE]
                    assert(join_response[PCRF.CONTROL_TYPE] == PCT.JOIN)
                    if join_response[PCRF.SUCCESS] is True:
                        self.game_port = join_response[PCRF.PORT]
                        self.game_host = join_response[PCRF.HOST]
                        self.num_seats = join_response[PCRF.NUM_SEATS]
                        success = True
                    else:
                        success = False
                        error_message = join_response[PCRF.ERROR]
                else:
                    logging.error('The table manager sent back an ' +
                                  'inappropriate response. Message: %s',
                                  str(message))
                    success = False
                    error_message = "The table sent an unexpected response"
        except:
            success = False
            error_message = "The table sent a malformed response"
            logging.error('An error occurred processing the table manager\'s' +
                          ' response to a join request. Response: %s\n' +
                          'stacktrace: %s', str(response),
                          traceback.format_exc())
        return success, error_message