Files
newbury-exhibit-v2/server/state.js
T

127 lines
5.4 KiB
JavaScript

/* Seed/live persistence.
* data/scene.json — committed seed (survives in git)
* data/live/scene.live.json — runtime edits, volume-mounted in Docker, survives redeploys
*/
const fs = require('fs');
const path = require('path');
const DATA = path.join(__dirname, '..', 'data');
const LIVE_DIR = process.env.LIVE_DIR || path.join(DATA, 'live');
const SEED = path.join(DATA, 'scene.json');
const LIVE = path.join(LIVE_DIR, 'scene.live.json');
const PLAYLIST_LIVE = path.join(LIVE_DIR, 'playlist.live.json');
const MODELS_LIVE = path.join(LIVE_DIR, 'models.live.json');
const MODELS_SEED = path.join(DATA, 'models.json');
const CHARS_LIVE = path.join(LIVE_DIR, 'characters.live.json');
const LANDING_LIVE = path.join(LIVE_DIR, 'landing.live.json');
fs.mkdirSync(LIVE_DIR, { recursive: true });
function readJSON(p, fallback) {
try { return JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return fallback; }
}
function writeJSON(p, obj) {
fs.writeFileSync(p, JSON.stringify(obj, null, 2));
}
let scene = readJSON(LIVE, null) || readJSON(SEED, { anchors: [], buildings: [], spawns: [], paths: [], world: { units: 'cm' } });
let ghosts = readJSON(path.join(DATA, 'ghosts.json'), []);
let gradients = readJSON(path.join(DATA, 'gradients.json'), {});
let models = readJSON(MODELS_LIVE, null) || readJSON(MODELS_SEED, { models: [] });
/* characters: per-ghost visual overrides keyed by ghost id, plus defaults.
* { defaults: {...}, byId: { 'mad-fenton': { modelId, opacity, topColor, bottomColor,
* faceTextureUrl, torsoTextureUrl, heightCm, scale } } } */
let characters = readJSON(CHARS_LIVE, null) || { defaults: {}, byId: {} };
/* landing: end-user facing start screen + details page. All text is editable except
* that the disclaimer is ALWAYS rendered (wording editable, presence is not). */
const LANDING_DEFAULTS = {
title: 'NEWBURY NIGHTS',
subtitle: 'Point your phone at the Newbury Crests to reveal the hidden side of the town.',
logoUrl: '',
backgroundUrl: '',
startButton: 'Start Ghost Watching',
detailsButton: 'About this build',
detailsTitle: 'About Newbury Nights',
detailsMarkdown: 'Write about your build here.\n\nUse **bold**, *italic*, and blank lines for paragraphs.',
disclaimer: 'Fan-made tribute experience. Not affiliated with, sponsored, or endorsed by the LEGO Group. LEGO® and Hidden Side™ are trademarks of the LEGO Group.',
accent: '#51eaf1',
textColor: '#e8ecff',
overlay: 0.55,
};
let landing = Object.assign({}, LANDING_DEFAULTS, readJSON(LANDING_LIVE, null) || {});
const listeners = [];
function validateScene(s) {
if (!s || typeof s !== 'object') throw new Error('scene must be object');
for (const k of ['anchors', 'buildings', 'spawns']) {
if (!Array.isArray(s[k])) throw new Error(`scene.${k} must be array`);
}
s.table = Object.assign({ width: 120, depth: 100, offsetX: 0, offsetZ: 0, show: true }, s.table || {});
s.ghostHeightCm = Number(s.ghostHeightCm || 4); // minifigure-scale default
s.paths = Array.isArray(s.paths) ? s.paths : [];
for (const pth of s.paths) {
if (!pth.id) throw new Error('path.id required');
if (!Array.isArray(pth.points) || pth.points.length < 2) throw new Error(`path ${pth.id} needs >= 2 points`);
pth.mode = pth.mode === 'pingpong' ? 'pingpong' : 'loop';
pth.enabled = pth.enabled !== false;
}
for (const a of s.anchors) {
if (typeof a.markerId !== 'number') throw new Error('anchor.markerId required');
if (!Array.isArray(a.position) || a.position.length !== 3) throw new Error('anchor.position [x,y,z] required');
a.mount = a.mount || 'flat'; // 'flat' | 'wall' | 'custom'
a.yawDeg = Number(a.yawDeg || 0);
a.pitchDeg = Number(a.pitchDeg || 0);
a.rollDeg = Number(a.rollDeg || 0);
a.sizeMM = Number(a.sizeMM || 60); // printed marker size
a.enabled = a.enabled !== false;
}
return s;
}
module.exports = {
getScene: () => scene,
putScene(s) {
scene = validateScene(s);
writeJSON(LIVE, scene);
listeners.forEach(f => f());
return scene;
},
resetToSeed() {
scene = readJSON(SEED, scene);
writeJSON(LIVE, scene);
listeners.forEach(f => f());
return scene;
},
getGhosts: () => ({ ghosts, gradients }),
getLanding: () => landing,
putLanding(l) {
if (!l || typeof l !== 'object') throw new Error('landing must be object');
const next = Object.assign({}, LANDING_DEFAULTS, l);
// disclaimer may be reworded but never emptied — it protects the project
if (!String(next.disclaimer || '').trim()) next.disclaimer = LANDING_DEFAULTS.disclaimer;
landing = next;
writeJSON(LANDING_LIVE, landing);
listeners.forEach(f => f());
return landing;
},
getCharacters: () => characters,
putCharacters(c) {
if (!c || typeof c !== 'object') throw new Error('characters must be object');
characters = { defaults: c.defaults || {}, byId: c.byId || {} };
writeJSON(CHARS_LIVE, characters);
listeners.forEach(f => f());
return characters;
},
liveDir: LIVE_DIR,
getModels: () => models,
putModels(m) {
if (!m || !Array.isArray(m.models)) throw new Error('models.models must be array');
models = m; writeJSON(MODELS_LIVE, models); return models;
},
getPlaylistConfig: (fallback) => readJSON(PLAYLIST_LIVE, null) || readJSON(path.join(DATA, 'playlist.json'), fallback),
putPlaylistConfig(cfg) { writeJSON(PLAYLIST_LIVE, cfg); return cfg; },
onChange: (f) => listeners.push(f),
};