- Each player draws their own daily card and zoom point on their first visit, so a shared answer no longer helps; the migration keeps past games with their day's shared card. - The server crops the illustration with sharp to the part visible at the current zoom; the full image only arrives once the game is over. - The admin overview shows the day's winners and players instead of a single card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
38 lines
1.7 KiB
JavaScript
38 lines
1.7 KiB
JavaScript
// Rôle : 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;
|
|
};
|
|
}
|