// Rôle : mesure, pour le Miroir brisé, les morceaux presque identiques de chaque illustration (carrée en 4 × 4, portrait en 5 × 7) // et les écrit dans services/cards/content/mirror-illustrations.json. Le jeu écarte les illustrations trop unies (mirror-rules.js). // Usage : node scripts/mirror-illustrations.mjs. À relancer après l'ajout ou le changement d'une illustration ; le test des données // signale une carte jouable sans mesure. import { writeFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import sharp from "sharp"; import { loadCardsCatalog } from "../services/cards/catalog.js"; import { MIRROR_MODES } from "../services/cards/mirror-rules.js"; const root = fileURLToPath(new URL("..", import.meta.url)); const catalog = loadCardsCatalog(`${root}website/public/data/cards/catalog.json`); // Chaque morceau est réduit à une grille de 6 × 6 couleurs moyennes ; deux morceaux dont l'écart moyen reste sous 6 % se // confondent à l'œil (ciel sombre, brume, fond uni), d'après des paires vérifiées à la main le 28 novembre 2026. const GRID = 6; const CELL = 10; const THRESHOLD = 0.06; async function measure(path, cols, rows) { const size = GRID * CELL; const width = cols * size; const { data } = await sharp(`${root}website/public${path}`).resize(width, rows * size, { fit: "fill" }).removeAlpha().raw().toBuffer({ resolveWithObject: true }); const pieces = []; for (let row = 0; row < rows; row += 1) for (let col = 0; col < cols; col += 1) { const features = []; for (let gy = 0; gy < GRID; gy += 1) for (let gx = 0; gx < GRID; gx += 1) { const sum = [0, 0, 0]; for (let y = 0; y < CELL; y += 1) for (let x = 0; x < CELL; x += 1) { const offset = ((row * size + gy * CELL + y) * width + col * size + gx * CELL + x) * 3; for (let channel = 0; channel < 3; channel += 1) sum[channel] += data[offset + channel]; } features.push(...sum.map((value) => value / (CELL * CELL))); } pieces.push(features); } const distance = (left, right) => Math.sqrt(left.reduce((total, value, index) => total + (value - right[index]) ** 2, 0) / left.length) / 255; const ambiguous = pieces.filter((piece, index) => pieces.some((other, otherIndex) => otherIndex !== index && distance(piece, other) < THRESHOLD)).length; return { pieces: pieces.length, ambiguous }; } const result = { threshold: THRESHOLD, square: {}, portrait: {} }; for (const card of [...catalog.cards].sort((left, right) => left.id.localeCompare(right.id))) { for (const mode of Object.values(MIRROR_MODES)) { const path = mode.format === "portrait" ? card.fullImage : card.image; if (path) result[mode.format][card.id] = await measure(path, mode.cols, mode.rows); } } writeFileSync(`${root}services/cards/content/mirror-illustrations.json`, `${JSON.stringify(result, null, 2)}\n`); const excluded = Object.entries(result).filter(([key]) => key !== "threshold").flatMap(([format, cards]) => Object.entries(cards).filter(([, value]) => value.ambiguous / value.pieces > 0.2).map(([id, value]) => `${format}:${id} (${value.ambiguous}/${value.pieces})`)); console.log(`${Object.keys(result.square).length} carrées, ${Object.keys(result.portrait).length} portraits ; trop unies : ${excluded.join(", ") || "aucune"}`);