// Rôle : charge et valide les destinations des Expéditions Sokko TCG, définies dans un fichier du serveur pour garder secrètes les destinations cachées. import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { cardExpeditionData, isExpeditionCard, STAT_IDS } from "./expedition-rules.js"; export const DEFAULT_DESTINATIONS_PATH = fileURLToPath(new URL("./content/expedition-destinations.json", import.meta.url)); const ID_PATTERN = /^[a-z0-9-]+$/; export const DESTINATION_IMAGE_PATTERN = /^\/static\/img\/expeditions\/destinations\/[a-z0-9_-]+\.webp$/; // Une condition ou un danger doit pouvoir être rempli par au moins ce nombre de cartes rare ou mieux. export const MIN_MATCHING_CARDS = 3; function fail(destination, message) { throw new TypeError(`Expedition destination ${destination?.id ?? "?"}: ${message}`); } // Cartes rare ou mieux qui portent une des étiquettes, ou qui appartiennent à la collection. function matchingCards(catalog, { tags = [], collectionId = null }) { return catalog.cards.filter((card) => { if (!isExpeditionCard(catalog, card.id)) return false; const data = cardExpeditionData(catalog, card.id); return collectionId ? data.collectionId === collectionId : data.tags.some((tag) => tags.includes(tag)); }); } export function validateExpeditionDestinations(destinations, catalog) { if (!Array.isArray(destinations) || !destinations.length) throw new TypeError("Expedition destinations must be a non-empty array."); const tagIds = new Set(catalog.expeditionTags.map((tag) => tag.id)); const collectionIds = new Set(catalog.collections.map((collection) => collection.id)); const cardIds = new Set(catalog.cards.map((card) => card.id)); const ids = new Set(); for (const destination of destinations) { if (!ID_PATTERN.test(destination?.id ?? "")) fail(destination, "needs a lowercase id."); if (ids.has(destination.id)) fail(destination, "duplicate id."); ids.add(destination.id); if (typeof destination.name !== "string" || !destination.name || destination.name.length > 60) fail(destination, "name must contain 1 to 60 characters."); if (typeof destination.description !== "string" || !destination.description || destination.description.length > 200) fail(destination, "description must contain 1 to 200 characters."); if (destination.coverCardId != null && !cardIds.has(destination.coverCardId)) fail(destination, "unknown cover card."); // Paysage de la destination (facultatif) : affiché dans la forme du coup de pinceau ; sinon, l'image de sa carte de couverture. if (destination.image != null && !DESTINATION_IMAGE_PATTERN.test(destination.image)) fail(destination, "image must be a /static/img/expeditions/destinations/*.webp path."); if (typeof destination.hidden !== "boolean") fail(destination, "hidden must be a boolean."); if (!Array.isArray(destination.stats) || destination.stats.length !== STAT_IDS.length || new Set(destination.stats).size !== STAT_IDS.length || destination.stats.some((stat) => !STAT_IDS.includes(stat))) { fail(destination, "stats must order the four stats."); } // Thème de la destination : une carte qui porte l'une de ces étiquettes donne l'Affinité à l'équipe. if (!Array.isArray(destination.themeTags) || !destination.themeTags.length || destination.themeTags.some((tag) => !tagIds.has(tag))) fail(destination, "needs known theme tags."); if (matchingCards(catalog, { tags: destination.themeTags }).length < MIN_MATCHING_CARDS) fail(destination, `theme matches fewer than ${MIN_MATCHING_CARDS} rare or better cards.`); // Cartes des Carnets des Veilleurs que la destination peut rapporter : rare ou mieux, d'une collection des Expéditions. const expeditionCollections = new Set(catalog.collections.filter((collection) => collection.packCategory === "expedition").map((collection) => collection.id)); if (!Array.isArray(destination.logbookCards) || destination.logbookCards.some((cardId) => { const card = catalog.cards.find((item) => item.id === cardId); return !card || !expeditionCollections.has(card.collectionId) || !isExpeditionCard(catalog, cardId); })) fail(destination, "logbookCards must list rare or better cards of an expedition collection."); if (!Array.isArray(destination.conditions) || !destination.conditions.length) fail(destination, "needs at least one condition."); for (const condition of destination.conditions) { if (condition?.type === "tag") { if (!tagIds.has(condition.tag)) fail(destination, `unknown condition tag ${condition.tag}.`); if (matchingCards(catalog, { tags: [condition.tag] }).length < MIN_MATCHING_CARDS) fail(destination, `condition ${condition.tag} matches fewer than ${MIN_MATCHING_CARDS} rare or better cards.`); } else if (condition?.type === "collection") { if (!collectionIds.has(condition.collectionId)) fail(destination, `unknown condition collection ${condition.collectionId}.`); if (matchingCards(catalog, { collectionId: condition.collectionId }).length < MIN_MATCHING_CARDS) fail(destination, `condition ${condition.collectionId} matches fewer than ${MIN_MATCHING_CARDS} rare or better cards.`); } else { fail(destination, "condition type must be tag or collection."); } } if (!Array.isArray(destination.dangers) || !destination.dangers.length) fail(destination, "needs at least one danger."); const dangerIds = new Set(); for (const danger of destination.dangers) { if (!ID_PATTERN.test(danger?.id ?? "") || dangerIds.has(danger.id)) fail(destination, "every danger needs a unique lowercase id."); dangerIds.add(danger.id); if (typeof danger.label !== "string" || !danger.label || danger.label.length > 40) fail(destination, `danger ${danger.id} label must contain 1 to 40 characters.`); if (!STAT_IDS.includes(danger.stat)) fail(destination, `danger ${danger.id} needs a known stat.`); if (!Array.isArray(danger.unlessTags) || !danger.unlessTags.length || danger.unlessTags.some((tag) => !tagIds.has(tag))) fail(destination, `danger ${danger.id} needs known counter tags.`); if (matchingCards(catalog, { tags: danger.unlessTags }).length < MIN_MATCHING_CARDS) fail(destination, `danger ${danger.id} can be countered by fewer than ${MIN_MATCHING_CARDS} rare or better cards.`); } } if (!destinations.some((destination) => !destination.hidden)) throw new TypeError("Expedition destinations need at least one visible destination."); // Une carte de destination débloque une destination qui existe. for (const card of catalog.cards.filter((item) => item.kind === "destination")) { if (!ids.has(card.destinationId)) throw new TypeError(`Destination card ${card.id} names unknown destination ${card.destinationId}.`); } return destinations; } export function loadExpeditionDestinations(path, catalog) { return validateExpeditionDestinations(JSON.parse(readFileSync(path, "utf8")), catalog); }