depends.py: get_days and get_solution_parts

This commit is contained in:
Jörn-Michael Miehe 2023-09-12 02:58:10 +00:00
parent b73185e4fb
commit 95dfe2a9df
3 changed files with 50 additions and 2 deletions

View file

@ -5,6 +5,7 @@ from fastapi import Depends
from PIL import Image, ImageFont from PIL import Image, ImageFont
from .advent_image import _XY, AdventImage from .advent_image import _XY, AdventImage
from .calendar_config import CalendarConfig, get_calendar_config
from .config import Config, get_config from .config import Config, get_config
from .image_helpers import list_images_auto, load_image from .image_helpers import list_images_auto, load_image
from .sequence_helpers import Random, set_len, shuffle from .sequence_helpers import Random, set_len, shuffle
@ -21,6 +22,41 @@ async def shuffle_solution(
return "".join(await shuffle(cfg.puzzle.solution)) return "".join(await shuffle(cfg.puzzle.solution))
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_solution_parts(
cfg: Config = Depends(get_config),
days: list[int] = Depends(get_days),
) -> dict[int, set[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, set[str]] = {}
for day, letter in zip(solution_days, cfg.puzzle.solution):
result[day] = result.get(day, set())
result[day].add(letter)
return result
async def shuffle_images_auto( async def shuffle_images_auto(
images: list[str] = Depends(list_images_auto), images: list[str] = Depends(list_images_auto),
) -> list[str]: ) -> list[str]:

View file

@ -1,9 +1,11 @@
import itertools import itertools
import random import random
from typing import Any, Self, Sequence from typing import Any, Self, Sequence, TypeVar
from .config import get_config from .config import get_config
T = TypeVar("T")
class Random(random.Random): class Random(random.Random):
@classmethod @classmethod
@ -11,6 +13,9 @@ class Random(random.Random):
cfg = await get_config() cfg = await get_config()
return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_seed}{bonus_salt}") return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_seed}{bonus_salt}")
def shuffled(self, population: Sequence[T]) -> Sequence[T]:
return self.sample(population, k=len(population))
async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list: async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list:
# Zufallsgenerator # Zufallsgenerator

View file

@ -5,13 +5,20 @@ from pydantic import BaseModel
from ..core.calendar_config import CalendarConfig, get_calendar_config from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import Config, get_config from ..core.config import Config, get_config
from ..core.depends import shuffle_solution from ..core.depends import get_solution_parts, shuffle_solution
from ..core.settings import SETTINGS from ..core.settings import SETTINGS
from ._security import require_admin, user_is_admin from ._security import require_admin, user_is_admin
router = APIRouter(prefix="/admin", tags=["admin"]) router = APIRouter(prefix="/admin", tags=["admin"])
@router.get("/parts")
async def get_parts(
shuffle=Depends(get_solution_parts),
) -> dict[int, set[str]]:
return shuffle
@router.get("/is_admin") @router.get("/is_admin")
async def is_admin( async def is_admin(
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),