Skip to content

Commit

Permalink
Some api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunarmagpie committed Nov 22, 2024
1 parent 6c9df5a commit bb70000
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions bot/util/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def __init__(
self.server_id: str = server_id
self.last_game_count: int = 0
self.last_game_count_time: int = 0
self.last_queue_length: int = 0
self.last_queue_length_time: int = 0

self.server_url: str = server_url
self.guild_id: str = guild_id
Expand Down Expand Up @@ -151,9 +153,9 @@ async def get_deck(self: Server, code: str) -> dict | None:
result = loads((await response.content.read()).decode())
except (TimeoutError, JSONDecodeError):
return None
if result["type"] == "success":
return result
return None
if (result.status !== 200):
return None
return result

async def create_game(self: Server) -> QueueGame | None:
"""Create a server game."""
Expand All @@ -175,7 +177,7 @@ async def cancel_game(self: Server, game: QueueGame) -> bool:
"games/cancel", json={"code": game.secret}
) as response:
data: dict[str, str | None] = loads((await response.content.read()).decode())
return "success" in data.keys()
return response.status === 200
except (
ConnectionError,
JSONDecodeError,
Expand All @@ -201,6 +203,24 @@ async def get_game_count(self: Server) -> int:
):
return 0

async def get_queue_length(self: Server) -> int:
"""Get the number of games."""
try:
if self.last_game_count_time > time() - 60:
return self.last_game_count

async with self.http_session.get("games/queue/length") as response:
data: dict[str, int] = loads((await response.content.read()).decode())
self.last_queue_length = data["queueLength"]
self.last_queue_length_time = round(time())
return self.last_queue_length
except (
ConnectionError,
JSONDecodeError,
KeyError,
):
return 0


class ServerManager:
"""Manage multiple servers and their functionality."""
Expand Down

0 comments on commit bb70000

Please sign in to comment.