new game
Build and Push Docker Images / build-and-push (push) Successful in 19s

This commit is contained in:
Bram committed 2026-11-18 14:44:36 +01:00
1 parent 3f8984bb09
commit 97b2fa58a5
6 files changed
+1196 -180

No files matched your search

+556 -17
View File
@@ -64,6 +64,12 @@
<path d="M44 28h4c5 0 8 3 8 8s-3 8-8 8h-4"/>
<path d="M22 16c0-3 3-4 3-7M31 16c0-3 3-4 3-7M40 16c0-3 3-4 3-7"/>
<path d="M14 55h32"/>
</svg>`,
book: `
<svg viewBox="0 0 64 64" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<path d="M32 17c-6-4-14-5-21-3v34c7-2 15-1 21 3 6-4 14-5 21-3V14c-7-2-15-1-21 3Z"/>
<path d="M32 17v34"/>
<path d="M15 21c5-1 10 0 14 2M49 21c-5-1-10 0-14 2"/>
</svg>`
};
@@ -88,10 +94,21 @@
dialogAuthor: $("#book-dialog-author"),
dialogIntro: $("#book-dialog-intro"),
dialogQuote: $("#book-dialog-quote"),
quizTask: $("#quiz-task"),
memoryTask: $("#memory-task"),
shelfTask: $("#shelf-task"),
question: $("#book-question"),
options: $("#book-options"),
memoryPrompt: $("#memory-prompt"),
memoryStats: $("#memory-stats"),
memoryGrid: $("#memory-grid"),
shelfPrompt: $("#shelf-prompt"),
shelfHelp: $("#shelf-help"),
shelfTray: $("#shelf-tray"),
shelfRacks: $("#shelf-racks"),
feedback: $("#book-feedback"),
hint: $("#book-hint"),
minigameRetry: $("#minigame-retry"),
stamp: $("#letter-stamp"),
stampLetter: $("#stamp-letter"),
doneBtn: $("#book-done-btn"),
@@ -138,6 +155,14 @@
return list[Math.floor(Math.random() * list.length)];
}
function shuffle(list) {
for (let index = list.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(Math.random() * (index + 1));
[list[index], list[swapIndex]] = [list[swapIndex], list[index]];
}
return list;
}
function renderEmblem(name) {
return EMBLEMS[name] || EMBLEMS.star;
}
@@ -234,6 +259,16 @@
playTone({ freq: 180, endFreq: 140, type: "sine", duration: 0.18, gain: 0.05, delay: 0.08 });
}
function playFlip() {
playNoise({ duration: 0.08, gain: 0.05, frequency: 1900 });
playTone({ freq: 430, endFreq: 520, type: "sine", duration: 0.07, gain: 0.025 });
}
function playMatch() {
playTone({ freq: 587.33, endFreq: 587.33, type: "triangle", duration: 0.12, gain: 0.06 });
playTone({ freq: 880, endFreq: 880, type: "sine", duration: 0.18, gain: 0.05, delay: 0.08 });
}
function playUnlock() {
playNoise({ duration: 0.07, gain: 0.08, frequency: 2500 });
playTone({ freq: 340, endFreq: 120, type: "square", duration: 0.13, gain: 0.05 });
@@ -376,7 +411,11 @@
letter: "",
answerIndex: null,
doneText: "",
explanation: ""
explanation: "",
memory: null,
shelf: null,
minigameDone: false,
completing: false
};
state.books.set(meta.id, book);
@@ -387,6 +426,10 @@
}
});
els.shelves.forEach((shelf) => {
shelf.hidden = shelf.children.length === 0;
});
buildSlots();
updateProgress();
updateSlots();
@@ -450,6 +493,7 @@
book.answerIndex = saved.answer;
book.doneText = saved.done;
book.explanation = saved.explanation;
book.minigameDone = true;
book.element.classList.add("is-solved");
book.element.setAttribute(
"aria-label",
@@ -457,7 +501,7 @@
);
}
function renderOptions(book) {
function renderQuiz(book) {
els.options.replaceChildren();
const keys = ["A", "B", "C", "D", "E", "F"];
@@ -489,7 +533,6 @@
button.classList.add("is-correct");
}
});
showSolvedState(book);
}
}
@@ -514,6 +557,7 @@
return;
}
const kind = book.meta.kind || "quiz";
state.currentBookId = id;
state.bookReturnFocus = document.activeElement;
state.requestSerial += 1;
@@ -524,17 +568,45 @@
els.dialogAuthor.textContent = book.meta.author;
els.dialogIntro.textContent = book.meta.intro;
els.dialogQuote.textContent = book.meta.quote;
els.question.textContent = book.meta.question;
els.quizTask.hidden = true;
els.memoryTask.hidden = true;
els.shelfTask.hidden = true;
els.options.replaceChildren();
els.memoryGrid.replaceChildren();
els.shelfTray.replaceChildren();
els.shelfRacks.replaceChildren();
els.feedback.textContent = "";
els.feedback.className = "feedback";
els.hint.hidden = true;
els.hint.textContent = "";
els.minigameRetry.hidden = true;
els.stamp.hidden = true;
els.stamp.classList.remove("is-visible");
els.doneBtn.hidden = true;
els.doneBtn.textContent = "Terug naar de kast";
renderOptions(book);
if (kind === "quiz") {
els.quizTask.hidden = false;
els.question.textContent = book.meta.question;
renderQuiz(book);
} else if (kind === "memory") {
els.memoryTask.hidden = false;
els.memoryPrompt.textContent = book.meta.prompt;
renderMemory(book);
} else if (kind === "shelf") {
els.shelfTask.hidden = false;
els.shelfPrompt.textContent = book.meta.prompt;
renderShelf(book);
}
if (book.solved) {
showSolvedState(book);
} else if (book.minigameDone) {
els.feedback.textContent = "Je hebt de opdracht al voltooid. Rond het boek af om de letter te vinden.";
els.minigameRetry.hidden = false;
}
els.bookModal.hidden = false;
syncBodyLock();
@@ -545,11 +617,447 @@
els.doneBtn.focus();
return;
}
const firstOption = $(".option", els.options);
(firstOption || els.bookClose).focus();
if (book.minigameDone) {
els.minigameRetry.focus();
return;
}
const focusTarget = kind === "memory"
? $(".memory-card", els.memoryGrid)
: kind === "shelf"
? $(".shelf-item", els.shelfTray)
: $(".option", els.options);
(focusTarget || els.bookClose).focus();
}, 90);
}
function ensureMemory(book) {
if (!book.memory) {
const deck = [];
book.meta.pairs.forEach((pair) => {
deck.push({ pairId: pair.id, label: pair.label, emblem: pair.emblem });
deck.push({ pairId: pair.id, label: pair.label, emblem: pair.emblem });
});
book.memory = {
deck: shuffle(deck),
flipped: [],
matched: new Set(),
lock: false,
timer: null,
mistakes: 0,
done: false
};
}
if (book.solved) {
book.meta.pairs.forEach((pair) => book.memory.matched.add(pair.id));
book.memory.flipped = [];
book.memory.lock = false;
book.memory.done = true;
if (book.memory.timer) {
window.clearTimeout(book.memory.timer);
book.memory.timer = null;
}
}
return book.memory;
}
function renderMemory(book) {
const memory = ensureMemory(book);
const total = book.meta.pairs.length;
els.memoryStats.textContent = `${memory.matched.size} van ${total} paren gevonden`;
els.memoryGrid.replaceChildren();
memory.deck.forEach((card, index) => {
const matched = memory.matched.has(card.pairId);
const flipped = memory.flipped.includes(index);
const button = document.createElement("button");
button.type = "button";
button.className = "memory-card";
if (matched || flipped) {
button.classList.add("is-flipped");
}
if (matched) {
button.classList.add("is-matched");
}
button.disabled = book.solved;
button.dataset.index = String(index);
button.setAttribute(
"aria-label",
matched
? `${card.label}, gevonden`
: flipped
? `${card.label}, omgedraaid`
: `Dichte kaart ${index + 1}`
);
button.setAttribute("aria-pressed", String(matched || flipped));
const inner = document.createElement("span");
inner.className = "memory-inner";
const back = document.createElement("span");
back.className = "memory-face memory-back";
const backMark = document.createElement("span");
backMark.className = "memory-back-mark";
backMark.innerHTML = renderEmblem("book");
back.appendChild(backMark);
const front = document.createElement("span");
front.className = "memory-face memory-front";
const emblem = document.createElement("span");
emblem.className = "memory-emblem";
emblem.innerHTML = renderEmblem(card.emblem);
const label = document.createElement("span");
label.className = "memory-label";
label.textContent = card.label;
front.append(emblem, label);
inner.append(back, front);
button.appendChild(inner);
button.addEventListener("click", () => flipMemoryCard(book, index));
els.memoryGrid.appendChild(button);
});
}
function flipMemoryCard(book, index) {
const memory = ensureMemory(book);
if (book.solved || memory.done || memory.lock || memory.flipped.includes(index)) {
return;
}
const card = memory.deck[index];
if (!card || memory.matched.has(card.pairId)) {
return;
}
memory.flipped.push(index);
const buttons = $$(".memory-card", els.memoryGrid);
const button = buttons[index];
if (button) {
button.classList.add("is-flipped");
button.setAttribute("aria-label", `${card.label}, omgedraaid`);
button.setAttribute("aria-pressed", "true");
}
playFlip();
if (memory.flipped.length < 2) {
return;
}
const [firstIndex, secondIndex] = memory.flipped;
const first = memory.deck[firstIndex];
const second = memory.deck[secondIndex];
if (!first || !second) {
memory.flipped = [];
return;
}
if (first.pairId === second.pairId) {
memory.matched.add(first.pairId);
memory.flipped = [];
playMatch();
[firstIndex, secondIndex].forEach((cardIndex) => {
const cardButton = buttons[cardIndex];
if (cardButton) {
cardButton.classList.add("is-matched");
cardButton.setAttribute("aria-label", `${first.label}, gevonden`);
}
});
els.memoryStats.textContent = `${memory.matched.size} van ${book.meta.pairs.length} paren gevonden`;
els.feedback.className = "feedback is-correct";
els.feedback.textContent = `Gevonden: ${first.label}.`;
if (memory.matched.size === book.meta.pairs.length) {
memory.done = true;
book.minigameDone = true;
els.memoryStats.textContent = `${book.meta.pairs.length} van ${book.meta.pairs.length} paren gevonden.`;
els.feedback.textContent = "Alle paren gevonden. Het boek is bijna uit.";
memory.timer = window.setTimeout(() => completeMinigame(book), 550);
}
return;
}
memory.lock = true;
memory.mistakes += 1;
playWrong();
els.feedback.className = "feedback is-error";
els.feedback.textContent = "Bijna. Onthoud de plek en probeer opnieuw.";
if (memory.mistakes >= 2) {
els.hint.textContent = book.meta.hint;
els.hint.hidden = false;
}
memory.timer = window.setTimeout(() => {
memory.flipped = [];
memory.lock = false;
if (state.currentBookId === book.id) {
$$(".memory-card", els.memoryGrid).forEach((cardButton, cardIndex) => {
if (memory.matched.has(memory.deck[cardIndex].pairId)) {
return;
}
cardButton.classList.remove("is-flipped");
cardButton.setAttribute("aria-label", `Dichte kaart ${cardIndex + 1}`);
cardButton.setAttribute("aria-pressed", "false");
});
}
memory.timer = null;
}, 850);
}
function ensureShelf(book) {
if (!book.shelf) {
book.shelf = {
items: shuffle(book.meta.items.map((item) => ({ ...item }))),
placed: {},
selectedId: null,
mistakes: 0,
done: false,
timer: null
};
}
if (book.solved) {
book.meta.items.forEach((item) => {
book.shelf.placed[item.id] = item.shelf;
});
book.shelf.selectedId = null;
book.shelf.done = true;
if (book.shelf.timer) {
window.clearTimeout(book.shelf.timer);
book.shelf.timer = null;
}
}
return book.shelf;
}
function createMiniBook(item, placed) {
const element = document.createElement(placed ? "span" : "button");
if (!placed) {
element.type = "button";
}
element.className = `shelf-item${placed ? " is-placed" : ""}`;
element.style.setProperty("--item-cover", item.color || "#2e4a30");
element.style.setProperty("--item-cover-2", item.color2 || "#557c4c");
element.style.setProperty("--item-ink", item.ink || "#f7eedb");
const emblem = document.createElement("span");
emblem.className = "mini-emblem";
emblem.setAttribute("aria-hidden", "true");
emblem.innerHTML = renderEmblem(item.emblem);
const label = document.createElement("span");
label.className = "mini-label";
label.textContent = item.label;
element.append(emblem, label);
return element;
}
function renderShelf(book) {
const shelf = ensureShelf(book);
const placedCount = Object.keys(shelf.placed).length;
const total = book.meta.items.length;
els.shelfPrompt.textContent = book.meta.prompt;
els.shelfHelp.textContent = book.solved || shelf.done
? `Alle ${total} boeken staan op hun plek.`
: `${placedCount} van ${total} boeken geplaatst. Klik op een boek en daarna op het juiste rek.`;
els.shelfTray.replaceChildren();
shelf.items.forEach((item) => {
if (shelf.placed[item.id]) {
return;
}
const button = createMiniBook(item, false);
button.dataset.id = item.id;
button.dataset.label = item.label;
const selected = shelf.selectedId === item.id;
button.classList.toggle("is-selected", selected);
button.setAttribute("aria-pressed", String(selected));
button.setAttribute("aria-label", `${item.label}${selected ? ", geselecteerd" : ""}`);
button.addEventListener("click", () => selectShelfItem(book, item.id));
els.shelfTray.appendChild(button);
});
els.shelfRacks.replaceChildren();
book.meta.shelves.forEach((shelfMeta) => {
const placedItem = book.meta.items.find(
(item) => shelf.placed[item.id] === shelfMeta.id
);
const rack = document.createElement("button");
rack.type = "button";
rack.className = "rack";
if (placedItem) {
rack.classList.add("is-filled");
}
if (shelf.selectedId) {
rack.classList.add("is-targetable");
}
rack.disabled = book.solved || shelf.done;
rack.setAttribute(
"aria-label",
placedItem
? `${shelfMeta.label}: ${placedItem.label} staat op zijn plek.`
: `Plaats het geselecteerde boek in ${shelfMeta.label}.`
);
const label = document.createElement("span");
label.className = "rack-label";
label.textContent = shelfMeta.label;
const hint = document.createElement("span");
hint.className = "rack-hint";
hint.textContent = shelfMeta.hint;
const slot = document.createElement("span");
slot.className = "rack-slot";
if (placedItem) {
slot.appendChild(createMiniBook(placedItem, true));
}
rack.append(label, hint, slot);
rack.addEventListener("click", () => chooseShelf(book, shelfMeta.id));
els.shelfRacks.appendChild(rack);
});
}
function selectShelfItem(book, itemId) {
const shelf = ensureShelf(book);
if (book.solved || shelf.done) {
return;
}
const item = shelf.items.find((entry) => entry.id === itemId);
shelf.selectedId = shelf.selectedId === itemId ? null : itemId;
$$(".shelf-item", els.shelfTray).forEach((button) => {
const selected = button.dataset.id === shelf.selectedId;
button.classList.toggle("is-selected", selected);
button.setAttribute("aria-pressed", String(selected));
button.setAttribute(
"aria-label",
`${button.dataset.label}${selected ? ", geselecteerd" : ""}`
);
});
$$(".rack", els.shelfRacks).forEach((rack) => {
rack.classList.toggle("is-targetable", Boolean(shelf.selectedId));
});
els.feedback.className = "feedback";
els.feedback.textContent = shelf.selectedId && item
? `Kies een rek voor ${item.label}.`
: "Kies een boek en daarna een rek.";
playFlip();
}
function chooseShelf(book, shelfId) {
const shelf = ensureShelf(book);
if (book.solved || shelf.done) {
return;
}
if (!shelf.selectedId) {
els.feedback.className = "feedback is-error";
els.feedback.textContent = "Kies eerst een boek uit de stapel.";
playWrong();
return;
}
const item = shelf.items.find((entry) => entry.id === shelf.selectedId);
if (!item) {
return;
}
if (item.shelf !== shelfId) {
shelf.mistakes += 1;
playWrong();
els.feedback.className = "feedback is-error";
els.feedback.textContent = `${item.label} hoort niet bij dit rek. Kijk naar het onderwerp.`;
if (shelf.mistakes >= 2) {
els.hint.textContent = book.meta.hint;
els.hint.hidden = false;
}
const racks = $$(".rack", els.shelfRacks);
const targetIndex = book.meta.shelves.findIndex((entry) => entry.id === shelfId);
const rack = racks[targetIndex];
if (rack) {
rack.classList.remove("is-wrong");
void rack.offsetWidth;
rack.classList.add("is-wrong");
window.setTimeout(() => rack.classList.remove("is-wrong"), 400);
}
return;
}
shelf.placed[item.id] = shelfId;
shelf.selectedId = null;
playMatch();
if (Object.keys(shelf.placed).length === book.meta.items.length) {
shelf.done = true;
book.minigameDone = true;
els.feedback.className = "feedback is-correct";
els.feedback.textContent = "Alle boeken staan op hun plek. Het boek is bijna uit.";
renderShelf(book);
shelf.timer = window.setTimeout(() => completeMinigame(book), 550);
return;
}
els.feedback.className = "feedback is-correct";
els.feedback.textContent = `${item.label} staat goed.`;
renderShelf(book);
const nextItem = $(".shelf-item", els.shelfTray);
if (nextItem) {
nextItem.focus();
}
}
async function completeMinigame(book) {
if (!book || book.solved || book.completing) {
return;
}
const serial = ++state.requestSerial;
state.requestInFlight = true;
book.completing = true;
const isCurrent = () => serial === state.requestSerial && state.currentBookId === book.id;
els.minigameRetry.hidden = true;
els.feedback.className = "feedback";
els.feedback.textContent = "De laatste bladzijde wordt geschreven...";
ensureAudio();
try {
const response = await fetch("/api/complete", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ bookId: book.id })
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || "De leeshoek is even niet bereikbaar.");
}
book.completing = false;
markSolved(book, data, null, isCurrent());
} catch (error) {
book.completing = false;
if (isCurrent()) {
els.feedback.className = "feedback is-error";
els.feedback.textContent = `${error.message} Het boek is bijna klaar; probeer het nog eens.`;
els.minigameRetry.hidden = false;
playWrong();
}
} finally {
if (serial === state.requestSerial) {
state.requestInFlight = false;
}
}
}
async function answerBook(book, index) {
if (book.solved || state.requestInFlight) {
return;
@@ -653,12 +1161,14 @@
setLog(book.doneText || `Juist. De letter ${book.letter} is gevonden.`, "done");
if (updateDialog) {
$$(".option", els.options).forEach((button, index) => {
button.disabled = true;
if (index === answerIndex) {
button.classList.add("is-correct");
}
});
if ((book.meta.kind || "quiz") === "quiz") {
$$(".option", els.options).forEach((button, index) => {
button.disabled = true;
if (index === answerIndex) {
button.classList.add("is-correct");
}
});
}
showSolvedState(book);
window.setTimeout(() => els.doneBtn.focus(), 260);
}
@@ -686,8 +1196,9 @@
playUnlock();
}
if (wasLocked && announce) {
const word = state.order.map((book) => book.letter).join("");
setLog(
"Alle acht boeken zijn gelezen. De letters vormen LEESHOEK — de lade klikt open!",
`Alle ${state.order.length} boeken zijn gelezen. De letters vormen ${word} — de lade klikt open!`,
"done"
);
}
@@ -769,6 +1280,17 @@
if (els.bookModal.hidden) {
return;
}
const book = state.currentBookId ? state.books.get(state.currentBookId) : null;
if (book && book.memory) {
if (book.memory.timer) {
window.clearTimeout(book.memory.timer);
book.memory.timer = null;
}
book.memory.flipped = [];
book.memory.lock = false;
}
els.bookModal.hidden = true;
state.currentBookId = null;
state.requestInFlight = false;
@@ -799,12 +1321,22 @@
function resetGame() {
state.books.forEach((book) => {
if (book.memory && book.memory.timer) {
window.clearTimeout(book.memory.timer);
}
if (book.shelf && book.shelf.timer) {
window.clearTimeout(book.shelf.timer);
}
book.solved = false;
book.attempts = 0;
book.letter = "";
book.answerIndex = null;
book.doneText = "";
book.explanation = "";
book.memory = null;
book.shelf = null;
book.minigameDone = false;
book.completing = false;
book.element.classList.remove("is-solved");
book.element.setAttribute(
"aria-label",
@@ -821,6 +1353,7 @@
updateProgress();
updateSlots();
els.secret.classList.remove("is-complete");
els.minigameRetry.hidden = true;
els.log.className = "log";
setLog("Nieuwe ronde, dezelfde kast. Kies een boek en begin opnieuw.", "done");
closeVoucher();
@@ -970,7 +1503,7 @@
setText("#game-subtitle", config.subtitle);
setText(
"#voucher-lead",
`${config.recipient}, je hebt alle acht boeken gelezen. Achter de kast lag een envelop met iets om zelf een nieuw verhaal uit te kiezen.`
`${config.recipient}, je hebt alle ${config.books.length} boeken gelezen. Achter de kast lag een envelop met iets om zelf een nieuw verhaal uit te kiezen.`
);
if (config.title) {
@@ -1023,6 +1556,12 @@
els.playAgain.addEventListener("click", resetGame);
els.copyButton.addEventListener("click", copyCode);
els.drawer.addEventListener("click", handleDrawer);
els.minigameRetry.addEventListener("click", () => {
const book = state.currentBookId ? state.books.get(state.currentBookId) : null;
if (book && (book.meta.kind === "memory" || book.meta.kind === "shelf")) {
completeMinigame(book);
}
});
$$("[data-close]").forEach((node) => {
node.addEventListener("click", () => {
@@ -1078,7 +1617,7 @@
if (state.solvedCount === state.order.length) {
unlockDrawer({ announce: false, sound: false });
setLog(
"Welkom terug. Alle acht boeken zijn al gelezen — de lade staat op je te wachten.",
`Welkom terug. Alle ${state.order.length} boeken zijn al gelezen — de lade staat op je te wachten.`,
"done"
);
} else if (state.solvedCount > 0) {
@@ -1087,7 +1626,7 @@
"done"
);
} else {
setLog("De kast staat klaar. Kies een boek en los de kleine opdracht op.");
setLog("De kast staat klaar: twee vragen en twee minigames. Kies een boek.");
}
} catch (error) {
setLog(`De boekenkast kon niet geladen worden: ${error.message}`, "warning");