Add spec vetting and PC hard rules to encounter builder skill

Spec files are ephemeral (/tmp fine); the server DB is the durable
record — encounter docs carry hpFormula/ac/init, so past builds can be
reconstructed by querying live data, not from kept files.

Vetting before any write (vetSpec):
- monster/npc with neither maxHp nor hpFormula = hard reject (exit 3);
  silent 10 HP defaults caused real damage. --allow-defaults escape
  hatch warns loudly.
- character type in build spec = hard reject. PCs are added only from
  campaign roster verbatim; tooling never invents or edits PC stats.
- missing ac or initMod/initiative = warning, build continues.
- duplicate participant names in one encounter = warning.

Flag parsing: --flags split from args, usage updated.
This commit is contained in:
david raistrick committed 2026-11-18 11:11:56 -05:00
1 parent 15347bfdc8
commit dda8ed9790
2 files changed
+94 -2

No files matched your search

@@ -21,11 +21,23 @@
// initiative: optional manual override. If omitted, rolled d20 + initMod.
// 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.
'use strict';
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 = () =>
(typeof crypto !== 'undefined' && crypto.randomUUID)
? crypto.randomUUID()
@@ -84,10 +96,41 @@ async function findCampaignId(host, name) {
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() {
const [host, campaignName, specArg] = process.argv.slice(2);
const [host, campaignName, specArg] = ARGS;
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);
}
const specRaw = specArg === '-'
@@ -96,6 +139,14 @@ async function main() {
const encounters = JSON.parse(specRaw);
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);
console.log(`campaign: "${campaignName}" -> ${cid}`);