V1.29.0: free roam by invitation, no slot no spell, weapons change at the right moment

- Free roam no longer adds the whole party: the DM adds who is there
- A spell with no slot of its level or higher left cannot be declared; it
  takes the lowest slot left at or above its level. Cantrips take none
- In a battle in progress a player changes the weapon in hand only as the
  battle begins or on their own turn after their attack has landed
- Spells are listed cantrips first, then by level, with the level shown

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 10:05:25 +02:00
co-authored by Claude Opus 5.5
parent 555765916f
commit 9d0a4a2252
19 changed files with 280 additions and 62 deletions
+38 -1
View File
@@ -172,4 +172,41 @@ function attacksOf({ monster = null, npc = null }) {
return [{ name: 'Attack', attackBonus: stats.attackBonus || 0, damageDice: stats.damageDice || null, damageType: null }];
}
module.exports = { attackOutcome, saveSucceeds, lands, settleTarget, settle, rollDamage, rollCritDamage, sideOf, opponentsOf, targetWeights, pickTarget, attacksOf };
// Highest initiative first; ties keep the order combatants were added in.
// Anyone who has not rolled yet waits at the bottom.
function inTurnOrder(participants) {
const rolled = (p) => (p.initiativeRoll == null ? 0 : 1);
return [...participants].sort((a, b) => rolled(b) - rolled(a) || b.initiative - a.initiative || (a.id < b.id ? -1 : 1));
}
// Whether a combatant may change the weapon in their hand in a battle that
// is under way. There are two moments for it: as the battle begins, before
// anyone has made a move (round 1, still the first combatant's turn, nothing
// landed yet), and on their own turn once their attack has landed, before the
// turn passes. `applied` is the battle's landed actions: [{ round,
// actorParticipantId }]. `firstId` is whoever went first. `declared` is true
// while somebody's attack or spell is declared and still being resolved: that
// is a move being made.
function weaponSwapOpen({ round, activeId, firstId, participantId, applied = [], declared = false }) {
if (activeId == null) return true; // no turn has started
const opening = round === 1 && activeId === firstId && applied.length === 0 && !declared;
const afterOwnAttack = activeId === participantId && applied.some((a) => a.round === round && a.actorParticipantId === participantId);
return opening || afterOwnAttack;
}
module.exports = {
attackOutcome,
saveSucceeds,
lands,
settleTarget,
settle,
rollDamage,
rollCritDamage,
sideOf,
opponentsOf,
targetWeights,
pickTarget,
attacksOf,
inTurnOrder,
weaponSwapOpen,
};
+33
View File
@@ -140,3 +140,36 @@ describe('what a creature attacks with', () => {
expect(attacksOf({ npc: { attackBonus: null, damageDice: null } })).toEqual([]);
});
});
describe('changing weapons in a battle', () => {
const { inTurnOrder, weaponSwapOpen } = require('./combat');
const base = { round: 1, activeId: 'a', firstId: 'a', participantId: 'b', applied: [] };
test('the turn order is by initiative, the unrolled last', () => {
const order = inTurnOrder([
{ id: '1', initiativeRoll: null, initiative: 0 },
{ id: '2', initiativeRoll: 5, initiative: 7 },
{ id: '3', initiativeRoll: 12, initiative: 12 },
]);
expect(order.map((p) => p.id)).toEqual(['3', '2', '1']);
});
test('anyone may as the battle begins, before a move is made', () => {
expect(weaponSwapOpen(base)).toBe(true);
expect(weaponSwapOpen({ ...base, activeId: null })).toBe(true);
// The first turn has passed, or something has landed: the moment is gone.
expect(weaponSwapOpen({ ...base, activeId: 'b', participantId: 'c' })).toBe(false);
expect(weaponSwapOpen({ ...base, applied: [{ round: 1, actorParticipantId: 'a' }] })).toBe(false);
expect(weaponSwapOpen({ ...base, round: 2 })).toBe(false);
expect(weaponSwapOpen({ ...base, declared: true })).toBe(false); // an attack is being resolved
});
test('after that, only on their own turn once their attack has landed', () => {
const later = { round: 2, activeId: 'b', firstId: 'a', participantId: 'b' };
expect(weaponSwapOpen({ ...later, applied: [] })).toBe(false);
expect(weaponSwapOpen({ ...later, applied: [{ round: 1, actorParticipantId: 'b' }] })).toBe(false); // last round's
expect(weaponSwapOpen({ ...later, applied: [{ round: 2, actorParticipantId: 'b' }] })).toBe(true);
// Somebody else's turn, even though they attacked earlier this round.
expect(weaponSwapOpen({ ...later, activeId: 'c', applied: [{ round: 2, actorParticipantId: 'b' }] })).toBe(false);
});
});
+70 -2
View File
@@ -3983,6 +3983,71 @@ describe('battle actions', () => {
expect(await hp(two.id)).toBe(0); // 7 HP, 20 damage
const slotsAfter = (await request(app).get(`/characters/${mageId}`).set(dmAuth)).body.character.sheet.spellSlots.levels.find((l) => l.level === 3).remaining;
expect(slotsAfter).toBe(slotsBefore - 1);
// A cantrip takes no slot, however often it is cast. Spells are listed cantrips first.
expect(mira.spells.map((s) => [s.name, s.level])).toEqual([['Fire Bolt', 0], ['Fireball', 3]]);
const used = async () => (await request(app).get(`/characters/${mageId}`).set(dmAuth)).body.character.sheet.spellSlots.levels.map((l) => l.used);
const before = await used();
await request(app).put(`${e}/targets`).set(dmAuth).send({ participantIds: [one.id] });
const bolt = mira.spells.find((s) => s.name === 'Fire Bolt');
const boltId = (await request(app).post(`${e}/actions`).set(dmAuth).send({ source: { type: 'spell', characterSpellId: bolt.characterSpellId } })).body.actionId;
action = (await request(app).patch(`${e}/actions/${boltId}`).set(dmAuth).send({ rolls: [{ participantId: one.id, d20: 19 }], damage: 3 })).body.encounter.actions[0];
expect(action).toMatchObject({ kind: 'ATTACK', name: 'Fire Bolt' });
expect(action.detail.slotLevel).toBeNull();
const cast = await request(app).post(`${e}/actions/${boltId}/apply`).set(dmAuth).send({});
expect(cast.status).toBe(200);
expect(cast.body.slotNote).toBeNull();
expect(await used()).toEqual(before);
// With her last 3rd-level slot gone, Fireball cannot be declared; the cantrip still can.
await request(app).post(`/characters/${mageId}/spell-slots`).set(dmAuth).send({ action: 'set', level: 3, used: 2 });
const dry = await request(app).post(`${e}/actions`).set(dmAuth).send({ source: { type: 'spell', characterSpellId: fireball.characterSpellId } });
expect(dry.status).toBe(400);
expect(dry.body.error).toMatch(/no spell slot of level 3 or higher left/);
expect((await request(app).post(`${e}/actions`).set(dmAuth).send({ source: { type: 'spell', characterSpellId: bolt.characterSpellId } })).status).toBe(201);
});
itIfDb('a weapon is changed as the battle begins or on your own turn after attacking, and not in between', async () => {
const { dmAuth, playerAuth, campaign, e, one, heroId } = await battle('actswap');
const c = `/campaigns/${campaign.id}`;
const bow = (await request(app).post(`${c}/weapons`).set(dmAuth).send({ name: 'Shortbow', damageDice: '1d6', damageType: 'piercing', attackAbility: 'DEX', properties: [] })).body.weapon;
await request(app).post(`/characters/${heroId}/weapons`).set(dmAuth).send({ weaponId: bow.id });
await request(app).patch(c).set(dmAuth).send({ maxEquippedWeapons: 2 });
const held = async () => (await request(app).get(`/characters/${heroId}`).set(playerAuth)).body.character.weapons;
const sword = (await held()).find((w) => w.weapon.name === 'Longsword');
const equip = (auth, weaponId, equipped) => request(app).patch(`/characters/${heroId}/weapons/${weaponId}`).set(auth).send({ equipped });
// The battle has begun and nobody has made a move: the bow can come out, and go back.
expect((await equip(playerAuth, bow.id, true)).status).toBe(200);
expect((await equip(playerAuth, bow.id, false)).status).toBe(200);
// Aria attacks. While the attack is still being resolved nothing changes hands...
await request(app).put(`${e}/targets`).set(playerAuth).send({ participantIds: [one.id] });
const actionId = (await request(app).post(`${e}/actions`).set(playerAuth).send({ source: { type: 'weapon', characterWeaponId: sword.id } })).body.actionId;
await request(app).patch(`${e}/actions/${actionId}`).set(playerAuth).send({ rolls: [{ participantId: one.id, d20: 2 }] });
expect((await equip(playerAuth, bow.id, true)).status).toBe(400);
await request(app).post(`${e}/actions/${actionId}/apply`).set(dmAuth).send({});
// After her attack, before the next turn: she may swap.
expect((await equip(playerAuth, bow.id, true)).status).toBe(200);
// The wizard's turn: Aria's hands stay as they are, either way. The DM can still change them.
await request(app).post(`${e}/next-turn`).set(dmAuth);
const refused = await equip(playerAuth, bow.id, false);
expect(refused.status).toBe(400);
expect(refused.body.error).toMatch(/is in a battle \(Ambush\).*own turn once their attack has landed/);
expect((await equip(playerAuth, sword.weaponId, false)).status).toBe(400);
expect((await equip(dmAuth, bow.id, false)).status).toBe(200);
// Other gear is not held to it.
expect((await held()).find((w) => w.weapon.name === 'Shortbow').equipped).toBe(false);
// Round the table to Aria again: not before she has attacked this turn.
for (let i = 0; i < 3; i += 1) await request(app).post(`${e}/next-turn`).set(dmAuth);
expect((await request(app).get(e).set(dmAuth)).body.encounter.currentRound).toBe(2);
expect((await equip(playerAuth, bow.id, true)).status).toBe(400);
// Once the battle is over, gear is changed freely again.
await request(app).patch(e).set(dmAuth).send({ status: 'COMPLETE' });
expect((await equip(playerAuth, bow.id, true)).status).toBe(200);
});
itIfDb('with auto-roll on, a monster\u2019s turn opens with its attack aimed and rolled, kept from the players until the DM accepts it', async () => {
@@ -4049,7 +4114,7 @@ describe('battle actions', () => {
});
describe('free roam', () => {
itIfDb('the party is in it from the start, wanders between its places, and shops where it stands', async () => {
itIfDb('whoever the DM adds wanders between its places, and shops where they stand', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('roam');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Aria');
@@ -4072,7 +4137,10 @@ describe('free roam', () => {
const e = `/encounters/${created.body.encounter.id}`;
const view = async (auth) => (await request(app).get(e).set(auth)).body.encounter;
// Everyone alive is in it without being added, so the player can open it.
// Nobody is in it until the DM adds them: the party may be split up.
expect((await view(dmAuth)).participants).toEqual([]);
expect((await request(app).get(e).set(playerAuth)).status).toBe(404);
for (const characterId of [heroId, allyId]) await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId });
let scene = await view(playerAuth);
expect(scene.mode).toBe('FREE_ROAM');
expect(scene.participants.map((p) => p.displayName).sort()).toEqual(['Aria', 'Bo']);
+4 -18
View File
@@ -906,24 +906,10 @@ router.post('/:id/encounters', requireCampaignMember(), requireDM(), async (req,
return created;
});
// Free roam is for the whole party: its places are set, and everyone
// alive and finished is in it from the start (so their players see it).
if (encounter.mode === 'FREE_ROAM') {
if (places.length > 0) await prisma.encounterLocation.createMany({ data: places.map((locationId) => ({ encounterId: encounter.id, locationId })) });
const party = await prisma.character.findMany({ where: { campaignId: req.campaign.id, buildStage: 'COMPLETE' }, include: SHEET_INCLUDE, orderBy: { createdAt: 'asc' } });
const present = party.filter((character) => lifeState(character) !== 'DEAD');
if (present.length > 0) {
await prisma.encounterParticipant.createMany({
data: present.map((character) => ({
encounterId: encounter.id,
characterId: character.id,
displayName: character.name,
currentHp: character.currentHp,
maxHp: sheetOf(character).hp.max,
isMonster: false,
})),
});
}
// A free-roam scene's places. Who is in it is the DM's to add, as with
// any encounter: the party may be split, some of them somewhere else.
if (encounter.mode === 'FREE_ROAM' && places.length > 0) {
await prisma.encounterLocation.createMany({ data: places.map((locationId) => ({ encounterId: encounter.id, locationId })) });
}
res.status(201).json({ encounter });
} catch (err) {
+50 -3
View File
@@ -48,7 +48,9 @@ const { speciesBonusProblem } = require('../game/data/species');
const { SKILLS } = require('../game/data/skills');
const { CURRENCIES, COINS, COIN_VALUE, COIN_COLUMN, InsufficientGoldError, adjustGold, spendCoins, worthInGold, giveGear } = require('../utils/grants');
const { recordVisits } = require('../utils/locations');
const { SHEET_INCLUDE, sheetOf, settleHp } = require('../utils/sheet');
const { SHEET_INCLUDE, sheetOf, settleHp, bySpellLevel } = require('../utils/sheet');
const { inTurnOrder, weaponSwapOpen } = require('../game/combat');
const { isFallen } = require('../utils/actions');
const { absorbDamage, NO_DEATH_SAVES, lifeState, deathSaveInfo, settleDeathSaves, changeHp, rollDeathSave } = require('../utils/hp');
const router = express.Router();
@@ -173,7 +175,7 @@ function serializeCharacter(character, isOwner, isDM = true) {
usable: canUseGear(character.level, cw.level),
};
}),
spells: character.spells.map((cs) => ({ ...cs, usable: canUseGear(character.level, cs.spell.level) })),
spells: [...character.spells].sort(bySpellLevel).map((cs) => ({ ...cs, usable: canUseGear(character.level, cs.spell.level) })),
sheet: character.buildStage === 'COMPLETE' ? sheet : null,
};
}
@@ -1439,7 +1441,7 @@ router.get('/:id/quick-stats', requireCharacterAccess(), async (req, res, next)
damageDice: ii.item.damageDice ? scaledDamageDice(ii.item.damageDice, ii.level, ii.item.damageScaling, ii.item.minLevel) : null,
...effectsOf(ii.item),
}));
const spells = character.spells.map((cs) => ({
const spells = [...character.spells].sort(bySpellLevel).map((cs) => ({
characterSpellId: cs.id,
name: cs.spell.name,
level: cs.spell.level,
@@ -1620,6 +1622,46 @@ function requireDmForGear(req, res) {
return false;
}
// A weapon is not swapped in the middle of a fight. For a character in a
// battle that is under way, the weapon in hand changes only as the battle
// begins (before anyone has made a move) or on their own turn once their
// attack has landed, before the turn passes (game/combat.js, weaponSwapOpen).
// Returns what to tell the player when it is not one of those moments. The
// DM is not held to it: they referee, and put mistakes right.
async function weaponSwapWait(character) {
const rows = await prisma.encounterParticipant.findMany({
where: { characterId: character.id, encounter: { status: 'ACTIVE', mode: 'BATTLE', archivedAt: null } },
include: { encounter: true },
});
for (const row of rows) {
const { encounter } = row;
const participants = await prisma.encounterParticipant.findMany({
where: { encounterId: encounter.id },
include: { character: { select: { currentHp: true, deathSaveFailures: true, stable: true } } },
});
const first = inTurnOrder(participants).find((p) => !isFallen(p));
// What has landed, and whether an attack somebody declared is still being
// resolved (one the server lined up for a monster is not a move yet).
const actions = await prisma.encounterAction.findMany({
where: { encounterId: encounter.id, status: { in: ['APPLIED', 'PENDING'] } },
select: { round: true, actorParticipantId: true, status: true, auto: true },
});
const applied = actions.filter((a) => a.status === 'APPLIED');
const open = weaponSwapOpen({
round: encounter.currentRound,
activeId: encounter.activeParticipantId,
firstId: first ? first.id : null,
participantId: row.id,
applied,
declared: actions.some((a) => a.status === 'PENDING' && !a.auto),
});
if (!open) {
return `${character.name} is in a battle (${encounter.name}). A weapon can be changed as the battle begins, before anyone has made a move, or on ${character.name}'s own turn once their attack has landed. The DM can change it at any time.`;
}
}
return null;
}
function emitCharacterUpdated(req) {
req.app.get('io').to(`campaign:${req.character.campaignId}`).emit('character:updated', { characterId: req.character.id });
}
@@ -1745,6 +1787,11 @@ for (const gear of GEAR) {
const blocked = await equipBlocked(req, gear.pool, req.params.poolId);
if (blocked) return res.status(400).json({ error: blocked });
}
// Drawing or putting away a weapon in a battle waits for the right moment.
if (gear.pool === 'weapon' && !req.isDM && parsed.data.equipped !== undefined && parsed.data.equipped !== existing.equipped) {
const wait = await weaponSwapWait(req.character);
if (wait) return res.status(400).json({ error: wait });
}
const row = await prisma[gear.held].update({ where: whereHeld(req), data, include });
if (gear.ownLevel) await settleHp(prisma, [req.character.id]);
+3 -9
View File
@@ -8,10 +8,10 @@ const { loadCampaignMembership } = require('../middleware/campaignAccess');
const { computeCharacterSheet, computeWeaponStats, scaledDamageDice, canUseGear, abilityModifier, encounterDifficulty, MAX_GEAR_LEVEL } = require('../game/calculations');
const { CURRENCIES, InsufficientGoldError, adjustGold, spendCoins, giveGear } = require('../utils/grants');
const { moveCharacters, visitedLocationIds } = require('../utils/locations');
const { SHEET_INCLUDE } = require('../utils/sheet');
const { SHEET_INCLUDE, bySpellLevel } = require('../utils/sheet');
const { NO_DEATH_SAVES, lifeState, deathSaveInfo, changeHp } = require('../utils/hp');
const { subclassName } = require('../game/data/subclasses');
const { settle, rollDamage, rollCritDamage } = require('../game/combat');
const { settle, rollDamage, rollCritDamage, inTurnOrder } = require('../game/combat');
const { rollDie } = require('../game/dice');
const { ActionError, targetRows, describeSource, autoActions } = require('../utils/actions');
@@ -59,13 +59,6 @@ function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
// Highest initiative first; ties keep the order combatants were added in.
// Anyone who has not rolled yet waits at the bottom.
function inTurnOrder(participants) {
const rolled = (p) => (p.initiativeRoll == null ? 0 : 1);
return [...participants].sort((a, b) => rolled(b) - rolled(a) || b.initiative - a.initiative || (a.id < b.id ? -1 : 1));
}
// What is added to a combatant's d20 for initiative: the sheet's initiative
// bonus for a character, the Dexterity modifier for a monster, and nothing for
// an NPC (they have no ability scores). Takes a participant with its
@@ -181,6 +174,7 @@ function characterCombatInfo(character, subclasses = true) {
}));
const spells = character.spells
.filter((cs) => canUseGear(character.level, cs.spell.level))
.sort(bySpellLevel)
.map((cs) => ({
characterSpellId: cs.id,
name: cs.spell.name,
+13 -2
View File
@@ -115,8 +115,19 @@ function describeSource(actor, source) {
const spell = known.spell;
if (!canUseGear(character.level, spell.level)) throw new ActionError(`${spell.name} is beyond ${character.name}'s level.`);
const casting = sheet.spellcasting;
// A cantrip takes no slot; any other spell takes one of its own level or higher.
const slotLevel = spell.level > 0 ? Math.max(spell.level, source.slotLevel || spell.level) : null;
// A cantrip takes no slot; any other spell takes one of its own level, or
// the lowest one above it that is left. With none left it cannot be cast.
// (A character with no slots at all, handed a spell by the DM, casts it freely.)
let slotLevel = null;
if (spell.level > 0) {
slotLevel = Math.max(spell.level, source.slotLevel || spell.level);
if (sheet.spellSlots) {
const wanted = slotLevel;
const free = sheet.spellSlots.levels.find((l) => l.level >= wanted && l.remaining > 0);
if (!free) throw new ActionError(`${character.name} has no spell slot of level ${wanted} or higher left, so cannot cast ${spell.name}.`);
slotLevel = free.level;
}
}
const base = { damageDice: spell.damageDice || null, damageBonus: 0, damageType: null, characterId: character.id, spellLevel: spell.level, slotLevel };
if (spell.attackRoll) return { kind: 'ATTACK', name: spell.name, detail: { ...base, attackBonus: casting ? casting.attackBonus : 0 } };
if (spell.saveAbility) {
+5 -1
View File
@@ -34,4 +34,8 @@ async function settleHp(db, characterIds) {
return changed;
}
module.exports = { SHEET_INCLUDE, sheetOf, settleHp };
// A character's spells in the order they are listed everywhere: cantrips
// first, then by spell level, then by name.
const bySpellLevel = (a, b) => a.spell.level - b.spell.level || a.spell.name.localeCompare(b.spell.name);
module.exports = { SHEET_INCLUDE, sheetOf, settleHp, bySpellLevel };