-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d63061a
commit ec9b73f
Showing
9 changed files
with
233 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,69 @@ | ||
import re | ||
|
||
from opengsq.responses.quake2 import Player, Status | ||
from opengsq.binary_reader import BinaryReader | ||
from opengsq.protocols.quake1 import Quake1 | ||
|
||
|
||
class Quake2(Quake1): | ||
"""Quake2 Protocol""" | ||
full_name = 'Quake2 Protocol' | ||
""" | ||
This class represents the Quake2 Protocol. It provides methods to interact with the Quake2 API. | ||
""" | ||
|
||
full_name = "Quake2 Protocol" | ||
|
||
def __init__(self, host: str, port: int, timeout: float = 5.0): | ||
"""Quake2 Query Protocol""" | ||
""" | ||
Initializes the Quake2 Query Protocol. | ||
:param host: The host of the game server. | ||
:param port: The port of the game server. | ||
:param timeout: The timeout for the connection. Defaults to 5.0. | ||
""" | ||
super().__init__(host, port, timeout) | ||
self._response_header = 'print\n' | ||
self._response_header = "print\n" | ||
|
||
async def get_status(self) -> Status: | ||
""" | ||
Asynchronously retrieves the status of the game server. | ||
:return: A Status object containing the status of the game server. | ||
""" | ||
br = await self._get_response_binary_reader() | ||
return Status(info=self._parse_info(br), players=self._parse_players(br)) | ||
|
||
def _parse_players(self, br: BinaryReader) -> list: | ||
def _parse_players(self, br: BinaryReader): | ||
""" | ||
Parses the players from the given BinaryReader object. | ||
:param br: The BinaryReader object to parse the players from. | ||
:return: A list containing the players. | ||
""" | ||
players = [] | ||
|
||
for matches in self._get_player_match_collections(br): | ||
matches: list[re.Match] = [match.group() for match in matches] | ||
|
||
player = { | ||
'frags': int(matches[0]), | ||
'ping': int(matches[1]), | ||
} | ||
|
||
if len(matches) > 2: | ||
player['name'] = str(matches[2]).strip('"') | ||
|
||
if len(matches) > 3: | ||
player['address'] = str(matches[3]).strip('"') | ||
player = Player( | ||
frags=int(matches[0]), | ||
ping=int(matches[1]), | ||
name=str(matches[2]).strip('"') if len(matches) > 2 else "", | ||
address=str(matches[3]).strip('"') if len(matches) > 3 else "", | ||
) | ||
|
||
players.append(player) | ||
|
||
return players | ||
|
||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
import asyncio | ||
import json | ||
from dataclasses import asdict | ||
|
||
async def main_async(): | ||
quake2 = Quake2(host='46.165.236.118', port=27910, timeout=5.0) | ||
quake2 = Quake2(host="46.165.236.118", port=27910, timeout=5.0) | ||
status = await quake2.get_status() | ||
print(json.dumps(status, indent=None) + '\n') | ||
print(json.dumps(asdict(status), indent=None) + "\n") | ||
|
||
asyncio.run(main_async()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .player import Player | ||
from .status import Status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Player: | ||
""" | ||
Represents a player in the game. | ||
""" | ||
|
||
id: int | ||
"""The player's ID.""" | ||
|
||
score: int | ||
"""The player's score.""" | ||
|
||
time: int | ||
"""The player's time.""" | ||
|
||
ping: int | ||
"""The player's ping.""" | ||
|
||
name: str | ||
"""The player's name.""" | ||
|
||
skin: str | ||
"""The player's skin.""" | ||
|
||
color1: int | ||
"""The player's first color.""" | ||
|
||
color2: int | ||
"""The player's second color.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
from .player import Player | ||
|
||
|
||
@dataclass | ||
class Status: | ||
""" | ||
Represents the status of the server. | ||
""" | ||
|
||
info: dict[str, str] | ||
"""The server information.""" | ||
|
||
players: list[Player] | ||
"""The list of players.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .player import Player | ||
from .status import Status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Player: | ||
""" | ||
Represents a player in the game. | ||
""" | ||
|
||
frags: int | ||
"""The player's frags.""" | ||
|
||
ping: int | ||
"""The player's ping.""" | ||
|
||
name: str | ||
"""The player's name.""" | ||
|
||
address: str | ||
"""The player's address.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
from .player import Player | ||
|
||
|
||
@dataclass | ||
class Status: | ||
""" | ||
Represents the status of the server. | ||
""" | ||
|
||
info: dict[str, str] | ||
"""The server information.""" | ||
|
||
players: list[Player] | ||
"""The list of players.""" |