V1.7.0: area attacks on several targets
- More than one combatant can be marked as a target; the Target button now adds to the targets instead of moving the mark. - Several targets make an area attack, which is harder to land: every target's AC counts as 2 higher for each target beyond the first. The attack bar shows the AC to beat, and each target's card names the penalty. - The DM unticks any target the roll missed, then applies one total: split evenly between those hit (remainder to the first marked), or the full amount to each. Each share is shown before it is applied. - The single target column from V1.6.0 becomes a list, carried over by the migration. - All packages at version 1.7.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.6.0",
|
||||
"version": "1.7.0",
|
||||
"private": true,
|
||||
"type": "commonjs",
|
||||
"main": "src/index.js",
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
-- AlterTable: several combatants can be marked as targets at once (an area attack)
|
||||
ALTER TABLE "encounters" ADD COLUMN "targetParticipantIds" TEXT[] DEFAULT ARRAY[]::TEXT[];
|
||||
|
||||
-- Carry over a single target marked before this.
|
||||
UPDATE "encounters"
|
||||
SET "targetParticipantIds" = ARRAY["targetParticipantId"]
|
||||
WHERE "targetParticipantId" IS NOT NULL;
|
||||
|
||||
ALTER TABLE "encounters" DROP COLUMN "targetParticipantId";
|
||||
@@ -535,9 +535,10 @@ 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
|
||||
// Who is being attacked (or healed) right now, shown to everyone, in the
|
||||
// order they were picked. More than one makes it an area attack. Picked by
|
||||
// the DM or by the player on turn, and cleared when the turn passes.
|
||||
targetParticipantId String?
|
||||
targetParticipantIds String[] @default([])
|
||||
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
|
||||
|
||||
@@ -1923,34 +1923,34 @@ describe('targets', () => {
|
||||
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;
|
||||
const target = (auth, ...participantIds) => request(app).put(`${e}/targets`).set(auth).send({ participantIds });
|
||||
const seen = async (auth) => (await request(app).get(e).set(auth)).body.encounter.targetParticipantIds;
|
||||
|
||||
// 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);
|
||||
expect((await target(dmAuth, first.id)).body.encounter.targetParticipantIds).toEqual([first.id]);
|
||||
expect(await seen(playerAuth)).toEqual([first.id]);
|
||||
expect((await target(dmAuth, first.id, 'nobody')).status).toBe(400);
|
||||
|
||||
// Beginning the battle clears it and puts the hero on turn: now the player can.
|
||||
// Beginning the battle clears them and puts the hero on turn: now the
|
||||
// player can, and several at once is an area attack, kept in the order picked.
|
||||
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);
|
||||
expect(await seen(dmAuth)).toEqual([]);
|
||||
expect((await target(playerAuth, second.id, first.id, second.id)).status).toBe(200);
|
||||
expect(await seen(dmAuth)).toEqual([second.id, first.id]);
|
||||
|
||||
// Removing the target clears it.
|
||||
// Removing a target unmarks it and leaves the rest.
|
||||
await request(app).delete(`${e}/participants/${second.id}`).set(dmAuth);
|
||||
expect(await seen(dmAuth)).toBeNull();
|
||||
expect(await seen(dmAuth)).toEqual([first.id]);
|
||||
|
||||
// The turn passing clears it, and the player is no longer on turn.
|
||||
await target(playerAuth, first.id);
|
||||
// The turn passing clears them, and the player is no longer on turn.
|
||||
await request(app).post(`${e}/next-turn`).set(dmAuth);
|
||||
expect(await seen(dmAuth)).toBeNull();
|
||||
expect(await seen(dmAuth)).toEqual([]);
|
||||
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();
|
||||
expect((await target(dmAuth, hero.id)).body.encounter.targetParticipantIds).toEqual([hero.id]);
|
||||
expect((await target(dmAuth)).body.encounter.targetParticipantIds).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -262,8 +262,8 @@ 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;
|
||||
// Targets belong to the moment; beginning, ending or reopening clears them.
|
||||
if (data.status && data.status !== req.encounter.status) data.targetParticipantIds = [];
|
||||
|
||||
await prisma.encounter.update({ where: { id: req.encounter.id }, data });
|
||||
await respondWithEncounter(req, res);
|
||||
@@ -434,13 +434,14 @@ router.delete('/:id/participants/:participantId', requireEncounterAccess, requir
|
||||
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, and
|
||||
// the target goes with the turn — or with the combatant who was it.
|
||||
const { activeParticipantId, targetParticipantId } = req.encounter;
|
||||
// their targets go with the turn. Removing a target just unmarks it.
|
||||
const { activeParticipantId, targetParticipantIds } = 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, targetParticipantId: null } });
|
||||
} else if (targetParticipantId === req.params.participantId) {
|
||||
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { targetParticipantId: null } });
|
||||
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { activeParticipantId: next ? next.id : null, targetParticipantIds: [] } });
|
||||
} else if (targetParticipantIds.includes(req.params.participantId)) {
|
||||
const left = targetParticipantIds.filter((id) => id !== req.params.participantId);
|
||||
await prisma.encounter.update({ where: { id: req.encounter.id }, data: { targetParticipantIds: left } });
|
||||
}
|
||||
await prisma.encounterParticipant.delete({ where: { id: req.params.participantId } });
|
||||
await respondWithEncounter(req, res);
|
||||
@@ -450,19 +451,21 @@ router.delete('/:id/participants/:participantId', requireEncounterAccess, requir
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Target — who the combatant on turn is going for
|
||||
// Targets — who the combatant on turn is going for
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const targetSchema = z.object({ participantId: z.string().nullable() }); // null clears it
|
||||
// The whole list, in the order picked; an empty one clears it.
|
||||
const targetsSchema = z.object({ participantIds: z.array(z.string()).max(50) });
|
||||
|
||||
// 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) => {
|
||||
// Combatants can be marked as targets, for everyone to see; the DM's screen
|
||||
// then offers to apply damage to them. One is an ordinary attack, more is an
|
||||
// area attack. The DM can mark anyone at any time. A player can while their
|
||||
// own character is on turn in a battle.
|
||||
router.put('/:id/targets', requireEncounterAccess, async (req, res, next) => {
|
||||
try {
|
||||
const parsed = targetSchema.safeParse(req.body);
|
||||
const parsed = targetsSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
const { participantId } = parsed.data;
|
||||
const participantIds = [...new Set(parsed.data.participantIds)];
|
||||
|
||||
if (!req.isDM) {
|
||||
const { mode, status, activeParticipantId } = req.encounter;
|
||||
@@ -473,15 +476,13 @@ router.put('/:id/target', requireEncounterAccess, async (req, res, next) => {
|
||||
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 (!onTurn) return res.status(403).json({ error: "You can only pick targets 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' });
|
||||
}
|
||||
const found = await prisma.encounterParticipant.count({ where: { id: { in: participantIds }, encounterId: req.encounter.id } });
|
||||
if (found !== participantIds.length) 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 prisma.encounter.update({ where: { id: req.encounter.id }, data: { targetParticipantIds: participantIds } });
|
||||
await respondWithEncounter(req, res);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
@@ -507,7 +508,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
|
||||
targetParticipantIds: [], // the next combatant picks their own
|
||||
...(wrapped ? { currentRound: { increment: 1 } } : {}),
|
||||
},
|
||||
});
|
||||
@@ -525,7 +526,7 @@ router.post('/:id/advance-round', requireEncounterAccess, requireDM, async (req,
|
||||
data: {
|
||||
currentRound: { increment: 1 },
|
||||
status: 'ACTIVE',
|
||||
targetParticipantId: null,
|
||||
targetParticipantIds: [],
|
||||
// A new round starts back at the top of the initiative order.
|
||||
...(req.encounter.mode === 'BATTLE' ? { activeParticipantId: inTurnOrder(participants)[0]?.id ?? null } : {}),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user