feat: encounter UX, tracked effects, and character state improvements #10

Open
keen wants to merge 14 commits from fix/encounter-ux into main
pull from: fix/encounter-ux
2 changed files with 41 additions and 11 deletions
Showing only changes of commit 0e7bc7c099 - Show all commits

No files matched your search

+25 -6
View File
@@ -1329,8 +1329,7 @@ function EffectModal({ encounter, defaultScope, defaultTargetParticipantId, onCl
); );
} }
function EffectControlRow({ effect, participants, onAdjust, onSet, onRemove }) { function EffectControlRow({ effect, participants, onAdjust, onSet, onSetOwner, onRemove }) {
const owner = participants.find(p => p.id === effect.ownerParticipantId);
const [draftRounds, setDraftRounds] = useState(String(effect.remainingRounds)); const [draftRounds, setDraftRounds] = useState(String(effect.remainingRounds));
useEffect(() => setDraftRounds(String(effect.remainingRounds)), [effect.remainingRounds]); useEffect(() => setDraftRounds(String(effect.remainingRounds)), [effect.remainingRounds]);
const commitRounds = () => { const commitRounds = () => {
@@ -1357,7 +1356,18 @@ function EffectControlRow({ effect, participants, onAdjust, onSet, onRemove }) {
/> />
rounds rounds
</label> </label>
{owner && <span className="text-xs text-stone-300">Owner: {owner.name}</span>} <label className="inline-flex items-center gap-1 text-xs text-stone-300">
Owner
<select
value={effect.ownerParticipantId || ''}
onChange={(e) => onSetOwner(effect, e.target.value || null)}
aria-label={`Owner for ${effect.name}`}
className="rounded border border-stone-600 bg-stone-900 px-1.5 py-0.5 text-stone-100"
>
<option value="">None</option>
{participants.map(p => <option key={p.id} value={p.id}>{p.name}</option>)}
</select>
</label>
{effect.expiresOnDamage && <span className="text-xs text-red-300">Ends on damage</span>} {effect.expiresOnDamage && <span className="text-xs text-red-300">Ends on damage</span>}
<span className="ml-auto flex gap-1"> <span className="ml-auto flex gap-1">
<button type="button" onClick={() => onAdjust(effect, -1)} aria-label={`Decrease ${effect.name} duration`} className="px-2 py-0.5 rounded bg-stone-700 hover:bg-stone-600">−</button> <button type="button" onClick={() => onAdjust(effect, -1)} aria-label={`Decrease ${effect.name} duration`} className="px-2 py-0.5 rounded bg-stone-700 hover:bg-stone-600">−</button>
@@ -1788,7 +1798,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
} else if (presetId === 'spiritual_weapon') { } else if (presetId === 'spiritual_weapon') {
await combatAddEffect(encounter, { await combatAddEffect(encounter, {
presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter', presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter',
ownerParticipantId: participant.id, ownerParticipantId: encounter.currentTurnParticipantId || participant.id,
remainingRounds: 10, expiresOnDamage: false, remainingRounds: 10, expiresOnDamage: false,
playerDisplay: 'prominent', createdRound: encounter.round || 0, playerDisplay: 'prominent', createdRound: encounter.round || 0,
}, buildCtx(encounterPath)); }, buildCtx(encounterPath));
@@ -1817,6 +1827,15 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
} }
}; };
const handleSetEffectOwner = async (effect, ownerParticipantId) => {
if (!db) return;
try {
await combatUpdateEffect(encounter, effect.id, { ownerParticipantId }, buildCtx(encounterPath));
} catch (err) {
showToast(err.message || 'Failed to update effect owner.');
}
};
const handleRemoveEffect = async (effect) => { const handleRemoveEffect = async (effect) => {
if (!db) return; if (!db) return;
try { try {
@@ -2114,7 +2133,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
<div className="my-3 rounded-md border border-stone-700 bg-stone-950/60 p-3"> <div className="my-3 rounded-md border border-stone-700 bg-stone-950/60 p-3">
<h4 className="font-semibold text-purple-200">Encounter Effects</h4> <h4 className="font-semibold text-purple-200">Encounter Effects</h4>
{(encounter.effects || []).filter(effect => effect.scope === 'encounter' && (!effect.ownerParticipantId || !participants.some(p => p.id === effect.ownerParticipantId))).map(effect => ( {(encounter.effects || []).filter(effect => effect.scope === 'encounter' && (!effect.ownerParticipantId || !participants.some(p => p.id === effect.ownerParticipantId))).map(effect => (
<EffectControlRow key={effect.id} effect={effect} participants={participants} onAdjust={handleAdjustEffect} onSet={handleSetEffectDuration} onRemove={handleRemoveEffect} /> <EffectControlRow key={effect.id} effect={effect} participants={participants} onAdjust={handleAdjustEffect} onSet={handleSetEffectDuration} onSetOwner={handleSetEffectOwner} onRemove={handleRemoveEffect} />
))} ))}
</div> </div>
)} )}
@@ -2352,7 +2371,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
(effect.scope === 'participant' && effect.targetParticipantId === p.id) || (effect.scope === 'participant' && effect.targetParticipantId === p.id) ||
(effect.scope === 'encounter' && effect.ownerParticipantId === p.id) (effect.scope === 'encounter' && effect.ownerParticipantId === p.id)
).map(effect => ( ).map(effect => (
<EffectControlRow key={effect.id} effect={effect} participants={participants} onAdjust={handleAdjustEffect} onSet={handleSetEffectDuration} onRemove={handleRemoveEffect} /> <EffectControlRow key={effect.id} effect={effect} participants={participants} onAdjust={handleAdjustEffect} onSet={handleSetEffectDuration} onSetOwner={handleSetEffectOwner} onRemove={handleRemoveEffect} />
))} ))}
{/* Expandable conditions picker */} {/* Expandable conditions picker */}
+16 -5
View File
@@ -2,7 +2,7 @@ import { screen, fireEvent, waitFor, within } from '@testing-library/react';
import '@testing-library/jest-dom'; import '@testing-library/jest-dom';
import { import {
setupReady, addMonsterViaUI, renderApp, createCampaignViaUI, setupReady, addMonsterViaUI, renderApp, createCampaignViaUI,
selectCampaignByName, selectEncounterByName, MOCK_DB, selectCampaignByName, selectEncounterByName, startCombatViaUI, MOCK_DB,
} from './testHelpers'; } from './testHelpers';
function latestEncounter() { function latestEncounter() {
@@ -57,16 +57,27 @@ describe('tracked effects DM UI', () => {
test('Spiritual Weapon preset creates 10-round encounter effect', async () => { test('Spiritual Weapon preset creates 10-round encounter effect', async () => {
await setupReady('Weapon Camp', 'Weapon Encounter'); await setupReady('Weapon Camp', 'Weapon Encounter');
await addMonsterViaUI('Cleric', 30, 2); await addMonsterViaUI('Cleric', 30, 2);
await addMonsterViaUI('Wizard', 24, 3);
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' })); await startCombatViaUI();
const activeId = latestEncounter().currentTurnParticipantId;
const otherId = latestEncounter().participants.find(p => p.id !== activeId).id;
fireEvent.click(within(screen.getByTestId(`admin-participant-card-${otherId}`)).getByRole('button', { name: 'Open conditions' }));
fireEvent.click(screen.getByRole('button', { name: 'Spiritual Weapon' })); fireEvent.click(screen.getByRole('button', { name: 'Spiritual Weapon' }));
await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({ await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({
presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter', presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter',
remainingRounds: 10, expiresOnDamage: false, playerDisplay: 'prominent', ownerParticipantId: activeId, remainingRounds: 10,
expiresOnDamage: false, playerDisplay: 'prominent',
})); }));
const clericId = latestEncounter().participants.find(p => p.name === 'Cleric').id; expect(within(screen.getByTestId(`admin-participant-card-${activeId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument();
expect(within(screen.getByTestId(`admin-participant-card-${clericId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument();
const ownerSelect = screen.getByLabelText('Owner for Spiritual Weapon');
expect(ownerSelect).toHaveValue(activeId);
fireEvent.change(ownerSelect, { target: { value: otherId } });
await waitFor(() => expect(latestEncounter().effects[0].ownerParticipantId).toBe(otherId));
expect(within(screen.getByTestId(`admin-participant-card-${otherId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument();
expect(within(screen.getByTestId(`admin-participant-card-${activeId}`)).queryByText('SPIRITUAL WEAPON')).not.toBeInTheDocument();
}); });
test('generic ruleset offers custom effects only and rounds only', async () => { test('generic ruleset offers custom effects only and rounds only', async () => {