2023-09-08 19:08:13 +00:00
|
|
|
from io import BytesIO
|
2023-09-08 18:17:18 +00:00
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from fastapi import Depends
|
|
|
|
from PIL import Image, ImageFont
|
|
|
|
|
|
|
|
from .advent_image import _XY, AdventImage
|
2023-09-08 19:53:35 +00:00
|
|
|
from .config import Config, get_config
|
2023-09-08 18:17:18 +00:00
|
|
|
from .image_helpers import list_images_auto, load_image
|
|
|
|
from .sequence_helpers import Random, set_len, shuffle
|
|
|
|
from .webdav import WebDAV
|
|
|
|
|
|
|
|
|
2023-09-08 19:19:08 +00:00
|
|
|
async def shuffle_solution(
|
2023-09-08 19:53:35 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2023-09-08 19:19:08 +00:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Lösung: Reihenfolge zufällig bestimmen
|
|
|
|
"""
|
|
|
|
|
|
|
|
return "".join(await shuffle(cfg.puzzle.solution))
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
day: int,
|
|
|
|
shuffled_solution: str = Depends(shuffle_solution),
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Heute angezeigter Teil der Lösung
|
|
|
|
"""
|
|
|
|
|
|
|
|
return shuffled_solution[day]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_random(
|
|
|
|
day: int,
|
|
|
|
) -> Random:
|
|
|
|
"""
|
|
|
|
Tagesabhängige Zufallszahlen
|
|
|
|
"""
|
|
|
|
|
|
|
|
return await Random.get(day)
|
|
|
|
|
|
|
|
|
|
|
|
async def gen_auto_image(
|
|
|
|
day: int,
|
|
|
|
auto_images: list[str] = Depends(shuffle_images_auto),
|
2023-09-08 19:53:35 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2023-09-08 19:19:08 +00:00
|
|
|
rnd: Random = Depends(get_random),
|
|
|
|
part: str = Depends(get_part),
|
|
|
|
) -> 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,
|
2023-09-08 18:17:18 +00:00
|
|
|
)
|
|
|
|
|
2023-09-08 19:19:08 +00:00
|
|
|
return image.img
|
|
|
|
|
|
|
|
|
|
|
|
async def get_image(
|
|
|
|
day: int,
|
|
|
|
auto_images: list[str] = Depends(shuffle_images_auto),
|
2023-09-08 19:53:35 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2023-09-08 19:19:08 +00:00
|
|
|
rnd: Random = Depends(get_random),
|
|
|
|
part: str = Depends(get_part),
|
|
|
|
) -> Image.Image:
|
|
|
|
"""
|
|
|
|
Bild für einen Tag abrufen
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Versuche, aus "manual"-Ordner zu laden
|
|
|
|
return await load_image(f"images_manual/{day}.jpg")
|
|
|
|
|
|
|
|
except RuntimeError:
|
|
|
|
# Erstelle automatisch generiertes Bild
|
|
|
|
return await gen_auto_image(
|
|
|
|
day=day, auto_images=auto_images, cfg=cfg, rnd=rnd, part=part
|
|
|
|
)
|