advent22/api/advent22_api/core/depends.py

136 lines
3.2 KiB
Python

from io import BytesIO
from typing import cast
from fastapi import Depends
from PIL import Image, ImageFont
from .advent_image import _XY, AdventImage
from .calendar_config import CalendarConfig, get_calendar_config
from .config import Config, get_config
from .image_helpers import list_images_auto, load_image
from .sequence_helpers import Random, set_len, shuffle
from .webdav import WebDAV
async def get_days(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> list[int]:
"""
Alle Tage, für die es ein Türchen gibt
"""
return list(set(door.day for door in cal_cfg.doors))
async def get_day_parts(
cfg: Config = Depends(get_config),
days: list[int] = Depends(get_days),
) -> dict[int, str]:
"""
Lösung auf vorhandene Tage aufteilen
"""
solution_length = len(cfg.puzzle.solution)
num_days = len(days)
rnd = await Random.get()
solution_days = [
*rnd.shuffled(days * (solution_length // num_days)),
*rnd.sample(days, solution_length % num_days),
]
result: dict[int, str] = {}
for day, letter in zip(solution_days, cfg.puzzle.solution):
result[day] = result.get(day, "")
result[day] += letter
return result
async def shuffle_images_auto(
images: list[str] = Depends(list_images_auto),
) -> list[str]:
"""
Bilder: Reihenfolge zufällig bestimmen
"""
ls = set_len(images, 24)
return await shuffle(ls)
async def get_part_for_day(
day: int,
parts: dict[int, str] = Depends(get_day_parts),
) -> str:
"""
Heute angezeigter Teil der Lösung
"""
return parts[day]
async def get_random_for_day(
day: int,
) -> Random:
"""
Tagesabhängige Zufallszahlen
"""
return await Random.get(day)
async def gen_auto_image_for_day(
day: int,
auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part_for_day),
) -> Image.Image:
"""
Automatisch generiertes Bild erstellen
"""
# Datei existiert garantiert!
img = await load_image(auto_images[day])
image = await AdventImage.from_img(img)
font = ImageFont.truetype(
font=BytesIO(await WebDAV.read_bytes(f"files/{cfg.server.font}")),
size=50,
)
# Buchstaben verstecken
for letter in part:
await image.hide_text(
xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))),
text=letter,
font=font,
)
return image.img
async def get_image_for_day(
day: int,
auto_images: list[str] = Depends(shuffle_images_auto),
cfg: Config = Depends(get_config),
rnd: Random = Depends(get_random_for_day),
part: str = Depends(get_part_for_day),
) -> Image.Image:
"""
Bild für einen Tag abrufen
"""
try:
# Versuche, aus "manual"-Ordner zu laden
img = await load_image(f"images_manual/{day}.jpg")
# Als AdventImage verarbeiten
image = await AdventImage.from_img(img)
return image.img
except RuntimeError:
# Erstelle automatisch generiertes Bild
return await gen_auto_image_for_day(
day=day, auto_images=auto_images, cfg=cfg, rnd=rnd, part=part
)