status: baselines with exception reporting, growth as baseline shift, pick for random fields
This commit is contained in:
+66
-16
@@ -21,8 +21,11 @@
|
||||
// discard <rid> "reason" the PC discards a record (--gm likewise)
|
||||
// search <terms...> query the records (max 10; a miss is a miss)
|
||||
// unarchive <rid> [a|b] GM pulls a record back into the rings
|
||||
// set <key> "value" the PC updates her status (--gm to override)
|
||||
// unset <key> the PC clears a status key
|
||||
// set <key> "value" the PC declares an excursion (--gm to override)
|
||||
// norm <key> "baseline" the PC adopts/shifts a baseline (growth)
|
||||
// clear <key> excursion ends — back to baseline
|
||||
// unset <key> the PC drops a field from her paper
|
||||
// pick "a" "b" "c" ... dice for prose — uniform random choice
|
||||
// dump canonical dump to dumps/memory.sql
|
||||
// (also auto-written by every mutation)
|
||||
// restore --force rebuild the db from dumps/memory.sql
|
||||
@@ -73,8 +76,8 @@ function resolveRun() {
|
||||
return null;
|
||||
}
|
||||
|
||||
const RUN = cmd === 'runs' ? null : resolveRun();
|
||||
if (cmd !== 'runs' && (!RUN || !/^[a-z0-9-]+$/.test(RUN))) {
|
||||
const RUN = (cmd === 'runs' || cmd === 'pick') ? null : resolveRun();
|
||||
if (cmd !== 'runs' && cmd !== 'pick' && (!RUN || !/^[a-z0-9-]+$/.test(RUN))) {
|
||||
fail('no run specified — use --run <name> or {"run":"name"} in config.json');
|
||||
}
|
||||
|
||||
@@ -174,14 +177,18 @@ function renderStatus(db) {
|
||||
const rows = db.prepare('SELECT * FROM status ORDER BY updated DESC, key').all();
|
||||
const lines = [
|
||||
'<!-- GENERATED by tool/memory.mjs render — do not edit by hand. -->',
|
||||
"<!-- The player's own current state, maintained via ((status key: value)) in OOC. -->",
|
||||
'<!-- Stances are feelings; facts live in the rings. GM overrides are rare and journaled. -->',
|
||||
"<!-- The player's own piece of paper: baselines are the defaults she plays to; -->",
|
||||
'<!-- excursions are declared and cleared. Stances are feelings; facts live in the rings. -->',
|
||||
'',
|
||||
'# Status',
|
||||
'',
|
||||
];
|
||||
if (!rows.length) lines.push('(nothing set — fresh start)');
|
||||
for (const r of rows) lines.push(`- **${r.key}:** ${r.value}`);
|
||||
for (const r of rows) {
|
||||
const shown = r.value || r.normal;
|
||||
const ex = r.value && r.value !== r.normal && r.normal ? ` *(norm: ${r.normal})*` : '';
|
||||
lines.push(`- **${r.key}:** ${shown}${ex}`);
|
||||
}
|
||||
lines.push('');
|
||||
const file = path.join(CANON_DIR, 'status.md');
|
||||
fs.mkdirSync(CANON_DIR, { recursive: true });
|
||||
@@ -216,7 +223,7 @@ function dumpDb(db) {
|
||||
L.push(`INSERT INTO records VALUES (${r.id},${q(r.kind)},${q(r.label)},${q(r.detail)},${q(r.created)});`);
|
||||
}
|
||||
for (const r of db.prepare('SELECT * FROM status ORDER BY key').all()) {
|
||||
L.push(`INSERT INTO status VALUES (${q(r.key)},${q(r.value)},${q(r.updated)});`);
|
||||
L.push(`INSERT INTO status VALUES (${q(r.key)},${q(r.normal)},${q(r.value)},${q(r.updated)});`);
|
||||
}
|
||||
for (const r of db.prepare('SELECT * FROM events ORDER BY id').all()) {
|
||||
L.push(`INSERT INTO events VALUES (${r.id},${q(r.window)},${q(r.op)},${q(r.target)},${q(r.reason)});`);
|
||||
@@ -319,7 +326,12 @@ function cmdSeed(force, seedPathArg) {
|
||||
}
|
||||
if (!Array.isArray(status) && status && typeof status === 'object') {
|
||||
for (const [k, v] of Object.entries(status)) {
|
||||
if (!k.trim() || typeof v !== 'string' || !v.trim()) errors.push(`status["${k}"] must be a non-empty string`);
|
||||
const okStr = typeof v === 'string' && v.trim();
|
||||
const okObj = v && typeof v === 'object' && !Array.isArray(v)
|
||||
&& (typeof v.normal === 'string' || typeof v.value === 'string');
|
||||
if (!k.trim() || (!okStr && !okObj)) {
|
||||
errors.push(`status["${k}"] must be a non-empty string (normal and starting value) or {"normal", "value"}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const [i, r] of (Array.isArray(records) ? records : []).entries()) {
|
||||
@@ -343,8 +355,10 @@ function cmdSeed(force, seedPathArg) {
|
||||
journal(db, 'seed', 'insert', `e${Number(res.lastInsertRowid)}`, 'seeded');
|
||||
}
|
||||
for (const [k, v] of Object.entries(status)) {
|
||||
db.prepare('INSERT INTO status (key, value, updated) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated = excluded.updated')
|
||||
.run(norm(k), norm(v), sf.date);
|
||||
const n = typeof v === 'string' ? v : (v.normal ?? '');
|
||||
const val = typeof v === 'string' ? v : (v.value ?? '');
|
||||
db.prepare("INSERT INTO status (key, normal, value, updated) VALUES (?, ?, ?, ?) ON CONFLICT(key) DO UPDATE SET normal = excluded.normal, value = excluded.value, updated = excluded.updated")
|
||||
.run(norm(k), norm(n), norm(val), sf.date);
|
||||
journal(db, 'seed', 'status', `s:${norm(k)}`, 'seeded');
|
||||
}
|
||||
for (const r of records) {
|
||||
@@ -664,25 +678,57 @@ function cmdDiscard(ridStr, reason) {
|
||||
function cmdSet(key, value, reason) {
|
||||
const db = openDb();
|
||||
if (!key?.trim() || !value?.trim()) fail('usage: set <key> "value" ["reason"]');
|
||||
db.prepare('INSERT INTO status (key, value, updated) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated = excluded.updated')
|
||||
db.prepare("INSERT INTO status (key, normal, value, updated) VALUES (?, '', ?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated = excluded.updated")
|
||||
.run(norm(key), norm(value), today());
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${norm(key)}`, reason ?? (AS_GM ? 'GM override' : 'player declaration'));
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${norm(key)}`, reason ?? (AS_GM ? 'GM override' : 'excursion declared'));
|
||||
console.log(`status ${norm(key)}: ${norm(value)}`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
|
||||
function cmdNorm(key, baseline, reason) {
|
||||
const db = openDb();
|
||||
if (!key?.trim() || !baseline?.trim()) fail('usage: norm <key> "baseline" ["reason"]');
|
||||
db.prepare("INSERT INTO status (key, normal, value, updated) VALUES (?, ?, '', ?) ON CONFLICT(key) DO UPDATE SET normal = excluded.normal, updated = excluded.updated")
|
||||
.run(norm(key), norm(baseline), today());
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${norm(key)}`, reason ?? (AS_GM ? 'GM override' : 'baseline set'));
|
||||
console.log(`status ${norm(key)} — norm: ${norm(baseline)}`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
|
||||
function cmdClear(key) {
|
||||
const db = openDb();
|
||||
const row = db.prepare('SELECT * FROM status WHERE key = ?').get(norm(key ?? ''));
|
||||
if (!row) fail(`no such status key: ${key}`);
|
||||
if (!row.normal) {
|
||||
db.prepare('DELETE FROM status WHERE key = ?').run(row.key);
|
||||
} else {
|
||||
db.prepare("UPDATE status SET value = '', updated = ? WHERE key = ?").run(today(), row.key);
|
||||
}
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${row.key}`, 'returned to baseline');
|
||||
console.log(`status ${row.key}: back to ${row.normal || '—'}`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
|
||||
function cmdUnset(key) {
|
||||
const db = openDb();
|
||||
const row = db.prepare('SELECT * FROM status WHERE key = ?').get(norm(key ?? ''));
|
||||
if (!row) fail(`no such status key: ${key}`);
|
||||
db.prepare('DELETE FROM status WHERE key = ?').run(row.key);
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${row.key}`, 'cleared');
|
||||
console.log(`status ${row.key}: cleared`);
|
||||
journal(db, AS_GM ? 'gm' : 'pc', 'status', `s:${row.key}`, 'field dropped');
|
||||
console.log(`status ${row.key}: dropped`);
|
||||
renderAll(db);
|
||||
dumpDb(db);
|
||||
}
|
||||
|
||||
function cmdPick(options) {
|
||||
if (options.length < 2) fail('usage: pick "option one" "option two" [...]');
|
||||
const choice = options[Math.floor(Math.random() * options.length)];
|
||||
console.log(`picked: ${choice}`);
|
||||
}
|
||||
|
||||
function cmdSearch(terms) {
|
||||
const db = openDb();
|
||||
if (!recordsEnabled()) fail('this character keeps no records — nothing to consult');
|
||||
@@ -718,7 +764,8 @@ function cmdStatus() {
|
||||
console.log(`run: ${RUN}${recordsEnabled() ? '' : ' (no records)'}`);
|
||||
const nr = db.prepare('SELECT COUNT(*) AS n FROM records').get().n;
|
||||
console.log(`records: ${nr}`);
|
||||
const sk = db.prepare('SELECT key FROM status ORDER BY updated DESC, key').all().map((r) => r.key);
|
||||
const sk = db.prepare('SELECT * FROM status ORDER BY updated DESC, key').all()
|
||||
.map((r) => r.key + (r.value && r.value !== r.normal && r.normal ? '!' : ''));
|
||||
console.log(`status: ${sk.length ? sk.join(', ') : '(nothing set)'}`);
|
||||
for (const ring of ['a', 'b', 'cold']) {
|
||||
const n = db.prepare('SELECT COUNT(*) AS n FROM entries WHERE ring = ?').get(ring).n;
|
||||
@@ -748,7 +795,10 @@ switch (cmd) {
|
||||
case 'amend': (args.length >= 2) ? cmdAmend(args[0], args[1]) : fail('usage: amend <rid> "new detail"'); break;
|
||||
case 'discard': (args.length >= 1) ? cmdDiscard(args[0], args[1]) : fail('usage: discard <rid> "reason"'); break;
|
||||
case 'set': (args.length >= 2) ? cmdSet(args[0], args[1], args[2]) : fail('usage: set <key> "value" ["reason"]'); break;
|
||||
case 'norm': (args.length >= 2) ? cmdNorm(args[0], args[1], args[2]) : fail('usage: norm <key> "baseline" ["reason"]'); break;
|
||||
case 'clear': (args.length >= 1) ? cmdClear(args[0]) : fail('usage: clear <key>'); break;
|
||||
case 'unset': (args.length >= 1) ? cmdUnset(args[0]) : fail('usage: unset <key>'); break;
|
||||
case 'pick': cmdPick(args); break;
|
||||
case 'search': cmdSearch(args); break;
|
||||
case 'unarchive': (args.length >= 1) ? cmdUnarchive(args[0], args[1]) : fail('usage: unarchive <rid> [a|b]'); break;
|
||||
case 'status': cmdStatus(); break;
|
||||
|
||||
Reference in New Issue
Block a user