diff --git a/opengsq/protocols/killingfloor.py b/opengsq/protocols/killingfloor.py index d8f93fa..046181c 100644 --- a/opengsq/protocols/killingfloor.py +++ b/opengsq/protocols/killingfloor.py @@ -1,3 +1,4 @@ +from opengsq.responses.killingfloor import Status from opengsq.binary_reader import BinaryReader from opengsq.exceptions import InvalidPacketException from opengsq.protocol_socket import UdpClient @@ -5,11 +6,21 @@ class KillingFloor(Unreal2): - """Killing Floor Protocol""" - full_name = 'Killing Floor Protocol' + """ + This class represents the Killing Floor Protocol. It provides methods to interact with the Killing Floor API. + """ - async def get_details(self): - response = await UdpClient.communicate(self, b'\x79\x00\x00\x00' + bytes([self._DETAILS])) + full_name = "Killing Floor Protocol" + + async def get_details(self) -> Status: + """ + Asynchronously retrieves the details of the game server. + + :return: A Status object containing the details of the game server. + """ + response = await UdpClient.communicate( + self, b"\x79\x00\x00\x00" + bytes([self._DETAILS]) + ) # Remove the first 4 bytes \x80\x00\x00\x00 br = BinaryReader(response[4:]) @@ -17,41 +28,42 @@ async def get_details(self): if header != self._DETAILS: raise InvalidPacketException( - 'Packet header mismatch. Received: {}. Expected: {}.' - .format(chr(header), chr(self._DETAILS)) + "Packet header mismatch. Received: {}. Expected: {}.".format( + chr(header), chr(self._DETAILS) + ) ) - details = {} - details['ServerId'] = br.read_long() # 0 - details['ServerIP'] = br.read_string() # empty - details['GamePort'] = br.read_long() - details['QueryPort'] = br.read_long() # 0 - details['ServerName'] = self._read_string(br) - details['MapName'] = self._read_string(br) - details['GameType'] = self._read_string(br) - details['NumPlayers'] = br.read_long() - details['MaxPlayers'] = br.read_long() - details['WaveCurrent'] = br.read_long() - details['WaveTotal'] = br.read_long() - details['Ping'] = br.read_long() - details['Flags'] = br.read_long() - details['Skill'] = self._read_string(br) - - return details - - -if __name__ == '__main__': + return Status( + server_id=br.read_long(), + server_ip=br.read_string(), + game_port=br.read_long(), + query_port=br.read_long(), + server_name=self._read_string(br), + map_name=self._read_string(br), + game_type=self._read_string(br), + num_players=br.read_long(), + max_players=br.read_long(), + wave_current=br.read_long(), + wave_total=br.read_long(), + ping=br.read_long(), + flags=br.read_long(), + skill=self._read_string(br), + ) + + +if __name__ == "__main__": import asyncio import json + from dataclasses import asdict async def main_async(): # killingfloor - killingFloor = KillingFloor(host='185.80.128.168', port=7708, timeout=10.0) + killingFloor = KillingFloor(host="185.80.128.168", port=7708, timeout=10.0) details = await killingFloor.get_details() - print(json.dumps(details, indent=None) + '\n') + print(json.dumps(asdict(details), indent=None) + "\n") rules = await killingFloor.get_rules() - print(json.dumps(rules, indent=None) + '\n') + print(json.dumps(rules, indent=None) + "\n") players = await killingFloor.get_players() - print(json.dumps(players, indent=None) + '\n') + print(json.dumps([asdict(player) for player in players], indent=None) + "\n") asyncio.run(main_async()) diff --git a/opengsq/responses/killingfloor/__init__.py b/opengsq/responses/killingfloor/__init__.py new file mode 100644 index 0000000..2f91dae --- /dev/null +++ b/opengsq/responses/killingfloor/__init__.py @@ -0,0 +1 @@ +from .status import Status diff --git a/opengsq/responses/killingfloor/status.py b/opengsq/responses/killingfloor/status.py new file mode 100644 index 0000000..97498e8 --- /dev/null +++ b/opengsq/responses/killingfloor/status.py @@ -0,0 +1,15 @@ +from dataclasses import dataclass +from ..unreal2 import Status as Unreal2Status + + +@dataclass +class Status(Unreal2Status): + """ + Represents the status of a server. + """ + + wave_current: int + """The current wave number in a game.""" + + wave_total: int + """The total number of waves in a game."""