From 15347bfdc8068d80dafa8e93e2f3c50680c5d30f Mon Sep 17 00:00:00 2001 From: david raistrick <1108844+keen99@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:11:03 -0500 Subject: [PATCH] feat: add encounter roster visibility and kill actions - persist independent Hide Disabled and Hide Dead encounter controls - keep dead PCs exempt from Hide Dead while allowing Hide Disabled to hide any inactive participant - add confirmed Kill action that sets zero HP, dead, and inactive without deleting - add undoable Heal Full action above Revive - fix random initiative reroll test flake --- shared/tests/turn.generic.test.js | 41 +++++-- shared/tests/turn.rerollall.test.js | 1 - shared/turn.js | 76 +++++++++++-- src/App.js | 102 +++++++++++++++-- .../EncounterParticipantVisibility.test.js | 107 ++++++++++++++++++ 5 files changed, 300 insertions(+), 27 deletions(-) create mode 100644 src/tests/EncounterParticipantVisibility.test.js diff --git a/shared/tests/turn.generic.test.js b/shared/tests/turn.generic.test.js index e4f537e..1901204 100644 --- a/shared/tests/turn.generic.test.js +++ b/shared/tests/turn.generic.test.js @@ -1,9 +1,9 @@ // Generic (non-5e) ruleset: no death saves, negative HP allowed, down status. const shared = require('@ttrpg/shared'); -const { mockCtx } = require('./_helpers'); +const { mockCtx, undoLast } = require('./_helpers'); const { buildCharacterParticipant, buildMonsterParticipant, - startEncounter, applyHpChange, deathSave, markDead, reviveParticipant, + startEncounter, applyHpChange, deathSave, markDead, reviveParticipant, healFullParticipant, } = shared; function char(id, hp = 100) { @@ -121,25 +121,48 @@ describe('generic ruleset', () => { expect(p.isActive).toBe(true); }); - test('markDead sets status dead (character, stays active)', async () => { + test('markDead kills and disables character', async () => { const { ctx } = mockCtx(); - let e = enc([char('a', 10)]); + let e = enc([{ ...char('a', 10), tempHp: 4, deathSaveSuccesses: 2, deathSaveFailures: 1 }]); e = await startEncounter(e, ctx); - e = await applyHpChange(e, 'a', 'damage', 10, ctx); // down const newEnc = await markDead(e, 'a', ctx); const p = newEnc.participants.find(x => x.id === 'a'); - expect(p.status).toBe('dead'); - expect(p.isActive).toBe(true); + expect(p).toMatchObject({ + currentHp: 0, tempHp: 0, status: 'dead', isActive: false, + deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: null, + }); + + const undone = await undoLast(ctx, newEnc); + expect(undone.participants.find(x => x.id === 'a')).toMatchObject({ + currentHp: 10, tempHp: 4, status: 'conscious', isActive: true, + deathSaveSuccesses: 2, deathSaveFailures: 1, + }); }); - test('markDead on monster = dead + inactive', async () => { + test('healFullParticipant restores dead participant to full, active, conscious; undo restores death', async () => { + const { ctx } = mockCtx(); + let e = enc([{ ...char('a', 20), currentHp: 0, status: 'dead', isActive: false, deathSaveSuccesses: 2, deathSaveFailures: 1, conditions: ['unconscious'], stableRegainHours: 3 }]); + const healed = await healFullParticipant(e, 'a', ctx); + expect(healed.participants[0]).toMatchObject({ + currentHp: 20, status: 'conscious', isActive: true, + deathSaveSuccesses: 0, deathSaveFailures: 0, stableRegainHours: null, + }); + expect(healed.participants[0].conditions).not.toContain('unconscious'); + + const undone = await undoLast(ctx, healed); + expect(undone.participants[0]).toMatchObject({ currentHp: 0, status: 'dead', isActive: false, stableRegainHours: 3 }); + expect(undone.participants[0].conditions).toContain('unconscious'); + }); + + test('markDead on monster = 0 HP + dead + inactive', async () => { const { ctx } = mockCtx(); let e = enc([mon('m', 10)]); e = await startEncounter(e, ctx); - e = await applyHpChange(e, 'm', 'damage', 5, ctx); // down (5hp) + e = await applyHpChange(e, 'm', 'damage', 5, ctx); // 5 HP remain const newEnc = await markDead(e, e.participants[0].id, ctx); const p = newEnc.participants[0]; expect(p.status).toBe('dead'); + expect(p.currentHp).toBe(0); expect(p.isActive).toBe(false); }); diff --git a/shared/tests/turn.rerollall.test.js b/shared/tests/turn.rerollall.test.js index 002706e..0d33d8b 100644 --- a/shared/tests/turn.rerollall.test.js +++ b/shared/tests/turn.rerollall.test.js @@ -51,7 +51,6 @@ describe('rerollAllInitiatives', () => { 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); diff --git a/shared/turn.js b/shared/turn.js index 0ba6775..5d6ef14 100644 --- a/shared/turn.js +++ b/shared/turn.js @@ -237,6 +237,7 @@ function expandUndo(entry, currentEnc) { case 'death_save': case 'stabilize': case 'revive': + case 'heal_full': case 'mark_dead': case 'deactivate_dead_monster': { // Full death-state restore: currentHp, status, death-save counters, @@ -249,6 +250,7 @@ function expandUndo(entry, currentEnc) { const restoreFields = (vals) => { const out = {}; if (vals.currentHp !== undefined) out.currentHp = vals.currentHp; + if (vals.tempHp !== undefined) out.tempHp = vals.tempHp; if (vals.status !== undefined) out.status = vals.status; if (vals.deathSaveSuccesses !== undefined) out.deathSaveSuccesses = vals.deathSaveSuccesses; if (vals.deathSaveFailures !== undefined) out.deathSaveFailures = vals.deathSaveFailures; @@ -1206,30 +1208,90 @@ async function reviveParticipant(encounter, participantId, ctx) { return commit(encounter, patch, log, ctx); } -// markDead: DM manually sets status dead. Both rulesets. Monster = auto-inactive. +async function healFullParticipant(encounter, participantId, ctx) { + const participant = (encounter.participants || []).find(p => p.id === participantId); + if (!participant) throw new Error('Participant not found.'); + const status = participant.status || (participant.currentHp <= 0 ? 'down' : 'conscious'); + const oldValues = { + currentHp: participant.currentHp, + status, + deathSaveSuccesses: participant.deathSaveSuccesses || 0, + deathSaveFailures: participant.deathSaveFailures || 0, + stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null, + isActive: participant.isActive, + conditions: [...(participant.conditions || [])], + }; + const updates = { + currentHp: participant.maxHp, + status: 'conscious', + deathSaveSuccesses: 0, + deathSaveFailures: 0, + stableRegainHours: null, + isActive: true, + }; + const updatedParticipants = (encounter.participants || []).map(p => + p.id === participantId ? { ...p, ...withDeathStatusConditions(p, updates) } : p + ); + const newP = updatedParticipants.find(p => p.id === participantId); + const newValues = { + currentHp: newP.currentHp, + status: newP.status, + deathSaveSuccesses: newP.deathSaveSuccesses || 0, + deathSaveFailures: newP.deathSaveFailures || 0, + stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null, + isActive: newP.isActive, + conditions: [...(newP.conditions || [])], + }; + return commit(encounter, { participants: updatedParticipants }, { + type: 'heal_full', participantId, participantName: participant.name, + message: `${participant.name} healed to full (${participant.maxHp} HP)`, + delta: { currentHp: participant.maxHp, status: 'conscious', isActive: true }, + undo: { oldValues, newValues }, + }, ctx); +} + +// markDead: DM manually kills participant. Sets 0 HP + inactive. async function markDead(encounter, participantId, ctx) { const participant = (encounter.participants || []).find(p => p.id === participantId); if (!participant) throw new Error('Participant not found.'); if (participant.status === 'dead') return encounter; const oldValues = { currentHp: participant.currentHp, + tempHp: participant.tempHp || 0, status: participant.status || 'conscious', + deathSaveSuccesses: participant.deathSaveSuccesses || 0, + deathSaveFailures: participant.deathSaveFailures || 0, + stableRegainHours: participant.stableRegainHours !== undefined ? participant.stableRegainHours : null, isActive: participant.isActive, conditions: [...(participant.conditions || [])], }; - const updates = { status: 'dead', ...(participant.type === 'monster' ? { isActive: false } : {}) }; + const updates = { + currentHp: 0, + tempHp: 0, + status: 'dead', + deathSaveSuccesses: 0, + deathSaveFailures: 0, + stableRegainHours: null, + isActive: false, + }; const updatedParticipants = (encounter.participants || []).map(p => p.id === participantId ? { ...p, ...withDeathStatusConditions(p, updates) } : p ); const newP = updatedParticipants.find(p => p.id === participantId); const newValues = { - currentHp: newP.currentHp, status: newP.status, - isActive: newP.isActive, conditions: [...(newP.conditions || [])], + currentHp: newP.currentHp, + tempHp: newP.tempHp || 0, + status: newP.status, + deathSaveSuccesses: newP.deathSaveSuccesses || 0, + deathSaveFailures: newP.deathSaveFailures || 0, + stableRegainHours: newP.stableRegainHours !== undefined ? newP.stableRegainHours : null, + isActive: newP.isActive, + conditions: [...(newP.conditions || [])], }; return commit(encounter, { participants: updatedParticipants }, { type: 'mark_dead', participantId, participantName: participant.name, - message: `${participant.name} marked dead`, - delta: { status: 'dead' }, + message: `${participant.name} killed`, + delta: { status: 'dead', currentHp: 0, isActive: false }, undo: { oldValues, newValues }, }, ctx); } @@ -1389,7 +1451,7 @@ module.exports = { makeEffect, makeParticipant, buildCharacterParticipant, buildMonsterParticipant, startEncounter, nextTurn, togglePause, addParticipant, addParticipants, updateParticipant, removeParticipant, - toggleParticipantActive, applyHpChange, setTempHp, deathSave, stabilizeParticipant, reviveParticipant, markDead, toggleCondition, + toggleParticipantActive, applyHpChange, setTempHp, deathSave, stabilizeParticipant, reviveParticipant, healFullParticipant, markDead, toggleCondition, addEffect, updateEffect, removeEffect, reorderParticipants, endEncounter, rerollAllInitiatives, activateDisplay, clearDisplay, toggleHidePlayerHp, diff --git a/src/App.js b/src/App.js index f5965a9..cd61bdf 100644 --- a/src/App.js +++ b/src/App.js @@ -127,6 +127,7 @@ const { deathSave: combatDeathSave, stabilizeParticipant, reviveParticipant, + healFullParticipant, markDead, toggleCondition: combatToggleCondition, reorderParticipants, @@ -1423,6 +1424,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp const [draggedItemId, setDraggedItemId] = useState(null); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [itemToDelete, setItemToDelete] = useState(null); + const [killTarget, setKillTarget] = useState(null); const [lastRollDetails, setLastRollDetails] = useState(null); const [openConditionsId, setOpenConditionsId] = useState(null); const [customConditionInput, setCustomConditionInput] = useState(''); @@ -1439,6 +1441,16 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp ]; const participants = encounter.participants || []; + const isDeadParticipant = p => p.status === 'dead' || ((encounter.ruleset || '5e') === '5e' && p.type === 'monster' && p.currentHp === 0); + // Independent filters: Hide Disabled applies to every inactive participant, + // including dead PCs. Hide Dead applies only to dead monsters/NPCs. + const deadParticipants = participants.filter(p => isDeadParticipant(p) && p.type !== 'character'); + const disabledParticipants = participants.filter(p => p.isActive === false); + const visibleParticipants = participants.filter(p => { + if (encounter.hideDisabledParticipants && p.isActive === false) return false; + if (encounter.hideDeadParticipants && isDeadParticipant(p) && p.type !== 'character') return false; + return true; + }); const [editingField, setEditingField] = useState(null); const [editingId, setEditingId] = useState(null); const blurTimeoutRef = useRef(null); @@ -1739,6 +1751,15 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp } }; + const handleHealFull = async (participantId) => { + if (!db) return; + try { + await healFullParticipant(encounter, participantId, buildCtx(encounterPath)); + } catch (err) { + showToast(err.message); + } + }; + const handleMarkDead = async (participantId) => { if (!db) return; try { @@ -1748,6 +1769,20 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp } }; + const confirmKill = async () => { + if (!killTarget) return; + await handleMarkDead(killTarget.id); + setKillTarget(null); + }; + + const toggleParticipantVisibility = async (field) => { + try { + await storage.updateDoc(encounterPath, { [field]: !encounter[field] }); + } catch (err) { + showToast('Failed to update participant visibility.'); + } + }; + const handleCritDamage = async (participantId) => { if (!db) return; try { @@ -1890,8 +1925,9 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp setDraggedItemId(null); }; - // 1-list model: participants[] IS the display order. No re-sort. - const sortedParticipants = participants; + // 1-list model: participants[] IS display order. Visibility filters only render; + // encounter data/order remain untouched. + const sortedParticipants = visibleParticipants; const initiativeGroups = participants.reduce((acc, p) => { acc[p.initiative] = (acc[p.initiative] || 0) + 1; @@ -2138,7 +2174,28 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp )} +
+ Roster visibility + + +
+ {participants.length === 0 &&

No participants added yet.

} + {participants.length > 0 && visibleParticipants.length === 0 &&

All participants hidden.

}