From 878988191f1bddf2dc57ca8d809c2510679591e6 Mon Sep 17 00:00:00 2001 From: Tyrannicodin Date: Sat, 30 Nov 2024 00:27:59 +0000 Subject: [PATCH 1/2] Check response statuses --- bot/util/datagen.py | 4 ++++ bot/util/server.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bot/util/datagen.py b/bot/util/datagen.py index beed9bf..fb07329 100644 --- a/bot/util/datagen.py +++ b/bot/util/datagen.py @@ -157,6 +157,8 @@ async def get_image(self: DataGenerator, path: str) -> Image.Image: return self.cache[path] async with self.http_session.get(path) as response: self.cache[path] = Image.open(BytesIO(await response.content.read())) + if not response.ok: + return Image.new("RGBA", (0, 0)) return self.cache[path] except Image.UnidentifiedImageError: return Image.new("RGBA", (0, 0)) @@ -166,6 +168,8 @@ async def load_data(self: DataGenerator) -> list[Card]: cards = [] async with self.http_session.get("cards") as response: content = await response.content.read() + if not response.ok: + return [] iterator = loads(content.decode()) if has_progression: diff --git a/bot/util/server.py b/bot/util/server.py index ebf12a8..fed189b 100644 --- a/bot/util/server.py +++ b/bot/util/server.py @@ -151,10 +151,10 @@ async def get_deck(self: Server, code: str) -> dict | None: try: async with self.http_session.get(f"deck/{code}") as response: result = loads((await response.content.read()).decode()) + if not response.ok: + return None except (TimeoutError, JSONDecodeError): return None - if result.status != 200: - return None return result async def create_game(self: Server) -> QueueGame | None: @@ -162,6 +162,8 @@ async def create_game(self: Server) -> QueueGame | None: try: async with self.http_session.get("games/create") as response: data: dict[str, str | int] = loads((await response.content.read()).decode()) + if not response.ok: + return None return QueueGame(data) except ( ConnectionError, @@ -176,7 +178,7 @@ async def cancel_game(self: Server, game: QueueGame) -> bool: async with self.http_session.delete( "games/cancel", json={"code": game.secret} ) as response: - data: dict[str, str | None] = loads((await response.content.read()).decode()) + loads((await response.content.read()).decode()) return response.status == 200 except ( ConnectionError, @@ -193,6 +195,8 @@ async def get_game_count(self: Server) -> int: async with self.http_session.get("games/count") as response: data: dict[str, int] = loads((await response.content.read()).decode()) + if not response.ok: + return 0 self.last_game_count = data["games"] self.last_game_count_time = round(time()) return self.last_game_count @@ -211,6 +215,8 @@ async def get_queue_length(self: Server) -> int: async with self.http_session.get("games/queue/length") as response: data: dict[str, int] = loads((await response.content.read()).decode()) + if not response.ok: + return 0 self.last_queue_length = data["queueLength"] self.last_queue_length_time = round(time()) return self.last_queue_length From 155842ed0594881550b0176048e339e9d36e6d0b Mon Sep 17 00:00:00 2001 From: Tyrannicodin Date: Sat, 30 Nov 2024 00:29:16 +0000 Subject: [PATCH 2/2] Fix for api changes --- bot/exts/card.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bot/exts/card.py b/bot/exts/card.py index 0706d0b..912dd80 100644 --- a/bot/exts/card.py +++ b/bot/exts/card.py @@ -90,7 +90,7 @@ def __init__( async def get_stats( self: CardExt, server: Server, deck: list[Card] - ) -> tuple[Image.Image, tuple[int, int, int], dict[str, int], int]: + ) -> tuple[Image.Image, tuple[int, int, int], dict[str, int]]: """Get information and an image of a deck. Args: @@ -110,7 +110,6 @@ async def get_stats( "redstone": 0, "farm": 0, } - cost: int = 0 hermits, items, effects = ([] for _ in range(3)) for card in deck: if card.category == "item": @@ -121,7 +120,6 @@ async def get_stats( type_counts[card.hermit_type] += 1 else: effects.append(card) - cost += card.cost hermits.sort(key=lambda x: x.text_id) items.sort(key=lambda x: x.text_id) @@ -139,7 +137,7 @@ async def get_stats( for i, card_image in enumerate(card_images): card_image = card_image.resize((200, 200)).convert("RGBA") im.paste(card_image, ((i % width) * 200, (i // width) * 200), card_image) - return im, (len(hermits), len(effects), len(items)), type_counts, cost + return im, (len(hermits), len(effects), len(items)), type_counts @global_autocomplete("card_name") async def card_autocomplete(self: CardExt, ctx: AutocompleteContext) -> None: @@ -189,8 +187,8 @@ async def deck(self: CardExt, ctx: SlashContext, code: str, *, hide_hash: bool = ).add_field("Deck loading", "Please wait") message = await ctx.send(embed=e) - im, card_type_counts, hermit_type_counts, cost = await self.get_stats( - server, [server.data_generator.universe[card["props"]["id"]] for card in deck["cards"]] + im, card_type_counts, hermit_type_counts = await self.get_stats( + server, [server.data_generator.universe[card] for card in deck["cards"]] ) if len(deck["tags"]) == 0: e.color = rgb_to_int(TYPE_COLORS[Counter(hermit_type_counts).most_common()[0][0]]) @@ -198,7 +196,7 @@ async def deck(self: CardExt, ctx: SlashContext, code: str, *, hide_hash: bool = e.fields.clear() e = ( e.set_image("attachment://deck.png") - .add_field("Token cost", str(cost), inline=True) + .add_field("Token cost", str(deck["cost"]), inline=True) .add_field( "HEI ratio", f"{card_type_counts[0]}:{card_type_counts[1]}:{card_type_counts[2]}",