gm surface: home flow, run identity, full-assembly editor, notepad, wrap replay; sigils and dateless rings
This commit is contained in:
+92
-42
@@ -9,9 +9,11 @@
|
||||
// commands:
|
||||
// packages list packages (presets) and their flags
|
||||
// runs list runs (instances) and their fill levels
|
||||
// create <package> <run-id> create a run: copy the package wholesale
|
||||
// create <package> <pc-name> create a run: copy the package wholesale
|
||||
// (shared prompts included — battery
|
||||
// included), init the db, render the views.
|
||||
// included), give it an anonymous id and
|
||||
// the pc's name (distinct per package),
|
||||
// init the db, render the views.
|
||||
// From then on the run dir is the only
|
||||
// truth; the package is never read again.
|
||||
// init create the run's database and stubs
|
||||
@@ -35,6 +37,7 @@
|
||||
// turn; exact match, non-blocking; JSON result
|
||||
// (lost / gained / conflicts / paper)
|
||||
// paper-set "descr" ... GM override: replace the whole paper
|
||||
// (no descrs = clear it)
|
||||
// paper-ingest <file> read a file's <status> block into the paper
|
||||
// (turn-0 at new-game)
|
||||
// pick "a" "b" "c" ... dice for prose — uniform random choice
|
||||
@@ -46,6 +49,7 @@
|
||||
// status counts, caps, the paper, what's sinking
|
||||
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
@@ -86,7 +90,7 @@ const [cmd, ...args] = argv;
|
||||
const NO_RUN_NEEDED = new Set(['runs', 'packages', 'create', 'pick']);
|
||||
const RUN = NO_RUN_NEEDED.has(cmd) ? null : runOverride;
|
||||
if (!NO_RUN_NEEDED.has(cmd) && (!RUN || !/^[a-z0-9-]+$/.test(RUN))) {
|
||||
fail('no run specified — use --run <id> (see: runs); runs are created with: create <package> <run-id>');
|
||||
fail('no run specified — use --run <id> (see: runs); runs are created with: create <package> <pc-name>');
|
||||
}
|
||||
|
||||
// per-run paths: set once from --run, and re-pointed by create (which
|
||||
@@ -174,7 +178,7 @@ function renderRing(db, ring) {
|
||||
];
|
||||
if (rows.length === 0) lines.push('(empty — nothing here yet)');
|
||||
for (const r of rows) {
|
||||
lines.push(`- [e${r.id}] [${r.last_touched}] [${r.type}] **${r.label}**${r.detail ? ` — ${r.detail}` : '.'}`);
|
||||
lines.push(`- [e${r.id}] [${r.type}] **${r.label}**${r.detail ? ` — ${r.detail}` : '.'}`);
|
||||
}
|
||||
lines.push('');
|
||||
const file = path.join(CANON_DIR, `ring-${ring}.md`);
|
||||
@@ -238,10 +242,14 @@ function dumpDb(db) {
|
||||
return file;
|
||||
}
|
||||
|
||||
function insertEntry(db, { ring, type, label, detail, date, win, reason }) {
|
||||
// stamps are mechanical (real, internal, never rendered): they drive
|
||||
// the LRU — overflow, view order, "what's sinking". Story time lives
|
||||
// in the prose of the entries, never in a column.
|
||||
function insertEntry(db, { ring, type, label, detail, win, reason }) {
|
||||
const now = today();
|
||||
const res = db.prepare(
|
||||
'INSERT INTO entries (ring, type, label, detail, created, last_touched) VALUES (?, ?, ?, ?, ?, ?)'
|
||||
).run(ring, type, norm(label), norm(detail), date, date);
|
||||
).run(ring, type, norm(label), norm(detail), now, now);
|
||||
const id = Number(res.lastInsertRowid);
|
||||
journal(db, win, 'insert', `e${id}`, reason);
|
||||
return id;
|
||||
@@ -315,7 +323,6 @@ function cmdSeed(force, seedPathArg) {
|
||||
}
|
||||
|
||||
const errors = [];
|
||||
if (!sf.date || !/^\d{4}-\d{2}-\d{2}$/.test(sf.date)) errors.push('date must be YYYY-MM-DD (the story-start date)');
|
||||
const entries = sf.entries ?? [];
|
||||
const records = sf.records ?? [];
|
||||
if (!Array.isArray(entries)) { errors.push('entries must be an array'); }
|
||||
@@ -344,14 +351,15 @@ function cmdSeed(force, seedPathArg) {
|
||||
|
||||
db.exec('BEGIN');
|
||||
try {
|
||||
const now = today();
|
||||
for (const e of entries) {
|
||||
const res = db.prepare('INSERT INTO entries (ring, type, label, detail, created, last_touched) VALUES (?, ?, ?, ?, ?, ?)')
|
||||
.run(e.ring, e.type, norm(e.label), norm(e.detail ?? ''), sf.date, sf.date);
|
||||
.run(e.ring, e.type, norm(e.label), norm(e.detail ?? ''), now, now);
|
||||
journal(db, 'seed', 'insert', `e${Number(res.lastInsertRowid)}`, 'seeded');
|
||||
}
|
||||
for (const r of records) {
|
||||
const res = db.prepare('INSERT INTO records (kind, label, detail, created) VALUES (?, ?, ?, ?)')
|
||||
.run(norm(r.kind), norm(r.label), norm(r.detail ?? ''), sf.date);
|
||||
.run(norm(r.kind), norm(r.label), norm(r.detail ?? ''), now);
|
||||
journal(db, 'seed', 'record', `r${Number(res.lastInsertRowid)}`, 'seeded');
|
||||
}
|
||||
db.exec('COMMIT');
|
||||
@@ -360,7 +368,7 @@ function cmdSeed(force, seedPathArg) {
|
||||
fail(`seed failed, rolled back: ${e.message}`);
|
||||
}
|
||||
|
||||
console.log(`seeded [${RUN}]: ${entries.length} entries, ${records.length} records (as of ${sf.date})`);
|
||||
console.log(`seeded [${RUN}]: ${entries.length} entries, ${records.length} records`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
@@ -376,14 +384,10 @@ function cmdApply(manifestPath) {
|
||||
|
||||
const { errors, need, asArray, ref } = makeValidator(db);
|
||||
need(mf.window, 'window');
|
||||
need(mf.date, 'date');
|
||||
need(mf.summary, 'summary');
|
||||
if (typeof mf.window === 'string' && !/^[a-z0-9-]+$/i.test(mf.window)) {
|
||||
errors.push('window must be a short slug (letters, digits, dashes)');
|
||||
}
|
||||
if (typeof mf.date === 'string' && !/^\d{4}-\d{2}-\d{2}$/.test(mf.date)) {
|
||||
errors.push('date must be YYYY-MM-DD');
|
||||
}
|
||||
const logFile = mf.window ? path.join(LOG_DIR, `${mf.window}.md`) : null;
|
||||
if (logFile && fs.existsSync(logFile)) {
|
||||
errors.push(`${rel(logFile)} already exists — delete it first if this is a re-run`);
|
||||
@@ -441,11 +445,11 @@ function cmdApply(manifestPath) {
|
||||
for (const e of ins) {
|
||||
const id = insertEntry(db, {
|
||||
ring: e.ring, type: e.type, label: e.label, detail: e.detail ?? '',
|
||||
date: mf.date, win: mf.window, reason: e.reason,
|
||||
win: mf.window, reason: e.reason,
|
||||
});
|
||||
applied.insert.push({ id, ...e });
|
||||
}
|
||||
const touchDate = (id) => db.prepare('UPDATE entries SET last_touched = ? WHERE id = ?').run(mf.date, id);
|
||||
const touchDate = (id) => db.prepare('UPDATE entries SET last_touched = ? WHERE id = ?').run(today(), id);
|
||||
const fetch = (id) => db.prepare('SELECT * FROM entries WHERE id = ?').get(id);
|
||||
for (const e of touch) {
|
||||
const id = parseId(e.id);
|
||||
@@ -455,19 +459,19 @@ function cmdApply(manifestPath) {
|
||||
}
|
||||
for (const e of promote) {
|
||||
const id = parseId(e.id);
|
||||
db.prepare("UPDATE entries SET ring = 'a', last_touched = ? WHERE id = ?").run(mf.date, id);
|
||||
db.prepare("UPDATE entries SET ring = 'a', last_touched = ? WHERE id = ?").run(today(), id);
|
||||
journal(db, mf.window, 'promote', `e${id}`, e.reason);
|
||||
applied.promote.push({ row: fetch(id), reason: e.reason });
|
||||
}
|
||||
for (const e of demote) {
|
||||
const id = parseId(e.id);
|
||||
db.prepare("UPDATE entries SET ring = 'b', last_touched = ? WHERE id = ?").run(mf.date, id);
|
||||
db.prepare("UPDATE entries SET ring = 'b', last_touched = ? WHERE id = ?").run(today(), id);
|
||||
journal(db, mf.window, 'demote', `e${id}`, e.reason);
|
||||
applied.demote.push({ row: fetch(id), reason: e.reason });
|
||||
}
|
||||
for (const e of reword) {
|
||||
const id = parseId(e.id);
|
||||
db.prepare('UPDATE entries SET detail = ?, last_touched = ? WHERE id = ?').run(norm(e.detail), mf.date, id);
|
||||
db.prepare('UPDATE entries SET detail = ?, last_touched = ? WHERE id = ?').run(norm(e.detail), today(), id);
|
||||
journal(db, mf.window, 'reword', `e${id}`, e.reason);
|
||||
applied.reword.push({ row: fetch(id), detail: e.detail, reason: e.reason });
|
||||
}
|
||||
@@ -559,9 +563,11 @@ function cmdInit() {
|
||||
|
||||
const PACKAGE_FILES = ['backdrop.md', 'turn-0.md', 'character.md', 'seed.json', 'settings.json', 'gm.md'];
|
||||
// battery included, copied per run and then the run's to edit: the craft
|
||||
// tier and the housekeeping prompt. system.md and invariants.md are
|
||||
// repo-fed at assembly (opening+guards, substrate facts) — never copied
|
||||
const SHARED_PROMPTS = ['handbook.md', 'housekeeper.md'];
|
||||
// tier. housekeeper.md is substrate-internal like invariants.md —
|
||||
// repo-fed at wrap time, never copied, never user-exposed; system.md
|
||||
// and invariants.md are likewise repo-fed at assembly (opening+guards,
|
||||
// substrate facts)
|
||||
const SHARED_PROMPTS = ['handbook.md'];
|
||||
|
||||
function cmdPackages() {
|
||||
if (!fs.existsSync(PACKAGES_DIR)) { if (AS_JSON) console.log('[]'); else console.log('(no packages)'); return; }
|
||||
@@ -570,29 +576,64 @@ function cmdPackages() {
|
||||
if (!d.isDirectory()) continue;
|
||||
const p = path.join(PACKAGES_DIR, d.name);
|
||||
const has = (f) => fs.existsSync(path.join(p, f));
|
||||
let records = false;
|
||||
try { records = JSON.parse(fs.readFileSync(path.join(p, 'settings.json'), 'utf8')).records === true; } catch {}
|
||||
let settings = {};
|
||||
try { settings = JSON.parse(fs.readFileSync(path.join(p, 'settings.json'), 'utf8')); } catch {}
|
||||
list.push({
|
||||
name: d.name, records,
|
||||
name: d.name,
|
||||
display: settings.name ?? d.name,
|
||||
pc: settings.pc ?? null,
|
||||
records: settings.records === true,
|
||||
turn0: has('turn-0.md'), seed: has('seed.json'),
|
||||
complete: has('backdrop.md') && has('character.md'),
|
||||
});
|
||||
}
|
||||
if (AS_JSON) { console.log(JSON.stringify(list)); return; }
|
||||
for (const x of list) {
|
||||
console.log(`${x.name}: records ${x.records ? 'on' : 'off'} · turn-0 ${x.turn0 ? 'yes' : 'no'} · seed ${x.seed ? 'yes' : 'no'} · backdrop+card ${x.complete ? 'ok' : 'MISSING'}`);
|
||||
console.log(`${x.name}: ${x.display} — records ${x.records ? 'on' : 'off'} · turn-0 ${x.turn0 ? 'yes' : 'no'} · seed ${x.seed ? 'yes' : 'no'} · backdrop+card ${x.complete ? 'ok' : 'MISSING'}`);
|
||||
}
|
||||
}
|
||||
|
||||
function cmdCreate(pkgName, runId, model) {
|
||||
if (!pkgName || !runId || !/^[a-z0-9-]+$/.test(runId)) {
|
||||
fail('usage: create <package> <run-id> ["model"]');
|
||||
// run ids are anonymous: they carry no facts, so there is nothing to
|
||||
// rename and nothing for a name to collide with. The names that
|
||||
// matter — the package display name and the pc's name — live in the
|
||||
// run's settings copy and drive the display ("La Caramba / Rell").
|
||||
const readRunSettings = (runDir) => {
|
||||
try { return JSON.parse(fs.readFileSync(path.join(runDir, 'settings.json'), 'utf8')); } catch { return {}; }
|
||||
};
|
||||
const runDisplay = (pkg, settings) =>
|
||||
settings.pc ? `${settings.name ?? pkg} / ${settings.pc}` : (settings.name ?? pkg);
|
||||
|
||||
function anonId() {
|
||||
for (;;) {
|
||||
const id = randomBytes(4).toString('hex'); // 8 hex chars
|
||||
if (!fs.existsSync(path.join(RUNS_DIR, id))) return id;
|
||||
}
|
||||
}
|
||||
|
||||
function cmdCreate(pkgName, pc, displayName, model) {
|
||||
if (!pkgName || !/^[a-z0-9-]+$/.test(pkgName) || !pc || !String(pc).trim()) {
|
||||
fail('usage: create <package> <pc-name> ["package display name"] ["model"]');
|
||||
}
|
||||
const pkgDir = path.join(PACKAGES_DIR, pkgName);
|
||||
if (!fs.existsSync(pkgDir)) fail(`no such package: ${pkgName} — see: packages`);
|
||||
const runDir = path.join(RUNS_DIR, runId);
|
||||
if (fs.existsSync(runDir)) fail(`run '${runId}' already exists — a disposable run is deleted, not recreated (Remove-Item ${rel(runDir)})`);
|
||||
pc = String(pc).trim();
|
||||
// different runs of the same package require different pc names
|
||||
if (fs.existsSync(RUNS_DIR)) {
|
||||
for (const d of fs.readdirSync(RUNS_DIR, { withFileTypes: true })) {
|
||||
if (!d.isDirectory()) continue;
|
||||
const rd = path.join(RUNS_DIR, d.name);
|
||||
let meta = null;
|
||||
try { meta = JSON.parse(fs.readFileSync(path.join(rd, 'run.json'), 'utf8')); } catch { continue; }
|
||||
if (meta?.package !== pkgName) continue;
|
||||
const st = readRunSettings(rd);
|
||||
if (String(st.pc ?? '').toLowerCase() === pc.toLowerCase()) {
|
||||
fail(`a run of '${pkgName}' named '${pc}' already exists: ${d.name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const runId = anonId();
|
||||
const runDir = path.join(RUNS_DIR, runId);
|
||||
fs.mkdirSync(runDir, { recursive: true });
|
||||
for (const f of PACKAGE_FILES) {
|
||||
const src = path.join(pkgDir, f);
|
||||
@@ -602,6 +643,12 @@ function cmdCreate(pkgName, runId, model) {
|
||||
const src = path.join(ROOT, f);
|
||||
if (fs.existsSync(src)) fs.copyFileSync(src, path.join(runDir, f));
|
||||
}
|
||||
// first-class identity lands in the settings copy: the pc name is
|
||||
// this run's own; the display name defaults to the package's
|
||||
const settings = readRunSettings(runDir);
|
||||
settings.pc = pc;
|
||||
settings.name = displayName?.trim() || settings.name || pkgName;
|
||||
fs.writeFileSync(path.join(runDir, 'settings.json'), JSON.stringify(settings, null, 2) + '\n');
|
||||
const meta = { package: pkgName, created: today() };
|
||||
if (model) meta.model = model;
|
||||
fs.writeFileSync(path.join(runDir, 'run.json'), JSON.stringify(meta, null, 2) + '\n');
|
||||
@@ -612,14 +659,15 @@ function cmdCreate(pkgName, runId, model) {
|
||||
setRunRoot(runDir); // views and dump land inside the new run
|
||||
const db = new DatabaseSync(DB_PATH);
|
||||
db.exec(fs.readFileSync(SCHEMA_PATH, 'utf8'));
|
||||
console.log(`created run '${runId}' from package '${pkgName}': ${rel(runDir)}`);
|
||||
if (AS_JSON) { console.log(JSON.stringify({ id: runId, package: pkgName, name: settings.name, pc })); return; }
|
||||
console.log(`created run '${runId}' — ${runDisplay(pkgName, settings)}: ${rel(runDir)}`);
|
||||
console.log(` seed rides the copy (${fs.existsSync(path.join(runDir, 'seed.json')) ? 'present' : 'absent'}), editable until the first window-open applies it`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
|
||||
function cmdRuns() {
|
||||
if (!fs.existsSync(RUNS_DIR)) { if (AS_JSON) console.log('[]'); else console.log('(no runs — create one: create <package> <run-id>)'); return; }
|
||||
if (!fs.existsSync(RUNS_DIR)) { if (AS_JSON) console.log('[]'); else console.log('(no runs — create one: create <package> <pc-name>)'); return; }
|
||||
const list = [];
|
||||
let found = 0;
|
||||
for (const d of fs.readdirSync(RUNS_DIR, { withFileTypes: true })) {
|
||||
@@ -634,19 +682,21 @@ function cmdRuns() {
|
||||
pkg = meta.package ?? '?';
|
||||
model = meta.model ?? null;
|
||||
} catch {}
|
||||
const st = readRunSettings(runDir);
|
||||
const base = { id: d.name, package: pkg, model, pc: st.pc ?? null, display: runDisplay(pkg, st) };
|
||||
const dbPath = path.join(runDir, 'db', 'memory.db');
|
||||
if (!fs.existsSync(dbPath)) { list.push({ id: d.name, package: pkg, model, initialized: false }); continue; }
|
||||
if (!fs.existsSync(dbPath)) { list.push({ ...base, initialized: false }); continue; }
|
||||
const db = new DatabaseSync(dbPath);
|
||||
db.exec(fs.readFileSync(SCHEMA_PATH, 'utf8'));
|
||||
const n = (ring) => db.prepare('SELECT COUNT(*) AS n FROM entries WHERE ring = ?').get(ring).n;
|
||||
list.push({ id: d.name, package: pkg, model, initialized: true, fill: { a: n('a'), b: n('b'), cold: n('cold') } });
|
||||
list.push({ ...base, initialized: true, fill: { a: n('a'), b: n('b'), cold: n('cold') } });
|
||||
}
|
||||
if (AS_JSON) { console.log(JSON.stringify(list)); return; }
|
||||
for (const x of list) {
|
||||
if (!x.initialized) { console.log(`${x.id} [${x.package}]: (not initialized)`); continue; }
|
||||
console.log(`${x.id} [${x.package}]: a ${x.fill.a}/${CAPS.a} · b ${x.fill.b}/${CAPS.b} · cold ${x.fill.cold}`);
|
||||
if (!x.initialized) { console.log(`${x.id} [${x.package}]: ${x.display} (not initialized)`); continue; }
|
||||
console.log(`${x.id} [${x.package}]: ${x.display} — a ${x.fill.a}/${CAPS.a} · b ${x.fill.b}/${CAPS.b} · cold ${x.fill.cold}`);
|
||||
}
|
||||
if (!found) console.log('(no runs — create one: create <package> <run-id>)');
|
||||
if (!found) console.log('(no runs — create one: create <package> <pc-name>)');
|
||||
}
|
||||
|
||||
function today() { return new Date().toISOString().slice(0, 10); }
|
||||
@@ -659,7 +709,7 @@ function gmOp(ring, type, label, detail) {
|
||||
db.exec('BEGIN');
|
||||
let id;
|
||||
try {
|
||||
id = insertEntry(db, { ring, type, label, detail: detail ?? '', date: today(), win: 'gm', reason: 'GM edit' });
|
||||
id = insertEntry(db, { ring, type, label, detail: detail ?? '', win: 'gm', reason: 'GM edit' });
|
||||
db.exec('COMMIT');
|
||||
} catch (e) {
|
||||
db.exec('ROLLBACK');
|
||||
@@ -871,7 +921,7 @@ function cmdSearch(terms) {
|
||||
const params = terms.map((t) => `%${t}%`);
|
||||
const rows = db.prepare(`SELECT * FROM records WHERE ${conds} ORDER BY id LIMIT 10`).all(...params);
|
||||
if (!rows.length) { console.log('(no records match)'); return; }
|
||||
for (const r of rows) console.log(`[r${r.id}] [${r.kind}] **${r.label}**${r.detail ? ` — ${r.detail}` : ''} (${r.created})`);
|
||||
for (const r of rows) console.log(`[r${r.id}] [${r.kind}] **${r.label}**${r.detail ? ` — ${r.detail}` : ''}`);
|
||||
const total = db.prepare(`SELECT COUNT(*) AS n FROM records WHERE ${conds}`).get(...params).n;
|
||||
if (total > rows.length) console.log(`…${total - rows.length} more omitted — narrow the query`);
|
||||
}
|
||||
@@ -940,7 +990,7 @@ function cmdStatus() {
|
||||
switch (cmd) {
|
||||
case 'packages': cmdPackages(); break;
|
||||
case 'runs': cmdRuns(); break;
|
||||
case 'create': cmdCreate(args[0], args[1], args[2]); break;
|
||||
case 'create': cmdCreate(args[0], args[1], args[2], args[3]); break;
|
||||
case 'seed': cmdSeed(FORCE, args[0]); break;
|
||||
case 'init': cmdInit(); break;
|
||||
case 'render': renderAll(openDb()); break;
|
||||
@@ -953,7 +1003,7 @@ switch (cmd) {
|
||||
case 'discard': (args.length >= 1) ? cmdDiscard(args[0], args[1]) : fail('usage: discard <rid> "reason"'); break;
|
||||
case 'paper': cmdPaper(); break;
|
||||
case 'paper-apply': (args.length >= 1) ? cmdPaperApply(args.join(' ')) : fail('usage: paper-apply "<turn text>"'); break;
|
||||
case 'paper-set': (args.length >= 1) ? cmdPaperSet(args) : fail('usage: paper-set "descr" ["descr" ...]'); break;
|
||||
case 'paper-set': cmdPaperSet(args); break; // no args = clear the paper (a legitimate GM override)
|
||||
case 'paper-ingest': (args.length === 1) ? cmdPaperIngest(args[0]) : fail('usage: paper-ingest <file>'); break;
|
||||
case 'pick': cmdPick(args); break;
|
||||
case 'search': cmdSearch(args); break;
|
||||
|
||||
Reference in New Issue
Block a user