remove static "loesungswort"

This commit is contained in:
Jörn-Michael Miehe 2022-10-14 23:21:14 +00:00
parent 3bee806262
commit 4ea2b37362
2 changed files with 12 additions and 8 deletions

View file

@ -1,6 +1,6 @@
import itertools import itertools
import random import random
from typing import Sequence from typing import Any, Sequence
from ..dav_common import dav_get_file from ..dav_common import dav_get_file
@ -10,6 +10,10 @@ async def get_loesungswort() -> str:
return fp.read().decode("utf8").strip() return fp.read().decode("utf8").strip()
async def get_rnd(bonus_salt: Any = "") -> random.Random:
loesungswort = await get_loesungswort()
return random.Random(f"{loesungswort}{bonus_salt}")
async def set_length(seq: Sequence, length: int) -> list: async def set_length(seq: Sequence, length: int) -> list:
# `seq` unendlich wiederholen # `seq` unendlich wiederholen
@ -18,9 +22,10 @@ async def set_length(seq: Sequence, length: int) -> list:
return list(itertools.islice(infinite, length)) return list(itertools.islice(infinite, length))
async def shuffle(seq: Sequence) -> list: async def shuffle(seq: Sequence, rnd: random.Random | None = None) -> list:
# Zufallsgenerator # Zufallsgenerator
rnd = random.Random(await get_loesungswort()) if rnd is None:
rnd = await get_rnd()
# Elemente mischen # Elemente mischen
return rnd.sample(seq, len(seq)) return rnd.sample(seq, len(seq))

View file

@ -1,5 +1,4 @@
import colorsys import colorsys
import random
import re import re
# from datetime import date # from datetime import date
from io import BytesIO from io import BytesIO
@ -10,15 +9,14 @@ from PIL import ImageDraw, ImageFont
from ..dav_common import dav_get_file, dav_list_files from ..dav_common import dav_get_file, dav_list_files
from ._image import AdventImage from ._image import AdventImage
from ._misc import set_length, shuffle from ._misc import get_loesungswort, get_rnd, set_length, shuffle
router = APIRouter(prefix="/days", tags=["days"]) router = APIRouter(prefix="/days", tags=["days"])
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
@router.on_event("startup") @router.on_event("startup")
async def startup() -> None: async def startup() -> None:
loesungswort = await get_loesungswort()
print(loesungswort) print(loesungswort)
print("".join(await shuffle(loesungswort))) print("".join(await shuffle(loesungswort)))
@ -26,6 +24,7 @@ async def startup() -> None:
@router.get("/letter/{index}") @router.get("/letter/{index}")
async def get_letter( async def get_letter(
index: int, index: int,
loesungswort: str = Depends(get_loesungswort),
) -> str: ) -> str:
return (await shuffle(loesungswort))[index] return (await shuffle(loesungswort))[index]
@ -99,7 +98,7 @@ async def get_picture_for_day(
font = ImageFont.truetype("Lena.ttf", 50) font = ImageFont.truetype("Lena.ttf", 50)
# Position des Buchstaben bestimmen # Position des Buchstaben bestimmen
rnd = random.Random(f"{loesungswort}{index}") rnd = await get_rnd(index)
xy = tuple(rnd.choices(range(30, 470), k=2)) xy = tuple(rnd.choices(range(30, 470), k=2))
# betroffenen Bildbereich bestimmen # betroffenen Bildbereich bestimmen