Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #9

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bot/exts/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions bot/exts/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
25 changes: 0 additions & 25 deletions bot/util/card_palettes.py

This file was deleted.

100 changes: 5 additions & 95 deletions bot/util/datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion bot/util/probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading