sokko-g/services/cards/mirror-settings.js
Shinuwa 9cf48ea0d0 Add the Broken Mirror puzzle to the Guild
A daily puzzle: a card illustration cut into pieces, rebuilt by swapping
two pieces of the same row or column within a limited number of swaps.
The normal picture is a square card in 4 x 4, drawn for each player, with
placed pieces outlined in green; the hard one is a 5 x 7 portrait shared
by everyone each day, with a result to copy. The limit is the computer's
best result (beam search) plus a margin, and every swap left adds
stardust. Two companions of the daily team can each give one help, by
specialty; a double specialty picks one when used.

The server keeps the piece positions, sends the picture already shuffled
and checks every swap. Uniform illustrations are left out, from measures
written by scripts/mirror-illustrations.mjs. The Guild gets its quest and
a Codex tile, the administration a tab (opening, rewards, margins, the
day's summary), and migration 30 its tables. New collections must plan at
least one double-specialty character, counted by
scripts/team-specialties.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 13:38:46 +01:00

47 lines
2.6 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.

// Rôle : réglages du Miroir brisé (ouverture, gains, marges d'échanges) : valeurs par défaut, validation et lecture tolérante.
import { MIRROR_MODE_IDS } from "./mirror-rules.js";
const MAX_REWARD = 100_000;
const MAX_MARGIN = 30;
// Gains : base d'une image reconstituée et part par échange restant. Repères : la Carte du jour rapporte 20 en production et la
// Course 20 / 15 / 10 / 5. Marges : échanges permis au-delà du meilleur résultat de l'ordinateur ; en Normal, un joueur attentif
// réussit environ 9 fois sur 10 avec 3 (simulation, docs/miroir-brise.md).
export const DEFAULT_MIRROR_SETTINGS = Object.freeze({
enabled: false,
rewards: Object.freeze({ normal: Object.freeze({ base: 10, perSwap: 1 }), hard: Object.freeze({ base: 20, perSwap: 2 }) }),
margins: Object.freeze({ normal: 3, hard: 6 })
});
export function mirrorSettingsError(settings) {
const integer = (value, max) => Number.isInteger(value) && value >= 0 && value <= max;
if (!settings || typeof settings !== "object" || Array.isArray(settings)) return "Réglages du Miroir brisé invalides.";
if (typeof settings.enabled !== "boolean") return "L’ouverture du Miroir brisé doit être vraie ou fausse.";
for (const mode of MIRROR_MODE_IDS) {
const reward = settings.rewards?.[mode];
if (!reward || !integer(reward.base, MAX_REWARD) || !integer(reward.perSwap, MAX_REWARD)) return `Gains du Miroir brisé : des entiers entre 0 et ${MAX_REWARD.toLocaleString("fr-FR")}.`;
if (!integer(settings.margins?.[mode], MAX_MARGIN)) return `Marges d’échanges : des entiers entre 0 et ${MAX_MARGIN}.`;
}
return null;
}
// Garde seulement les champs connus.
export const normalizeMirrorSettings = ({ enabled, rewards, margins }) => ({
enabled,
rewards: Object.fromEntries(MIRROR_MODE_IDS.map((mode) => [mode, { base: rewards[mode].base, perSwap: rewards[mode].perSwap }])),
margins: Object.fromEntries(MIRROR_MODE_IDS.map((mode) => [mode, margins[mode]]))
});
// Réglages enregistrés : des valeurs invalides laissent la place aux valeurs par défaut, pour que le jeu reste jouable.
export function readMirrorSettings(stored, onInvalid = () => {}) {
const defaults = DEFAULT_MIRROR_SETTINGS;
const merged = {
...defaults, ...stored,
rewards: Object.fromEntries(MIRROR_MODE_IDS.map((mode) => [mode, { ...defaults.rewards[mode], ...stored?.rewards?.[mode] }])),
margins: { ...defaults.margins, ...stored?.margins }
};
const error = mirrorSettingsError(merged);
if (!error) return normalizeMirrorSettings(merged);
onInvalid(error);
return normalizeMirrorSettings({ ...defaults, enabled: stored?.enabled === true });
}