- A new watchers-logbooks collection of category expedition never comes from packs, the shop or codes, and administrators cannot open it as a pack. - Its six commons carry powers instead of stats (Longue-vue, Provisions, Carte au trésor, Talisman, Boussole folle, Filet à étoiles filantes); a card holds one power and each counts once per team. - Powers change travel time, loot, score or tier, hide requirements, or catch a logbook card; each destination lists the logbook cards it can bring back. - A workshop turns a plan and three duplicate commons into a power common. - The fifteen cards ship without images with ready ChatGPT prompts; a card only drops or gets crafted once illustrated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
4.4 KiB
JavaScript
70 lines
4.4 KiB
JavaScript
// Rôle : valide et normalise les récompenses distribuées par codes (des cartes et des paquets avec leurs options).
|
||
import { isDrawableEdition, validateRarityWeights } from "./catalog.js";
|
||
|
||
function invalid(message) {
|
||
throw Object.assign(new Error(message), { status: 400, code: "REWARD_INVALID" });
|
||
}
|
||
|
||
export const MAX_REWARD_ITEMS = 50;
|
||
export const MAX_ITEM_QUANTITY = 10;
|
||
|
||
// Une récompense est une liste d'éléments : { items: [élément, …] }, au plus 50 éléments.
|
||
// Élément « carte » : { type: "card", editionId, quantity: 1 }, chaque carte au plus une fois
|
||
// Élément « paquet » : { type: "pack", collectionId, rarityWeights | null, guarantee | null, quantity }
|
||
// guarantee : { type: "rarity", rarity, count (1 à 3) } ou { type: "edition", editionId }
|
||
export function normalizeReward(reward, catalog) {
|
||
const items = reward?.items;
|
||
if (!Array.isArray(items) || !items.length) invalid("Ajoutez au moins une carte ou un paquet à offrir.");
|
||
if (items.length > MAX_REWARD_ITEMS) invalid(`Au plus ${MAX_REWARD_ITEMS} éléments par récompense.`);
|
||
const cardIds = items.filter((item) => item?.type === "card").map((item) => item.editionId);
|
||
if (new Set(cardIds).size !== cardIds.length) invalid("Chaque carte ne peut être offerte qu’une fois par récompense.");
|
||
return { items: items.map((item) => {
|
||
const quantity = item?.quantity ?? 1;
|
||
if (item?.type === "card" && quantity !== 1) invalid("Une carte s’offre en un seul exemplaire.");
|
||
if (!Number.isInteger(quantity) || quantity < 1 || quantity > MAX_ITEM_QUANTITY) invalid(`Quantité invalide : de 1 à ${MAX_ITEM_QUANTITY} par élément.`);
|
||
return { ...normalizeItem(item, catalog), quantity };
|
||
}) };
|
||
}
|
||
|
||
function normalizeItem(reward, catalog) {
|
||
if (reward?.type === "card") {
|
||
if (!catalog.editions.some((edition) => edition.id === reward.editionId)) invalid("Choisissez une carte du catalogue.");
|
||
return { type: "card", editionId: reward.editionId };
|
||
}
|
||
if (reward?.type !== "pack") invalid("Choisissez une carte ou un paquet à offrir.");
|
||
const collection = catalog.collections.find((item) => item.id === reward.collectionId);
|
||
if (!collection) invalid("Choisissez une collection.");
|
||
const cardIds = new Set(catalog.cards.filter((card) => card.collectionId === collection.id).map((card) => card.id));
|
||
if (collection.packCategory === "shop") invalid("Ce paquet ne s’obtient que dans la boutique.");
|
||
if (collection.packCategory === "expedition") invalid("Cette collection ne s’obtient qu’en expédition.");
|
||
const drawable = catalog.editions.filter((edition) => cardIds.has(edition.cardId) && isDrawableEdition(edition));
|
||
const rarityIds = new Set(catalog.rarities.map((rarity) => rarity.id));
|
||
|
||
let rarityWeights = null;
|
||
if (reward.rarityWeights != null) {
|
||
try {
|
||
validateRarityWeights(reward.rarityWeights, rarityIds, collection.id);
|
||
} catch {
|
||
invalid("Taux personnalisés invalides : un entier positif ou nul par rareté, pour un total strictement positif.");
|
||
}
|
||
if (!drawable.some((edition) => reward.rarityWeights[edition.rarity] > 0)) invalid("Ces taux ne permettent de tirer aucune carte de la collection.");
|
||
rarityWeights = Object.fromEntries(catalog.rarities.map((rarity) => [rarity.id, reward.rarityWeights[rarity.id]]));
|
||
}
|
||
|
||
let guarantee = null;
|
||
if (reward.guarantee?.type === "rarity") {
|
||
const { rarity, count } = reward.guarantee;
|
||
const rank = catalog.rarities.findIndex((item) => item.id === rarity);
|
||
if (rank < 0 || !Number.isInteger(count) || count < 1 || count > 3) invalid("Garantie invalide : de 1 à 3 cartes d’une rareté connue.");
|
||
if (!drawable.some((edition) => catalog.rarities.findIndex((item) => item.id === edition.rarity) >= rank)) invalid("Cette collection n’a aucune carte tirable de cette rareté ou mieux.");
|
||
guarantee = { type: "rarity", rarity, count };
|
||
} else if (reward.guarantee?.type === "edition") {
|
||
const edition = catalog.editions.find((item) => item.id === reward.guarantee.editionId);
|
||
if (!edition || !cardIds.has(edition.cardId)) invalid("La carte garantie doit appartenir à la collection du paquet.");
|
||
guarantee = { type: "edition", editionId: edition.id };
|
||
} else if (reward.guarantee != null) {
|
||
invalid("Garantie inconnue.");
|
||
}
|
||
if (!rarityWeights && !collection.dropWeights) invalid("Cette collection n’a pas de taux de tirage : définissez des taux personnalisés.");
|
||
return { type: "pack", collectionId: collection.id, rarityWeights, guarantee };
|
||
}
|