V1.21.0: a DM's toolkit: the battle at a glance, subclasses, character state, rolls, XP
Seven steps in one commit, 1.15.0 to 1.21.0. They change the same files and were not saved apart, so they go in together. - The battle at a glance (1.15). A strip across the top of an encounter lists everyone in turn order and stays in view; a click goes to that card. On a wide screen the stat block of whoever is on turn stays beside the cards, and Pin keeps anyone else's there. Saving throws, every skill and passive Perception, Investigation and Insight are on encounter cards and in the character panel, which "Sheet" on a card and a button on the party overview open. For the DM, N is Next Turn and S opens the sheet of whoever is on turn. The party overview shows passive scores and spell save DC. - Subclasses (1.16). Three per class, picked in the level-up checklist at level 2 (cleric, druid, wizard) or 3, shown beside the class everywhere, and changed by the DM from the sheet: the old one's lines come out of Special Abilities and the new one's go in. Characters already at that level keep the path they were on. - Character state (1.17). Inspiration, exhaustion and conditions on the sheet and in the panel, and counters for what a class does a set number of times (Rage, Ki, Channel Divinity...), which the player spends and a rest gives back. A long rest takes a level of exhaustion off. A character's conditions are their own now: they stay from one encounter to the next. - Faster battles (1.18). Up to 20 of a monster added at once, numbered. One button rolls initiative for the monsters and NPCs. A condition can be given a number of rounds and then comes off by itself. Hovering over a condition shows its rule, and damaging someone who is Concentrating shows the save to call for. - Rolls from the sheet (1.19). A number with a dotted underline rolls when clicked, with its bonus: skills, saves, ability checks, attacks, damage, spell dice, and a monster's attack, damage and abilities. The dice roller has advantage and disadvantage. "Call for a roll" rolls one check or save for the whole party. Rolls stay in the browser. - XP and difficulty (1.20). An encounter shows the DM how hard it is, Trivial to Deadly, from the monsters' challenge ratings against the characters in it, and what it is worth in XP; one button shares that out, once. The sheet and the party overview show XP and when a level is due. Levelling up stays the DM's decision. - Subclasses are optional per campaign (1.21). With "Characters choose a subclass" unticked in the campaign's Settings nobody is asked, each class follows its first subclass, and none is named. - Conditions, exhaustion, counters and subclass features are reminders for the table: none of them changes a number on the sheet. - The docs cover all of it, with generated tables of what each subclass adds. - New migrations: a character's subclass; inspiration, exhaustion, conditions and spent counters on a character; the rounds left on a combatant's conditions; the XP awarded for an encounter; the campaign's subclass setting. - All packages at version 1.21.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,12 +12,15 @@ const {
|
||||
scaledDamageDice,
|
||||
canUseGear,
|
||||
spellSlotState,
|
||||
xpProgress,
|
||||
MAX_GEAR_LEVEL,
|
||||
MAX_LEVEL,
|
||||
MAX_SLOT_LEVEL,
|
||||
ABILITIES,
|
||||
} = require('../game/calculations');
|
||||
const { LevelUpError, advance, levelUpPlan } = require('../game/levelUp');
|
||||
const { LevelUpError, advance, levelUpPlan, switchSubclass, subclassByDefault } = require('../game/levelUp');
|
||||
const { resourceTally, MAX_EXHAUSTION } = require('../game/classResources');
|
||||
const { SUBCLASS_LEVEL, subclassesOf, findSubclass, subclassName } = require('../game/data/subclasses');
|
||||
const {
|
||||
rollDie,
|
||||
rollAbilityScore,
|
||||
@@ -52,10 +55,15 @@ async function loadFullCharacter(id) {
|
||||
weapons: { include: { weapon: true } },
|
||||
spells: { include: { spell: true } },
|
||||
currentLocation: true,
|
||||
campaign: { select: { subclassesEnabled: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Whether a character's campaign uses subclasses. Takes a character loaded
|
||||
// with its campaign (loadFullCharacter).
|
||||
const usesSubclasses = (character) => !character.campaign || character.campaign.subclassesEnabled;
|
||||
|
||||
// privateNotes is a real privacy boundary, not a UI hide — it's only ever
|
||||
// included in the payload for the character's own owning player.
|
||||
//
|
||||
@@ -67,6 +75,8 @@ async function loadFullCharacter(id) {
|
||||
function serializeCharacter(character, isOwner, isDM = true) {
|
||||
const restricted = !isOwner && !isDM;
|
||||
const sheet = computeCharacterSheet(character, character.skillProficiencies, character.saveProficiencies);
|
||||
// With subclasses switched off for the campaign, none is named or offered.
|
||||
const subclasses = usesSubclasses(character);
|
||||
return {
|
||||
id: character.id,
|
||||
campaignId: character.campaignId,
|
||||
@@ -75,8 +85,14 @@ function serializeCharacter(character, isOwner, isDM = true) {
|
||||
name: character.name,
|
||||
species: character.species,
|
||||
characterClass: character.characterClass,
|
||||
subclass: subclasses ? character.subclass : null,
|
||||
subclassName: subclasses ? subclassName(character.characterClass, character.subclass) : null,
|
||||
// What the class can choose from, and the level it chooses at.
|
||||
subclassLevel: subclasses ? (SUBCLASS_LEVEL[character.characterClass] ?? null) : null,
|
||||
subclassOptions: subclasses ? subclassesOf(character.characterClass).map(({ key, name, summary }) => ({ key, name, summary })) : [],
|
||||
level: character.level,
|
||||
xp: character.xp,
|
||||
xpProgress: xpProgress(character.xp, character.level),
|
||||
motivation: character.motivation,
|
||||
halfElfBonusAbility1: character.halfElfBonusAbility1,
|
||||
halfElfBonusAbility2: character.halfElfBonusAbility2,
|
||||
@@ -91,6 +107,9 @@ function serializeCharacter(character, isOwner, isDM = true) {
|
||||
currentHp: character.currentHp,
|
||||
tempHp: character.tempHp,
|
||||
deathSaves: deathSaveInfo(character),
|
||||
inspiration: character.inspiration,
|
||||
exhaustion: character.exhaustion,
|
||||
conditions: character.conditions,
|
||||
armorBonus: character.armorBonus,
|
||||
specialAbilities: character.specialAbilities,
|
||||
background: character.background,
|
||||
@@ -117,11 +136,17 @@ function serializeCharacter(character, isOwner, isDM = true) {
|
||||
: null,
|
||||
usable: canUseGear(character.level, ii.level),
|
||||
})),
|
||||
weapons: character.weapons.map((cw) => ({
|
||||
...cw,
|
||||
scaledDamageDice: scaledDamageDice(cw.weapon.damageDice, cw.level, cw.weapon.damageScaling, cw.weapon.minLevel),
|
||||
usable: canUseGear(character.level, cw.level),
|
||||
})),
|
||||
weapons: character.weapons.map((cw) => {
|
||||
// What this character rolls with it: to hit, and on top of its dice.
|
||||
const { attackBonus, damageBonus } = computeWeaponStats(cw.weapon, sheet.abilities, sheet.proficiencyBonus, true, cw.level);
|
||||
return {
|
||||
...cw,
|
||||
scaledDamageDice: scaledDamageDice(cw.weapon.damageDice, cw.level, cw.weapon.damageScaling, cw.weapon.minLevel),
|
||||
attackBonus,
|
||||
damageBonus,
|
||||
usable: canUseGear(character.level, cw.level),
|
||||
};
|
||||
}),
|
||||
spells: character.spells.map((cs) => ({ ...cs, usable: canUseGear(character.level, cs.spell.level) })),
|
||||
sheet: character.buildStage === 'COMPLETE' ? sheet : null,
|
||||
};
|
||||
@@ -163,8 +188,12 @@ const patchCharacterSchema = z.object({
|
||||
privateNotes: z.string().max(4000).nullable().optional(),
|
||||
currentLocationId: z.string().nullable().optional(),
|
||||
level: z.number().int().min(1).max(MAX_LEVEL).optional(),
|
||||
xp: z.number().int().min(0).optional(),
|
||||
subclass: z.string().max(40).nullable().optional(), // a key from game/data/subclasses.js; null clears it
|
||||
xp: z.number().int().min(0).max(10000000).optional(),
|
||||
hitDiceUsed: z.number().int().min(0).max(MAX_LEVEL).optional(),
|
||||
inspiration: z.boolean().optional(),
|
||||
exhaustion: z.number().int().min(0).max(MAX_EXHAUSTION).optional(),
|
||||
conditions: z.array(z.string().trim().min(1).max(40)).max(20).optional(),
|
||||
});
|
||||
|
||||
// Progression and placement are the DM's call, and so is rewriting what the
|
||||
@@ -174,8 +203,10 @@ const patchCharacterSchema = z.object({
|
||||
// edit the rest of their own sheet. Gold is not patchable at all — it only
|
||||
// moves through the ledger (POST /:id/gold). The armor bonus is the DM's
|
||||
// adjustment on top of what equipped gear gives, so it is the DM's too. Hit
|
||||
// Dice are spent on a rest, which the DM runs.
|
||||
const DM_ONLY_PATCH_FIELDS = ['currentHp', 'tempHp', 'level', 'xp', 'currentLocationId', 'speedOverride', 'armorBonus', 'specialAbilities', 'background', 'appearance', 'hitDiceUsed'];
|
||||
// Dice are spent on a rest, which the DM runs. Exhaustion and conditions are
|
||||
// the DM's to give and take away. Inspiration is the DM's to give; the player
|
||||
// may spend it (checked below).
|
||||
const DM_ONLY_PATCH_FIELDS = ['currentHp', 'tempHp', 'level', 'subclass', 'xp', 'currentLocationId', 'speedOverride', 'armorBonus', 'specialAbilities', 'background', 'appearance', 'hitDiceUsed', 'exhaustion', 'conditions'];
|
||||
|
||||
function clamp(value, min, max) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
@@ -190,8 +221,12 @@ router.patch('/:id', requireCharacterAccess({ write: true }), async (req, res, n
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
|
||||
if (!req.isDM && DM_ONLY_PATCH_FIELDS.some((field) => parsed.data[field] !== undefined)) {
|
||||
return res.status(403).json({ error: 'Only the DM can change HP, temp HP, Hit Dice, level, XP, speed, armor bonus, location, special abilities, background or appearance.' });
|
||||
return res.status(403).json({ error: 'Only the DM can change HP, temp HP, Hit Dice, level, subclass, XP, speed, armor bonus, location, exhaustion, conditions, special abilities, background or appearance.' });
|
||||
}
|
||||
if (!req.isDM && parsed.data.inspiration === true) {
|
||||
return res.status(403).json({ error: 'Only the DM can give inspiration. You can spend it.' });
|
||||
}
|
||||
if (parsed.data.conditions) parsed.data.conditions = [...new Set(parsed.data.conditions)];
|
||||
|
||||
const newLevel = parsed.data.level ?? req.character.level;
|
||||
if (parsed.data.level !== undefined || parsed.data.hitDiceUsed !== undefined) {
|
||||
@@ -205,15 +240,37 @@ router.patch('/:id', requireCharacterAccess({ write: true }), async (req, res, n
|
||||
for (const slot of slots ? slots.levels : []) tally[slot.level - 1] = slot.used;
|
||||
parsed.data.spellSlotsUsed = Array.from(tally, (used) => used || 0);
|
||||
}
|
||||
if (req.character.buildStage === 'COMPLETE' && newLevel > req.character.level && parsed.data.specialAbilities === undefined) {
|
||||
// Setting a higher level by hand still writes down what the class gains
|
||||
// on the way there. A Boon is left as a line saying one is owed: picking
|
||||
// it is part of levelling up properly (POST /:id/level-up).
|
||||
let abilities = req.character.specialAbilities;
|
||||
const characterClass = req.character.characterClass;
|
||||
const askedSubclass = parsed.data.subclass; // before a level change may take one by default
|
||||
let abilities = parsed.data.specialAbilities === undefined ? req.character.specialAbilities : null;
|
||||
const writesAbilities = req.character.buildStage === 'COMPLETE' && parsed.data.specialAbilities === undefined;
|
||||
if (writesAbilities && newLevel > req.character.level) {
|
||||
// Setting a higher level by hand still writes down what the class and
|
||||
// subclass give on the way there. A Boon, and a subclass not yet picked,
|
||||
// are left as a line saying one is owed: picking them is part of
|
||||
// levelling up properly (POST /:id/level-up).
|
||||
let key = req.character.subclass;
|
||||
for (let level = req.character.level + 1; level <= newLevel; level++) {
|
||||
abilities = advance(abilities, req.character.characterClass, level).specialAbilities;
|
||||
// With subclasses switched off, the class's first one is taken at the
|
||||
// level it would have been chosen.
|
||||
const taken = req.campaign.subclassesEnabled ? null : subclassByDefault(characterClass, level, key);
|
||||
abilities = advance(abilities, characterClass, level, undefined, taken ? { key: taken, chosen: true } : { key }).specialAbilities;
|
||||
if (taken) key = taken;
|
||||
}
|
||||
parsed.data.specialAbilities = abilities;
|
||||
if (key !== req.character.subclass && askedSubclass === undefined) parsed.data.subclass = key;
|
||||
}
|
||||
if (askedSubclass !== undefined && askedSubclass !== req.character.subclass) {
|
||||
if (!req.campaign.subclassesEnabled) return res.status(400).json({ error: 'Subclasses are switched off for this campaign.' });
|
||||
const picked = askedSubclass;
|
||||
if (picked !== null) {
|
||||
if (!findSubclass(characterClass, picked)) return res.status(400).json({ error: 'That is not a subclass of this character\'s class.' });
|
||||
if (newLevel < SUBCLASS_LEVEL[characterClass]) {
|
||||
return res.status(400).json({ error: `${req.character.name} chooses a subclass at level ${SUBCLASS_LEVEL[characterClass]}.` });
|
||||
}
|
||||
}
|
||||
// The old subclass's lines come off the sheet and the new one's go on.
|
||||
if (writesAbilities) parsed.data.specialAbilities = switchSubclass(abilities, characterClass, newLevel, req.character.subclass, picked);
|
||||
}
|
||||
|
||||
// Damage is soaked by temp HP before it reaches real HP — unless this
|
||||
@@ -401,6 +458,65 @@ router.post('/:id/spell-slots', requireCharacterAccess({ write: true }), async (
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Class counters — Rage, Ki, Channel Divinity and the like. Which a character
|
||||
// has, and how many, comes from class, subclass and level
|
||||
// (game/classResources.js); the character row keeps how many are spent.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const resourceKey = z.string().min(1).max(40);
|
||||
|
||||
const resourceSchema = z.discriminatedUnion('action', [
|
||||
// Using it: one is spent, or `amount` from a pool (Lay on Hands, Ki).
|
||||
z.object({ action: z.literal('use'), key: resourceKey, amount: z.number().int().min(1).max(999).default(1) }),
|
||||
// The DM setting how many are spent: giving some back, or fixing a mistake.
|
||||
z.object({ action: z.literal('set'), key: resourceKey, used: z.number().int().min(0).max(999) }),
|
||||
// The DM giving every counter back.
|
||||
z.object({ action: z.literal('restore') }),
|
||||
]);
|
||||
|
||||
// The owning player spends their own. Getting any back is the DM's: by hand
|
||||
// here, or for everyone with a rest (POST /campaigns/:id/rest).
|
||||
router.post('/:id/resources', requireCharacterAccess({ write: true }), async (req, res, next) => {
|
||||
try {
|
||||
const parsed = resourceSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
if (req.character.buildStage !== 'COMPLETE') return res.status(400).json({ error: 'Character build is not finished yet.' });
|
||||
const input = parsed.data;
|
||||
if (input.action !== 'use' && !req.isDM) return res.status(403).json({ error: 'Only the DM can give a use back.' });
|
||||
|
||||
const character = await loadFullCharacter(req.character.id);
|
||||
const resources = sheetOf(character).resources;
|
||||
if (input.action === 'restore') {
|
||||
for (const resource of resources) resource.used = 0;
|
||||
} else {
|
||||
const resource = resources.find((r) => r.key === input.key);
|
||||
if (!resource) return res.status(400).json({ error: `${character.name} has no such counter.` });
|
||||
if (input.action === 'use') {
|
||||
if (input.amount > resource.remaining) {
|
||||
const back = resource.restoresOn === 'SHORT' ? 'short or long' : 'long';
|
||||
return res.status(400).json({
|
||||
error:
|
||||
resource.remaining === 0
|
||||
? `${character.name} has no ${resource.name} left. It comes back on a ${back} rest.`
|
||||
: `${character.name} has only ${resource.remaining} ${resource.name} left.`,
|
||||
});
|
||||
}
|
||||
resource.used += input.amount;
|
||||
} else {
|
||||
resource.used = Math.min(input.used, resource.max);
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.character.update({ where: { id: character.id }, data: { resourcesUsed: resourceTally(resources) } });
|
||||
const full = await loadFullCharacter(character.id);
|
||||
emitCharacterUpdated(req);
|
||||
res.json({ character: serializeCharacter(full, req.isOwner) });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Levelling up — one level at a time, with the checklist that goes with it
|
||||
// (game/levelUp.js): the new level, full HP, the class's new special
|
||||
@@ -423,7 +539,7 @@ router.get('/:id/level-up', requireCharacterAccess(), async (req, res, next) =>
|
||||
try {
|
||||
const character = await loadFullCharacter(req.character.id);
|
||||
if (!canLevelUp(req, res, character)) return;
|
||||
const plan = levelUpPlan(character);
|
||||
const plan = levelUpPlan(character, { subclasses: usesSubclasses(character) });
|
||||
const after = sheetOf({ ...character, level: plan.toLevel, spellSlotsUsed: [] });
|
||||
res.json({ plan: { ...plan, maxHp: { from: sheetOf(character).hp.max, to: after.hp.max }, spellSlots: after.spellSlots } });
|
||||
} catch (err) {
|
||||
@@ -432,6 +548,7 @@ router.get('/:id/level-up', requireCharacterAccess(), async (req, res, next) =>
|
||||
});
|
||||
|
||||
const levelUpSchema = z.object({
|
||||
subclass: z.string().optional(), // which subclass, at the level the class chooses one
|
||||
boon: z.string().optional(), // which Boon, at a level that grants one
|
||||
weaponId: z.string().nullable().optional(), // the weapon that gets the free upgrade
|
||||
spellIds: z.array(z.string()).max(50).default([]), // spells newly granted
|
||||
@@ -441,11 +558,16 @@ router.post('/:id/level-up', requireCharacterAccess({ write: true }), async (req
|
||||
try {
|
||||
const parsed = levelUpSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
const { boon, weaponId, spellIds } = parsed.data;
|
||||
const { subclass, boon, weaponId, spellIds } = parsed.data;
|
||||
const character = await loadFullCharacter(req.character.id);
|
||||
if (!canLevelUp(req, res, character)) return;
|
||||
|
||||
const plan = levelUpPlan(character);
|
||||
const plan = levelUpPlan(character, { subclasses: usesSubclasses(character) });
|
||||
if (plan.subclass && !subclass) return res.status(400).json({ error: `Level ${plan.toLevel} needs a subclass: pick one.` });
|
||||
if (!plan.subclass && subclass) return res.status(400).json({ error: `There is no subclass to pick at level ${plan.toLevel}.` });
|
||||
if (subclass && !plan.subclass.options.some((option) => option.key === subclass)) {
|
||||
return res.status(400).json({ error: 'That is not a subclass of this character\'s class.' });
|
||||
}
|
||||
if (plan.boon && !boon) return res.status(400).json({ error: `Level ${plan.toLevel} grants a Boon: pick one.` });
|
||||
if (!plan.boon && boon) return res.status(400).json({ error: `Level ${plan.toLevel} does not grant a Boon.` });
|
||||
if (weaponId && !plan.weapons.some((w) => w.weaponId === weaponId)) {
|
||||
@@ -458,7 +580,9 @@ router.post('/:id/level-up', requireCharacterAccess({ write: true }), async (req
|
||||
|
||||
let gained;
|
||||
try {
|
||||
gained = advance(character.specialAbilities, character.characterClass, plan.toLevel, boon);
|
||||
// The subclass picked, or the one taken for them where the campaign has no choosing.
|
||||
const newSubclass = subclass || plan.subclassTaken;
|
||||
gained = advance(character.specialAbilities, character.characterClass, plan.toLevel, boon, newSubclass ? { key: newSubclass, chosen: true } : { key: character.subclass });
|
||||
} catch (err) {
|
||||
if (!(err instanceof LevelUpError)) throw err;
|
||||
return res.status(400).json({ error: err.message });
|
||||
@@ -478,6 +602,7 @@ router.post('/:id/level-up', requireCharacterAccess({ write: true }), async (req
|
||||
where: { id: character.id },
|
||||
data: {
|
||||
level: plan.toLevel,
|
||||
...(subclass || plan.subclassTaken ? { subclass: subclass || plan.subclassTaken } : {}),
|
||||
specialAbilities: gained.specialAbilities,
|
||||
armorBonus,
|
||||
tempHp: Math.max(character.tempHp, gained.tempHp),
|
||||
@@ -603,6 +728,7 @@ router.post('/:id/copy', requireCharacterAccess(), async (req, res, next) => {
|
||||
name: source.name,
|
||||
species: source.species,
|
||||
characterClass: source.characterClass,
|
||||
subclass: source.subclass,
|
||||
level: source.level,
|
||||
xp: source.xp,
|
||||
motivation: source.motivation,
|
||||
@@ -1135,6 +1261,7 @@ router.get('/:id/quick-stats', requireCharacterAccess(), async (req, res, next)
|
||||
level: character.level,
|
||||
species: character.species,
|
||||
characterClass: character.characterClass,
|
||||
subclassName: usesSubclasses(character) ? subclassName(character.characterClass, character.subclass) : null,
|
||||
currentHp: character.currentHp,
|
||||
maxHp: sheet.hp.max,
|
||||
tempHp: character.tempHp,
|
||||
@@ -1146,8 +1273,16 @@ router.get('/:id/quick-stats', requireCharacterAccess(), async (req, res, next)
|
||||
spellcasting: sheet.spellcasting,
|
||||
spellSlots: sheet.spellSlots,
|
||||
hitDice: sheet.hitDice,
|
||||
resources: sheet.resources,
|
||||
inspiration: character.inspiration,
|
||||
exhaustion: character.exhaustion,
|
||||
conditions: character.conditions,
|
||||
passivePerception: sheet.passivePerception,
|
||||
passiveInvestigation: sheet.passiveInvestigation,
|
||||
passiveInsight: sheet.passiveInsight,
|
||||
abilities: sheet.abilities,
|
||||
saves: sheet.saves,
|
||||
skills: sheet.skills,
|
||||
specialAbilities: character.specialAbilities,
|
||||
// What equipped gear is adding and taking away, and what else it does.
|
||||
gear: sheet.gear,
|
||||
|
||||
Reference in New Issue
Block a user