|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +from opengsq.responses.warfork import Player |
| 6 | +from opengsq.responses.quake2 import Status |
| 7 | +from opengsq.binary_reader import BinaryReader |
| 8 | +from opengsq.protocols.quake3 import Quake3 |
| 9 | + |
| 10 | + |
| 11 | +class Warfork(Quake3): |
| 12 | + """ |
| 13 | + This class represents the Quake3 Protocol for Warfork. It provides methods to interact with the Warfork API. |
| 14 | + """ |
| 15 | + |
| 16 | + full_name = "Warfork Protocol" |
| 17 | + |
| 18 | + def __init__(self, host: str, port: int, timeout: float = 5.0): |
| 19 | + """ |
| 20 | + Initializes the Quake3 object with the given parameters. |
| 21 | +
|
| 22 | + :param host: The host of the server. |
| 23 | + :param port: The port of the server. |
| 24 | + :param timeout: The timeout for the server connection. |
| 25 | + """ |
| 26 | + super().__init__(host, port, timeout) |
| 27 | + |
| 28 | + async def get_status(self, strip_color=True) -> Status: |
| 29 | + br = await self._get_response_binary_reader() |
| 30 | + |
| 31 | + status = Status(info=self._parse_info(br), players=self._parse_players(br)) |
| 32 | + if not strip_color: |
| 33 | + return status |
| 34 | + |
| 35 | + if "sv_hostname" in status.info: |
| 36 | + status.info["sv_hostname"] = Quake3.strip_colors(status.info["sv_hostname"]) |
| 37 | + |
| 38 | + for player in status.players: |
| 39 | + if player.name: |
| 40 | + player.name = Quake3.strip_colors(player.name) |
| 41 | + |
| 42 | + return status |
| 43 | + |
| 44 | + def _parse_players(self, br: BinaryReader): |
| 45 | + """ |
| 46 | + Parses the players from the given BinaryReader object. |
| 47 | +
|
| 48 | + :param br: The BinaryReader object to parse the players from. |
| 49 | + :return: A list containing the players. |
| 50 | + """ |
| 51 | + players = [] |
| 52 | + |
| 53 | + for matches in self._get_player_match_collections(br): |
| 54 | + matches: list[re.Match] = [match.group() for match in matches] |
| 55 | + player = Player( |
| 56 | + frags=int(matches[0]), |
| 57 | + ping=int(matches[1]), |
| 58 | + name=str(matches[2]).strip('"') if len(matches) > 2 else "", |
| 59 | + team=int(matches[3]) if len(matches) > 3 else 0, |
| 60 | + ) |
| 61 | + players.append(player) |
| 62 | + |
| 63 | + return players |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + import asyncio |
| 68 | + |
| 69 | + async def main_async(): |
| 70 | + quake3 = Quake3(host="108.61.18.110", port=27960, timeout=5.0) |
| 71 | + info = await quake3.get_info() |
| 72 | + print(info) |
| 73 | + status = await quake3.get_status() |
| 74 | + print(status) |
| 75 | + |
| 76 | + asyncio.run(main_async()) |
0 commit comments