Compare the subject and theme of each daily guess, let players choose their hints and find cards by related words
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 22m58s
Validate and deploy Sokko G / deploy (push) Successful in 17s

Collection and rarity hints only helped players who owned the daily card. Each guess now shows the common part of its subject and whether the theme of its name matches. A wrong guess earns one hint of the player's choice, the same list for everyone so that nothing reveals the ownership: album, rarity, collection, damaged page or name shape. Hidden words never leave the server, and the ownership is the one of the draw.

The guess list also finds a card by a related word, and the album no longer searches the description of unknown cards. Principles and data: docs/carte-du-jour.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-24 23:09:08 +01:00
parent 5a794e5220
commit 62042c4c36
24 changed files with 1081 additions and 128 deletions

View file

@ -7,7 +7,8 @@ import { test } from "node:test";
import sharp from "sharp";
import { createCardsApiHandler } from "../services/cards/api.js";
import { buildPublishedCatalog, prepareVersionedCatalog } from "../services/cards/catalog.js";
import { computeStreak, dailyCandidates, dailyCrop, dailyDayKey, dailyZoom, drawDailyPuzzle, guessHint, MAX_DAILY_GUESSES, nextDailyAt, shiftDayKey } from "../services/cards/daily.js";
import { computeStreak, dailyCandidates, dailyCrop, dailyDayKey, dailyZoom, drawDailyPuzzle, MAX_DAILY_GUESSES, nextDailyAt, shiftDayKey } from "../services/cards/daily.js";
import { canUseDailyHint, compareClues, DAILY_HINTS, dailyCluesIssues, dailyHintCredits, damagedPage, loadDailyClues, nameShape, prepareDailyClues } from "../services/cards/daily-clues.js";
import { CardsDatabase } from "../services/cards/database.js";
import { CardsService } from "../services/cards/service.js";
@ -55,21 +56,75 @@ test("the daily card comes from an open collection and avoids recent cards", ()
assert.ok(!dailyCandidates(withoutImage).some((item) => item.id === candidates[0].id));
});
test("a daily guess tells the collection and how the answer rarity compares", () => {
const nebula = catalog.cards.filter((item) => item.collectionId === "primordial-nebula");
const legendary = nebula.find((item) => standardRarity(item.id) === "legendary");
const common = nebula.find((item) => standardRarity(item.id) === "common");
const arcadeCommon = catalog.cards.find((item) => item.collectionId === "dream-arcade" && standardRarity(item.id) === "common");
assert.deepEqual(guessHint(catalog, legendary.id, common.id), { cardId: common.id, correct: false, sameCollection: true, rarity: "higher" });
assert.deepEqual(guessHint(catalog, common.id, legendary.id), { cardId: legendary.id, correct: false, sameCollection: true, rarity: "lower" });
assert.deepEqual(guessHint(catalog, common.id, arcadeCommon.id), { cardId: arcadeCommon.id, correct: false, sameCollection: false, rarity: "equal" });
assert.equal(guessHint(catalog, common.id, common.id).correct, true);
test("the daily zoom loosens with each wrong guess", () => {
assert.equal(dailyZoom(0, false), 6);
assert.equal(dailyZoom(5, false), 1.15);
assert.equal(dailyZoom(9, false), 1.15);
assert.equal(dailyZoom(2, true), 1);
});
const clues = loadDailyClues();
const cardNamed = (name) => baseCatalog.cards.find((item) => item.name === name);
const shown = (parts) => parts.map((part) => (part.hidden ? "▒" : part.text)).join("");
test("every card of the catalog has a daily subject and a theme, and no subject holds too many cards", () => {
assert.deepEqual(dailyCluesIssues(baseCatalog, clues), []);
const incomplete = dailyCluesIssues({ cards: [...baseCatalog.cards, { id: "new-card", name: "Chose Inconnue" }] }, clues);
assert.deepEqual(incomplete, ["new-card: no subject", "new-card: no theme for « Chose Inconnue »"]);
assert.deepEqual(dailyCluesIssues({ cards: baseCatalog.cards.slice(1) }, clues), [`${baseCatalog.cards[0].id}: unknown card`]);
});
test("a daily theme comes from the words of the name, the subject only by default", () => {
const theme = (name) => clues.themeLabels.get(clues.themeOf(name));
assert.equal(theme("Renardeau des Congères"), "Hiver");
assert.equal(theme("Renardeau des Neiges Astrales"), "Hiver");
// « Voie lactée » et « Îles réunies » se lisent en entier ; « Astral » cède devant un mot plus parlant.
assert.equal(theme("Panda de la Voie Lactée"), "Cosmos");
assert.equal(theme("Sapin des Îles Réunies"), "Fête");
assert.equal(theme("Encrier Astral"), "Cosmos");
assert.equal(theme("Constellation du Loup"), "Lumière");
assert.equal(theme("Premier Croissant"), "Lune");
const content = { subjects: [{ id: "animal", label: "Animal" }], themes: [{ id: "one", label: "Un", words: ["lune"] }, { id: "two", label: "Deux", words: ["Lune"] }], compoundSubjects: [], cards: {} };
assert.throws(() => prepareDailyClues(content), /already belongs to one/);
assert.throws(() => prepareDailyClues({ ...content, themes: [], subjects: [{ id: "animal/oiseau", label: "Oiseau" }] }), /has no parent animal/);
assert.throws(() => prepareDailyClues({ ...content, themes: [], cards: { card: { subject: "objet" } } }), /unknown subject objet/);
});
test("a daily guess shows the common part of the subject and the theme, never the rest of the answer", () => {
const answer = cardNamed("Renardeau des Congères");
const compare = (name) => compareClues(clues, answer, cardNamed(name));
assert.deepEqual(compare("Épée du Socle Ancien"), { subject: { common: [], different: "Objet" }, theme: { label: "Combat", same: false } });
assert.deepEqual(compare("Loutre Pilote du Bateau du Ciel").subject, { common: ["Animal", "Mammifère"], different: "Mustélidé" });
assert.deepEqual(compare("Loup des Aurores").subject, { common: ["Animal", "Mammifère", "Canidé"], different: null });
// Une espèce sans branche s'arrête à « Mammifère » : rien ne dit que la réponse va plus loin.
assert.deepEqual(compare("Phoque des Glaces Lunaires"), { subject: { common: ["Animal", "Mammifère"], different: null }, theme: { label: "Hiver", same: true } });
// Une carte sans indices reste jouable, sans comparaison.
assert.deepEqual(compareClues(clues, answer, { id: "new-card", name: "Chose Inconnue" }), { subject: { common: [], different: null }, theme: null });
});
test("the damaged page and the name shape hide words without sending them", () => {
const hedgehog = cardNamed("Hérisson à la Guirlande");
assert.equal(shown(damagedPage(hedgehog)), "En voulant aider à décorer le sapin, il s'est emmêlé dans la ▒.");
assert.equal(shown(nameShape("Renardeau des Neiges Astrales")), "▒ des ▒ ▒");
assert.equal(shown(nameShape("L'Échange de l'Aube")), "L'▒ de l'▒");
assert.ok(nameShape(hedgehog.name).filter((part) => part.hidden).every((part) => Object.keys(part).join() === "hidden"));
for (const card of baseCatalog.cards) assert.ok(damagedPage(card).some((part) => part.text) && nameShape(card.name).some((part) => part.hidden), card.id);
});
test("a wrong daily guess earns one hint of the player's choice", () => {
assert.equal(dailyHintCredits(0, 0), 0);
assert.equal(dailyHintCredits(3, 1), 2);
assert.equal(dailyHintCredits(6, 0), DAILY_HINTS.length);
assert.equal(canUseDailyHint("album", 0, []), false);
assert.equal(canUseDailyHint("album", 1, []), true);
assert.equal(canUseDailyHint("album", 2, ["album"]), false);
assert.equal(canUseDailyHint("rarity", 1, ["album"]), false);
assert.equal(canUseDailyHint("page", 1, []), false);
assert.equal(canUseDailyHint("shape", 3, []), false);
assert.equal(canUseDailyHint("shape", 4, ["album"]), true);
assert.equal(canUseDailyHint("unknown", 6, []), false);
});
function dailyService(start = Date.parse("2026-11-13T09:00:00Z"), random = () => 0) {
const clock = { now: start };
const database = new CardsDatabase(":memory:");
@ -103,7 +158,14 @@ test("each player draws a daily card on the first visit and the answer stays hid
assert.equal(daily.maxGuesses, MAX_DAILY_GUESSES);
assert.equal(daily.reward, 10);
assert.equal(daily.answerCardId, null);
assert.ok(!JSON.stringify(daily).includes(service.todayPuzzle(player.id).cardId));
// Les mots de recherche couvrent toutes les cartes proposables : ils ne désignent pas la réponse.
const { searchWords, ...hiddenAnswer } = daily;
assert.deepEqual(searchWords["starlit-ice-penguin"], ["pingouin", "oiseau"]);
assert.equal(Object.keys(searchWords).some((cardId) => service.index.cards.get(cardId).collectionId === "dream-arcade"), false);
assert.ok(!JSON.stringify(hiddenAnswer).includes(service.todayPuzzle(player.id).cardId));
assert.deepEqual(daily.hints, []);
assert.equal(daily.hintCredits, 0);
assert.deepEqual(daily.hintChoices.map((choice) => choice.available), [false, false, false, false, false]);
// Le point de zoom reste au serveur, qui n'envoie que la zone visible de l'illustration.
assert.equal(daily.focus, undefined);
assert.deepEqual(service.dailyImage(player.id).focus, { x: 20, y: 20 });
@ -135,7 +197,8 @@ test("wrong daily guesses unzoom the card until the player runs out of guesses",
assert.deepEqual({ status: state.daily.status, guessCount: state.daily.guessCount }, { status: "playing", guessCount: 1 });
assert.equal(daily.guesses.length, 1);
assert.equal(daily.guesses[0].correct, false);
assert.equal(daily.guesses[0].sameCollection, true);
assert.ok(Array.isArray(daily.guesses[0].subject.common));
assert.equal(typeof daily.guesses[0].theme.same, "boolean");
assert.equal(daily.zoom, 4.2);
assert.throws(() => service.guessDaily(player.id, { day: "2026-11-13", cardId: first }), (error) => error.code === "DAILY_GUESS_REPEATED");
assert.throws(() => service.guessDaily(player.id, { day: "2026-11-13", cardId: "unknown-card" }), (error) => error.code === "DAILY_GUESS_INVALID");
@ -152,6 +215,55 @@ test("wrong daily guesses unzoom the card until the player runs out of guesses",
assert.equal(service.getState(player.id).wallet.stardust, 0);
});
test("a player flips the daily hints in the order of their choice, for free", () => {
const { clock, service, player, wrongCards } = dailyService();
const day = "2026-11-13";
const flip = (hint) => service.useDailyHint(player.id, { day, hint });
const others = wrongCards();
const puzzle = service.todayPuzzle(player.id);
const answer = service.index.cards.get(puzzle.cardId);
assert.throws(() => flip("album"), (error) => error.code === "DAILY_HINT_LOCKED");
assert.throws(() => flip("answer"), (error) => error.code === "DAILY_HINT_INVALID");
assert.throws(() => service.useDailyHint(player.id, { day: "demain", hint: "album" }), (error) => error.code === "DAILY_HINT_INVALID");
assert.deepEqual(service.guessDaily(player.id, { day, cardId: others[0] }).daily.hintChoices.filter((choice) => choice.available).map((choice) => choice.id), ["album", "rarity", "collection"]);
assert.throws(() => flip("page"), (error) => error.code === "DAILY_HINT_LOCKED");
// La possession est celle du tirage : une carte reçue pendant la partie ne change pas l'indice.
const edition = service.catalog.editions.find((item) => item.cardId === answer.id);
service.db.prepare("INSERT INTO collection_cards(player_id, edition_id, quantity, acquired_at) VALUES (?, ?, 1, 0)").run(player.id, edition.id);
const album = flip("album");
assert.deepEqual(album.hints, [{ id: "album", value: false }]);
assert.equal(album.hintCredits, 0);
assert.equal(album.guesses.length, 1);
assert.deepEqual(flip("album").hints, album.hints);
assert.throws(() => flip("rarity"), (error) => error.code === "DAILY_HINT_LOCKED");
for (const cardId of others.slice(1, 4)) service.guessDaily(player.id, { day, cardId });
assert.equal(service.getDaily(player.id).hintCredits, 3);
flip("shape");
flip("page");
const view = flip("collection");
assert.deepEqual(view.hints.map((hint) => hint.id), ["album", "shape", "page", "collection"]);
assert.equal(view.hints.at(-1).value, answer.collectionId);
assert.deepEqual(view.hints[1].value, nameShape(answer.name));
assert.ok(!JSON.stringify(view).includes(answer.id));
const won = service.guessDaily(player.id, { day, cardId: answer.id }).daily;
assert.equal(won.hints.length, 4);
assert.equal(won.earned, 10);
assert.equal(won.hintCredits, 0);
assert.throws(() => flip("rarity"), (error) => error.code === "DAILY_FINISHED");
clock.now = Date.parse("2026-11-13T23:00:00Z");
assert.throws(() => flip("rarity"), (error) => error.code === "DAILY_EXPIRED");
});
test("a daily game drawn before the hints reads the ownership in the collection", () => {
const { service, player, wrongCards } = dailyService();
const answer = service.todayPuzzle(player.id).cardId;
service.db.prepare("UPDATE daily_attempts SET owned = NULL").run();
const edition = service.catalog.editions.find((item) => item.cardId === answer);
service.db.prepare("INSERT INTO collection_cards(player_id, edition_id, quantity, acquired_at) VALUES (?, ?, 1, 0)").run(player.id, edition.id);
service.guessDaily(player.id, { day: "2026-11-13", cardId: wrongCards()[0] });
assert.deepEqual(service.useDailyHint(player.id, { day: "2026-11-13", hint: "album" }).hints, [{ id: "album", value: true }]);
});
test("finding the daily card credits the reward once and records it in the player history", () => {
const { service, admin, player, wrongCards } = dailyService();
service.updateShopSetting(admin.id, { ...service.shopSetting, dailyReward: 25 });
@ -225,7 +337,7 @@ test("the daily API requires a session, protects guesses and serves only the vis
const handler = createCardsApiHandler({ projectRoot: resolve("."), enabled: true, devAuth: true, databasePath: ":memory:", env: { NODE_ENV: "test" }, random: () => 0 });
t.after(() => handler.close());
for (const path of ["/api/cards/daily", "/api/cards/daily/image"]) assert.equal((await call(handler, { path })).status, 401);
assert.equal((await call(handler, { method: "POST", path: "/api/cards/daily/guesses", body: {} })).status, 401);
for (const path of ["/api/cards/daily/guesses", "/api/cards/daily/hints"]) assert.equal((await call(handler, { method: "POST", path, body: {} })).status, 401);
const login = await call(handler, { method: "POST", path: "/api/cards/auth/dev", body: {} });
const cookie = login.headers["set-cookie"].split(";")[0];
@ -250,6 +362,12 @@ test("the daily API requires a session, protects guesses and serves only the vis
const unprotected = await call(handler, { method: "POST", path: "/api/cards/daily/guesses", headers: { cookie }, body: { day: daily.json.day, cardId: answer } });
assert.equal(unprotected.status, 403);
const wrong = handler.service.catalog.cards.find((item) => item.id !== answer && daily.json.collectionIds.includes(item.collectionId)).id;
await call(handler, { method: "POST", path: "/api/cards/daily/guesses", headers: { cookie, "x-csrf-token": csrf }, body: { day: daily.json.day, cardId: wrong } });
assert.equal((await call(handler, { method: "POST", path: "/api/cards/daily/hints", headers: { cookie }, body: { day: daily.json.day, hint: "rarity" } })).status, 403);
const hint = await call(handler, { method: "POST", path: "/api/cards/daily/hints", headers: { cookie, "x-csrf-token": csrf }, body: { day: daily.json.day, hint: "rarity" } });
assert.equal(hint.status, 200);
assert.deepEqual(hint.json.daily.hints, [{ id: "rarity", value: standardRarity(answer) }]);
const won = await call(handler, { method: "POST", path: "/api/cards/daily/guesses", headers: { cookie, "x-csrf-token": csrf }, body: { day: daily.json.day, cardId: answer } });
assert.equal(won.status, 200);
assert.equal(won.json.daily.status, "won");