feat(turn): D&D 5e death saves — success/fail tracking

Old model broken: single deathSaves counter, 3=revive. No fails tracked,
successes conflated with fails. DM couldn't model real death save outcomes.

New model (D&D 5e):
- deathSave(enc, id, type, n) — type='success'|'fail', n=1|2|3
- Participant fields: deathSaves (successes), deathFails, isStable, isDying
- 3 successes = stable (0hp unconscious, safe until healed)
- 3 failures = dead (isDying, caller animates removal)
- Toggling same pip = undo that pip
- Returns { patch, log, status } — status: 'stable'|'dead'|'pending'
  isDying back-compat flag kept for App.js removal animation

App.js:
- UI split into green ✓ saves row + red ✕ fails row
- Status badges: Stabilized (emerald) / Dying (red pulse)
- handleDeathSaveChange(participantId, type, saveNumber) — new sig
- makeParticipant init: deathFails + isStable defaults

Data loss: old single-counter participants lose saves (feature didn't work,
no real state to preserve). User confirmed acceptable.

Tests:
- turn.deathsave.test.js: RED-then-GREEN, 3-success stable, 3-fail dead,
  independent tracking, new signature
- Updated 6 callers across characterization/combat/dead-skip/logging/scenario
- Logs UI tests: new title selectors (Success N / Fail N)

243 tests green.
This commit is contained in:
david raistrick
2026-07-04 17:56:14 -04:00
parent f2797595f4
commit 9c90c1500c
10 changed files with 214 additions and 72 deletions
+49 -25
View File
@@ -129,7 +129,9 @@ function makeParticipant(opts) {
isNpc: opts.isNpc || false,
conditions: opts.conditions || [],
isActive: opts.isActive !== undefined ? opts.isActive : true,
deathSaves: opts.deathSaves || 0,
deathSaves: opts.deathSaves || 0, // successes (0-3)
deathFails: opts.deathFails || 0, // failures (0-3)
isStable: opts.isStable || false,
isDying: opts.isDying || false,
};
}
@@ -492,41 +494,63 @@ function applyHpChange(encounter, participantId, changeType, amount) {
};
}
// DEATH_SAVE — verbatim from ParticipantManager.handleDeathSaveChange
// saveNumber: 1 | 2 | 3. Returns isDying flag if 3rd save hit (client triggers removal animation).
function deathSave(encounter, participantId, saveNumber) {
// DEATH_SAVE — D&D 5e model. Separate success/fail tracking.
// type: 'success' | 'fail'
// n: 1 | 2 | 3 (which pip clicked)
// 3 successes = stable (0hp unconscious, safe). 3 fails = dead (isDying,
// caller animates removal). Toggling same pip = undo that pip.
// Returns { patch, log, status } where status: 'stable'|'dead'|'pending'.
function deathSave(encounter, participantId, type, n) {
const participant = (encounter.participants || []).find(p => p.id === participantId);
if (!participant) throw new Error('Participant not found.');
const currentSaves = participant.deathSaves || 0;
const newSaves = currentSaves === saveNumber ? saveNumber - 1 : saveNumber;
if (type !== 'success' && type !== 'fail') {
throw new Error(`deathSave type must be 'success' or 'fail', got "${type}".`);
}
const name = participant.name;
const undo = { participants: encounter.participants || [] };
if (newSaves === 3) {
// Mark dying — caller waits for animation, then calls removeParticipant.
const updatedParticipants = (encounter.participants || []).map(p =>
p.id === participantId ? { ...p, deathSaves: newSaves, isDying: true } : p
);
return {
patch: { participants: updatedParticipants },
log: {
message: `${name} failed 3 death saves — dying`,
undo,
},
isDying: true,
};
// Toggle: clicking current-value pip decrements (undo that pip).
const current = type === 'success'
? (participant.deathSaves || 0)
: (participant.deathFails || 0);
const next = current === n ? n - 1 : n;
const field = type === 'success' ? 'deathSaves' : 'deathFails';
const updates = { [field]: next };
// Clear terminal flags on any change (re-eval below).
let status = 'pending';
const newSuccesses = type === 'success' ? next : (participant.deathSaves || 0);
const newFails = type === 'fail' ? next : (participant.deathFails || 0);
if (newSuccesses >= 3) {
updates.isStable = true;
updates.isDying = false;
status = 'stable';
} else if (newFails >= 3) {
updates.isStable = false;
updates.isDying = true;
status = 'dead';
} else {
updates.isStable = false;
updates.isDying = false;
}
const updatedParticipants = (encounter.participants || []).map(p =>
p.id === participantId ? { ...p, deathSaves: newSaves } : p
p.id === participantId ? { ...p, ...updates } : p
);
const message = status === 'stable'
? `${name} stabilized (3 death saves)`
: status === 'dead'
? `${name} failed 3 death saves — dead`
: `${name} death ${type}: ${next}/3`;
return {
patch: { participants: updatedParticipants },
log: {
message: `${name} death save: ${newSaves}/3`,
undo,
},
isDying: false,
log: { message, undo },
status,
isDying: status === 'dead', // back-compat for App.js handler
};
}