Skip to content

Commit

Permalink
Update Killing Floor Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Jan 23, 2024
1 parent d4f7a62 commit 42d1909
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
72 changes: 42 additions & 30 deletions opengsq/protocols/killingfloor.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
from opengsq.responses.killingfloor import Status
from opengsq.binary_reader import BinaryReader
from opengsq.exceptions import InvalidPacketException
from opengsq.protocol_socket import UdpClient
from opengsq.protocols.unreal2 import Unreal2


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:])
header = br.read_byte()

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())
1 change: 1 addition & 0 deletions opengsq/responses/killingfloor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .status import Status
15 changes: 15 additions & 0 deletions opengsq/responses/killingfloor/status.py
Original file line number Diff line number Diff line change
@@ -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."""

0 comments on commit 42d1909

Please sign in to comment.