This commit is contained in:
1 parent
3f8984bb09
commit
97b2fa58a5
6 files changed
+1196
-180
No files matched your search
@@ -24,12 +24,17 @@ DEFAULT_CODE = "VUL-JE-CADEAUBONCODE-IN"
|
||||
MAX_BODY_BYTES = 16 * 1024
|
||||
PUBLIC_BOOK_FIELDS = (
|
||||
"id",
|
||||
"kind",
|
||||
"title",
|
||||
"author",
|
||||
"intro",
|
||||
"quote",
|
||||
"question",
|
||||
"options",
|
||||
"prompt",
|
||||
"pairs",
|
||||
"shelves",
|
||||
"items",
|
||||
"hint",
|
||||
"color",
|
||||
"color2",
|
||||
@@ -58,20 +63,58 @@ def load_game(path: Path) -> dict[str, Any]:
|
||||
raise ValueError("Elke boek-id moet uniek zijn.")
|
||||
|
||||
for book in books:
|
||||
answer = book.get("answer")
|
||||
options = book.get("options")
|
||||
if not isinstance(options, list) or not options:
|
||||
raise ValueError(f"Boek {book['id']} heeft geen antwoordopties.")
|
||||
if (
|
||||
isinstance(answer, bool)
|
||||
or not isinstance(answer, int)
|
||||
or not 0 <= answer < len(options)
|
||||
):
|
||||
raise ValueError(f"Boek {book['id']} heeft een ongeldig antwoord.")
|
||||
kind = book.get("kind")
|
||||
if kind not in ("quiz", "memory", "shelf"):
|
||||
raise ValueError(f"Boek {book['id']} heeft een onbekend soort opdracht.")
|
||||
|
||||
for field in ("letter", "fact", "done", "hint"):
|
||||
if not isinstance(book.get(field), str) or not book[field]:
|
||||
raise ValueError(f"Boek {book['id']} mist '{field}'.")
|
||||
|
||||
if kind == "quiz":
|
||||
answer = book.get("answer")
|
||||
options = book.get("options")
|
||||
if not isinstance(options, list) or len(options) < 2:
|
||||
raise ValueError(f"Boek {book['id']} heeft geen antwoordopties.")
|
||||
if (
|
||||
isinstance(answer, bool)
|
||||
or not isinstance(answer, int)
|
||||
or not 0 <= answer < len(options)
|
||||
):
|
||||
raise ValueError(f"Boek {book['id']} heeft een ongeldig antwoord.")
|
||||
|
||||
if kind == "memory":
|
||||
pairs = book.get("pairs")
|
||||
if not isinstance(pairs, list) or len(pairs) < 2:
|
||||
raise ValueError(f"Boek {book['id']} heeft te weinig memoryparen.")
|
||||
pair_ids = [pair.get("id") for pair in pairs]
|
||||
if len(pair_ids) != len(set(pair_ids)):
|
||||
raise ValueError(f"Boek {book['id']} heeft dubbele memoryparen.")
|
||||
for pair in pairs:
|
||||
for field in ("id", "label", "emblem"):
|
||||
if not isinstance(pair.get(field), str) or not pair[field]:
|
||||
raise ValueError(f"Memorypaar in {book['id']} mist '{field}'.")
|
||||
|
||||
if kind == "shelf":
|
||||
shelves = book.get("shelves")
|
||||
items = book.get("items")
|
||||
if not isinstance(shelves, list) or not shelves:
|
||||
raise ValueError(f"Boek {book['id']} heeft geen rekken.")
|
||||
if not isinstance(items, list) or not items:
|
||||
raise ValueError(f"Boek {book['id']} heeft geen boeken om te plaatsen.")
|
||||
shelf_ids = {shelf.get("id") for shelf in shelves}
|
||||
if len(shelf_ids) != len(shelves):
|
||||
raise ValueError(f"Boek {book['id']} heeft dubbele rekken.")
|
||||
item_ids = [item.get("id") for item in items]
|
||||
if len(item_ids) != len(set(item_ids)):
|
||||
raise ValueError(f"Boek {book['id']} heeft dubbele boeken.")
|
||||
for item in items:
|
||||
if item.get("shelf") not in shelf_ids:
|
||||
raise ValueError(f"Boek {book['id']} verwijst naar een onbekend rek.")
|
||||
for field in ("id", "label", "shelf", "emblem"):
|
||||
if not isinstance(item.get(field), str) or not item[field]:
|
||||
raise ValueError(f"Boek in {book['id']} mist '{field}'.")
|
||||
|
||||
return game
|
||||
|
||||
|
||||
@@ -86,7 +129,7 @@ def public_config(game: dict[str, Any]) -> dict[str, Any]:
|
||||
"title": os.environ.get("GAME_TITLE", game.get("title", "Mama's Geheime Leeshoek")),
|
||||
"subtitle": os.environ.get(
|
||||
"GAME_SUBTITLE",
|
||||
game.get("subtitle", "Lees de acht boeken en open de geheime lade."),
|
||||
game.get("subtitle", "Open de vier boeken en open de geheime lade."),
|
||||
),
|
||||
"books": [public_book(book) for book in game["books"]],
|
||||
}
|
||||
@@ -130,7 +173,7 @@ class GameRequestHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
def do_POST(self) -> None:
|
||||
path = urlparse(self.path).path
|
||||
if path not in ("/api/solve", "/api/reveal"):
|
||||
if path not in ("/api/solve", "/api/complete", "/api/reveal"):
|
||||
self.send_json(HTTPStatus.NOT_FOUND, {"ok": False, "error": "Onbekend pad."})
|
||||
return
|
||||
|
||||
@@ -144,6 +187,8 @@ class GameRequestHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
if path == "/api/solve":
|
||||
self.handle_solve(payload)
|
||||
elif path == "/api/complete":
|
||||
self.handle_complete(payload)
|
||||
else:
|
||||
self.handle_reveal(payload)
|
||||
|
||||
@@ -159,6 +204,13 @@ class GameRequestHandler(SimpleHTTPRequestHandler):
|
||||
)
|
||||
return
|
||||
|
||||
if book["kind"] != "quiz":
|
||||
self.send_json(
|
||||
HTTPStatus.BAD_REQUEST,
|
||||
{"ok": False, "error": "Dit boek heeft geen quizvraag."},
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
isinstance(answer, bool)
|
||||
or not isinstance(answer, int)
|
||||
@@ -191,6 +243,34 @@ class GameRequestHandler(SimpleHTTPRequestHandler):
|
||||
},
|
||||
)
|
||||
|
||||
def handle_complete(self, payload: dict[str, Any]) -> None:
|
||||
book_id = payload.get("bookId")
|
||||
book = self.data.books.get(book_id) if isinstance(book_id, str) else None
|
||||
|
||||
if book is None:
|
||||
self.send_json(
|
||||
HTTPStatus.NOT_FOUND,
|
||||
{"ok": False, "error": "Dit boek staat niet in de kast."},
|
||||
)
|
||||
return
|
||||
|
||||
if book["kind"] not in ("memory", "shelf"):
|
||||
self.send_json(
|
||||
HTTPStatus.BAD_REQUEST,
|
||||
{"ok": False, "error": "Dit boek heeft geen minigame."},
|
||||
)
|
||||
return
|
||||
|
||||
self.send_json(
|
||||
HTTPStatus.OK,
|
||||
{
|
||||
"ok": True,
|
||||
"letter": book["letter"],
|
||||
"explanation": book["fact"],
|
||||
"done": book["done"],
|
||||
},
|
||||
)
|
||||
|
||||
def handle_reveal(self, payload: dict[str, Any]) -> None:
|
||||
completed = payload.get("completed")
|
||||
if not isinstance(completed, list) or not all(
|
||||
|
||||
Reference in new issue
Block a user