/** * @file Répartition des statistiques d'une carte depuis sa fiche : ses points de niveau suivent sa collection * (« Par défaut »), un profil (une stat privilégiée ; une étoile marque celui qui lui donne la stat la * plus haute) ou la répartition du joueur (« Personnalisé »). Le choix en cours s'affiche dans le * récapitulatif de la fiche (`onPreview`) avant de valider. Le premier choix est gratuit ; ensuite, la * répartition se verrouille : la réinitialiser coûte un Parchemin d'oubli et remet la carte en * « Personnalisé », sans point placé, d'où un nouveau choix gratuit (un profil répartit tout seul). * Ajouter des points en « Personnalisé » reste gratuit. */ import { useState } from "react"; import { setCardPlan } from "../cardsApi.js"; import { fillTemplate } from "../textFormat.js"; import { MaskIcon } from "./ExpeditionLabels.jsx"; const STATS = ["bravoure", "endurance", "fortune", "orientation"]; // Profils et la stat que chacun privilégie (libellés et aides : cards.cardSheet.plan.profiles). const PROFILES = [{ id: "leader", stat: "bravoure" }, { id: "guardian", stat: "endurance" }, { id: "seeker", stat: "fortune" }, { id: "navigator", stat: "orientation" }]; const ZERO = Object.fromEntries(STATS.map((stat) => [stat, 0])); const total = (stats) => Object.values(stats).reduce((sum, value) => sum + value, 0); const toDraft = (allocation) => Object.fromEntries(STATS.map((stat) => [stat, String(allocation?.[stat] ?? 0)])); const toAllocation = (draft) => Object.fromEntries(STATS.map((stat) => [stat, Math.max(0, Number.parseInt(draft[stat], 10) || 0)])); const plural = (count) => (count > 1 ? "s" : ""); /** * `locked` : la carte est en expédition, sa répartition ne change pas avant son retour. `content` : textes cards.cardSheet.plan, * `statNames` : noms des stats. */ export function CardPlan({ progress, oblivions, content, statNames, locked = false, busy, runMutation, onPreview = () => {} }) { const plan = progress.plan; // Choix encore ouvert : premier choix, ou après une réinitialisation. const open = !plan.paid; const placed = plan.allocation || ZERO; const [choice, setChoice] = useState(plan.id); const [draft, setDraft] = useState(() => toDraft(plan.allocation)); const free = choice === "free"; const profile = PROFILES.find((item) => item.id === choice) || null; const allocation = toAllocation(draft); const left = plan.points - total(allocation); const statsFor = (id, points) => (id === "free" ? Object.fromEntries(STATS.map((stat) => [stat, plan.levelBase[stat] + points[stat]])) : plan.previews[id]); const unchangedFor = (id, points) => id === plan.id && (id !== "free" || STATS.every((stat) => points[stat] === placed[stat])); const unchanged = unchangedFor(choice, allocation); // Le profil qui donne à la carte sa stat la plus haute. const best = PROFILES.reduce((top, item) => (plan.previews[item.id][item.stat] > plan.previews[top.id][top.stat] ? item : top), PROFILES[0]).id; const report = (id, points) => onPreview(unchangedFor(id, points) ? null : statsFor(id, points)); const choose = (id) => { setChoice(id); report(id, allocation); }; /** Un point de plus ne dépasse ni le plafond d'une stat, ni les points restants ; sans réinitialisation, on ne retire pas un point placé. */ const setPoints = (stat, raw) => { const limit = Math.min(plan.maxStatPoints, allocation[stat] + left); const next = { ...draft, [stat]: raw !== "" && Number(raw) > limit ? String(limit) : raw }; setDraft(next); report("free", toAllocation(next)); }; const cancel = () => { setChoice(plan.id); setDraft(toDraft(plan.allocation)); onPreview(null); }; const save = async () => { if (await runMutation(() => setCardPlan(progress.editionId, choice, free ? allocation : null))) onPreview(null); }; const reset = async () => { if (await runMutation(() => setCardPlan(progress.editionId, "free", ZERO))) onPreview(null); }; const removesPoints = free && plan.id === "free" && STATS.some((stat) => allocation[stat] < placed[stat]); const editable = !locked && (open || plan.id === "free"); const option = (id, label, stat = null) => ; // « Valider » et « Annuler » dans le cadre du choix en cours, sous ce qui a changé. const actions = !unchanged &&
{content.firstFree}
} {removesPoints &&{content.removeNeedsReset}
}{plan.points ? fillTemplate(content.pointsGained, { count: plan.points, s: plural(plan.points) }) : content.pointsLater}
{/* Deux cadres : « Par défaut » ou « Personnalisé » (et ses points juste dessous), puis les profils ; le cadre du choix en cours est souligné d'or. */}{content.help[choice]}
} {free &&{!plan.points ? content.noPoints : <> {left > 0 ? fillTemplate(content.left, { count: left, s: plural(left) }) : content.allPlaced} {fillTemplate(content.cap, { count: plan.maxStatPoints })} >}
{STATS.map((stat) => )}{content.profiles[profile.id].help}
} {profile && actions}{content.locked}
} {!open && !locked &&{`${fillTemplate(content.resetCount, { count: oblivions })} ${oblivions < 1 ? content.resetBuy : content.resetHelp}`}