Implement D&D 5e death-save state machine and cleanup combat ordering

- Add status-driven death-save model:
  - status: conscious | dying | stable | dead
  - deathSaveSuccesses/deathSaveFailures as display counters
  - remove old death-save fields as source of truth
- Add death-save actions:
  - success, fail, nat1, nat20
  - stabilize participant
  - revive dead participant to 0 HP, stable, unconscious
- Apply 5e HP transition rules:
  - characters and NPCs at 0 HP become dying
  - stable/dying participants gain unconscious condition
  - healing clears death saves and returns conscious
  - massive damage kills
  - non-NPC monsters at 0 HP become dead and inactive
- Split NPCs from monsters with type="npc":
  - NPCs use death saves like characters
  - NPCs remain DM-controlled/monster-colored in UI
  - new NPCs no longer persist isNpc flag
- Keep dead PCs/NPCs in encounter and initiative until DM removes or deactivates
- Allow DM active/inactive toggle for dead participants
- Hide inactive participants from player display
- Improve DM and player death-state UI:
  - show Dying/Dead/Unconscious states consistently
  - add revive button for dead participants
  - distinguish dead visual from inactive visual in DM view
- Fix initiative drag/reorder behavior:
  - downward drag now changes order instead of no-op
  - paused combat can reorder across current turn pointer
  - turnOrderIds stay synced to participants order
- Persist campaign collapse state in localStorage
- Update death-save docs and encounter builder docs
- Add/expand tests for:
  - death saves and HP transitions
  - dead/active/inactive behavior
  - NPC death-save behavior
  - player display visibility
  - drag reorder semantics
  - logging expectations

Tests:
- npm run test:all
- app: 94 passed
- shared: 158 passed, 5 skipped
- server: 32 passed
This commit is contained in:
david raistrick
2026-07-06 16:48:50 -04:00
parent 2569cc4497
commit 1d4ec873dc
22 changed files with 1617 additions and 531 deletions
+23 -30
View File
@@ -245,27 +245,26 @@ describe('applyHpChange', () => {
expect(newEnc.participants[0].currentHp).toBe(10);
});
test('damage to 0 deactivates + keeps turn order (unified)', async () => {
// Unified: death flips isActive=false (removed from active rotation).
// turnOrderIds unchanged (no turn-order patch on death).
test('damage to 0 marks dying, keeps active, keeps turn order', async () => {
const { storage, ctx } = mockCtx();
const ps = [p('a', 10, { currentHp: 3 }), p('b', 5)];
const ps = [p('a', 10, { type: 'character', currentHp: 3 }), p('b', 5)];
const e = enc(ps, { isStarted: true, turnOrderIds: ['a', 'b'], currentTurnParticipantId: 'a' });
const newEnc = await applyHpChange(e, 'a', 'damage', 5, ctx);
expect(newEnc.participants[0].currentHp).toBe(0);
expect(newEnc.participants[0].isActive).toBe(false);
expect(newEnc.participants[0].status).toBe('dying');
expect(newEnc.participants[0].isActive).not.toBe(false);
expect(lastPatch(storage).turnOrderIds).toBeUndefined();
expect(lastPatch(storage).currentTurnParticipantId).toBeUndefined();
});
test('heal above 0 reactivates + resets death saves (unified)', async () => {
// Unified: revive from 0 flips isActive=true, deathSaves reset.
test('heal from dying makes conscious and resets death-save counters', async () => {
const { ctx } = mockCtx();
const ps = [p('a', 10, { currentHp: 0, isActive: false, deathSaves: 2 })];
const ps = [p('a', 10, { currentHp: 0, status: 'dying', deathSaveSuccesses: 2, deathSaveFailures: 1 })];
const newEnc = await applyHpChange(enc(ps), 'a', 'heal', 5, ctx);
expect(newEnc.participants[0].currentHp).toBe(5);
expect(newEnc.participants[0].isActive).toBe(true);
expect(newEnc.participants[0].deathSaves).toBe(0);
expect(newEnc.participants[0].status).toBe('conscious');
expect(newEnc.participants[0].deathSaveSuccesses).toBe(0);
expect(newEnc.participants[0].deathSaveFailures).toBe(0);
});
test('heal clamps to maxHp', async () => {
@@ -285,27 +284,21 @@ describe('applyHpChange', () => {
});
describe('deathSave', () => {
test('increments fails', async () => {
test('fail increments failure counter while dying', async () => {
const { ctx } = mockCtx();
const ps = [p('a', 10, { currentHp: 0, deathFails: 0 })];
const { enc: newEnc } = await deathSave(enc(ps), 'a', 'fail', 1, ctx);
expect(newEnc.participants[0].deathFails).toBe(1);
const ps = [p('a', 10, { currentHp: 0, status: 'dying', deathSaveFailures: 0, deathSaveSuccesses: 0 })];
const { enc: newEnc, status } = await deathSave(enc(ps), 'a', 'fail', ctx);
expect(status).toBe('dying');
expect(newEnc.participants[0].deathSaveFailures).toBe(1);
});
test('clicking same fail decrements (toggle)', async () => {
test('third fail makes dead and resets counters', async () => {
const { ctx } = mockCtx();
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
const { enc: newEnc } = await deathSave(enc(ps), 'a', 'fail', 2, ctx);
expect(newEnc.participants[0].deathFails).toBe(1);
});
test('third fail sets isDying', async () => {
const { ctx } = mockCtx();
const ps = [p('a', 10, { currentHp: 0, deathFails: 2 })];
const { enc: newEnc, isDying } = await deathSave(enc(ps), 'a', 'fail', 3, ctx);
expect(newEnc.participants[0].deathFails).toBe(3);
expect(newEnc.participants[0].isDying).toBe(true);
expect(isDying).toBe(true);
const ps = [p('a', 10, { currentHp: 0, status: 'dying', deathSaveFailures: 2, deathSaveSuccesses: 0 })];
const { enc: newEnc, status } = await deathSave(enc(ps), 'a', 'fail', ctx);
expect(status).toBe('dead');
expect(newEnc.participants[0].status).toBe('dead');
expect(newEnc.participants[0].deathSaveFailures).toBe(0);
});
});
@@ -326,12 +319,12 @@ describe('toggleCondition', () => {
});
describe('reorderParticipants', () => {
test('drag before target (same-init tie)', async () => {
test('drag downward onto target moves after target (same-init tie)', async () => {
const { ctx } = mockCtx();
const ps = [p('a', 10), p('b', 10), p('c', 10)];
const newEnc = await reorderParticipants(enc(ps), 'a', 'c', ctx);
// drag a before c: remove a → [b,c], insert before c → [b,a,c]
expect(newEnc.participants.map(x => x.id)).toEqual(['b', 'a', 'c']);
// drag a downward onto c: remove a → [b,c], insert after c → [b,c,a]
expect(newEnc.participants.map(x => x.id)).toEqual(['b', 'c', 'a']);
});
test('cross-init drag blocked (no-op)', async () => {