@@ -14,7 +14,7 @@ import {
useRemoveParticipant ,
useAdvanceRound ,
useNextTurn ,
useSetTarget ,
useSetTargets ,
usePatchEncounter ,
useSetEncounterArchived ,
useDeleteEncounter ,
@@ -63,6 +63,11 @@ const CONDITIONS = [
'Concentrating' ,
] ;
// An attack on more than one target is an area attack, and harder to land:
// every target's AC counts as this much higher for each target beyond the first.
const AREA_AC_STEP = 2 ;
const areaPenalty = ( targetCount : number ) = > Math . max ( 0 , targetCount - 1 ) * AREA_AC_STEP ;
const KIND_LABELS = { CHARACTER : 'PC' , MONSTER : 'Monster' , NPC : 'NPC' } as const ;
const KIND_BADGES = { CHARACTER : 'badge-player' , MONSTER : 'badge-hostile' , NPC : 'badge-friendly' } as const ;
const MODE_LABELS : Record < EncounterMode , string > = { BATTLE : '⚔ Battle' , QUEST : '📜 Quest' } ;
@@ -101,6 +106,7 @@ function CombatantCard({
onPatch ,
canRollInitiative ,
targetedBy ,
acPenalty ,
onTarget ,
onRemove ,
onError ,
@@ -112,11 +118,13 @@ function CombatantCard({
// The DM for anyone; a player for their own character. The same goes for
// that character's death saves.
canRollInitiative : boolean ;
// Set while this combatant is the target: who is going for them, or '' when
// Set while this combatant is a target: who is going for them, or '' when
// nobody is on turn.
targetedBy? : string ;
// Marks this combatant as the target, or clears it. Absent for someone who
// may not pick one right now.
// What an area attack adds to this target's AC; 0 when they are the only one.
acPenalty : number ;
// Marks this combatant as a target, or unmarks them. Absent for someone who
// may not pick targets right now.
onTarget ? : ( ) = > void ;
onRemove : ( ) = > void ;
onError : ( err : unknown ) = > void ;
@@ -163,7 +171,12 @@ function CombatantCard({
< div className = "flex-between flex-wrap" >
< div >
{ isActive && < div className = "turn-marker" > ▶ On turn < / div > }
{ isTargeted && < div className = "target-marker" > 🎯 Targeted { targetedBy && ` by ${ targetedBy } ` } < / div > }
{ isTargeted && (
< div className = "target-marker" >
🎯 Targeted { targetedBy && ` by ${ targetedBy } ` }
{ acPenalty > 0 && ` · area attack, AC + ${ acPenalty } ` }
< / div >
) }
< div className = "flex-row flex-wrap" >
< span className = { ` badge ${ KIND_BADGES [ p . kind ] } ` } > { KIND_LABELS [ p . kind ] } < / span >
{ p . character ? (
@@ -179,7 +192,7 @@ function CombatantCard({
< button
className = { ` btn btn-target ${ isTargeted ? ' is-on' : '' } ` }
aria-pressed = { isTargeted }
title = { isTargeted ? 'Stop targeting' : ` Mark ${ p . displayName } as the target ` }
title = { isTargeted ? 'Stop targeting' : ` Mark ${ p . displayName } as a target. Marking more than one makes it an area attack. ` }
onClick = { onTarget }
>
🎯 { isTargeted ? 'Targeted' : 'Target' }
@@ -436,82 +449,149 @@ function attackLines(p: EncounterParticipant): string[] {
return [ [ stats . attackBonus != null && ` ${ fmtMod ( stats . attackBonus ) } to hit ` , stats . damageDice ] . filter ( Boolean ) . join ( ' · ' ) ] ;
}
// The DM's shortcut once a target is marked: who is attacking whom, the
type AreaMode = 'SPLIT' | 'EACH' ;
// How `amount` lands on `count` targets: all of it on each, or shared out
// evenly, any remainder going one point each to the first targets picked
// (7 between three is 3, 2 and 2).
function shares ( amount : number , count : number , mode : AreaMode ) : number [ ] {
if ( mode === 'EACH' ) return Array . from ( { length : count } , ( ) = > amount ) ;
const base = Math . floor ( amount / count ) ;
return Array . from ( { length : count } , ( _ , i ) = > base + ( i < amount % count ? 1 : 0 ) ) ;
}
// The DM's shortcut once targets are marked: who is attacking whom, the
// numbers needed to call the hit, and a box to apply the damage without
// hunting for the target's card.
// hunting for each target's card. With more than one target it is an area
// attack: each AC is shown with the penalty added, the DM ticks who was hit,
// and the amount is split between them or dealt to each in full.
function AttackBar ( {
attacker ,
target ,
targets ,
onApply ,
onRemove ,
onClear ,
} : {
attacker : EncounterParticipant | undefined ;
target : EncounterParticipant ;
onApply : ( currentHp : number ) = > void ;
targets : EncounterParticipant [ ] ;
onApply : ( changes : { participantId : string ; currentHp : number } [ ] ) = > void ;
onRemove : ( participantId : string ) = > void ;
onClear : ( ) = > void ;
} ) {
const [ amount , setAmount ] = useState ( '' ) ;
const info = target . characterCombatInfo ;
const ac = info ? . armorClass ? ? target . monster ? . ac ? ? target . npc ? . ac ;
const currentHp = target . currentHp ? ? 0 ;
const tracksHp = ( target . maxHp ? ? 0 ) > 0 ;
const attacks = attacker && attacker . id !== target . id ? attackLines ( attacker ) : [ ] ;
const [ mode , setMode ] = useState < AreaMode > ( 'SPLIT' ) ;
const [ missed , setMissed ] = useState < string [ ] > ( [ ] ) ;
const isArea = targets . length > 1 ;
const penalty = areaPenalty ( targets . length ) ;
const tracksHp = ( t : EncounterParticipant ) = > ( t . maxHp ? ? 0 ) > 0 ;
const hit = targets . filter ( ( t ) = > tracksHp ( t ) && ! missed . includes ( t . id ) ) ;
const value = Number ( amount ) || 0 ;
const portions = shares ( value , hit . length , isArea ? mode : 'EACH' ) ;
const selfOnly = targets . length === 1 && targets [ 0 ] . id === attacker ? . id ;
const attacks = attacker && ! selfOnly ? attackLines ( attacker ) : [ ] ;
function apply ( sign : 1 | - 1 ) {
const value = Number ( amount ) ;
if ( ! value ) return ;
onApply ( currentHp + sign * value ) ;
if ( ! value || hit . length === 0 ) return ;
onApply ( hit . map ( ( t , i ) = > ( { participantId : t.id , currentHp : ( t . currentHp ? ? 0 ) + sign * portions [ i ] } ) ) ) ;
setAmount ( '' ) ;
setMissed ( [ ] ) ;
}
return (
< div className = "card attack-bar" >
< div className = "flex-between flex-wrap" >
< div className = "attack-bar-title" >
{ attacker && attacker . id !== target . id && < > { attacker . displayName } → < / > } 🎯 { target . displayName }
{ attacker && ! selfOnly && < > { attacker . displayName } → < / > } 🎯 { isArea ? ` ${ targets . length } targets ` : targets [ 0 ] . displayName }
< / div >
< button className = "btn btn-ghost" onClick = { onClear } >
✕ Clear target
✕ Clear { isArea ? 'targets' : 'target' }
< / button >
< / div >
< div className = "stat-chips mt-sm" >
{ ac != null && < StatChip label = "Target AC" > { ac } < / StatChip > }
{ tracksHp && (
< StatChip label = "Target HP" >
{ currentHp } / { target . maxHp }
{ info && info . tempHp > 0 && ` + ${ info . tempHp } ` }
< / StatChip >
) }
< / div >
{ isArea && (
< p className = "combatant-sub" style = { { margin : '0.25rem 0 0' } } >
Area attack : every target 's AC counts as {penalty} higher (+{AREA_AC_STEP} for each target beyond the first). Untick anyone the roll missed.
</p>
)}
{targets.map((t) => {
const info = t.characterCombatInfo;
const ac = info?.armorClass ?? t.monster?.ac ?? t.npc?.ac;
const portion = portions[hit.indexOf(t)];
return (
<div key={t.id} className="attack-target">
{isArea && (
<label className="flex-row gap-sm">
<input
type="checkbox"
aria-label={`${t.displayName} was hit`}
disabled={!tracksHp(t)}
checked={hit.includes(t)}
onChange={(e) => setMissed(e.target.checked ? missed.filter((id) => id !== t.id) : [...missed, t.id])}
/>
<span className="attack-target-name">{t.displayName}</span>
</label>
)}
<div className="stat-chips">
{ac != null && (
<StatChip label={isArea ? ' AC to beat ' : ' Target AC '}>
{ac + penalty}
{penalty > 0 && ` (${ac} +${penalty})`}
</StatChip>
)}
{tracksHp(t) ? (
<StatChip label={isArea ? ' HP ' : ' Target HP '}>
{t.currentHp ?? 0}/{t.maxHp}
{info && info.tempHp > 0 && ` +${info.tempHp}`}
</StatChip>
) : (
<span className="combatant-sub">No hit points to track.</span>
)}
{isArea && value > 0 && portion !== undefined && <StatChip label="Share">{portion}</StatChip>}
</div>
{isArea && (
<button className="btn btn-ghost" aria-label={`Stop targeting ${t.displayName}`} onClick={() => onRemove(t.id)}>
✕
</button>
)}
</div>
);
})}
{attacks.map((line) => (
<div key={line} className="combatant-sub mt-sm">
⚔ {line}
</div>
))}
{ tracksHp ? (
{targets.some(tracksHp) && (
<div className="flex-row flex-wrap gap-sm mt-sm">
{ /* Remounted per target , so the cursor lands here as soon as one is marked. */ }
{ /* Remounted when the targets change , so the cursor lands here as soon as one is marked. */}
<NumberInput
key = { target . id }
key={targets.map((t) => t.id).join()}
autoFocus
min={1}
max={99999}
placeholder = "Amount"
aria-label = { ` HP change for ${ target . displayName } ` }
placeholder={isArea ? ' Total ' : ' Amount '}
aria-label={isArea ? ' HP change for the targets hit ' : `HP change for ${targets[0].displayName}`}
value={amount}
onChange={(e) => setAmount(e.target.value)}
onKeyDown={(e) => e.key === ' Enter ' && apply(-1)}
style={{ width: 90 }}
/>
< button className = "btn btn-danger" onClick = { ( ) = > apply ( - 1 ) } disabled = { ! amount } >
{isArea && (
<select aria-label="How the amount is shared" value={mode} onChange={(e) => setMode(e.target.value as AreaMode)} style={{ width: ' auto ' }}>
<option value="SPLIT">Split between those hit</option>
<option value="EACH">Full amount to each</option>
</select>
)}
<button className="btn btn-danger" onClick={() => apply(-1)} disabled={!value || hit.length === 0}>
− Damage
</button>
< button className = "btn" onClick = { ( ) = > apply ( 1 ) } disabled = { ! amount } >
<button className="btn" onClick={() => apply(1)} disabled={!value || hit.length === 0}>
+ Heal
</button>
</div>
) : (
< p className = "combatant-sub" > { target . displayName } has no hit points to track . < / p >
)}
</div>
);
@@ -761,7 +841,7 @@ export function EncounterPage() {
const removeParticipant = useRemoveParticipant(encounterId);
const advanceRound = useAdvanceRound(encounterId);
const nextTurn = useNextTurn(encounterId);
const setTarget = useSetTarget ( encounterId ) ;
const setTargets = useSetTargets(encounterId);
const patchEncounter = usePatchEncounter(encounterId);
const setArchived = useSetEncounterArchived(encounter?.campaignId ?? ' ');
const deleteEncounter = useDeleteEncounter(encounter?.campaignId ?? ' ');
@@ -778,8 +858,9 @@ export function EncounterPage() {
const isBattle = encounter.mode === ' BATTLE ';
const participants = encounter.participants ?? []; // already in initiative order
const active = isBattle && encounter.status === ' ACTIVE ' ? participants.find((p) => p.id === encounter.activeParticipantId) : undefined;
const target = participants . find ( ( p ) = > p . id === encounter . targetParticipantId ) ;
// The DM mark s a target whenever they like; a player while their own character is on turn.
const targetIds = encounter.targetParticipantIds ?? [];
const target s = targetIds.map((id) => participants.find((p) => p.id === id)).filter((p): p is EncounterParticipant => p !== undefined);
// The DM marks targets whenever they like; a player while their own character is on turn.
const canTarget = isDM || (active?.character != null && active.character.userId === user?.id);
const builtCharacters = characters?.filter((c) => c.buildStage === ' COMPLETE ') ?? [];
const onError = (err: unknown) => setFormError(extractErrorMessage(err));
@@ -809,8 +890,13 @@ export function EncounterPage() {
isActive={p.id === active?.id}
onPatch={(data) => patchParticipant.mutate({ participantId: p.id, ...data }, fail)}
canRollInitiative={isDM || (p.character != null && p.character.userId === user?.id)}
targetedBy = { p . id === target ? . id ? ( active && active . id !== p . id ? active . displayName : '' ) : undefined }
onTarget = { canTarget ? ( ) = > setTarget . mutate ( p . id === target ? . id ? null : p . id , fail ) : undefined }
targetedBy={targetIds.includes(p.id) ? (active && active.id !== p.id ? active.displayName : ' ') : undefined}
acPenalty={areaPenalty(targets.length)}
onTarget={
canTarget
? () => setTargets.mutate(targetIds.includes(p.id) ? targetIds.filter((id) => id !== p.id) : [...targetIds, p.id], fail)
: undefined
}
onRemove={() => removeParticipant.mutate(p.id, fail)}
onError={onError}
/>
@@ -947,12 +1033,13 @@ export function EncounterPage() {
< div className = "encounter-layout mt-md" >
< div >
{isDM && target && (
{ isDM && targets . length > 0 && (
< AttackBar
attacker = { active }
target={target}
onApply={(currentHp) => patchParticipant.mutate({ participantId: target.id, currentHp }, fail)}
onClear={() => setTarget.mutate(null, fail)}
targets = { targets }
onApply = { ( changes ) = > Promise . all ( changes . map ( ( change ) = > patchParticipant . mutateAsync ( change ) ) ) . catch ( onError ) }
onRemove = { ( id ) = > setTargets . mutate ( targetIds . filter ( ( x ) = > x !== id ) , fail ) }
onClear = { ( ) = > setTargets . mutate ( [ ] , fail ) }
/ >
) }
{ participants . length === 0 && (