// Rôle : valeurs par défaut, bornes, fusion, validation et différences des réglages des Expéditions Sokko TCG. // Règles pures : aucune lecture de la base ni de l'horloge. Le schéma est la seule source des bornes, pour le serveur comme pour l'administration. // Valeurs du document de conception. Une clé ajoutée plus tard prend sa valeur par défaut à la fusion, sans migration. export const DEFAULT_EXPEDITION_SETTINGS = Object.freeze({ enabled: false, day: { destinationsPerDay: 4, avoidRepeatDays: 1 }, durations: { hours: [2, 6, 12], malusPct: [0, 0, 15], endurancePerSlot: 12 }, solo: { maxConcurrent: 1, cards: 2, minStars: 1, maxStars: 4 }, power: { rare: 40, epic: 55, legendary: 70, mythic: 85, perLevel: 5, cap: 100 }, roles: { multiplierPct: 200 }, // Exigence d'une stat, par place, pour chaque nombre d'étoiles : stat principale, puis seconde, troisième et quatrième. requirements: { star1: [9], star2: [11, 10], star3: [15, 14, 11], star4: [19, 18, 14], star5: [22, 21, 17, 14], conditionMissPct: 25 }, danger: { malusPct: 20, shieldMaxPct: 50, endurancePerSlot: 14 }, score: { mainWeight: 2, otherWeight: 1, overflowPct: 50, overflowSpanPct: 100, capPct: 150, successPct: 80, triumphPct: 120 }, loot: { eclatsPerHourCents: 200, stardustPerHourCents: 25, packPerHourPermille: 12, maxPacks: 2, difficultyPct: [100, 130, 170, 220, 300], tierPct: [40, 100, 150], fortunePerPointPct: 2, fortuneCapPct: 50, // Multiplicateur de butin des destinations bonus : 5e, puis 6e. bonusSlotPct: [100, 125] }, destinations: {} }); export const DEFAULT_DESTINATION_SETTING = Object.freeze({ active: true, weight: 1 }); export const MAX_DESTINATION_WEIGHT = 100; const int = (path, min, max) => ({ path, kind: "int", min, max }); const ints = (path, length, min, max) => ({ path, kind: "ints", length, min, max }); // Bornes de chaque réglage. `ints` : liste de longueur fixe. Les réglages par destination sont validés à part. export const EXPEDITION_SETTINGS_SCHEMA = Object.freeze([ { path: "enabled", kind: "bool" }, int("day.destinationsPerDay", 1, 8), int("day.avoidRepeatDays", 0, 7), ints("durations.hours", 3, 1, 48), ints("durations.malusPct", 3, 0, 50), int("durations.endurancePerSlot", 1, 1000), int("solo.maxConcurrent", 1, 3), int("solo.cards", 1, 4), int("solo.minStars", 1, 5), int("solo.maxStars", 1, 5), int("power.rare", 1, 300), int("power.epic", 1, 300), int("power.legendary", 1, 300), int("power.mythic", 1, 300), int("power.perLevel", 0, 50), int("power.cap", 1, 300), int("roles.multiplierPct", 100, 400), ints("requirements.star1", 1, 1, 1000), ints("requirements.star2", 2, 1, 1000), ints("requirements.star3", 3, 1, 1000), ints("requirements.star4", 3, 1, 1000), ints("requirements.star5", 4, 1, 1000), int("requirements.conditionMissPct", 0, 100), int("danger.malusPct", 0, 90), int("danger.shieldMaxPct", 0, 100), int("danger.endurancePerSlot", 1, 1000), int("score.mainWeight", 1, 10), int("score.otherWeight", 1, 10), int("score.overflowPct", 0, 100), int("score.overflowSpanPct", 0, 300), int("score.capPct", 100, 300), int("score.successPct", 1, 300), int("score.triumphPct", 1, 300), int("loot.eclatsPerHourCents", 0, 100_000), int("loot.stardustPerHourCents", 0, 100_000), int("loot.packPerHourPermille", 0, 1000), int("loot.maxPacks", 0, 10), ints("loot.difficultyPct", 5, 0, 1000), ints("loot.tierPct", 3, 0, 1000), int("loot.fortunePerPointPct", 0, 100), int("loot.fortuneCapPct", 0, 500), ints("loot.bonusSlotPct", 2, 0, 1000) ]); const isObject = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value); export function readPath(source, path) { return path.split(".").reduce((value, key) => (isObject(value) ? value[key] : undefined), source); } function writePath(target, path, value) { const keys = path.split("."); let node = target; for (const key of keys.slice(0, -1)) { if (!isObject(node[key])) node[key] = {}; node = node[key]; } node[keys.at(-1)] = value; } function validField(field, value) { if (field.kind === "bool") return typeof value === "boolean"; const inRange = (item) => Number.isInteger(item) && item >= field.min && item <= field.max; if (field.kind === "int") return inRange(value); return Array.isArray(value) && value.length === field.length && value.every(inRange); } const validDestinationSetting = (value) => isObject(value) && typeof value.active === "boolean" && Number.isInteger(value.weight) && value.weight >= 0 && value.weight <= MAX_DESTINATION_WEIGHT; const copy = (value) => (Array.isArray(value) ? [...value] : value); // Réglages complets : chaque champ stocké et valide est gardé, sinon il reprend sa valeur par défaut ; les clés inconnues disparaissent. export function mergeExpeditionSettings(stored, destinationIds = [], onInvalid = null) { const merged = {}; for (const field of EXPEDITION_SETTINGS_SCHEMA) { const value = readPath(stored, field.path); const fallback = readPath(DEFAULT_EXPEDITION_SETTINGS, field.path); if (value !== undefined && !validField(field, value)) onInvalid?.(field.path); writePath(merged, field.path, copy(value !== undefined && validField(field, value) ? value : fallback)); } merged.destinations = {}; for (const id of destinationIds) { const value = isObject(stored?.destinations) ? stored.destinations[id] : undefined; if (value !== undefined && !validDestinationSetting(value)) onInvalid?.(`destinations.${id}`); merged.destinations[id] = validDestinationSetting(value) ? { active: value.active, weight: value.weight } : { ...DEFAULT_DESTINATION_SETTING }; } return merged; } // Message en français, ou null si les réglages sont valides (bornes, puis contraintes entre réglages). export function validateExpeditionSettings(settings, destinationIds = []) { if (!isObject(settings)) return "Réglages des Expéditions invalides."; for (const field of EXPEDITION_SETTINGS_SCHEMA) { if (!validField(field, readPath(settings, field.path))) { return field.kind === "bool" ? `Réglage ${field.path} invalide : oui ou non.` : `Réglage ${field.path} invalide : ${field.kind === "ints" ? `${field.length} entiers` : "un entier"} entre ${field.min.toLocaleString("fr-FR")} et ${field.max.toLocaleString("fr-FR")}.`; } } if (!isObject(settings.destinations)) return "Réglages des destinations invalides."; for (const id of Object.keys(settings.destinations)) { if (!destinationIds.includes(id)) return `Destination inconnue : ${id}.`; if (!validDestinationSetting(settings.destinations[id])) return `Réglage de la destination ${id} invalide : active oui ou non, poids entre 0 et ${MAX_DESTINATION_WEIGHT}.`; } const { hours } = settings.durations; if (hours.some((value, index) => index > 0 && value <= hours[index - 1])) return "Les durées doivent être croissantes."; if (settings.solo.minStars > settings.solo.maxStars) return "L'étoile minimale du solo dépasse l'étoile maximale."; const { successPct, triumphPct, capPct } = settings.score; if (!(successPct < triumphPct && triumphPct <= capPct)) return "Les seuils doivent respecter Réussite < Triomphe ≤ plafond du score."; const { perLevel, cap, ...bases } = settings.power; if (Object.values(bases).some((base) => base > cap)) return "Une Puissance de base dépasse le plafond."; if (perLevel === 0 && Object.values(bases).some((base) => base < cap)) return "Le gain par niveau doit être positif tant qu'une Puissance de base est sous le plafond."; return null; } // Différences champ par champ, pour l'historique d'administration. export function diffExpeditionSettings(previous, next) { const changes = []; for (const field of EXPEDITION_SETTINGS_SCHEMA) { const before = readPath(previous, field.path); const after = readPath(next, field.path); if (JSON.stringify(before) !== JSON.stringify(after)) changes.push({ path: field.path, previous: before, next: after }); } const ids = new Set([...Object.keys(previous?.destinations || {}), ...Object.keys(next?.destinations || {})]); for (const id of ids) { for (const key of ["active", "weight"]) { const before = previous?.destinations?.[id]?.[key]; const after = next?.destinations?.[id]?.[key]; if (before !== after) changes.push({ path: `destinations.${id}.${key}`, previous: before, next: after }); } } return changes; }