From ea9497da443968a3aa8aaae41871c5092a795995 Mon Sep 17 00:00:00 2001 From: Tyrannicodin Date: Sat, 23 Nov 2024 16:43:12 +0000 Subject: [PATCH] Cleanup (#9) --- bot/exts/card.py | 1 - bot/exts/game.py | 4 +- bot/util/card_palettes.py | 25 ---------- bot/util/datagen.py | 100 ++------------------------------------ bot/util/probability.py | 2 +- 5 files changed, 8 insertions(+), 124 deletions(-) delete mode 100644 bot/util/card_palettes.py diff --git a/bot/exts/card.py b/bot/exts/card.py index ef24df3..0706d0b 100644 --- a/bot/exts/card.py +++ b/bot/exts/card.py @@ -85,7 +85,6 @@ def __init__( client (Client): The discord bot client manager (ServerManager): The server connection manager _scheduler (AsyncIOScheduler): Event scheduler - generator (DataGenerator): Card data generator """ self.manager: ServerManager = manager diff --git a/bot/exts/game.py b/bot/exts/game.py index c9d43d7..9fa3047 100644 --- a/bot/exts/game.py +++ b/bot/exts/game.py @@ -94,7 +94,7 @@ async def count(self: GameExt, ctx: ComponentContext) -> None: game_count = await server.get_game_count() - if (game_count == 1): + if game_count == 1: game_message = "is 1 game" else: game_message = f"are {game_count} games" @@ -107,7 +107,7 @@ async def update_status(self: GameExt) -> None: for server in self.manager.servers: game_count += await server.get_game_count() - if (game_count == 1): + if game_count == 1: game_word = "game" else: game_word = "games" diff --git a/bot/util/card_palettes.py b/bot/util/card_palettes.py deleted file mode 100644 index 07a554d..0000000 --- a/bot/util/card_palettes.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Contains palettes of cards.""" - -from dataclasses import dataclass - - -@dataclass -class Palette: - """Palette information.""" - - BACKGROUND: tuple[int, int, int] = (226, 202, 139) - NAME: tuple[int, int, int] = (0, 0, 0) - BASIC_ATTACK: tuple[int, int, int] = (0, 0, 0) - SPECIAL_ATTACK: tuple[int, int, int] = (0, 0, 0) - HEALTH: tuple[int, int, int] = (246, 4, 1) - TYPE_BACKGROUND: tuple[int, int, int] = (255, 255, 255) - BASIC_DAMAGE: tuple[int, int, int] = (246, 4, 1) - SPECIAL_DAMAGE: tuple[int, int, int] = (23, 66, 234) - - -palettes: dict[str, Palette] = { - "base": Palette(), - "alter_egos": Palette((25, 25, 25), (255, 255, 255), (255, 255, 255), (255, 255, 255)), - "pharaoh": Palette((239, 228, 103), (246, 4, 1), (246, 4, 1), (23, 66, 234)), - "advent_of_tcg": Palette((206, 211, 206)), -} diff --git a/bot/util/datagen.py b/bot/util/datagen.py index 16f8ef8..beed9bf 100644 --- a/bot/util/datagen.py +++ b/bot/util/datagen.py @@ -3,15 +3,12 @@ from __future__ import annotations from io import BytesIO -from json import load, loads -from typing import Any, Literal +from json import loads +from ssl import SSLContext +from typing import Any -from aiohttp import ClientResponse, ClientSession -from numpy import array -from PIL import Image, ImageDraw -from PIL.ImageFilter import GaussianBlur - -from .card_palettes import Palette, palettes +from aiohttp import ClientSession +from PIL import Image try: has_progression = True @@ -20,91 +17,6 @@ has_progression = False -def change_color( - im: Image.Image, origin: tuple[int, int, int], new: tuple[int, int, int] -) -> Image.Image: - """Change one color to another in an image. - - Args: - ---- - im (Image): The target image to change - origin (tuple): The original color - new (tuple): The new color - """ - data = array(im) - - alpha = len(data.T) == 4 - if alpha: - red, blue, green, _1 = data.T - else: - red, blue, green = data.T - white_areas = (red == origin[0]) & (blue == origin[1]) & (green == origin[2]) - data[..., :3][white_areas.T] = new # Transpose back needed - return Image.fromarray(data) - - -def draw_no_fade( - image: Image.Image, - method: str, - color: tuple[int, int, int], - *args: tuple, - **kwargs: dict, -) -> None: - """Perform an image modification ensuring no fade is made between two colors. - - Args: - ---- - image (Image): The image to modify - method (str): The method to perform on the image - color (tuple): The color of the modification - *args (tuple): Other method arguments - **kwargs (dict): Other keyword method arguments - """ - bw_im = Image.new("1", image.size) - bw_im_draw = ImageDraw.Draw(bw_im) - - getattr(bw_im_draw, method)(*args, **kwargs, fill=1) - - rgba = array(bw_im.convert("RGBA")) - rgba[rgba[..., 0] == 0] = [0, 0, 0, 0] # Convert black to transparrent - rgba[rgba[..., 0] == 255] = (*color, 255) # Convert white to desired colour - image.paste(Image.fromarray(rgba), (0, 0), Image.fromarray(rgba)) - - -def drop_shadow( - image: Image.Image, radius: int, color: tuple[int, int, int, Literal[0]] -) -> Image.Image: - """Generate a drop shadow for an image. - - Args: - ---- - image (Image): The image to create the shaadow for - radius (int): The size of the shadow in pixels - color (tuple): The color of the shadow - - Returns: - ------- - Image containg the drop shadow - """ - base = Image.new("RGBA", (image.width + radius * 2, image.height + radius * 2), color) - alpha = Image.new("L", (image.width + radius * 2, image.height + radius * 2)) - alpha.paste(image.getchannel("A"), (radius, radius)) - base.putalpha(alpha.filter(GaussianBlur(radius))) - return base - - -class Colors: - """Usefull colors.""" - - WHITE = (255, 255, 255) - REPLACE = (0, 172, 96) - REPLACE_2 = (1, 172, 96) - HEALTH_HI = (124, 205, 17) - HEALTH_MID = (213, 118, 39) - HEALTH_LOW = (150, 41, 40) - SHADOW = (0, 0, 0) - - TYPE_COLORS = { "miner": (110, 105, 108), "terraform": (217, 119, 147), @@ -147,8 +59,6 @@ def __init__(self: Card, data: dict, generator: DataGenerator) -> None: self.name: str = data["name"] self.rarityName: str = f"{data['name']} ({self.rarity})" - self.palette: Palette = palettes[data["palette"] if "palette" in data.keys() else "base"] - class HermitCard(Card): """Image creator for a hermit card.""" diff --git a/bot/util/probability.py b/bot/util/probability.py index e26761f..b6abbab 100644 --- a/bot/util/probability.py +++ b/bot/util/probability.py @@ -43,7 +43,7 @@ def probability(hermits_in_deck: int, draws: int, desired_hermits: int) -> float or draws > deck_size - opening_hand_size ): return 0 - res: float + res: float = 0 for i in range(1, opening_hand_size + 1): hermits_in_first_hand = initial_hand_chance(hermits_in_deck, i) if i >= desired_hermits: