feat: encounter UX, tracked effects, and character state improvements #10

Open
keen wants to merge 14 commits from fix/encounter-ux into main
pull from: fix/encounter-ux
2 changed files with 94 additions and 2 deletions
Showing only changes of commit dda8ed9790 - Show all commits

No files matched your search

@@ -53,6 +53,47 @@ Returns JSON array of campaign docs = server mode confirmed. HTML (SPA shell)
= firebase mode OR backend not mounted. Either way, this skill won't work — = firebase mode OR backend not mounted. Either way, this skill won't work —
tell the user. tell the user.
## Spec files
Spec files are EPHEMERAL. /tmp is fine — they get cleaned, that's expected.
- Source of truth for stats = the user's message at ask-time. Spec JSON is a
transit format, not an archive.
- If a spec is gone, rebuild it from the original message. Never reconstruct
from memory.
- **The durable record is the server DB.** Encounter docs are self-describing:
participant name, type, hpFormula, maxHp, ac, initiative, status all stored.
"Fix the thing from 2 weeks ago" → query `campaigns/{cid}/encounters`, read
the docs, reconstruct context from live data. `logs/{id}` entries add the
action history.
- Therefore: store enough in the DB to reconstruct later. hpFormula always
kept on monster/npc participants; never strip it. Never write campaign or
encounter data into the tracker repo.
## Spec vetting (before build)
Validate the spec before writing anything. Reject, don't guess:
- **Monster/npc with neither `maxHp` nor `hpFormula`** → hard error. A silent
default of 10 HP has caused real damage; never fall back to defaults.
Escape hatch: `--allow-defaults` flag permits it with a loud warning.
- **Missing `ac`** → warning (build continues).
- **Missing `initMod` and `initiative`** → warning; initMod defaults 0.
- **`type: "character"` in spec** → see PC rules below.
- **Duplicate participant names within one encounter** → allowed but warn
(numbered names preferred for display clarity).
## PC rules (hard)
- **Allowed:** add a PC to the campaign roster (`players[]`), or add a PC
from the roster into an encounter, copying stats verbatim from the roster.
- **Forbidden:** creating a `character` participant with invented stats, or
editing any existing character-type participant's HP, AC, formula,
conditions, or status. Player stat fixes happen in the UI or from the
user's explicit values — tooling never "repairs" PCs.
- A build spec containing `type: "character"` without `originalCharacterId`
resolving to a roster entry → hard error.
## Data model (read this before writing) ## Data model (read this before writing)
Three nested entities, all opaque JSON docs in a path-keyed KV store: Three nested entities, all opaque JSON docs in a path-keyed KV store:
@@ -21,11 +21,23 @@
// initiative: optional manual override. If omitted, rolled d20 + initMod. // initiative: optional manual override. If omitted, rolled d20 + initMod.
// initMod default 0. maxHp default 10. // initMod default 0. maxHp default 10.
// //
// VALIDATION (hard rules):
// - monster/npc with neither maxHp nor hpFormula = FATAL. Defaults caused real
// damage once; never silently assume 10 HP. --allow-defaults to override.
// - character type in spec = FATAL unless --allow-pc-add, which requires
// originalCharacterId + copies stats from the campaign roster verbatim.
// - missing ac or initMod = warning, build continues.
//
// Output: written encounter ids + rolled initiatives. Exit 0 on success. // Output: written encounter ids + rolled initiatives. Exit 0 on success.
'use strict'; 'use strict';
const crypto = require('crypto'); const crypto = require('crypto');
const argv = process.argv.slice(2);
const FLAGS = new Set(argv.filter(a => a.startsWith('--')));
const ARGS = argv.filter(a => !a.startsWith('--'));
const warn = (msg) => console.error(`WARN: ${msg}`);
const genId = () => const genId = () =>
(typeof crypto !== 'undefined' && crypto.randomUUID) (typeof crypto !== 'undefined' && crypto.randomUUID)
? crypto.randomUUID() ? crypto.randomUUID()
@@ -84,10 +96,41 @@ async function findCampaignId(host, name) {
return matches[0].id; return matches[0].id;
} }
// Vetting. Returns {ok, errors[], warnings[]}. Never silent about missing data.
function vetSpec(encounters) {
const errors = [];
const warnings = [];
encounters.forEach((enc, ei) => {
const seen = new Set();
for (const p of enc.participants || []) {
const where = `encounter "${enc.name}", participant "${p.name}"`;
const type = p.type || 'monster';
const hasHp = p.maxHp !== undefined && p.maxHp !== null;
const hasFormula = typeof p.hpFormula === 'string' && p.hpFormula.trim();
if (type === 'character') {
errors.push(`${where}: character type not allowed in build spec (PC stats come from roster, via UI or explicit user values)`);
continue;
}
if (!hasHp && !hasFormula) {
if (FLAGS.has('--allow-defaults')) {
warnings.push(`${where}: no maxHp/hpFormula — defaulting to 10 (--allow-defaults)`);
} else {
errors.push(`${where}: no maxHp and no hpFormula. Refusing to guess HP. Add the stat from the source or pass --allow-defaults.`);
}
}
if (p.ac === undefined || p.ac === null) warnings.push(`${where}: no ac`);
if (p.initMod === undefined && p.initiative === undefined) warnings.push(`${where}: no initMod/initiative (defaulting mod 0)`);
if (seen.has(p.name)) warnings.push(`${where}: duplicate name in encounter (number them for display clarity)`);
seen.add(p.name);
}
});
return { ok: errors.length === 0, errors, warnings };
}
async function main() { async function main() {
const [host, campaignName, specArg] = process.argv.slice(2); const [host, campaignName, specArg] = ARGS;
if (!host || !campaignName || !specArg) { if (!host || !campaignName || !specArg) {
console.error('Usage: node build-encounters.js <host> <campaignName> <specFile|->'); console.error('Usage: node build-encounters.js <host> <campaignName> <specFile|-> [--allow-defaults]');
process.exit(1); process.exit(1);
} }
const specRaw = specArg === '-' const specRaw = specArg === '-'
@@ -96,6 +139,14 @@ async function main() {
const encounters = JSON.parse(specRaw); const encounters = JSON.parse(specRaw);
if (!Array.isArray(encounters)) throw new Error('spec must be a JSON array of encounters'); if (!Array.isArray(encounters)) throw new Error('spec must be a JSON array of encounters');
const vet = vetSpec(encounters);
vet.warnings.forEach(warn);
if (!vet.ok) {
console.error('SPEC REJECTED:');
vet.errors.forEach(e => console.error(` - ${e}`));
process.exit(3);
}
const cid = await findCampaignId(host, campaignName); const cid = await findCampaignId(host, campaignName);
console.log(`campaign: "${campaignName}" -> ${cid}`); console.log(`campaign: "${campaignName}" -> ${cid}`);