Skip to content

Commit

Permalink
Update protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Jan 24, 2024
1 parent 71cf49d commit 31bc90e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
5 changes: 2 additions & 3 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
modules.rst
opengsq.protocols.rst
opengsq.rst
*.rst
!index.rst
32 changes: 11 additions & 21 deletions opengsq/protocols/quake3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from opengsq.responses.quake2 import Status
from opengsq.binary_reader import BinaryReader
from opengsq.protocols.quake2 import Quake2
from opengsq.exceptions import InvalidPacketException
Expand Down Expand Up @@ -52,31 +53,20 @@ async def get_info(self, strip_color=True) -> dict:

return info

async def get_status(self, strip_color=True) -> dict:
"""
Asynchronously retrieves the status of the game server. The status includes information about the server and players.
:param strip_color: A boolean indicating whether to strip color codes from the status.
:return: A dictionary containing the status of the game server.
"""
async def get_status(self, strip_color=True) -> Status:
br = await self._get_response_binary_reader()

status = {
"info": self._parse_info(br),
"players": self._parse_players(br),
}
status = Status(info=self._parse_info(br), players=self._parse_players(br))

if not strip_color:
return status

if "sv_hostname" in status["info"]:
status["info"]["sv_hostname"] = Quake3.strip_colors(
status["info"]["sv_hostname"]
)
if "sv_hostname" in status.info:
status.info["sv_hostname"] = Quake3.strip_colors(status.info["sv_hostname"])

for player in status["players"]:
if "name" in player:
player["name"] = Quake3.strip_colors(player["name"])
for player in status.players:
if player.name:
player.name = Quake3.strip_colors(player.name)

return status

Expand All @@ -96,10 +86,10 @@ def strip_colors(text: str) -> str:
import json

async def main_async():
quake3 = Quake3(host="85.10.197.106", port=27960, timeout=5.0)
quake3 = Quake3(host="108.61.18.110", port=27960, timeout=5.0)
info = await quake3.get_info()
status = await quake3.get_status()
print(json.dumps(info, indent=None) + "\n")
print(json.dumps(status, indent=None) + "\n")
print(json.dumps(info, indent=None, default=lambda o: o.__dict__) + "\n")
print(json.dumps(status, indent=None, default=lambda o: o.__dict__) + "\n")

asyncio.run(main_async())
12 changes: 6 additions & 6 deletions tests/protocols/result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ async def save_result(self, function_name, result, is_json=True):

if self.enable_save:
if is_json:
if is_dataclass(result):
result = asdict(result)
elif isinstance(result, list):
# set asdict to all items
result = [asdict(item) for item in result if is_dataclass(item)]
# if is_dataclass(result):
# result = asdict(result)
# elif isinstance(result, list):
# # set asdict to all items
# result = [asdict(item) for item in result if is_dataclass(item)]

result = json.dumps(result, indent=4, ensure_ascii=False)
result = json.dumps(result, indent=4, ensure_ascii=False, default=lambda o: o.__dict__)

with open(os.path.join(self.__protocol_path, f'{function_name}.{(is_json and "json" or "txt")}'), 'w', encoding='utf-8') as f:
print(result, file=f)
Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/test_doom3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# handler.enable_save = True

# Quake 4
doom3 = Doom3(host='88.99.0.7', port=28007)
doom3 = Doom3(host='178.162.135.83', port=27735)

@pytest.mark.asyncio
async def test_get_info():
Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/test_minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# handler.enable_save = True

# Minecraft
test = Minecraft(host='valistar.site', port=25565)
test = Minecraft(host='mc.goldcraft.ir', port=25565)

@pytest.mark.asyncio
async def test_get_status():
Expand Down

0 comments on commit 31bc90e

Please sign in to comment.