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:
@@ -0,0 +1,90 @@
|
||||
// Death saves: broken model. Current = single counter, 3=dying. D&D 5e needs
|
||||
// separate success/fail tracking:
|
||||
// 3 successes = stable (0hp unconscious, safe until healed)
|
||||
// 3 failures = dead
|
||||
// Current tracks "saves" (really fails via red X UI), no successes at all.
|
||||
// "Stable" state doesn't exist.
|
||||
//
|
||||
// RED: prove current can't model 3-success stability.
|
||||
|
||||
'use strict';
|
||||
|
||||
const shared = require('@ttrpg/shared');
|
||||
const { makeParticipant, startEncounter, applyHpChange, deathSave } = shared;
|
||||
|
||||
function p(id) {
|
||||
return makeParticipant({ id, name: id, type: 'character',
|
||||
initiative: 10, maxHp: 100, currentHp: 100 });
|
||||
}
|
||||
function enc(ps, extra = {}) {
|
||||
return { name:'t', participants:ps, isStarted:false, isPaused:false,
|
||||
round:0, currentTurnParticipantId:null, turnOrderIds:[], ...extra };
|
||||
}
|
||||
const apply = (e, r) => r && r.patch ? { ...e, ...r.patch } : e;
|
||||
|
||||
describe('death saves: missing success/stable model', () => {
|
||||
test('3 successes makes character stable (not dead, not dying)', () => {
|
||||
// Character at 0hp. Roll 3 successful death saves.
|
||||
let e = enc([p('a')]);
|
||||
e = apply(e, startEncounter(e));
|
||||
e = apply(e, applyHpChange(e, 'a', 'damage', 100)); // 0hp
|
||||
expect(e.participants[0].currentHp).toBe(0);
|
||||
|
||||
// Roll 3 successful saves. Should mark stable.
|
||||
e = apply(e, deathSave(e, 'a', 'success', 1));
|
||||
e = apply(e, deathSave(e, 'a', 'success', 2));
|
||||
const r3 = deathSave(e, 'a', 'success', 3);
|
||||
e = apply(e, r3);
|
||||
|
||||
// RED: current deathSave signature = (enc, id, saveNumber). No type arg.
|
||||
// Will throw or misbehave. Want: status field.
|
||||
expect(r3.status).toBe('stable');
|
||||
expect(e.participants[0].isDying).toBe(false);
|
||||
expect(e.participants[0].isStable).toBe(true);
|
||||
expect(e.participants[0].deathFails).toBe(0);
|
||||
});
|
||||
|
||||
test('3 failures makes character dead (isDying for removal)', () => {
|
||||
let e = enc([p('a')]);
|
||||
e = apply(e, startEncounter(e));
|
||||
e = apply(e, applyHpChange(e, 'a', 'damage', 100));
|
||||
|
||||
const r1 = deathSave(e, 'a', 'fail', 1);
|
||||
e = apply(e, r1);
|
||||
const r2 = deathSave(e, 'a', 'fail', 2);
|
||||
e = apply(e, r2);
|
||||
const r3 = deathSave(e, 'a', 'fail', 3);
|
||||
e = apply(e, r3);
|
||||
|
||||
expect(r3.status).toBe('dead');
|
||||
expect(e.participants[0].isDying).toBe(true);
|
||||
expect(e.participants[0].deathFails).toBe(3);
|
||||
});
|
||||
|
||||
test('successes and fails tracked independently', () => {
|
||||
// Mixed: 1 success, 2 fails, then 2 more successes = stable.
|
||||
let e = enc([p('a')]);
|
||||
e = apply(e, startEncounter(e));
|
||||
e = apply(e, applyHpChange(e, 'a', 'damage', 100));
|
||||
|
||||
e = apply(e, deathSave(e, 'a', 'success', 1));
|
||||
expect(e.participants[0].deathSaves).toBe(1);
|
||||
expect(e.participants[0].deathFails).toBe(0);
|
||||
|
||||
e = apply(e, deathSave(e, 'a', 'fail', 1));
|
||||
e = apply(e, deathSave(e, 'a', 'fail', 2));
|
||||
expect(e.participants[0].deathSaves).toBe(1);
|
||||
expect(e.participants[0].deathFails).toBe(2);
|
||||
|
||||
// 2 more successes → total 3 successes → stable
|
||||
e = apply(e, deathSave(e, 'a', 'success', 2));
|
||||
const r = deathSave(e, 'a', 'success', 3);
|
||||
expect(r.status).toBe('stable');
|
||||
expect(e.participants[0].deathFails).toBe(2); // fails unchanged
|
||||
});
|
||||
|
||||
test('deathSave signature: (enc, id, type, n)', () => {
|
||||
// type = 'success' | 'fail'. n = 1|2|3.
|
||||
expect(deathSave.length).toBe(4);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user