diff --git a/Ideen.md b/Ideen.md index e8722e9..4a903bb 100644 --- a/Ideen.md +++ b/Ideen.md @@ -2,8 +2,6 @@ - Extra-Türchen (kein Buchstabe, nur manuelles Bild) - Türchen mit Tag "0" einem zufälligen Tag zuweisen -- Option "Nur Groß-/Kleinbuchstaben" (standard nur groß) -- Option "Leerzeichen ignorieren" (standard ja) - Option "Überspringe leere Türchen" (standard ja) - Option "custom Zuordnung Buchstaben" (standard leer) @@ -20,3 +18,5 @@ - Türchen anzeigen im DoorMapEditor - Lösungsbuchstaben weniger als türchen erzeugt bug - Türchen sichtbar machen (besser für touch, standard nein) +- Option "Nur Groß-/Kleinbuchstaben" (standard nur groß) +- Option "Leerzeichen ignorieren" (standard ja) diff --git a/api/advent22_api/core/config.py b/api/advent22_api/core/config.py index fa0aba1..255cdbd 100644 --- a/api/advent22_api/core/config.py +++ b/api/advent22_api/core/config.py @@ -1,6 +1,9 @@ +import re import tomllib +from enum import Enum +from random import Random -from pydantic import BaseModel +from pydantic import BaseModel, field_validator from .settings import SETTINGS from .webdav import WebDAV @@ -11,13 +14,72 @@ class User(BaseModel): password: str +class TransformedString(BaseModel): + class __Whitespace(str, Enum): + # Leerzeichen an Anfang und Ende entfernen + STRIP = "STRIP" + + # whitespace entfernen + IGNORE = "IGNORE" + + class __Case(str, Enum): + # GROSSBUCHSTABEN + UPPER = "UPPER" + + # kleinbuchstaben + LOWER = "LOWER" + + # ZuFÄllIg + RANDOM = "RANDOM" + + value: str + + whitespace: __Whitespace | None = __Whitespace.IGNORE + case: __Case | None = __Case.UPPER + + @field_validator("whitespace", "case", mode="before") + def transform_from_str(cls, v) -> str | None: + if (result := str(v).upper()) != "KEEP": + return result + + @property + def clean(self) -> str: + result = self.value + + # Whitespace bearbeiten + if self.whitespace is self.__Whitespace.STRIP: + result = result.strip() + + elif self.whitespace is self.__Whitespace.IGNORE: + result = re.sub(string=result, pattern=r"\s+", repl="") + + # Groß-/Kleinschreibung verarbeiten + if self.case is self.__Case.UPPER: + result = result.upper() + + elif self.case is self.__Case.LOWER: + result = result.lower() + + elif self.case is self.__Case.RANDOM: + rnd = Random(self.value) + + def randomcase(c: str) -> str: + if rnd.choice((True, False)): + return c.upper() + return c.lower() + + result = "".join(randomcase(c) for c in result) + + return result + + class Puzzle(BaseModel): # Titel # TODO penner neue Route GET /user/title title: str # Lösungswort - solution: str + solution: TransformedString # Tag, an dem der Kalender startet # TODO penner diff --git a/api/advent22_api/core/depends.py b/api/advent22_api/core/depends.py index 8e10534..61ccaf3 100644 --- a/api/advent22_api/core/depends.py +++ b/api/advent22_api/core/depends.py @@ -46,7 +46,7 @@ async def get_all_parts( Lösung auf vorhandene Tage aufteilen """ - solution_length = len(cfg.puzzle.solution) + solution_length = len(cfg.puzzle.solution.clean) num_days = len(days) rnd = await Random.get() @@ -60,7 +60,7 @@ async def get_all_parts( ] result: dict[int, str] = {} - for day, letter in zip(solution_days, cfg.puzzle.solution): + for day, letter in zip(solution_days, cfg.puzzle.solution.clean): result[day] = result.get(day, "") result[day] += letter diff --git a/api/advent22_api/core/helpers.py b/api/advent22_api/core/helpers.py index 64b6fde..70fe716 100644 --- a/api/advent22_api/core/helpers.py +++ b/api/advent22_api/core/helpers.py @@ -18,7 +18,7 @@ class Random(random.Random): @classmethod async def get(cls, bonus_salt: Any = "") -> Self: cfg = await get_config() - return cls(f"{cfg.puzzle.solution}{cfg.random_seed}{bonus_salt}") + return cls(f"{cfg.puzzle.solution.clean}{cfg.random_seed}{bonus_salt}") def shuffled(self, population: Sequence[T]) -> Sequence[T]: return self.sample(population, k=len(population)) diff --git a/api/advent22_api/routers/admin.py b/api/advent22_api/routers/admin.py index e6a77ba..9464107 100644 --- a/api/advent22_api/routers/admin.py +++ b/api/advent22_api/routers/admin.py @@ -59,7 +59,7 @@ async def get_config_model( return ConfigModel.model_validate( { "puzzle": { - "solution": cfg.puzzle.solution, + "solution": cfg.puzzle.solution.clean, "first": event_dates.first, "next": event_dates.next, "last": event_dates.last, diff --git a/ui/src/components/admin/ConfigView.vue b/ui/src/components/admin/ConfigView.vue index 41909b0..35e11ef 100644 --- a/ui/src/components/admin/ConfigView.vue +++ b/ui/src/components/admin/ConfigView.vue @@ -11,7 +11,9 @@