Examples > text-adventures COMPANION SERVER.JS (3/13) let gameCache = null; function games() { if (gameCache) return gameCache; let meta = {}; try { meta = JSON.parse(fs.readFileSync(path.join(GAMES_DIR, 'games.json'), 'utf8')); } catch { /* optional */ } const list = new Map(); let files = []; try { files = fs.readdirSync(GAMES_DIR); } catch { /* no games yet */ } for (const f of files.sort()) { if (!/\.(z[345678]|zblorb|zlb)$/i.test(f)) continue; const id = f.replace(/\.[^.]+$/, '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); try { const story = readStory(path.join(GAMES_DIR, f)); if (![3, 4, 5, 7, 8].includes(story[0])) { console.warn(`skip ${f}: version ${story[0]}`); continue; } const m = meta[id] || {}; list.set(id, { id, file: f, story, title: m.title || f.replace(/\.[^.]+$/, '').replace(/[_-]+/g, ' ') }); } catch (e) { console.warn(`skip ${f}: ${e.message}`); } } gameCache = list; return list; } // ------------------------------------------------------------------ state const validSession = (s) => /^[A-Za-z0-9_-]{8,64}$/.test(s); const sessionDir = (sid) => path.join(DATA_DIR, sid); const stateFile = (sid, gid) => path.join(sessionDir(sid), gid + '.json'); const transcriptFile = (sid, gid) => path.join(sessionDir(sid), gid + '.txt'); Next: 12 Companion server.js (4/13)