feat: death-save display, stable-hour recovery, reroll-all initiative

- Player display: death-save pips for dying participants (3 successes / 3 failures), stable shows note without pips
- stableRegainHours: 1d4 hours rolled on natural stabilization (3rd save success, manual stabilize, revive-to-stable); cleared on heal, damage-while-stable, inline HP edit, and death; undo/redo round-trips through all paths
- Roster writeback: end-of-encounter sync writes stable hours to characters (stable -> hours, conscious -> null); character card badge shows hours; stable state carried into new encounters via buildCharacterParticipant; editor Cur HP > 0 clears
- End-combat message corrected: states turn position and round reset while initiative values are kept
- Reroll All Initiative: d20 + initMod for every participant in a single write, undoable, pre-combat or paused only; confirmation modal in UI, hidden during active combat
- Add All (Roll Init) hoisted to Add Participants header row so it stays visible when the section is collapsed; header title remains the collapse toggle
This commit is contained in:
david raistrick committed 2026-09-23 20:39:28 -04:00
1 parent 8ecd31d570
commit 8d71f0ba4b
9 files changed
+664 -33

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
'use strict';
// rerollAllInitiatives: bulk d20+initMod for every participant.
// Single commit, single log, undo restores old values + order.
const {
makeParticipant,
startEncounter,
rerollAllInitiatives,
} = require('@ttrpg/shared');
const { mockCtx, undoLast } = require('./_helpers');
function part(id, init, initMod, extra = {}) {
return makeParticipant({
id, name: id, type: initMod !== undefined ? 'monster' : 'character',
initiative: init, initMod, maxHp: 20, currentHp: 20, ...extra,
});
}
function enc(participants, extra = {}) {
return {
id: 'e1', name: 'reroll-all-test', participants,
isStarted: false, isPaused: false, round: 0,
currentTurnParticipantId: null, turnOrderIds: [],
...extra,
};
}
const find = (e, id) => e.participants.find(p => p.id === id);
describe('rerollAllInitiatives', () => {
test('rolls d20+initMod for every participant; single write', async () => {
const { ctx, storage } = mockCtx();
const e = enc([part('a', 10, 5), part('b', 8, 2), part('c', 15, 0)]);
const out = await rerollAllInitiatives(e, ctx);
for (const id of ['a', 'b', 'c']) {
const p = find(out, id);
const mod = id === 'a' ? 5 : id === 'b' ? 2 : 0;
expect(p.initiative).toBeGreaterThanOrEqual(mod + 1);
expect(p.initiative).toBeLessThanOrEqual(mod + 20);
}
// one write for the whole operation
expect(storage.calls.filter(c => c.fn === 'updateDoc' && c.path === 'encounters/e1')).toHaveLength(1);
});
test('undo restores exact old initiatives and order', async () => {
const { ctx } = mockCtx();
const e = enc([part('a', 10, 5), part('b', 8, 2)]);
const orderBefore = [...e.turnOrderIds];
const out = await rerollAllInitiatives(e, ctx);
expect(find(out, 'a').initiative).not.toBe(10);
const undone = await undoLast(ctx, out);
expect(find(undone, 'a').initiative).toBe(10);
expect(find(undone, 'b').initiative).toBe(8);
expect(undone.turnOrderIds).toEqual(orderBefore);
});
test('inactive participants rerolled too (they stay inactive)', async () => {
const { ctx } = mockCtx();
const e = enc([part('a', 10, 1), part('b', 9, 1, { isActive: false })]);
const out = await rerollAllInitiatives(e, ctx);
expect(find(out, 'b').isActive).toBe(false);
expect(find(out, 'b').initiative).toBeGreaterThanOrEqual(2);
});
test('throws during active combat (not paused)', async () => {
const { ctx } = mockCtx();
let e = enc([part('a', 10, 1)]);
e = await startEncounter(e, ctx);
await expect(rerollAllInitiatives({ ...e, isPaused: false }, ctx)).rejects.toThrow(/paused/i);
});
});
+99
View File
@@ -0,0 +1,99 @@
'use strict';
// Stable regain hours: stabilization rolls 1d4, persists until healed/revived.
// 3rd death save -> stable; manual stabilize -> stable; revive dead -> stable (5e).
const {
makeParticipant,
startEncounter,
applyHpChange,
deathSave,
stabilizeParticipant,
reviveParticipant,
} = require('@ttrpg/shared');
const { mockCtx } = require('./_helpers');
function pc(id, extra = {}) {
return makeParticipant({
id,
name: id,
type: 'character',
initiative: 10,
maxHp: 100,
currentHp: 100,
...extra,
});
}
function enc(participants, extra = {}) {
return {
id: 'e1',
name: 'stable-hours-test',
participants,
isStarted: false,
isPaused: false,
round: 0,
currentTurnParticipantId: null,
turnOrderIds: [],
...extra,
};
}
const part = e => e.participants.find(p => p.id === 'a');
async function dyingEnc() {
const { ctx } = mockCtx();
let e = enc([pc('a')]);
e = await startEncounter(e, ctx);
e = await applyHpChange(e, 'a', 'damage', 100, ctx);
return { e, ctx };
}
describe('stableRegainHours', () => {
test('3rd death save success rolls 1d4 and stores stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
let out = e;
for (let i = 0; i < 3; i++) out = (await deathSave(out, 'a', 'success', ctx)).enc;
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('manual stabilize also rolls and stores hours', async () => {
const { e, ctx } = await dyingEnc();
const out = await stabilizeParticipant(e, 'a', ctx);
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('heal clears stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
let out = await stabilizeParticipant(e, 'a', ctx);
expect(part(out).stableRegainHours).toBeGreaterThanOrEqual(1);
out = await applyHpChange(out, 'a', 'heal', 5, ctx);
expect(part(out).status).toBe('conscious');
expect(part(out).stableRegainHours == null).toBe(true);
});
test('reviveParticipant to stable rolls fresh hours', async () => {
const { e, ctx } = await dyingEnc();
let out = e;
for (let i = 0; i < 3; i++) out = (await deathSave(out, 'a', 'fail', ctx)).enc;
expect(part(out).status).toBe('dead');
out = await reviveParticipant(out, 'a', ctx);
const p = part(out);
expect(p.status).toBe('stable');
expect(p.stableRegainHours).toBeGreaterThanOrEqual(1);
expect(p.stableRegainHours).toBeLessThanOrEqual(4);
});
test('dying (not yet stable) has no stableRegainHours', async () => {
const { e, ctx } = await dyingEnc();
const out = (await deathSave(e, 'a', 'fail', ctx)).enc;
expect(part(out).status).toBe('dying');
expect(part(out).stableRegainHours == null).toBe(true);
});
});
+68
View File
@@ -204,3 +204,71 @@ describe('character writeback on endEncounter', () => {
expect(storage._writes.find(w => w.path === 'campaigns/camp1')).toBeUndefined();
});
});
describe('stable regain hours writeback', () => {
function ctxFor(storage) {
return {
storage,
encounterPath: 'campaigns/camp1/encounters/enc1',
logPath: 'logs/log1',
logCollection: 'logs',
displayPath: 'activeDisplay/status',
};
}
test('stable char carries stableRegainHours to roster', async () => {
const storage = makeMockStorage();
const players = [
{ id: 'char1', name: 'Fighter', defaultMaxHp: 30, defaultAc: 17, defaultCurrentHp: 30 },
];
storage._docs.set('campaigns/camp1', { id: 'camp1', name: 'Camp', players, syncCharacters: true });
const enc = makeEncounter({
participants: [
{ id: 'p1', name: 'Fighter', type: 'character', originalCharacterId: 'char1', initiative: 15, maxHp: 30, currentHp: 0, ac: 17, status: 'stable', stableRegainHours: 3 },
],
});
await endEncounter(enc, ctxFor(storage));
const campWrite = storage._writes.find(w => w.path === 'campaigns/camp1');
expect(campWrite).toBeDefined();
const fighter = campWrite.patch.players.find(p => p.id === 'char1');
expect(fighter.stableRegainHours).toBe(3);
});
test('conscious char clears stale roster stableRegainHours', async () => {
const storage = makeMockStorage();
const players = [
{ id: 'char1', name: 'Fighter', defaultMaxHp: 30, defaultAc: 17, defaultCurrentHp: 30, stableRegainHours: 2 },
];
storage._docs.set('campaigns/camp1', { id: 'camp1', name: 'Camp', players, syncCharacters: true });
const enc = makeEncounter({
participants: [
{ id: 'p1', name: 'Fighter', type: 'character', originalCharacterId: 'char1', initiative: 15, maxHp: 30, currentHp: 12, ac: 17, status: 'conscious' },
],
});
await endEncounter(enc, ctxFor(storage));
const campWrite = storage._writes.find(w => w.path === 'campaigns/camp1');
expect(campWrite).toBeDefined();
const fighter = campWrite.patch.players.find(p => p.id === 'char1');
expect(fighter.stableRegainHours).toBeNull();
});
});
describe('buildCharacterParticipant carries stable state', () => {
const { buildCharacterParticipant } = shared;
test('roster stable char becomes stable participant with hours', () => {
const char = { id: 'c1', name: 'Rogue', defaultMaxHp: 40, defaultCurrentHp: 0, defaultInitMod: 2, stableRegainHours: 4 };
const { participant } = buildCharacterParticipant(char);
expect(participant.currentHp).toBe(0);
expect(participant.status).toBe('stable');
expect(participant.stableRegainHours).toBe(4);
expect(participant.conditions).toContain('unconscious');
});
test('healthy roster char stays conscious, no hours', () => {
const char = { id: 'c2', name: 'Cleric', defaultMaxHp: 30, defaultCurrentHp: 30, defaultInitMod: 1 };
const { participant } = buildCharacterParticipant(char);
expect(participant.status).toBe('conscious');
expect(participant.stableRegainHours).toBeNull();
});
});