Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 3
0
 def get_server_clients_detailed(self):
     response = self.send_request(OPCODE_CLIENTS_DETAILED)
     result = []
     if response is None:
         # SA-MP server will return null if there's a lot of players
         # We'll handle this by returning an empty list instead to avoid type error.
         return result
     num_clients = decode_int(response[:2])
     offset = 2
     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
Ejemplo n.º 4
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,
        )
Ejemplo n.º 5
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
Ejemplo n.º 6
0
 def get_server_clients(self):
     response = self.send_request(OPCODE_CLIENTS)
     result = []
     if response is None:
         # SA-MP server will return null if there's a lot of players
         # We'll handle this by returning an empty list instead to avoid type error.
         return result
     num_clients = decode_int(response[:2])
     offset = 2
     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
Ejemplo n.º 7
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
Ejemplo n.º 8
0
 def test_decode_string_4_offset_4(self):
     input = b'    \x0f\x00\x00\x00Convoy Trucking\x00\x00'
     expected = 'Convoy Trucking'
     result = utils.decode_string(input, 4, 4)
     self.assertEqual(expected, result)
Ejemplo n.º 9
0
 def test_decode_string_2(self):
     input = b'\x04\x00Test-----'
     expected = 'Test'
     result = utils.decode_string(input, 0, 2)
     self.assertEqual(expected, result)
Ejemplo n.º 10
0
 def test_decode_string(self):
     input = b'\x04Test+++'
     expected = 'Test'
     result = utils.decode_string(input, 0, 1)
     self.assertEqual(expected, result)
Ejemplo n.º 11
0
    def test_decode_string__unicode(self):
        input = b'\x00\xa2\x00,\x01%\x00\x00\x00- Excellent Dreams Role Play\x99 | VOICE'
        expected = u'- Excellent Dreams Role Play™ | VOICE'

        result = utils.decode_string(input, 0, 9)
        self.assertEqual(expected, result)