sokko-g/services/cards/daily-image.js
Shinuwa 22b397ebfa Document Sokko G's architecture and folders, and move JavaScript file headers to JSDoc @file blocks
Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md
(setup and step-by-step recipes) and a README in each main folder, with an
index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to
JSDoc @file blocks, define a three-level header template in website/AGENTS.md
and check the header in tests/file-headers.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:42:13 +01:00

42 lines
1.7 KiB
JavaScript

/**
* @file Prépare l'illustration de la Carte du jour envoyée au joueur : seulement la zone visible au zoom
* courant, agrandie, pour que ni « Ouvrir l'image » ni les outils du navigateur ne montrent la carte
* entière avant la fin de la partie.
*/
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import sharp from "sharp";
import { dailyCrop } from "./daily.js";
// Taille du morceau envoyé : le cadre de jeu fait 360 px au plus, doublés pour les écrans denses.
const OUTPUT_SIZE = 720;
// Quelques joueurs par jour partagent rarement une carte et un point de zoom : un petit cache évite surtout de recalculer
// un morceau quand la page se recharge.
const CACHE_LIMIT = 64;
export function createDailyImageRenderer(publicRoot) {
const cache = new Map();
return function render({ image, focus, zoom }) {
const path = join(publicRoot, image);
// Partie terminée : l'illustration entière, telle quelle.
if (!(zoom > 1)) return readFile(path);
const key = `${image}|${focus.x}|${focus.y}|${zoom}`;
const cached = cache.get(key);
if (cached) {
cache.delete(key);
cache.set(key, cached);
return cached;
}
const rendered = (async () => {
const source = sharp(path);
const { width, height } = await source.metadata();
return source.extract(dailyCrop(width, height, focus, zoom)).resize({ width: OUTPUT_SIZE }).webp({ quality: 86 }).toBuffer();
})();
cache.set(key, rendered);
if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value);
// Un échec ne reste pas en cache : le prochain appel réessaie.
rendered.catch(() => cache.delete(key));
return rendered;
};
}