File: examples/plugins/text-adventures/plugin.js (84 lines)
// text-adventures: web setup for the on-device game catalog (device.json).
//
// Writes config.json {server, session}. device.json uses both:
// {cfg.server} the companion server (examples/companions/text-adventures)
// {cfg.session} a random id for this reader: the server keeps its saved games under it
//
// The game itself never runs here. The server plays it, and the reader shows the
// turns as catalog rows.
const REQUIRED_CAPS = ['catalog.lists', 'catalog.list_index_mapping', 'catalog.item_details', 'catalog.server_paths'];
CrossPoint.registerPlugin(async (container, api) => {
const $ = (s) => container.querySelector(s);
const b64 = (t) => { let s = ''; for (const c of new TextEncoder().encode(t)) s += String.fromCharCode(c); return btoa(s); };
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
const newSession = () => Array.from(crypto.getRandomValues(new Uint8Array(12)), (b) => b.toString(16).padStart(2, '0')).join('');
async function readConfig() {
try { const r = await fetch(api.pluginFile('config.json')); return r.ok ? await r.json() : {}; } catch (e) { return {}; }
}
const writeConfig = (cfg) => api.writeFile(`${api.dir}/config.json`, b64(JSON.stringify(cfg)));