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>
187 lines
11 KiB
JavaScript
187 lines
11 KiB
JavaScript
// Rôle : présente la Carte du jour : illustration zoomée, propositions comparées, indices au choix, révélation et résultat à partager.
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { CategoryAutocompleteInput } from "../../components/CategoryAutocompleteInput.jsx";
|
|
import { flipDailyHint, guessDaily, loadDaily, loadDailyImage } from "./cardsApi.js";
|
|
import { readCollection } from "./collectionState.js";
|
|
import { DailyGuesses } from "./DailyGuesses.jsx";
|
|
import { DailyHints } from "./DailyHints.jsx";
|
|
import { dailyGuessResult, dailyShareText, fillTemplate, findCardByName, formatCountdown, formatStreak } from "./dailyShare.js";
|
|
import { NebulaCard } from "./NebulaCard.jsx";
|
|
import { formatStardust } from "./stardustFormat.js";
|
|
|
|
const COUNTDOWN_TICK_MS = 30_000;
|
|
// Un mot répandu (« étoile ») désigne plus de cartes que les huit suggestions habituelles : la liste défile.
|
|
const MAX_SUGGESTIONS = 40;
|
|
|
|
// Édition de référence d'une carte, pour l'afficher : la standard, jamais une variation mythique.
|
|
function referenceEntry(catalog, cardId) {
|
|
const card = catalog.cards.find((item) => item.id === cardId);
|
|
const editions = catalog.editions.filter((edition) => edition.cardId === cardId && edition.rarity !== "mythic");
|
|
const edition = editions.find((item) => !item.promotional) || editions[0];
|
|
return card && edition ? { editionId: edition.id, edition, card } : null;
|
|
}
|
|
|
|
function DailyResult({ catalog, state, daily, content, now }) {
|
|
const [copied, setCopied] = useState(false);
|
|
const answer = referenceEntry(catalog, daily.answerCardId);
|
|
const owned = readCollection(state).ownedCardIds.has(daily.answerCardId);
|
|
const won = daily.status === "won";
|
|
async function share() {
|
|
const text = dailyShareText(daily, { title: content.shareTitle, url: `${window.location.origin}/#/cards/daily` });
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
setCopied(true);
|
|
} catch {
|
|
setCopied(false);
|
|
}
|
|
}
|
|
return <section className="nds-panel nebula-daily-result" aria-labelledby="nebula-daily-result-title" data-status={daily.status}>
|
|
<div className="nebula-daily-result-copy">
|
|
<h3 id="nebula-daily-result-title">{won ? fillTemplate(content.wonTitle, { count: daily.guesses.length, max: daily.maxGuesses }) : content.lostTitle}</h3>
|
|
{!won && <p>{content.lostText}</p>}
|
|
{answer && <p className="nebula-daily-answer"><strong>{answer.card.name}</strong>{answer.card.description && <span>{answer.card.description}</span>}</p>}
|
|
<div className="nebula-daily-result-badges">
|
|
{won && daily.earned > 0 && <span className="nds-badge" data-variant="gold">{fillTemplate(content.earned, { reward: formatStardust(daily.earned) })}</span>}
|
|
<span className="nds-badge" data-variant={owned ? "success" : "neutral"}>{owned ? content.owned : content.notOwned}</span>
|
|
{daily.hints.length > 0 && <span className="nds-badge" data-variant="neutral">{fillTemplate(content.hintsUsed, { count: daily.hints.length })}</span>}
|
|
{daily.streak > 0 && <span className="nds-badge" data-variant="neutral">{fillTemplate(content.streak, { streak: formatStreak(daily.streak) })}</span>}
|
|
</div>
|
|
<div className="nds-toolbar nebula-daily-result-actions">
|
|
<button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={share}>{copied ? content.shared : content.share}</button>
|
|
<span className="nebula-daily-next">{fillTemplate(content.next, { time: formatCountdown(daily.nextAt - now) })}</span>
|
|
</div>
|
|
</div>
|
|
{answer && <div className="nebula-daily-result-card"><NebulaCard catalog={catalog} entry={answer} size="md" /></div>}
|
|
</section>;
|
|
}
|
|
|
|
export function DailyPanel({ catalog, state, runMutation, busy, content }) {
|
|
const [daily, setDaily] = useState(null);
|
|
const [loadError, setLoadError] = useState("");
|
|
const [reloadKey, setReloadKey] = useState(0);
|
|
const [image, setImage] = useState({ day: "", url: "", failed: false });
|
|
const [guess, setGuess] = useState("");
|
|
const [notice, setNotice] = useState("");
|
|
const [now, setNow] = useState(() => Date.now());
|
|
const day = daily?.day || "";
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
loadDaily()
|
|
.then((result) => { if (active) { setDaily(result); setLoadError(""); } })
|
|
.catch((error) => { if (active) setLoadError(error.code === "DAILY_UNAVAILABLE" ? content.unavailable : error.message); });
|
|
return () => { active = false; };
|
|
}, [content.unavailable, reloadKey]);
|
|
|
|
// Le serveur n'envoie que la zone visible au zoom courant : chaque essai raté recharge un morceau plus large, et
|
|
// l'illustration entière n'arrive qu'en fin de partie. Le morceau précédent reste affiché pendant le chargement.
|
|
const zoom = daily?.zoom ?? 0;
|
|
useEffect(() => {
|
|
if (!day) return undefined;
|
|
let active = true;
|
|
let url = "";
|
|
loadDailyImage()
|
|
.then((objectUrl) => {
|
|
url = objectUrl;
|
|
if (active) setImage({ day, url: objectUrl, failed: false });
|
|
else URL.revokeObjectURL(objectUrl);
|
|
})
|
|
.catch(() => { if (active) setImage({ day, url: "", failed: true }); });
|
|
return () => {
|
|
active = false;
|
|
if (url) URL.revokeObjectURL(url);
|
|
};
|
|
}, [day, zoom]);
|
|
|
|
useEffect(() => {
|
|
const timer = setInterval(() => setNow(Date.now()), COUNTDOWN_TICK_MS);
|
|
return () => clearInterval(timer);
|
|
}, []);
|
|
|
|
const guessable = useMemo(() => {
|
|
if (!daily) return [];
|
|
const collections = new Set(daily.collectionIds);
|
|
const guessed = new Set(daily.guesses.map((item) => item.cardId));
|
|
return catalog.cards.filter((card) => collections.has(card.collectionId) && !guessed.has(card.id)).sort((left, right) => left.name.localeCompare(right.name, "fr"));
|
|
}, [catalog.cards, daily]);
|
|
const options = useMemo(() => guessable.map((card) => card.name), [guessable]);
|
|
const searchWords = daily?.searchWords;
|
|
const keywords = useMemo(() => new Map(guessable.filter((card) => searchWords?.[card.id]).map((card) => [card.name, searchWords[card.id]])), [guessable, searchWords]);
|
|
|
|
if (loadError) return <section className="nds-empty-state" role="alert"><h2>{content.title}</h2><p>{loadError}</p></section>;
|
|
if (!daily) return <section className="nds-empty-state" aria-busy="true"><h2>{content.title}</h2></section>;
|
|
|
|
const expired = now >= daily.nextAt;
|
|
const finished = daily.status !== "playing";
|
|
const imageUrl = image.day === daily.day ? image.url : "";
|
|
const imageFailed = image.day === daily.day && image.failed;
|
|
|
|
// Les refus propres à la partie restent dans ce panneau ; runMutation garde le reste (erreurs réseau, solde après une victoire).
|
|
// Les mots de recherche, propres au jour, ne viennent qu'au chargement de la partie.
|
|
async function play(action) {
|
|
const result = await runMutation(async () => {
|
|
try {
|
|
return await action();
|
|
} catch (error) {
|
|
if (String(error.code || "").startsWith("DAILY_")) return { dailyError: error };
|
|
throw error;
|
|
}
|
|
});
|
|
if (result?.daily) setDaily((current) => ({ ...current, ...result.daily }));
|
|
else if (result?.dailyError?.code === "DAILY_EXPIRED") setNow(daily.nextAt);
|
|
else if (result?.dailyError) setNotice(result.dailyError.message);
|
|
return Boolean(result?.daily);
|
|
}
|
|
|
|
async function submit(event) {
|
|
event.preventDefault();
|
|
setNotice("");
|
|
const card = findCardByName(catalog.cards.filter((item) => daily.collectionIds.includes(item.collectionId)), guess);
|
|
if (!card) return setNotice(content.guessUnknown);
|
|
if (daily.guesses.some((item) => item.cardId === card.id)) return setNotice(content.guessRepeated);
|
|
if (await play(() => guessDaily(daily.day, card.id))) setGuess("");
|
|
}
|
|
|
|
function flipHint(hint) {
|
|
setNotice("");
|
|
return play(() => flipDailyHint(daily.day, hint));
|
|
}
|
|
|
|
return <section className="nebula-daily" aria-labelledby="nebula-daily-title">
|
|
<div className="nds-section-heading nebula-section-heading">
|
|
<div><p className="eyebrow">{fillTemplate(content.number, { number: daily.number })}</p><h2 id="nebula-daily-title">{content.title}</h2></div>
|
|
<div className="nebula-daily-heading-actions">
|
|
<span className="nds-badge" data-variant="gold">{fillTemplate(content.reward, { reward: formatStardust(daily.reward) })}</span>
|
|
<a className="nds-button" data-size="sm" data-variant="secondary" href="#/cards/guild">{content.back}</a>
|
|
</div>
|
|
</div>
|
|
{expired && <div className="nds-inline-notice nebula-daily-expired" data-variant="warning" role="status"><span>{content.expired}</span><button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={() => { setNow(Date.now()); setReloadKey((key) => key + 1); }}>{content.reload}</button></div>}
|
|
<div className="nebula-daily-layout">
|
|
<div className="nds-panel nebula-daily-stage">
|
|
<div className="nebula-daily-frame" data-finished={finished || undefined}>
|
|
{imageUrl ? <img key={imageUrl} src={imageUrl} alt={content.imageAlt} draggable="false" /> : <span className="nebula-daily-frame-status" role={imageFailed ? "alert" : "status"}>{imageFailed ? content.imageError : content.imageLoading}</span>}
|
|
</div>
|
|
<ol className="nebula-daily-steps" aria-label={fillTemplate(content.attempts, { count: Math.min(daily.guesses.length + (finished ? 0 : 1), daily.maxGuesses), max: daily.maxGuesses })}>
|
|
{Array.from({ length: daily.maxGuesses }, (_, index) => {
|
|
const item = daily.guesses[index];
|
|
const result = item ? dailyGuessResult(item) : "empty";
|
|
return <li key={index} data-result={result} />;
|
|
})}
|
|
</ol>
|
|
</div>
|
|
<div className="nebula-daily-play">
|
|
{finished ? <DailyResult catalog={catalog} state={state} daily={daily} content={content} now={now} /> : <form className="nds-panel nebula-daily-form" onSubmit={submit}>
|
|
{daily.guesses.length === 0 && <p>{content.intro} {content.maybeNotOwned}</p>}
|
|
<label htmlFor="nebula-daily-guess">{content.guessLabel} · {fillTemplate(content.attempts, { count: daily.guesses.length + 1, max: daily.maxGuesses })}</label>
|
|
<div className="nebula-daily-form-row">
|
|
<CategoryAutocompleteInput id="nebula-daily-guess" name="guess" options={options} keywords={keywords} value={guess} placeholder={content.guessPlaceholder} maxOptions={MAX_SUGGESTIONS} disabled={busy || expired} onChange={(event) => { setGuess(event.target.value); setNotice(""); }} />
|
|
<button className="nds-button" data-variant="primary" type="submit" disabled={busy || expired || !guess.trim()}>{content.guessAction}</button>
|
|
</div>
|
|
{notice && <p className="nds-inline-notice" data-variant="warning" role="status">{notice}</p>}
|
|
</form>}
|
|
<DailyGuesses catalog={catalog} daily={daily} content={content} />
|
|
</div>
|
|
<DailyHints catalog={catalog} daily={daily} busy={busy || expired} content={content} onUse={flipHint} />
|
|
</div>
|
|
</section>;
|
|
}
|