feat: add round-based tracked effects
- add generic encounter effects with round countdown, damage expiry, logs, and undo/redo - add 5e Turned and Spiritual Weapon presets inside existing Conditions panel - support custom participant or encounter effects with owner, notes, duration, and player visibility - convert 5e minute durations to rounds while keeping generic rules round-only - show prominent or compact effects on player and DM participant cards - attach owner-linked encounter effects to owner card with global fallback for ownerless effects - add direct turns-left editing plus step controls
This commit is contained in:
1 parent
bca406fca9
commit
295c7e06b4
5 files changed
+814
-28
No files matched your search
+360
-1
@@ -130,6 +130,9 @@ const {
|
||||
markDead,
|
||||
toggleCondition: combatToggleCondition,
|
||||
reorderParticipants,
|
||||
addEffect: combatAddEffect,
|
||||
updateEffect: combatUpdateEffect,
|
||||
removeEffect: combatRemoveEffect,
|
||||
} = shared;
|
||||
const ROLL_DISPLAY_DURATION = 5000;
|
||||
|
||||
@@ -1189,6 +1192,204 @@ function CharacterManager({ campaignId, campaignCharacters, syncCharacters }) {
|
||||
);
|
||||
}
|
||||
|
||||
function EffectModal({ encounter, defaultScope, defaultTargetParticipantId, onClose, onAdd }) {
|
||||
const is5e = (encounter.ruleset || '5e') === '5e';
|
||||
const participants = encounter.participants || [];
|
||||
const [presetId, setPresetId] = useState('custom');
|
||||
const [name, setName] = useState('');
|
||||
const [scope, setScope] = useState(defaultScope || 'participant');
|
||||
const [targetParticipantId, setTargetParticipantId] = useState(defaultTargetParticipantId || participants[0]?.id || '');
|
||||
const [ownerParticipantId, setOwnerParticipantId] = useState(encounter.currentTurnParticipantId || '');
|
||||
const [duration, setDuration] = useState('10');
|
||||
const [durationUnit, setDurationUnit] = useState('rounds');
|
||||
const [expiresOnDamage, setExpiresOnDamage] = useState(false);
|
||||
const [playerDisplay, setPlayerDisplay] = useState('compact');
|
||||
const [notes, setNotes] = useState('');
|
||||
|
||||
const selectPreset = (value) => {
|
||||
setPresetId(value);
|
||||
if (value === 'turned') {
|
||||
setName('Turned');
|
||||
setScope('participant');
|
||||
setTargetParticipantId(defaultTargetParticipantId || participants[0]?.id || '');
|
||||
setDuration('10');
|
||||
setDurationUnit('rounds');
|
||||
setExpiresOnDamage(true);
|
||||
setPlayerDisplay('prominent');
|
||||
} else if (value === 'spiritual_weapon') {
|
||||
setName('Spiritual Weapon');
|
||||
setScope('encounter');
|
||||
setDuration('10');
|
||||
setDurationUnit('rounds');
|
||||
setExpiresOnDamage(false);
|
||||
setPlayerDisplay('prominent');
|
||||
} else {
|
||||
setName('');
|
||||
setScope(defaultScope || 'participant');
|
||||
setTargetParticipantId(defaultTargetParticipantId || participants[0]?.id || '');
|
||||
setDuration('10');
|
||||
setDurationUnit('rounds');
|
||||
setExpiresOnDamage(false);
|
||||
setPlayerDisplay('compact');
|
||||
}
|
||||
};
|
||||
|
||||
const submit = (e) => {
|
||||
e.preventDefault();
|
||||
const n = Math.max(1, parseInt(duration, 10) || 1);
|
||||
if (!name.trim() || (scope === 'participant' && !targetParticipantId)) return;
|
||||
onAdd({
|
||||
presetId,
|
||||
name: name.trim(),
|
||||
scope,
|
||||
targetParticipantId: scope === 'participant' ? targetParticipantId : null,
|
||||
ownerParticipantId: ownerParticipantId || null,
|
||||
remainingRounds: durationUnit === 'minutes' && is5e ? n * 10 : n,
|
||||
expiresOnDamage,
|
||||
playerDisplay,
|
||||
notes,
|
||||
createdRound: encounter.round || 0,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal onClose={onClose} title="Add Tracked Effect">
|
||||
<form onSubmit={submit} className="space-y-3">
|
||||
<label className="block text-sm text-stone-300">
|
||||
Effect preset
|
||||
<select aria-label="Effect preset" value={presetId} onChange={(e) => selectPreset(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
<option value="custom">Custom</option>
|
||||
{is5e && <option value="turned">Turned</option>}
|
||||
{is5e && <option value="spiritual_weapon">Spiritual Weapon</option>}
|
||||
</select>
|
||||
</label>
|
||||
<label className="block text-sm text-stone-300">
|
||||
Effect name
|
||||
<input aria-label="Effect name" value={name} onChange={(e) => setName(e.target.value)} maxLength={60} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white" />
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<label className="block text-sm text-stone-300">
|
||||
Scope
|
||||
<select aria-label="Effect scope" value={scope} onChange={(e) => setScope(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
<option value="participant">Participant</option>
|
||||
<option value="encounter">Encounter</option>
|
||||
</select>
|
||||
</label>
|
||||
{scope === 'participant' && (
|
||||
<label className="block text-sm text-stone-300">
|
||||
Target
|
||||
<select aria-label="Effect target" value={targetParticipantId} onChange={(e) => setTargetParticipantId(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
{participants.map(p => <option key={p.id} value={p.id}>{p.name}</option>)}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
<label className="block text-sm text-stone-300">
|
||||
Owner (optional)
|
||||
<select aria-label="Effect owner" value={ownerParticipantId} onChange={(e) => setOwnerParticipantId(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
<option value="">None</option>
|
||||
{participants.map(p => <option key={p.id} value={p.id}>{p.name}</option>)}
|
||||
</select>
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<label className="block text-sm text-stone-300">
|
||||
Duration
|
||||
<input aria-label="Duration" type="number" min="1" value={duration} onChange={(e) => setDuration(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white" />
|
||||
</label>
|
||||
<label className="block text-sm text-stone-300">
|
||||
Duration unit
|
||||
<select aria-label="Duration unit" value={durationUnit} onChange={(e) => setDurationUnit(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
<option value="rounds">Rounds</option>
|
||||
{is5e && <option value="minutes">Minutes (×10 rounds)</option>}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<label className="block text-sm text-stone-300">
|
||||
Player display
|
||||
<select aria-label="Player display" value={playerDisplay} onChange={(e) => setPlayerDisplay(e.target.value)} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white">
|
||||
<option value="hidden">Hidden from players</option>
|
||||
<option value="compact">Compact badge</option>
|
||||
<option value="prominent">Prominent banner</option>
|
||||
</select>
|
||||
</label>
|
||||
<label className="flex items-center gap-2 text-sm text-stone-300">
|
||||
<input type="checkbox" checked={expiresOnDamage} onChange={(e) => setExpiresOnDamage(e.target.checked)} />
|
||||
End when target takes damage
|
||||
</label>
|
||||
<label className="block text-sm text-stone-300">
|
||||
Notes (optional)
|
||||
<input aria-label="Effect notes" value={notes} onChange={(e) => setNotes(e.target.value)} maxLength={160} className="mt-1 w-full px-3 py-2 bg-stone-800 border border-stone-600 rounded text-white" />
|
||||
</label>
|
||||
<div className="flex justify-end gap-3 pt-2">
|
||||
<button type="button" onClick={onClose} className="px-4 py-2 rounded bg-stone-700 text-stone-200">Cancel</button>
|
||||
<button type="submit" disabled={!name.trim() || (scope === 'participant' && !targetParticipantId)} className="px-4 py-2 rounded bg-purple-700 hover:bg-purple-600 text-white disabled:opacity-40">Add Effect</button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
function EffectControlRow({ effect, participants, onAdjust, onSet, onRemove }) {
|
||||
const owner = participants.find(p => p.id === effect.ownerParticipantId);
|
||||
const [draftRounds, setDraftRounds] = useState(String(effect.remainingRounds));
|
||||
useEffect(() => setDraftRounds(String(effect.remainingRounds)), [effect.remainingRounds]);
|
||||
const commitRounds = () => {
|
||||
const value = Math.max(1, parseInt(draftRounds, 10) || 1);
|
||||
setDraftRounds(String(value));
|
||||
if (value !== effect.remainingRounds) onSet(effect, value);
|
||||
};
|
||||
return (
|
||||
<div className="mt-2 flex flex-wrap items-center gap-2 rounded-md border border-purple-500 bg-purple-950/70 px-3 py-2 text-purple-100" data-testid={`dm-effect-${effect.id}`}>
|
||||
<span className="font-bold tracking-wide">{effect.name.toUpperCase()}</span>
|
||||
<label className="inline-flex items-center gap-1 text-sm text-purple-200">
|
||||
<input
|
||||
type="number"
|
||||
min="1"
|
||||
value={draftRounds}
|
||||
onChange={(e) => setDraftRounds(e.target.value)}
|
||||
onBlur={commitRounds}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') e.currentTarget.blur();
|
||||
if (e.key === 'Escape') { setDraftRounds(String(effect.remainingRounds)); e.currentTarget.blur(); }
|
||||
}}
|
||||
aria-label={`Turns left for ${effect.name}`}
|
||||
className="w-14 rounded border border-purple-500 bg-stone-900 px-1.5 py-0.5 text-center text-white"
|
||||
/>
|
||||
rounds
|
||||
</label>
|
||||
{owner && <span className="text-xs text-stone-300">Owner: {owner.name}</span>}
|
||||
{effect.expiresOnDamage && <span className="text-xs text-red-300">Ends on damage</span>}
|
||||
<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={`Increase ${effect.name} duration`} className="px-2 py-0.5 rounded bg-stone-700 hover:bg-stone-600">+</button>
|
||||
<button type="button" onClick={() => onRemove(effect)} aria-label={`Remove ${effect.name}`} className="px-2 py-0.5 rounded bg-red-900 hover:bg-red-800 text-red-200">×</button>
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PlayerEffectDisplay({ effect, ownerName }) {
|
||||
if (!effect || effect.playerDisplay === 'hidden') return null;
|
||||
const testId = effect.scope === 'encounter' ? `encounter-effect-${effect.id}` : `prominent-effect-${effect.id}`;
|
||||
if (effect.playerDisplay === 'prominent') {
|
||||
return (
|
||||
<div data-testid={testId} className="mt-3 rounded-lg border-2 border-purple-400 bg-purple-950/90 px-4 py-3 text-center text-purple-100 shadow-lg">
|
||||
<div className="text-lg md:text-xl font-bold tracking-widest">{effect.name.toUpperCase()}</div>
|
||||
<div className="text-sm md:text-base text-purple-200">
|
||||
{ownerName && <span>{ownerName} · </span>}{effect.remainingRounds} rounds remaining
|
||||
{effect.expiresOnDamage && <span className="ml-2 text-red-300">· Ends on damage</span>}
|
||||
</div>
|
||||
{effect.notes && <div className="mt-1 text-xs md:text-sm text-stone-300">{effect.notes}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 mt-2 mr-1 px-2 py-1 rounded-full bg-purple-900 border border-purple-500 text-xs md:text-sm text-purple-100 font-medium">
|
||||
{effect.name} · {effect.remainingRounds} rounds{ownerName ? ` · ${ownerName}` : ''}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PARTICIPANT MANAGER COMPONENT
|
||||
// ============================================================================
|
||||
@@ -1215,6 +1416,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
const [lastRollDetails, setLastRollDetails] = useState(null);
|
||||
const [openConditionsId, setOpenConditionsId] = useState(null);
|
||||
const [customConditionInput, setCustomConditionInput] = useState('');
|
||||
const [effectModal, setEffectModal] = useState(null);
|
||||
|
||||
// Per-campaign custom conditions (DM-added freeform strings).
|
||||
const { data: campaignDoc } = useFirestoreDocument(campaignId ? getPath.campaign(campaignId) : null);
|
||||
@@ -1555,6 +1757,75 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddEffect = async (effect) => {
|
||||
if (!db) return;
|
||||
try {
|
||||
await combatAddEffect(encounter, effect, buildCtx(encounterPath));
|
||||
setEffectModal(null);
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Failed to add effect.');
|
||||
}
|
||||
};
|
||||
|
||||
const togglePresetEffect = async (presetId, participant) => {
|
||||
if (!db) return;
|
||||
const existing = (encounter.effects || []).find(effect => presetId === 'turned'
|
||||
? effect.presetId === 'turned' && effect.targetParticipantId === participant.id
|
||||
: effect.presetId === 'spiritual_weapon' && effect.ownerParticipantId === participant.id);
|
||||
try {
|
||||
if (existing) {
|
||||
await combatRemoveEffect(encounter, existing.id, buildCtx(encounterPath));
|
||||
return;
|
||||
}
|
||||
if (presetId === 'turned') {
|
||||
await combatAddEffect(encounter, {
|
||||
presetId: 'turned', name: 'Turned', scope: 'participant',
|
||||
targetParticipantId: participant.id,
|
||||
ownerParticipantId: encounter.currentTurnParticipantId || null,
|
||||
remainingRounds: 10, expiresOnDamage: true,
|
||||
playerDisplay: 'prominent', createdRound: encounter.round || 0,
|
||||
}, buildCtx(encounterPath));
|
||||
} else if (presetId === 'spiritual_weapon') {
|
||||
await combatAddEffect(encounter, {
|
||||
presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter',
|
||||
ownerParticipantId: participant.id,
|
||||
remainingRounds: 10, expiresOnDamage: false,
|
||||
playerDisplay: 'prominent', createdRound: encounter.round || 0,
|
||||
}, buildCtx(encounterPath));
|
||||
}
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Failed to update effect.');
|
||||
}
|
||||
};
|
||||
|
||||
const handleAdjustEffect = async (effect, delta) => {
|
||||
if (!db) return;
|
||||
const next = Math.max(1, (effect.remainingRounds || 1) + delta);
|
||||
try {
|
||||
await combatUpdateEffect(encounter, effect.id, { remainingRounds: next }, buildCtx(encounterPath));
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Failed to update effect.');
|
||||
}
|
||||
};
|
||||
|
||||
const handleSetEffectDuration = async (effect, remainingRounds) => {
|
||||
if (!db) return;
|
||||
try {
|
||||
await combatUpdateEffect(encounter, effect.id, { remainingRounds }, buildCtx(encounterPath));
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Failed to update effect.');
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemoveEffect = async (effect) => {
|
||||
if (!db) return;
|
||||
try {
|
||||
await combatRemoveEffect(encounter, effect.id, buildCtx(encounterPath));
|
||||
} catch (err) {
|
||||
showToast(err.message || 'Failed to remove effect.');
|
||||
}
|
||||
};
|
||||
|
||||
// Add freeform condition: persist to campaign palette + apply to current participant.
|
||||
const addCustomCondition = async (label, participantId = null) => {
|
||||
const trimmed = (label || '').trim();
|
||||
@@ -1839,6 +2110,15 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
);
|
||||
})()}
|
||||
|
||||
{(encounter.effects || []).some(effect => effect.scope === 'encounter' && (!effect.ownerParticipantId || !participants.some(p => p.id === effect.ownerParticipantId))) && (
|
||||
<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>
|
||||
{(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} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{participants.length === 0 && <p className="text-sm text-stone-400">No participants added yet.</p>}
|
||||
|
||||
|
||||
@@ -1863,6 +2143,7 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
return (
|
||||
<li
|
||||
key={p.id}
|
||||
data-testid={`admin-participant-card-${p.id}`}
|
||||
draggable={isDraggable}
|
||||
onDragStart={isDraggable ? (e) => handleDragStart(e, p.id) : undefined}
|
||||
onDragOver={isDraggable ? handleDragOver : undefined}
|
||||
@@ -2067,6 +2348,13 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
</div>
|
||||
)}
|
||||
|
||||
{(encounter.effects || []).filter(effect =>
|
||||
(effect.scope === 'participant' && effect.targetParticipantId === p.id) ||
|
||||
(effect.scope === 'encounter' && effect.ownerParticipantId === p.id)
|
||||
).map(effect => (
|
||||
<EffectControlRow key={effect.id} effect={effect} participants={participants} onAdjust={handleAdjustEffect} onSet={handleSetEffectDuration} onRemove={handleRemoveEffect} />
|
||||
))}
|
||||
|
||||
{/* Expandable conditions picker */}
|
||||
{openConditionsId === p.id && (
|
||||
<div className="mt-2 p-2 bg-stone-900 rounded-md border border-stone-600">
|
||||
@@ -2086,6 +2374,48 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{(encounter.ruleset || '5e') === '5e' && (
|
||||
<>
|
||||
<p className="text-xs text-stone-400 mt-3 mb-1 font-medium">Tracked Effects</p>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{(() => {
|
||||
const active = (encounter.effects || []).some(effect => effect.presetId === 'turned' && effect.targetParticipantId === p.id);
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => togglePresetEffect('turned', p)}
|
||||
aria-label="Turned"
|
||||
className={`px-2 py-1 rounded text-xs transition-colors ${active ? 'bg-purple-700 border border-purple-300 text-white' : 'bg-stone-700 border border-stone-500 text-stone-300 hover:bg-stone-600'}`}
|
||||
>
|
||||
↪ Turned · 10 rounds
|
||||
</button>
|
||||
);
|
||||
})()}
|
||||
{(() => {
|
||||
const active = (encounter.effects || []).some(effect => effect.presetId === 'spiritual_weapon' && effect.ownerParticipantId === p.id);
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => togglePresetEffect('spiritual_weapon', p)}
|
||||
aria-label="Spiritual Weapon"
|
||||
className={`px-2 py-1 rounded text-xs transition-colors ${active ? 'bg-purple-700 border border-purple-300 text-white' : 'bg-stone-700 border border-stone-500 text-stone-300 hover:bg-stone-600'}`}
|
||||
>
|
||||
✨ Spiritual Weapon · 10 rounds
|
||||
</button>
|
||||
);
|
||||
})()}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setEffectModal({ scope: 'participant', targetParticipantId: p.id })}
|
||||
aria-label="Custom tracked effect"
|
||||
className="mt-2 px-2 py-1 rounded text-xs bg-purple-900 border border-purple-600 text-purple-100 hover:bg-purple-800"
|
||||
>
|
||||
+ Custom tracked effect
|
||||
</button>
|
||||
|
||||
{/* Add custom condition (per-campaign, freeform string) */}
|
||||
<div className="mt-2 flex gap-1">
|
||||
<input
|
||||
@@ -2206,6 +2536,17 @@ function ParticipantManager({ encounter, encounterPath, campaignCharacters, camp
|
||||
)}
|
||||
</div>
|
||||
|
||||
{effectModal && (
|
||||
<EffectModal
|
||||
key={`${effectModal.scope}-${effectModal.targetParticipantId || 'encounter'}`}
|
||||
encounter={encounter}
|
||||
defaultScope={effectModal.scope}
|
||||
defaultTargetParticipantId={effectModal.targetParticipantId}
|
||||
onClose={() => setEffectModal(null)}
|
||||
onAdd={handleAddEffect}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ConfirmationModal
|
||||
isOpen={showDeleteConfirm}
|
||||
onClose={() => setShowDeleteConfirm(false)}
|
||||
@@ -3684,6 +4025,7 @@ function PlayerParticipantCard({ id, isActive, status, onExit, className, childr
|
||||
return (
|
||||
<div
|
||||
ref={divRef}
|
||||
data-testid={`participant-card-${id}`}
|
||||
className={`[transition:opacity_1s_ease-in-out,transform_1s_ease-in-out,filter_1s_ease-in-out,border-color_1s_ease-in-out,box-shadow_300ms_ease-in-out] ${fadeClass} ${deathClass} ${className}`}
|
||||
>
|
||||
{children}
|
||||
@@ -3918,6 +4260,7 @@ function DisplayView() {
|
||||
}
|
||||
|
||||
const { name, participants, round, currentTurnParticipantId, isStarted, isPaused } = activeEncounterData;
|
||||
const trackedEffects = activeEncounterData.effects || [];
|
||||
const hidePlayerHp = activeDisplayData?.hidePlayerHp ?? true;
|
||||
const hideNpcHp = activeDisplayData?.hideNpcHp ?? false;
|
||||
|
||||
@@ -3974,6 +4317,12 @@ function DisplayView() {
|
||||
<p className="text-xl text-stone-400">No active participants.</p>
|
||||
)}
|
||||
|
||||
<div className="max-w-3xl mx-auto mb-4">
|
||||
{trackedEffects
|
||||
.filter(effect => effect.scope === 'encounter' && effect.playerDisplay !== 'hidden' && (!effect.ownerParticipantId || !participants.some(p => p.id === effect.ownerParticipantId)))
|
||||
.map(effect => <PlayerEffectDisplay key={effect.id} effect={effect} />)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-4 max-w-3xl mx-auto">
|
||||
{participantsToRender.map(p => {
|
||||
const isGeneric = (activeEncounterData.ruleset || '5e') === 'generic';
|
||||
@@ -4022,8 +4371,18 @@ function DisplayView() {
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{trackedEffects
|
||||
.filter(effect => (
|
||||
(effect.scope === 'participant' && effect.targetParticipantId === p.id) ||
|
||||
(effect.scope === 'encounter' && effect.ownerParticipantId === p.id)
|
||||
) && effect.playerDisplay !== 'hidden')
|
||||
.map(effect => {
|
||||
const owner = participants.find(ownerParticipant => ownerParticipant.id === effect.ownerParticipantId);
|
||||
return <PlayerEffectDisplay key={effect.id} effect={effect} ownerName={owner?.name} />;
|
||||
})}
|
||||
|
||||
{!(hidePlayerHp && p.type === 'character') && !(hideNpcHp && p.type !== 'character') && (
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex justify-between items-center mt-2">
|
||||
<div className="w-full bg-stone-700 rounded-full h-6 md:h-8 relative overflow-hidden border-2 border-stone-600">
|
||||
<div
|
||||
className={`h-full rounded-full transition-all ${isZeroHp ? 'bg-red-900' : (p.currentHp <= p.maxHp / 4 ? 'bg-red-500' : (p.currentHp <= p.maxHp / 2 ? 'bg-yellow-500' : 'bg-green-500'))}`}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import React from 'react';
|
||||
import { render, screen, within } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import App from '../App';
|
||||
import { MOCK_DB } from '../__mocks__/firebase/_mock-db';
|
||||
|
||||
const campaignPath = 'artifacts/ttrpg-initiative-tracker-default/public/data/campaigns/c1';
|
||||
const encounterPath = `${campaignPath}/encounters/e1`;
|
||||
const activeDisplayPath = 'artifacts/ttrpg-initiative-tracker-default/public/data/activeDisplay/status';
|
||||
|
||||
function seed(effects) {
|
||||
const participants = [
|
||||
{ id: 'p1', name: 'Cleric', type: 'character', initiative: 18, maxHp: 30, currentHp: 30, isActive: true, conditions: [] },
|
||||
{ id: 'p2', name: 'Zombie', type: 'monster', initiative: 8, maxHp: 22, currentHp: 22, isActive: true, conditions: [] },
|
||||
];
|
||||
MOCK_DB.set(campaignPath, { name: 'Camp', playerDisplayBackgroundUrl: '' });
|
||||
MOCK_DB.set(encounterPath, { name: 'Enc', participants, effects, ruleset: '5e', isStarted: true, round: 2, currentTurnParticipantId: 'p1' });
|
||||
MOCK_DB.set(activeDisplayPath, { activeCampaignId: 'c1', activeEncounterId: 'e1', hidePlayerHp: false });
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
const favicon = document.createElement('link');
|
||||
favicon.rel = 'icon';
|
||||
favicon.href = '/favicon.ico';
|
||||
document.head.appendChild(favicon);
|
||||
window.history.replaceState({}, '', '/display');
|
||||
global.alert = jest.fn();
|
||||
window.open = jest.fn();
|
||||
Element.prototype.scrollIntoView = jest.fn();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.querySelectorAll('link[rel="icon"]').forEach(l => l.remove());
|
||||
window.history.replaceState({}, '', '/');
|
||||
});
|
||||
|
||||
describe('player display tracked effects', () => {
|
||||
test('prominent participant effect renders clear banner with countdown', async () => {
|
||||
seed([{ id: 'fx1', presetId: 'turned', name: 'Turned', scope: 'participant', targetParticipantId: 'p2', ownerParticipantId: 'p1', remainingRounds: 7, expiresOnDamage: true, playerDisplay: 'prominent' }]);
|
||||
render(<App />);
|
||||
await screen.findByText('Zombie');
|
||||
expect(screen.getByTestId('prominent-effect-fx1')).toHaveTextContent('TURNED');
|
||||
expect(screen.getByTestId('prominent-effect-fx1')).toHaveTextContent('7 rounds remaining');
|
||||
expect(screen.getByTestId('prominent-effect-fx1')).toHaveTextContent('Ends on damage');
|
||||
});
|
||||
|
||||
test('compact participant effect renders badge; hidden effect does not render', async () => {
|
||||
seed([
|
||||
{ id: 'fx1', name: 'Bless', scope: 'participant', targetParticipantId: 'p2', remainingRounds: 3, playerDisplay: 'compact' },
|
||||
{ id: 'fx2', name: 'Secret', scope: 'participant', targetParticipantId: 'p2', remainingRounds: 4, playerDisplay: 'hidden' },
|
||||
]);
|
||||
render(<App />);
|
||||
await screen.findByText('Zombie');
|
||||
expect(screen.getByText(/Bless · 3 rounds/i)).toBeInTheDocument();
|
||||
expect(screen.queryByText(/Secret/i)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('owner-linked encounter effect renders prominently on owner card', async () => {
|
||||
seed([{ id: 'fx3', presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter', ownerParticipantId: 'p1', remainingRounds: 8, expiresOnDamage: false, playerDisplay: 'prominent' }]);
|
||||
render(<App />);
|
||||
await screen.findByText('Zombie');
|
||||
const clericCard = screen.getByTestId('participant-card-p1');
|
||||
const banner = within(clericCard).getByTestId('encounter-effect-fx3');
|
||||
expect(banner).toHaveTextContent('SPIRITUAL WEAPON');
|
||||
expect(banner).toHaveTextContent('Cleric');
|
||||
expect(banner).toHaveTextContent('8 rounds remaining');
|
||||
});
|
||||
|
||||
test('ownerless encounter effect remains in global fallback area', async () => {
|
||||
seed([{ id: 'fx4', name: 'Fog Cloud', scope: 'encounter', ownerParticipantId: null, remainingRounds: 5, playerDisplay: 'prominent' }]);
|
||||
render(<App />);
|
||||
await screen.findByText('Zombie');
|
||||
const banner = screen.getByTestId('encounter-effect-fx4');
|
||||
expect(banner).toHaveTextContent('FOG CLOUD');
|
||||
expect(screen.getByTestId('participant-card-p1')).not.toContainElement(banner);
|
||||
expect(screen.getByTestId('participant-card-p2')).not.toContainElement(banner);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
import { screen, fireEvent, waitFor, within } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
import {
|
||||
setupReady, addMonsterViaUI, renderApp, createCampaignViaUI,
|
||||
selectCampaignByName, selectEncounterByName, MOCK_DB,
|
||||
} from './testHelpers';
|
||||
|
||||
function latestEncounter() {
|
||||
const row = [...MOCK_DB._state.docs.entries()]
|
||||
.filter(([path]) => path.includes('/encounters/'))
|
||||
.pop();
|
||||
return row ? MOCK_DB.get(row[0]) : null;
|
||||
}
|
||||
|
||||
describe('tracked effects DM UI', () => {
|
||||
test('5e Turned preset creates prominent 10-round damage-expiring participant effect', async () => {
|
||||
await setupReady('Turned Camp', 'Turned Encounter');
|
||||
await addMonsterViaUI('Zombie', 22, -2);
|
||||
|
||||
expect(screen.queryByRole('button', { name: 'Add effect to Zombie' })).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' }));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Turned' }));
|
||||
|
||||
await waitFor(() => {
|
||||
const fx = latestEncounter().effects[0];
|
||||
expect(fx).toMatchObject({
|
||||
presetId: 'turned', name: 'Turned', scope: 'participant',
|
||||
remainingRounds: 10, expiresOnDamage: true, playerDisplay: 'prominent',
|
||||
});
|
||||
expect(fx.targetParticipantId).toBe(latestEncounter().participants.find(p => p.name === 'Zombie').id);
|
||||
});
|
||||
expect(await screen.findByText('TURNED')).toBeInTheDocument();
|
||||
expect(screen.getByLabelText('Turns left for Turned')).toHaveValue(10);
|
||||
});
|
||||
|
||||
test('custom encounter effect converts 5e minutes to rounds and supports prominent display', async () => {
|
||||
await setupReady('Effect Camp', 'Effect Encounter');
|
||||
await addMonsterViaUI('Cleric', 30, 2);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' }));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Custom tracked effect' }));
|
||||
fireEvent.change(screen.getByLabelText('Effect scope'), { target: { value: 'encounter' } });
|
||||
fireEvent.change(screen.getByLabelText('Effect name'), { target: { value: 'Spirit Guardians' } });
|
||||
fireEvent.change(screen.getByLabelText('Duration'), { target: { value: '2' } });
|
||||
fireEvent.change(screen.getByLabelText('Duration unit'), { target: { value: 'minutes' } });
|
||||
fireEvent.change(screen.getByLabelText('Player display'), { target: { value: 'prominent' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Add Effect' }));
|
||||
|
||||
await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({
|
||||
name: 'Spirit Guardians', scope: 'encounter', remainingRounds: 20,
|
||||
playerDisplay: 'prominent',
|
||||
}));
|
||||
expect(screen.getByText('SPIRIT GUARDIANS')).toBeInTheDocument();
|
||||
expect(screen.getByLabelText('Turns left for Spirit Guardians')).toHaveValue(20);
|
||||
});
|
||||
|
||||
test('Spiritual Weapon preset creates 10-round encounter effect', async () => {
|
||||
await setupReady('Weapon Camp', 'Weapon Encounter');
|
||||
await addMonsterViaUI('Cleric', 30, 2);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' }));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Spiritual Weapon' }));
|
||||
|
||||
await waitFor(() => expect(latestEncounter().effects[0]).toMatchObject({
|
||||
presetId: 'spiritual_weapon', name: 'Spiritual Weapon', scope: 'encounter',
|
||||
remainingRounds: 10, expiresOnDamage: false, playerDisplay: 'prominent',
|
||||
}));
|
||||
const clericId = latestEncounter().participants.find(p => p.name === 'Cleric').id;
|
||||
expect(within(screen.getByTestId(`admin-participant-card-${clericId}`)).getByText('SPIRITUAL WEAPON')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('generic ruleset offers custom effects only and rounds only', async () => {
|
||||
await renderApp();
|
||||
await createCampaignViaUI('Generic Effect Camp');
|
||||
await selectCampaignByName('Generic Effect Camp');
|
||||
fireEvent.click(screen.getByRole('button', { name: /Create Encounter/i }));
|
||||
fireEvent.change(await screen.findByLabelText(/Encounter Name/i), { target: { value: 'Generic Effect Encounter' } });
|
||||
fireEvent.click(screen.getByRole('radio', { name: /Generic/i }));
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Create$/i }));
|
||||
await selectEncounterByName('Generic Effect Encounter');
|
||||
await addMonsterViaUI('Generic Mob', 10, 0);
|
||||
|
||||
expect(screen.queryByRole('button', { name: 'Add encounter effect' })).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' }));
|
||||
expect(screen.queryByRole('button', { name: 'Turned' })).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: 'Spiritual Weapon' })).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Custom tracked effect' }));
|
||||
const durationOptions = [...screen.getByLabelText('Duration unit').options].map(o => o.value);
|
||||
expect(durationOptions).toEqual(['rounds']);
|
||||
});
|
||||
|
||||
test('effect duration controls adjust and remove effect', async () => {
|
||||
await setupReady('Control Camp', 'Control Encounter');
|
||||
await addMonsterViaUI('Ghoul', 18, 2);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open conditions' }));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Turned' }));
|
||||
await screen.findByText('TURNED');
|
||||
|
||||
const turnsInput = screen.getByLabelText('Turns left for Turned');
|
||||
fireEvent.change(turnsInput, { target: { value: '3' } });
|
||||
fireEvent.blur(turnsInput);
|
||||
await waitFor(() => expect(latestEncounter().effects[0].remainingRounds).toBe(3));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Decrease Turned duration' }));
|
||||
await waitFor(() => expect(latestEncounter().effects[0].remainingRounds).toBe(2));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Remove Turned' }));
|
||||
await waitFor(() => expect(latestEncounter().effects).toEqual([]));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user