sokko-g/scripts/mirror-illustrations.mjs
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

50 lines
3.2 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 : 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"}`);