|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from opengsq.protocols.gamespy1 import GameSpy1 |
| 4 | + |
| 5 | + |
| 6 | +class SSC(GameSpy1): |
| 7 | + """Serious Sam Classic: The First Encounter Protocol""" |
| 8 | + |
| 9 | + full_name = "Serious Sam Classic: The First Encounter" |
| 10 | + |
| 11 | + def __init__(self, host: str, port: int = 25601, timeout: float = 5.0): |
| 12 | + """ |
| 13 | + Initialize the Serious Sam Classic protocol. |
| 14 | +
|
| 15 | + :param host: The hostname or IP address of the server. |
| 16 | + :param port: The port number of the server (default: 25601). |
| 17 | + :param timeout: The timeout for the connection in seconds (default: 5.0). |
| 18 | + """ |
| 19 | + super().__init__(host, port, timeout) |
| 20 | + |
| 21 | + async def get_basic(self) -> dict[str, str]: |
| 22 | + """ |
| 23 | + Asynchronously retrieves comprehensive information about the game server. |
| 24 | +
|
| 25 | + For Serious Sam Classic, we return the full status information as the basic query. |
| 26 | +
|
| 27 | + :return: A dictionary containing comprehensive server information. |
| 28 | + """ |
| 29 | + # Get full status and flatten all information into one dict |
| 30 | + status = await self.get_status() |
| 31 | + |
| 32 | + # Combine info with player information in a flattened format |
| 33 | + result = dict(status.info) |
| 34 | + |
| 35 | + # Add player information as indexed fields |
| 36 | + for i, player in enumerate(status.players): |
| 37 | + for key, value in player.items(): |
| 38 | + result[f"{key}_{i}"] = value |
| 39 | + |
| 40 | + return result |
| 41 | + |
| 42 | + async def get_status(self, xserverquery: bool = False): |
| 43 | + """ |
| 44 | + Asynchronously retrieves the status of the game server. |
| 45 | +
|
| 46 | + Serious Sam Classic doesn't support XServerQuery, so we always use the legacy format. |
| 47 | +
|
| 48 | + :param xserverquery: Ignored for Serious Sam Classic (always uses legacy format). |
| 49 | + :return: A Status object containing the status of the game server. |
| 50 | + """ |
| 51 | + # Always use legacy format for Serious Sam Classic (no xserverquery) |
| 52 | + return await super().get_status(xserverquery=False) |
| 53 | + |
| 54 | + async def get_info(self, xserverquery: bool = False) -> dict[str, str]: |
| 55 | + """ |
| 56 | + Asynchronously retrieves the information of the current game running on the server. |
| 57 | +
|
| 58 | + :param xserverquery: Ignored for Serious Sam Classic (always uses legacy format). |
| 59 | + :return: A dictionary containing the information of the current game. |
| 60 | + """ |
| 61 | + return await super().get_info(xserverquery=False) |
| 62 | + |
| 63 | + async def get_rules(self, xserverquery: bool = False) -> dict[str, str]: |
| 64 | + """ |
| 65 | + Asynchronously retrieves the rules of the current game running on the server. |
| 66 | +
|
| 67 | + :param xserverquery: Ignored for Serious Sam Classic (always uses legacy format). |
| 68 | + :return: A dictionary containing the rules of the current game. |
| 69 | + """ |
| 70 | + return await super().get_rules(xserverquery=False) |
| 71 | + |
| 72 | + async def get_players(self, xserverquery: bool = False) -> list[dict[str, str]]: |
| 73 | + """ |
| 74 | + Asynchronously retrieves the information of each player on the server. |
| 75 | +
|
| 76 | + :param xserverquery: Ignored for Serious Sam Classic (always uses legacy format). |
| 77 | + :return: A list containing the information of each player. |
| 78 | + """ |
| 79 | + return await super().get_players(xserverquery=False) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + import asyncio |
| 84 | + |
| 85 | + async def main_async(): |
| 86 | + ssc = SSC(host="172.29.100.29", port=25601, timeout=5.0) |
| 87 | + status = await ssc.get_status() |
| 88 | + print(status) |
| 89 | + |
| 90 | + asyncio.run(main_async()) |
| 91 | + |
0 commit comments