sokko-g/services/cards/rewards.js
Shinuwa 22b397ebfa Document Sokko G's architecture and folders, and move JavaScript file headers to JSDoc @file blocks
Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md
(setup and step-by-step recipes) and a README in each main folder, with an
index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to
JSDoc @file blocks, define a three-level header template in website/AGENTS.md
and check the header in tests/file-headers.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:42:13 +01:00

77 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file 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, packRates | null, guarantee | null, quantity }
// packRates : sans taux personnalisés, copie des taux du paquet à la création de la campagne ; chaque joueur
// du code tire avec ces taux, même si l'administration modifie ensuite ceux du paquet.
// 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.");
const packRates = rarityWeights ? null : Object.fromEntries(catalog.rarities.map((rarity) => [rarity.id, collection.dropWeights[rarity.id] ?? 0]));
return { type: "pack", collectionId: collection.id, rarityWeights, packRates, guarantee };
}