Exemple #1
0
 def get_server_rules(self):
     response = self.send_request(OPCODE_RULES)
     num_rules = decode_int(response[:2])
     offset = 2
     result = []
     for n in range(num_rules):
         name = decode_string(response, offset, len_bytes=1)
         offset += 1 + len(name)
         value = decode_string(response, offset, len_bytes=1)
         offset += 1 + len(value)
         rule = Rule(
             name=str(name),
             value=value,
         )
         result.append(rule)
     return result
Exemple #2
0
 def send_rcon_command(self, command, args=tuple(), fetch_response=True):
     """
     Send any command to the server
     leading whitespace is stripped from the response
     :param command: the comand to send
     :param args: tuple or list of arguments to be appended to the command. Can be also a string or an int if only one argument is expected.
     :param fetch_response: Whether to receive response from server. Set this to False if you're not expecting a response; WARNING: If there is a response and you don't fetch it, it may be output as a response of your next command.
     :return list of lines responded from the server or None if fetch_response == False
     """
     command = build_rcon_command(command, args)
     command_length = encode_bytes(
         len(command) & 0xFF,
         len(command) >> 8 & 0xFF)
     payload = self.rcon_password_bytes + command_length + command
     self.send_request(OPCODE_RCON, extras=payload, return_response=False)
     if fetch_response:
         result = []
         while True:
             response = self.receive()
             if response is None:
                 break
             line = decode_string(response, 0, 2)
             if line:
                 result.append(line.lstrip())
             else:
                 break
     if len(result) == 1 and result[0] == 'Invalid RCON password.':
         raise InvalidRconPassword
     return result
Exemple #3
0
    def get_server_info(self):
        response = self.send_request(OPCODE_INFO)

        offset = 0
        hostname = decode_string(response, 5, 4)
        offset += len(hostname)
        gamemode = decode_string(response, offset + 9, 4)
        offset += len(gamemode)
        language = decode_string(response, offset + 13, 4)

        return ServerInfo(
            password=bool(response[0]),
            players=decode_int(response[1:3]),
            max_players=decode_int(response[3:5]),
            hostname=hostname,
            gamemode=gamemode,
            language=language,
        )
Exemple #4
0
 def get_server_clients(self):
     response = self.send_request(OPCODE_CLIENTS)
     num_clients = decode_int(response[:2])
     offset = 2
     result = []
     for n in range(num_clients):
         name = decode_string(response, offset, len_bytes=1)
         offset += 1 + len(name)
         score = decode_int(response[offset:offset + 4])
         offset += 4
         client = Client(
             name=name,
             score=score,
         )
         result.append(client)
     return result
Exemple #5
0
 def get_server_clients_detailed(self):
     response = self.send_request(OPCODE_CLIENTS_DETAILED)
     num_clients = decode_int(response[:2])
     offset = 2
     result = []
     for n in range(num_clients):
         player_id = decode_int(response[offset:offset])
         offset += 1
         name = decode_string(response, offset, len_bytes=1)
         offset += 1 + len(name)
         score = decode_int(response[offset:offset + 4])
         offset += 4
         ping = decode_int(response[offset:offset + 4])
         offset += 4
         detail = ClientDetail(
             id=player_id,
             name=name,
             score=score,
             ping=ping,
         )
         result.append(detail)
     return result