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

+72 -8
View File
@@ -27,6 +27,7 @@ const generateId = () =>
: `id_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
const rollD20 = () => Math.floor(Math.random() * 20) + 1;
const rollD4 = () => Math.floor(Math.random() * 4) + 1;
// Parse HP formula like "2d6+9" or "3d8-2" or "1d20". Roll, return total.
// Returns null on invalid input. Whitespace/case tolerant.
@@ -198,6 +199,16 @@ function expandUndo(entry, currentEnc) {
: { participants: [...parts, ...added.filter(p => !parts.some(x => x.id === p.id))] },
};
}
case 'reroll_all_init': {
// undo: restore pre-reroll array + order. redo: re-apply rolled array.
return {
encounterPath: encPath,
updates: { participants: u.participants.map(p => ({ ...p })), turnOrderIds: u.turnOrderIds },
redo: u.newParticipants && u.newTurnOrderIds
? { participants: u.newParticipants.map(p => ({ ...p })), turnOrderIds: u.newTurnOrderIds }
: undefined,
};
}
case 'remove_participant': {
const p = u.participant;
const turnState = {};
@@ -240,6 +251,7 @@ function expandUndo(entry, currentEnc) {
if (vals.status !== undefined) out.status = vals.status;
if (vals.deathSaveSuccesses !== undefined) out.deathSaveSuccesses = vals.deathSaveSuccesses;
if (vals.deathSaveFailures !== undefined) out.deathSaveFailures = vals.deathSaveFailures;
if (vals.stableRegainHours !== undefined) out.stableRegainHours = vals.stableRegainHours;
if (vals.isActive !== undefined) out.isActive = vals.isActive;
if (vals.conditions !== undefined) out.conditions = vals.conditions;
return out;
@@ -361,6 +373,7 @@ function makeParticipant(opts) {
status: opts.status || (opts.currentHp === 0 ? 'dying' : 'conscious'),
deathSaveSuccesses: opts.deathSaveSuccesses || 0,
deathSaveFailures: opts.deathSaveFailures || 0,
stableRegainHours: opts.stableRegainHours !== undefined ? opts.stableRegainHours : null,
hpFormula: opts.hpFormula || null,
ac: opts.ac !== undefined ? opts.ac : null,
tempHp: opts.tempHp !== undefined ? opts.tempHp : 0,
@@ -373,6 +386,8 @@ function buildCharacterParticipant(character) {
const finalInitiative = initiativeRoll + modifier;
const maxHp = character.defaultMaxHp || DEFAULT_MAX_HP;
const currentHp = character.defaultCurrentHp != null ? character.defaultCurrentHp : maxHp;
// Roster carried stable state (from writeback): still down at 0 HP, unconscious.
const isStable = currentHp === 0 && character.stableRegainHours != null;
return {
participant: makeParticipant({
name: character.name,
@@ -382,6 +397,9 @@ function buildCharacterParticipant(character) {
initMod: modifier,
maxHp,
currentHp,
status: isStable ? 'stable' : undefined,
stableRegainHours: isStable ? character.stableRegainHours : null,
conditions: isStable ? ['unconscious'] : [],
ac: character.defaultAc !== undefined ? character.defaultAc : null,
}),
roll: { roll: initiativeRoll, mod: modifier, total: finalInitiative },
@@ -664,6 +682,7 @@ async function applyHpChange(encounter, participantId, changeType, amount, optio
status: participant.status || (participant.currentHp === 0 ? 'dying' : 'conscious'),
deathSaveSuccesses: participant.deathSaveSuccesses || 0,
deathSaveFailures: participant.deathSaveFailures || 0,
stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null,
isActive: participant.isActive,
conditions: [...(participant.conditions || [])],
};
@@ -741,7 +760,7 @@ async function applyHpChange(encounter, participantId, changeType, amount, optio
if (failures >= 3) {
updates = { currentHp: 0, status: 'dead', deathSaveSuccesses: 0, deathSaveFailures: 0, tempHp: tempFinal, ...(participant.type === 'monster' ? { isActive: false } : {}) };
} else {
updates = { currentHp: 0, status: 'dying', deathSaveSuccesses: 0, deathSaveFailures: failures, tempHp: tempFinal };
updates = { currentHp: 0, status: 'dying', deathSaveSuccesses: 0, deathSaveFailures: failures, stableRegainHours: null, tempHp: tempFinal };
}
message = `${participant.name} took ${amount} damage while ${status === 'stable' ? 'stable' : 'dying'}`;
logDelta = { ...logDelta, to: 0, status: updates.status, deathSaveFailures: updates.deathSaveFailures };
@@ -763,7 +782,7 @@ async function applyHpChange(encounter, participantId, changeType, amount, optio
} else if (changeType === 'heal') {
if (status === 'dead') return encounter;
const newHp = Math.min(participant.maxHp, Math.max(1, participant.currentHp + amount));
updates = { currentHp: newHp, status: 'conscious', deathSaveSuccesses: 0, deathSaveFailures: 0 };
updates = { currentHp: newHp, status: 'conscious', deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: null };
message = `${participant.name} healed for ${amount} (${participant.currentHp} → ${newHp} HP)`;
logDelta = { ...logDelta, to: newHp, status: 'conscious', deathSavesCleared: true };
} else {
@@ -781,6 +800,7 @@ async function applyHpChange(encounter, participantId, changeType, amount, optio
status: newP.status,
deathSaveSuccesses: newP.deathSaveSuccesses || 0,
deathSaveFailures: newP.deathSaveFailures || 0,
stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null,
isActive: newP.isActive,
conditions: [...(newP.conditions || [])],
};
@@ -900,6 +920,7 @@ async function deathSave(encounter, participantId, outcome, ctx) {
status: statusBefore,
deathSaveSuccesses: participant.deathSaveSuccesses || 0,
deathSaveFailures: participant.deathSaveFailures || 0,
stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null,
isActive: participant.isActive,
conditions: [...(participant.conditions || [])],
};
@@ -910,8 +931,8 @@ async function deathSave(encounter, participantId, outcome, ctx) {
} else {
const successes = (participant.deathSaveSuccesses || 0) + (outcome === 'success' ? 1 : 0);
const failures = (participant.deathSaveFailures || 0) + (outcome === 'fail' ? 1 : outcome === 'nat1' ? 2 : 0);
if (failures >= 3) updates = { currentHp: 0, status: 'dead', deathSaveSuccesses: 0, deathSaveFailures: 0 };
else if (successes >= 3) updates = { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0 };
if (failures >= 3) updates = { currentHp: 0, status: 'dead', deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: null };
else if (successes >= 3) updates = { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: rollD4() };
else updates = { currentHp: 0, status: 'dying', deathSaveSuccesses: successes, deathSaveFailures: failures };
}
@@ -931,6 +952,7 @@ async function deathSave(encounter, participantId, outcome, ctx) {
status: newP.status,
deathSaveSuccesses: newP.deathSaveSuccesses || 0,
deathSaveFailures: newP.deathSaveFailures || 0,
stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null,
isActive: newP.isActive,
conditions: [...(newP.conditions || [])],
};
@@ -977,7 +999,7 @@ async function stabilizeParticipant(encounter, participantId, ctx) {
if (status === 'dead') throw new Error('Cannot stabilize dead participant.');
if (status === 'stable') return encounter;
const updates = { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0 };
const updates = { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: rollD4() };
const updatedParticipants = (encounter.participants || []).map(p =>
p.id === participantId ? { ...p, ...withDeathStatusConditions(p, updates) } : p
);
@@ -988,6 +1010,7 @@ async function stabilizeParticipant(encounter, participantId, ctx) {
status,
deathSaveSuccesses: participant.deathSaveSuccesses || 0,
deathSaveFailures: participant.deathSaveFailures || 0,
stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null,
isActive: participant.isActive,
conditions: [...(participant.conditions || [])],
};
@@ -997,6 +1020,7 @@ async function stabilizeParticipant(encounter, participantId, ctx) {
status: newP.status,
deathSaveSuccesses: newP.deathSaveSuccesses || 0,
deathSaveFailures: newP.deathSaveFailures || 0,
stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null,
isActive: newP.isActive,
conditions: [...(newP.conditions || [])],
};
@@ -1022,7 +1046,7 @@ async function reviveParticipant(encounter, participantId, ctx) {
// 5e: dead→stable at 0 HP, unconscious, clears death-save counters.
const updates = ruleset === 'generic'
? { status: 'conscious', ...(participant.type === 'monster' ? {} : {}), ...(participant.isActive === false ? { isActive: true } : {}) }
: { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0, isActive: true };
: { currentHp: 0, status: 'stable', deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: rollD4(), isActive: true };
const updatedParticipants = (encounter.participants || []).map(p =>
p.id === participantId ? { ...p, ...withDeathStatusConditions(p, updates) } : p
);
@@ -1033,6 +1057,7 @@ async function reviveParticipant(encounter, participantId, ctx) {
status,
deathSaveSuccesses: participant.deathSaveSuccesses || 0,
deathSaveFailures: participant.deathSaveFailures || 0,
stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null,
isActive: participant.isActive,
conditions: [...(participant.conditions || [])],
};
@@ -1042,6 +1067,7 @@ async function reviveParticipant(encounter, participantId, ctx) {
status: newP.status,
deathSaveSuccesses: newP.deathSaveSuccesses || 0,
deathSaveFailures: newP.deathSaveFailures || 0,
stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null,
isActive: newP.isActive,
conditions: [...(newP.conditions || [])],
};
@@ -1152,11 +1178,15 @@ async function endEncounter(encounter, ctx) {
const oldMaxHp = player.defaultMaxHp;
const oldAc = player.defaultAc;
const oldCurrentHp = player.defaultCurrentHp;
oldValues.push({ id: player.id, defaultMaxHp: oldMaxHp, defaultAc: oldAc, defaultCurrentHp: oldCurrentHp });
const oldStableRegainHours = player.stableRegainHours !== undefined ? player.stableRegainHours : null;
oldValues.push({ id: player.id, defaultMaxHp: oldMaxHp, defaultAc: oldAc, defaultCurrentHp: oldCurrentHp, stableRegainHours: oldStableRegainHours });
const newPlayer = { ...player };
if (cp.maxHp != null && cp.maxHp !== oldMaxHp) { newPlayer.defaultMaxHp = cp.maxHp; changed = true; }
if (cp.ac != null && cp.ac !== oldAc) { newPlayer.defaultAc = cp.ac; changed = true; }
if (cp.currentHp != null && cp.currentHp !== oldCurrentHp) { newPlayer.defaultCurrentHp = cp.currentHp; changed = true; }
// Stable state: carried while stable, cleared once conscious again.
const cpStableRegainHours = cp.status === 'stable' ? (cp.stableRegainHours !== undefined ? cp.stableRegainHours : null) : null;
if (cpStableRegainHours !== oldStableRegainHours) { newPlayer.stableRegainHours = cpStableRegainHours; changed = true; }
return newPlayer;
});
if (changed) {
@@ -1173,6 +1203,40 @@ async function endEncounter(encounter, ctx) {
return commit(encounter, patch, log, ctx);
}
// rerollAllInitiatives: d20 + stored initMod for EVERY participant, one write.
// Allowed pre-combat or paused. Slot-based re-insertion preserves roll order.
// Single log entry; undo restores old array + order.
async function rerollAllInitiatives(encounter, ctx) {
if (encounter.isStarted && !encounter.isPaused) {
throw new Error('Reroll initiative only before combat or while paused.');
}
const oldParticipants = encounter.participants || [];
if (oldParticipants.length === 0) return encounter;
const rolled = oldParticipants.map(p => ({
...p,
initiative: rollD20() + (p.initMod != null ? p.initMod : 0),
}));
// slot re-insertion: each rolled participant placed above lower inits
const newList = [];
for (const p of rolled) {
newList.splice(slotIndexForInit(newList, p.initiative), 0, p);
}
const patch = { participants: newList, ...syncTurnOrder(newList) };
const log = {
type: 'reroll_all_init',
message: `Initiative rerolled for all ${newList.length} participants`,
delta: { count: newList.length },
undo: {
participants: oldParticipants.map(p => ({ ...p })),
turnOrderIds: [...(encounter.turnOrderIds || [])],
newParticipants: newList.map(p => ({ ...p })),
newTurnOrderIds: patch.turnOrderIds,
},
};
return commit(encounter, patch, log, ctx);
}
// ----------------------------------------------------------------------------
// Display lifecycle — write activeDisplay doc, no combat log.
// ctx.displayPath required.
@@ -1199,6 +1263,6 @@ module.exports = {
startEncounter, nextTurn, togglePause,
addParticipant, addParticipants, updateParticipant, removeParticipant,
toggleParticipantActive, applyHpChange, setTempHp, deathSave, stabilizeParticipant, reviveParticipant, markDead, toggleCondition,
reorderParticipants, endEncounter,
reorderParticipants, endEncounter, rerollAllInitiatives,
activateDisplay, clearDisplay, toggleHidePlayerHp,
};