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
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "backend",
"version": "1.5.0",
"version": "1.6.0",
"private": true,
"type": "commonjs",
"main": "src/index.js",
@@ -0,0 +1,2 @@
-- AlterTable: the combatant currently marked as the target
ALTER TABLE "encounters" ADD COLUMN "targetParticipantId" TEXT;
+3
View File
@@ -535,6 +535,9 @@ model Encounter {
currentRound Int @default(1)
// Whose turn it is in a battle; null until the fight starts.
activeParticipantId String?
// Who is being attacked (or healed) right now, shown to everyone. Picked by
// the DM or by the player on turn, and cleared when the turn passes.
targetParticipantId String?
description String? // the scene / objective, shown to everyone
dmNotes String? // never sent to players
// Where the scene takes place. While the encounter is in progress, the
@@ -1912,6 +1912,48 @@ describe('initiative', () => {
});
});
describe('targets', () => {
itIfDb('are picked by the DM or the player on turn, shown to all, and go when the turn does', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('target');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Archer');
const goblin = (await request(app).post(`${c}/monsters/from-library`).set(dmAuth).send({ key: 'goblin' })).body.monster;
const e = `/encounters/${(await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Ambush' })).body.encounter.id}`;
const add = async (body) => (await request(app).post(`${e}/participants`).set(dmAuth).send(body)).body.participant;
const hero = await add({ characterId: heroId, initiativeRoll: 20 });
const first = await add({ monsterId: goblin.id, initiativeRoll: 10 });
const second = await add({ monsterId: goblin.id, initiativeRoll: 1 });
const target = (auth, participantId) => request(app).put(`${e}/target`).set(auth).send({ participantId });
const seen = async (auth) => (await request(app).get(e).set(auth)).body.encounter.targetParticipantId;
// Before the battle only the DM can mark anyone.
expect((await target(playerAuth, first.id)).status).toBe(403);
expect((await target(dmAuth, first.id)).body.encounter.targetParticipantId).toBe(first.id);
expect(await seen(playerAuth)).toBe(first.id);
expect((await target(dmAuth, 'nobody')).status).toBe(400);
// Beginning the battle clears it and puts the hero on turn: now the player can.
await request(app).patch(e).set(dmAuth).send({ status: 'ACTIVE' });
expect(await seen(dmAuth)).toBeNull();
expect((await target(playerAuth, second.id)).status).toBe(200);
expect(await seen(dmAuth)).toBe(second.id);
// Removing the target clears it.
await request(app).delete(`${e}/participants/${second.id}`).set(dmAuth);
expect(await seen(dmAuth)).toBeNull();
// The turn passing clears it, and the player is no longer on turn.
await target(playerAuth, first.id);
await request(app).post(`${e}/next-turn`).set(dmAuth);
expect(await seen(dmAuth)).toBeNull();
expect((await target(playerAuth, first.id)).status).toBe(403);
// The goblin's turn: the DM marks the hero, and can clear it again.
expect((await target(dmAuth, hero.id)).body.encounter.targetParticipantId).toBe(hero.id);
expect((await target(dmAuth, null)).body.encounter.targetParticipantId).toBeNull();
});
});
describe('number limits', () => {
itIfDb('are enforced by the server, whatever a form sends', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('limits');
+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 } : {}),
},