Skip to content

Commit

Permalink
Use New API (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunarmagpie authored Nov 30, 2024
1 parent ea9497d commit 76ecee0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
20 changes: 18 additions & 2 deletions bot/exts/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Extension,
Message,
OptionType,
PartialEmoji,
SlashContext,
component_callback,
slash_command,
Expand Down Expand Up @@ -104,19 +105,34 @@ async def count(self: GameExt, ctx: ComponentContext) -> None:
async def update_status(self: GameExt) -> None:
"""Update the bots status."""
game_count: int = 0
queue_length: int = 0

for server in self.manager.servers:
game_count += await server.get_game_count()
queue_length += await server.get_queue_length()

if game_count == 1:
game_word = "game"
else:
game_word = "games"

if queue_length == 1:
queue_word = "player queued"
else:
queue_word = "players queued"

if queue_length != 0 and game_count != 0:
message = f"{game_count} {game_word}, {queue_length} {queue_word}"
else:
message = f"{game_count} {game_word}"

await self.client.change_presence(
activity=Activity(
f"{game_count} {game_word}",
ActivityType.WATCHING,
message,
ActivityType.CUSTOM,
self.manager.servers[0].server_url,
state=message,
emoji=PartialEmoji("1075864519399706754", "bdubs"),
)
)

Expand Down
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_queue_length_time > time() - 60:
return self.last_queue_length

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 76ecee0

Please sign in to comment.