V1.6.0: targets and an attack bar in encounters

- One combatant at a time can be marked as the target with the Target
  button on its card. The DM can mark anyone at any time; a player can
  while their own character is on turn in a battle.
- The target's card is outlined for everyone and names who is going for it.
- While a target is marked the DM gets an attack bar above the list: who is
  attacking whom, the target's AC and HP, the attacker's weapons, spells or
  monster attack, and a box to apply damage or healing straight away.
- The target is cleared when the turn or round moves on, when the encounter
  begins, ends or is reopened, or when the target is removed.
- All packages at version 1.6.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 12:53:18 +02:00
co-authored by Claude Opus 5.5
parent ec48b65481
commit e4456b0df2
13 changed files with 316 additions and 11 deletions
+51 -3
View File
@@ -262,6 +262,9 @@ router.patch('/:id', requireEncounterAccess, requireDM, async (req, res, next) =
data.activeParticipantId = inTurnOrder(participants)[0]?.id ?? null;
}
// A target belongs to the moment; beginning, ending or reopening clears it.
if (data.status && data.status !== req.encounter.status) data.targetParticipantId = null;
await prisma.encounter.update({ where: { id: req.encounter.id }, data });
await respondWithEncounter(req, res);
} catch (err) {
@@ -430,10 +433,14 @@ router.delete('/:id/participants/:participantId', requireEncounterAccess, requir
const index = order.findIndex((p) => p.id === req.params.participantId);
if (index === -1) return res.status(404).json({ error: 'Combatant not found in this encounter' });
// Removing whoever is on turn passes the turn to the next combatant.
if (req.encounter.activeParticipantId === req.params.participantId) {
// Removing whoever is on turn passes the turn to the next combatant, and
// the target goes with the turn — or with the combatant who was it.
const { activeParticipantId, targetParticipantId } = req.encounter;
if (activeParticipantId === req.params.participantId) {
const next = order.length > 1 ? order[(index + 1) % order.length] : null;
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { activeParticipantId: next ? next.id : null } });
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { activeParticipantId: next ? next.id : null, targetParticipantId: null } });
} else if (targetParticipantId === req.params.participantId) {
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { targetParticipantId: null } });
}
await prisma.encounterParticipant.delete({ where: { id: req.params.participantId } });
await respondWithEncounter(req, res);
@@ -442,6 +449,45 @@ router.delete('/:id/participants/:participantId', requireEncounterAccess, requir
}
});
// ---------------------------------------------------------------------------
// Target — who the combatant on turn is going for
// ---------------------------------------------------------------------------
const targetSchema = z.object({ participantId: z.string().nullable() }); // null clears it
// One combatant at a time can be marked as the target, for everyone to see;
// the DM's screen then offers to apply damage to it. The DM can mark anyone
// at any time. A player can while their own character is on turn in a battle.
router.put('/:id/target', requireEncounterAccess, async (req, res, next) => {
try {
const parsed = targetSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
const { participantId } = parsed.data;
if (!req.isDM) {
const { mode, status, activeParticipantId } = req.encounter;
const onTurn =
mode === 'BATTLE' && status === 'ACTIVE' && activeParticipantId
? await prisma.encounterParticipant.findFirst({
where: { id: activeParticipantId, encounterId: req.encounter.id, character: { userId: req.user.id } },
select: { id: true },
})
: null;
if (!onTurn) return res.status(403).json({ error: "You can only pick a target on your own character's turn" });
}
if (participantId) {
const target = await prisma.encounterParticipant.findFirst({ where: { id: participantId, encounterId: req.encounter.id }, select: { id: true } });
if (!target) return res.status(400).json({ error: 'Combatant not found in this encounter' });
}
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { targetParticipantId: participantId } });
await respondWithEncounter(req, res);
} catch (err) {
next(err);
}
});
// ---------------------------------------------------------------------------
// Turns and rounds
// ---------------------------------------------------------------------------
@@ -461,6 +507,7 @@ router.post('/:id/next-turn', requireEncounterAccess, requireDM, async (req, res
data: {
status: 'ACTIVE',
activeParticipantId: order[wrapped || index === -1 ? 0 : index + 1].id,
targetParticipantId: null, // the next combatant picks their own
...(wrapped ? { currentRound: { increment: 1 } } : {}),
},
});
@@ -478,6 +525,7 @@ router.post('/:id/advance-round', requireEncounterAccess, requireDM, async (req,
data: {
currentRound: { increment: 1 },
status: 'ACTIVE',
targetParticipantId: null,
// A new round starts back at the top of the initiative order.
...(req.encounter.mode === 'BATTLE' ? { activeParticipantId: inTurnOrder(participants)[0]?.id ?? null } : {}),
},