V1.22.0: campaign settings for dice, scores, multiclassing, auto-rolls, XP and crits
- Settings grouped into Characters, Dice & rolls, Combat and Progression - Dice: digital, physical or both; a campaign's own choice applies to every character - Ability scores by 4d6, the standard array or point buy (27 points, 8 to 15) - Track XP can be turned off for milestone levelling: XP is hidden and not awarded - New settings saved ahead of their features: multiclassing, auto-rolled monster and NPC attacks, roll visibility, hidden monster rolls, HP on level up, crit rule - docs/CAMPAIGN_SETTINGS.md lists every setting Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.21.0",
|
||||
"version": "1.22.0",
|
||||
"private": true,
|
||||
"type": "commonjs",
|
||||
"main": "src/index.js",
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
-- Campaign settings: which dice, multiclassing, auto-rolled monster and NPC
|
||||
-- attacks, XP tracking, HP on a level up, the ability-score method, who sees
|
||||
-- a roll, and what a critical hit does.
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "CampaignDiceMode" AS ENUM ('DIGITAL', 'PHYSICAL', 'BOTH');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "HpMethod" AS ENUM ('AVERAGE', 'ROLL');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "ScoreMethod" AS ENUM ('ROLL_4D6', 'STANDARD_ARRAY', 'POINT_BUY');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "RollVisibility" AS ENUM ('ALL', 'OWN');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "CritRule" AS ENUM ('DOUBLE_DICE', 'MAX_PLUS_ROLL');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "campaigns" ADD COLUMN "abilityScoreMethod" "ScoreMethod" NOT NULL DEFAULT 'ROLL_4D6',
|
||||
ADD COLUMN "autoRollMonsters" BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN "autoRollNpcs" BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN "critRule" "CritRule" NOT NULL DEFAULT 'DOUBLE_DICE',
|
||||
ADD COLUMN "diceMode" "CampaignDiceMode" NOT NULL DEFAULT 'BOTH',
|
||||
ADD COLUMN "hideMonsterRolls" BOOLEAN NOT NULL DEFAULT true,
|
||||
ADD COLUMN "hpOnLevelUp" "HpMethod" NOT NULL DEFAULT 'AVERAGE',
|
||||
ADD COLUMN "multiclassEnabled" BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN "rollVisibility" "RollVisibility" NOT NULL DEFAULT 'ALL',
|
||||
ADD COLUMN "trackXp" BOOLEAN NOT NULL DEFAULT true;
|
||||
@@ -138,6 +138,42 @@ enum DiceMode {
|
||||
PHYSICAL
|
||||
}
|
||||
|
||||
// Which dice a campaign plays with. BOTH leaves it to each character (and
|
||||
// every roll can still be typed in from a real die).
|
||||
enum CampaignDiceMode {
|
||||
DIGITAL
|
||||
PHYSICAL
|
||||
BOTH
|
||||
}
|
||||
|
||||
// How a character's maximum HP grows on a level up: the fixed average of the
|
||||
// hit die (rounded up), or a roll of it.
|
||||
enum HpMethod {
|
||||
AVERAGE
|
||||
ROLL
|
||||
}
|
||||
|
||||
// How the builder arrives at the six ability scores.
|
||||
enum ScoreMethod {
|
||||
ROLL_4D6
|
||||
STANDARD_ARRAY
|
||||
POINT_BUY
|
||||
}
|
||||
|
||||
// Who sees a roll. ALL: everyone at the table. OWN: a player sees their own;
|
||||
// the DM always sees every roll.
|
||||
enum RollVisibility {
|
||||
ALL
|
||||
OWN
|
||||
}
|
||||
|
||||
// What a critical hit does to the damage: roll the dice twice (the rule), or
|
||||
// take the dice at their maximum and roll them once more on top.
|
||||
enum CritRule {
|
||||
DOUBLE_DICE
|
||||
MAX_PLUS_ROLL
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Identity & campaigns
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -182,6 +218,25 @@ model Campaign {
|
||||
// Off: nobody chooses a subclass. Each class follows its first one without
|
||||
// being asked, and subclasses are not named anywhere.
|
||||
subclassesEnabled Boolean @default(true)
|
||||
// On: a character can take levels in more than one class (game/multiclass.js).
|
||||
multiclassEnabled Boolean @default(false)
|
||||
// Which dice are rolled at this table; see CampaignDiceMode.
|
||||
diceMode CampaignDiceMode @default(BOTH)
|
||||
// On: when a monster's (or an NPC's) turn comes up in a battle, the server
|
||||
// picks a target, rolls the attack and the damage, and puts it to the DM to
|
||||
// accept, change or throw out.
|
||||
autoRollMonsters Boolean @default(false)
|
||||
autoRollNpcs Boolean @default(false)
|
||||
// Off: experience is not kept. No XP on the sheets, nothing to award after
|
||||
// an encounter, and nobody is shown as due a level.
|
||||
trackXp Boolean @default(true)
|
||||
hpOnLevelUp HpMethod @default(AVERAGE)
|
||||
abilityScoreMethod ScoreMethod @default(ROLL_4D6)
|
||||
rollVisibility RollVisibility @default(ALL)
|
||||
// On: the DM's rolls for monsters and NPCs are seen by the DM alone until
|
||||
// they are put into play.
|
||||
hideMonsterRolls Boolean @default(true)
|
||||
critRule CritRule @default(DOUBLE_DICE)
|
||||
ownerId String
|
||||
// Set while the campaign is archived: kept in full, but moved off the main
|
||||
// list and closed to new members until the DM restores it.
|
||||
|
||||
@@ -48,6 +48,31 @@ function rollAbilityScore() {
|
||||
return abilityScoreFromDice(dice);
|
||||
}
|
||||
|
||||
// The other two ways to make ability scores (Campaign.abilityScoreMethod).
|
||||
// The standard array is six fixed scores, assigned like rolled ones. Point
|
||||
// buy starts every score at 8 and spends a budget to raise them, to 15 at most.
|
||||
const STANDARD_ARRAY = [15, 14, 13, 12, 10, 8];
|
||||
const POINT_BUY_BUDGET = 27;
|
||||
const POINT_BUY_COSTS = { 8: 0, 9: 1, 10: 2, 11: 3, 12: 4, 13: 5, 14: 7, 15: 9 };
|
||||
|
||||
// What a set of point-buy scores costs, or null if a score is out of range.
|
||||
function pointBuyCost(scores) {
|
||||
let total = 0;
|
||||
for (const score of Object.values(scores)) {
|
||||
const cost = POINT_BUY_COSTS[score];
|
||||
if (cost === undefined) return null;
|
||||
total += cost;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Which dice a character rolls: the campaign's, unless it leaves the choice
|
||||
// to each character (BOTH).
|
||||
function effectiveDiceMode(campaign, character) {
|
||||
if (campaign && campaign.diceMode && campaign.diceMode !== 'BOTH') return campaign.diceMode;
|
||||
return character.diceMode;
|
||||
}
|
||||
|
||||
// Fisher-Yates-ish random pick of `count` distinct items from `options`,
|
||||
// using the same crypto RNG as dice rolls (never Math.random for anything
|
||||
// that ends up on a character sheet).
|
||||
@@ -154,6 +179,11 @@ module.exports = {
|
||||
rollDie,
|
||||
rollAbilityScore,
|
||||
abilityScoreFromDice,
|
||||
STANDARD_ARRAY,
|
||||
POINT_BUY_BUDGET,
|
||||
POINT_BUY_COSTS,
|
||||
pointBuyCost,
|
||||
effectiveDiceMode,
|
||||
speciesChoicesForRoll,
|
||||
classChoicesForRoll,
|
||||
randomSample,
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
const { pointBuyCost, POINT_BUY_BUDGET, STANDARD_ARRAY, effectiveDiceMode } = require('./buildFlow');
|
||||
|
||||
describe('point buy', () => {
|
||||
test('all eights cost nothing', () => {
|
||||
expect(pointBuyCost({ STR: 8, DEX: 8, CON: 8, INT: 8, WIS: 8, CHA: 8 })).toBe(0);
|
||||
});
|
||||
|
||||
test('the classic 15/15/15/8/8/8 spends exactly the budget', () => {
|
||||
expect(pointBuyCost({ STR: 15, DEX: 15, CON: 15, INT: 8, WIS: 8, CHA: 8 })).toBe(POINT_BUY_BUDGET);
|
||||
});
|
||||
|
||||
test('14 and 15 cost two points a step', () => {
|
||||
expect(pointBuyCost({ STR: 13 })).toBe(5);
|
||||
expect(pointBuyCost({ STR: 14 })).toBe(7);
|
||||
expect(pointBuyCost({ STR: 15 })).toBe(9);
|
||||
});
|
||||
|
||||
test('a score out of range is refused', () => {
|
||||
expect(pointBuyCost({ STR: 16 })).toBeNull();
|
||||
expect(pointBuyCost({ STR: 7 })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
test('the standard array is the six usual scores', () => {
|
||||
expect(STANDARD_ARRAY).toEqual([15, 14, 13, 12, 10, 8]);
|
||||
});
|
||||
|
||||
describe('effectiveDiceMode', () => {
|
||||
test('a campaign with one kind of dice decides for everyone', () => {
|
||||
expect(effectiveDiceMode({ diceMode: 'PHYSICAL' }, { diceMode: 'DIGITAL' })).toBe('PHYSICAL');
|
||||
expect(effectiveDiceMode({ diceMode: 'DIGITAL' }, { diceMode: 'PHYSICAL' })).toBe('DIGITAL');
|
||||
});
|
||||
|
||||
test('BOTH leaves it to the character', () => {
|
||||
expect(effectiveDiceMode({ diceMode: 'BOTH' }, { diceMode: 'PHYSICAL' })).toBe('PHYSICAL');
|
||||
});
|
||||
});
|
||||
@@ -3456,3 +3456,87 @@ describe('a campaign without subclasses', () => {
|
||||
expect((await request(app).patch(`/characters/${characterId}`).set(dmAuth).send({ subclass: 'SAMURAI' })).body.character.subclassName).toBe('Samurai');
|
||||
});
|
||||
});
|
||||
|
||||
describe('campaign settings', () => {
|
||||
itIfDb('are kept, checked, and read back by every member', async () => {
|
||||
const { dmAuth, playerAuth, campaign } = await setupCampaign('settings');
|
||||
expect(campaign).toMatchObject({
|
||||
diceMode: 'BOTH',
|
||||
multiclassEnabled: false,
|
||||
autoRollMonsters: false,
|
||||
autoRollNpcs: false,
|
||||
trackXp: true,
|
||||
hpOnLevelUp: 'AVERAGE',
|
||||
abilityScoreMethod: 'ROLL_4D6',
|
||||
rollVisibility: 'ALL',
|
||||
hideMonsterRolls: true,
|
||||
critRule: 'DOUBLE_DICE',
|
||||
});
|
||||
const patch = (auth, body) => request(app).patch(`/campaigns/${campaign.id}`).set(auth).send(body);
|
||||
expect((await patch(playerAuth, { trackXp: false })).status).toBe(403);
|
||||
expect((await patch(dmAuth, { diceMode: 'SOMETIMES' })).status).toBe(400);
|
||||
const changed = await patch(dmAuth, { diceMode: 'PHYSICAL', critRule: 'MAX_PLUS_ROLL', multiclassEnabled: true, rollVisibility: 'OWN' });
|
||||
expect(changed.status).toBe(200);
|
||||
const seen = (await request(app).get(`/campaigns/${campaign.id}`).set(playerAuth)).body.campaign;
|
||||
expect(seen).toMatchObject({ diceMode: 'PHYSICAL', critRule: 'MAX_PLUS_ROLL', multiclassEnabled: true, rollVisibility: 'OWN' });
|
||||
});
|
||||
|
||||
itIfDb("a campaign's dice decide for every character, unless it leaves them to each", async () => {
|
||||
const { dmAuth, campaign } = await setupCampaign('settingsdice');
|
||||
await request(app).patch(`/campaigns/${campaign.id}`).set(dmAuth).send({ diceMode: 'PHYSICAL' });
|
||||
const character = (await request(app).post(`/campaigns/${campaign.id}/characters`).set(dmAuth).send({ name: 'Rolla' })).body.character;
|
||||
const read = (await request(app).get(`/characters/${character.id}`).set(dmAuth)).body.character;
|
||||
expect(read).toMatchObject({ diceMode: 'PHYSICAL', diceModeChoosable: false });
|
||||
expect((await request(app).patch(`/characters/${character.id}/build/dice-mode`).set(dmAuth).send({ diceMode: 'DIGITAL' })).status).toBe(400);
|
||||
// Physical dice must be typed in.
|
||||
expect((await request(app).post(`/characters/${character.id}/build/roll`).set(dmAuth).send({})).status).toBe(400);
|
||||
expect((await request(app).post(`/characters/${character.id}/build/roll`).set(dmAuth).send({ value: 2 })).status).toBe(200);
|
||||
});
|
||||
|
||||
// Walks a new character to the ability-score step.
|
||||
async function toScores(campaignId, auth) {
|
||||
const id = (await request(app).post(`/campaigns/${campaignId}/characters`).set(auth).send({ name: 'Scorer' })).body.character.id;
|
||||
const species = (await request(app).post(`/characters/${id}/build/roll`).set(auth)).body.buildState.speciesChoices.options[0];
|
||||
await request(app).post(`/characters/${id}/build/choose`).set(auth).send(species === 'HALF_ELF' ? { species, halfElfBonusAbilities: ['STR', 'INT'] } : { species });
|
||||
let classRoll = await request(app).post(`/characters/${id}/build/roll`).set(auth);
|
||||
while (classRoll.body.buildState.classChoices.type === 'reroll') classRoll = await request(app).post(`/characters/${id}/build/roll`).set(auth);
|
||||
await request(app).post(`/characters/${id}/build/choose`).set(auth).send({ characterClass: classRoll.body.buildState.classChoices.options[0] });
|
||||
return id;
|
||||
}
|
||||
|
||||
itIfDb('the standard array gives everyone the same six scores to place', async () => {
|
||||
const { dmAuth, campaign } = await setupCampaign('settingsarray');
|
||||
await request(app).patch(`/campaigns/${campaign.id}`).set(dmAuth).send({ abilityScoreMethod: 'STANDARD_ARRAY' });
|
||||
const id = await toScores(campaign.id, dmAuth);
|
||||
const rolled = await request(app).post(`/characters/${id}/build/roll`).set(dmAuth);
|
||||
expect(rolled.status).toBe(200);
|
||||
expect(rolled.body.buildState.abilityRolls.map((r) => r.total)).toEqual([15, 14, 13, 12, 10, 8]);
|
||||
const chosen = await request(app).post(`/characters/${id}/build/choose`).set(dmAuth).send({ assignments: { STR: 0, DEX: 1, CON: 2, INT: 3, WIS: 4, CHA: 5 } });
|
||||
expect(chosen.status).toBe(200);
|
||||
});
|
||||
|
||||
itIfDb('point buy spends 27 points on scores from 8 to 15, and nothing is rolled', async () => {
|
||||
const { dmAuth, campaign } = await setupCampaign('settingsbuy');
|
||||
await request(app).patch(`/campaigns/${campaign.id}`).set(dmAuth).send({ abilityScoreMethod: 'POINT_BUY' });
|
||||
const id = await toScores(campaign.id, dmAuth);
|
||||
expect((await request(app).post(`/characters/${id}/build/roll`).set(dmAuth)).status).toBe(400);
|
||||
const buy = (pointBuy) => request(app).post(`/characters/${id}/build/choose`).set(dmAuth).send({ pointBuy });
|
||||
expect((await buy({ STR: 15, DEX: 15, CON: 15, INT: 15, WIS: 8, CHA: 8 })).status).toBe(400); // 36 points
|
||||
expect((await buy({ STR: 16, DEX: 8, CON: 8, INT: 8, WIS: 8, CHA: 8 })).status).toBe(400);
|
||||
const bought = await buy({ STR: 15, DEX: 14, CON: 14, INT: 8, WIS: 10, CHA: 10 }); // 9 + 7 + 7 + 0 + 2 + 2
|
||||
expect(bought.status).toBe(200);
|
||||
expect(bought.body.buildState.abilityRolls.map((r) => r.total)).toEqual([15, 14, 14, 8, 10, 10]);
|
||||
expect(bought.body.buildStage).toBe('ASSIGNING_PROFICIENCIES');
|
||||
});
|
||||
|
||||
itIfDb('without XP tracking there is nothing to award', async () => {
|
||||
const { dmAuth, playerAuth, campaign } = await setupCampaign('settingsxp');
|
||||
await request(app).patch(`/campaigns/${campaign.id}`).set(dmAuth).send({ trackXp: false });
|
||||
const characterId = await buildCharacter(campaign.id, playerAuth, 'Aria');
|
||||
const encounterId = (await request(app).post(`/campaigns/${campaign.id}/encounters`).set(dmAuth).send({ name: 'Riddle', mode: 'QUEST' })).body.encounter.id;
|
||||
await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId });
|
||||
const res = await request(app).post(`/encounters/${encounterId}/award-xp`).set(dmAuth).send({ xp: 100 });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/experience/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,6 +36,16 @@ function campaignSummary(campaign, role) {
|
||||
maxEquipped: campaign.maxEquipped,
|
||||
maxEquippedWeapons: campaign.maxEquippedWeapons,
|
||||
subclassesEnabled: campaign.subclassesEnabled,
|
||||
multiclassEnabled: campaign.multiclassEnabled,
|
||||
diceMode: campaign.diceMode,
|
||||
autoRollMonsters: campaign.autoRollMonsters,
|
||||
autoRollNpcs: campaign.autoRollNpcs,
|
||||
trackXp: campaign.trackXp,
|
||||
hpOnLevelUp: campaign.hpOnLevelUp,
|
||||
abilityScoreMethod: campaign.abilityScoreMethod,
|
||||
rollVisibility: campaign.rollVisibility,
|
||||
hideMonsterRolls: campaign.hideMonsterRolls,
|
||||
critRule: campaign.critRule,
|
||||
ownerId: campaign.ownerId,
|
||||
archivedAt: campaign.archivedAt,
|
||||
role,
|
||||
@@ -73,6 +83,16 @@ const createCampaignSchema = z.object({
|
||||
maxEquippedWeapons: z.number().int().min(0).max(20).optional(),
|
||||
// Off: nobody chooses a subclass, and each class follows its first one.
|
||||
subclassesEnabled: z.boolean().optional(),
|
||||
multiclassEnabled: z.boolean().optional(),
|
||||
diceMode: z.enum(['DIGITAL', 'PHYSICAL', 'BOTH']).optional(),
|
||||
autoRollMonsters: z.boolean().optional(),
|
||||
autoRollNpcs: z.boolean().optional(),
|
||||
trackXp: z.boolean().optional(),
|
||||
hpOnLevelUp: z.enum(['AVERAGE', 'ROLL']).optional(),
|
||||
abilityScoreMethod: z.enum(['ROLL_4D6', 'STANDARD_ARRAY', 'POINT_BUY']).optional(),
|
||||
rollVisibility: z.enum(['ALL', 'OWN']).optional(),
|
||||
hideMonsterRolls: z.boolean().optional(),
|
||||
critRule: z.enum(['DOUBLE_DICE', 'MAX_PLUS_ROLL']).optional(),
|
||||
});
|
||||
|
||||
// POST /campaigns — create, creator becomes DM/owner
|
||||
|
||||
@@ -33,6 +33,10 @@ const {
|
||||
composeFromPicks,
|
||||
validateTalents,
|
||||
composeSpecialAbilities,
|
||||
STANDARD_ARRAY,
|
||||
POINT_BUY_BUDGET,
|
||||
pointBuyCost,
|
||||
effectiveDiceMode,
|
||||
} = require('../game/buildFlow');
|
||||
const characterOptions = require('../game/data/characterOptions');
|
||||
const { SKILLS } = require('../game/data/skills');
|
||||
@@ -55,7 +59,7 @@ async function loadFullCharacter(id) {
|
||||
weapons: { include: { weapon: true } },
|
||||
spells: { include: { spell: true } },
|
||||
currentLocation: true,
|
||||
campaign: { select: { subclassesEnabled: true } },
|
||||
campaign: { select: { subclassesEnabled: true, diceMode: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -115,7 +119,10 @@ function serializeCharacter(character, isOwner, isDM = true) {
|
||||
background: character.background,
|
||||
appearance: character.appearance,
|
||||
privateNotes: isOwner ? character.privateNotes : undefined,
|
||||
diceMode: character.diceMode,
|
||||
// The dice this character rolls with, and whether that is theirs to choose
|
||||
// (the campaign leaves it to each character) or the campaign's.
|
||||
diceMode: effectiveDiceMode(character.campaign, character),
|
||||
diceModeChoosable: !character.campaign || character.campaign.diceMode === 'BOTH',
|
||||
currentLocationId: restricted ? null : character.currentLocationId,
|
||||
currentLocation: restricted ? null : character.currentLocation || null,
|
||||
gold: restricted ? null : character.gold,
|
||||
@@ -796,7 +803,7 @@ router.post('/:id/copy', requireCharacterAccess(), async (req, res, next) => {
|
||||
// a user-facing error string on failure, otherwise returns the value(s) in
|
||||
// the exact same shape the digital path would have produced.
|
||||
function resolveSingleRoll(character, body, sides) {
|
||||
if (character.diceMode !== 'PHYSICAL') return rollDie(sides);
|
||||
if (effectiveDiceMode(character.campaign, character) !== 'PHYSICAL') return rollDie(sides);
|
||||
const value = Number(body?.value);
|
||||
if (!validateManualRoll(sides, value)) {
|
||||
throw new Error(`Enter the d${sides} you rolled — a whole number from 1 to ${sides}.`);
|
||||
@@ -805,7 +812,11 @@ function resolveSingleRoll(character, body, sides) {
|
||||
}
|
||||
|
||||
function resolveAbilityRolls(character, body) {
|
||||
if (character.diceMode !== 'PHYSICAL') {
|
||||
// The standard array is not rolled at all: the same six scores for everyone.
|
||||
if (character.campaign.abilityScoreMethod === 'STANDARD_ARRAY') {
|
||||
return STANDARD_ARRAY.map((total) => ({ dice: [], dropped: null, total }));
|
||||
}
|
||||
if (effectiveDiceMode(character.campaign, character) !== 'PHYSICAL') {
|
||||
return Array.from({ length: 6 }, () => rollAbilityScore());
|
||||
}
|
||||
const sets = body?.abilitySets;
|
||||
@@ -826,6 +837,9 @@ router.patch('/:id/build/dice-mode', requireCharacterAccess({ write: true }), as
|
||||
const parsed = z.object({ diceMode: z.enum(['DIGITAL', 'PHYSICAL']) }).safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
|
||||
if (req.campaign.diceMode !== 'BOTH') {
|
||||
return res.status(400).json({ error: `This campaign plays with ${req.campaign.diceMode === 'PHYSICAL' ? 'physical' : 'digital'} dice only.` });
|
||||
}
|
||||
const alreadyRolling = character.buildStage !== 'ROLLING_SPECIES' || Boolean((character.buildState || {}).speciesRoll);
|
||||
if (alreadyRolling) {
|
||||
return res.status(400).json({ error: 'Dice mode can only be chosen before the first roll of the build.' });
|
||||
@@ -867,6 +881,9 @@ router.post('/:id/build/roll', requireCharacterAccess({ write: true }), async (r
|
||||
if (buildState.abilityRolls) {
|
||||
return res.status(400).json({ error: 'Ability scores were already rolled for this character.' });
|
||||
}
|
||||
if (req.campaign.abilityScoreMethod === 'POINT_BUY') {
|
||||
return res.status(400).json({ error: 'This campaign uses point buy: nothing is rolled, the scores are bought.' });
|
||||
}
|
||||
buildState.abilityRolls = resolveAbilityRolls(character, req.body);
|
||||
} else {
|
||||
return res.status(400).json({ error: `No die roll is required at the "${stage}" step.` });
|
||||
@@ -893,15 +910,20 @@ const chooseSchemas = {
|
||||
// Motivation is story only, so it is simply picked from the list (the stage
|
||||
// keeps its old name from when it was a die roll).
|
||||
ROLLING_MOTIVATION: z.object({ motivation: z.number().int() }),
|
||||
// Rolled scores (and the standard array) are assigned by index; point buy
|
||||
// sends the six scores themselves.
|
||||
ASSIGNING_SCORES: z.object({
|
||||
assignments: z.object({
|
||||
STR: z.number().int().min(0).max(5),
|
||||
DEX: z.number().int().min(0).max(5),
|
||||
CON: z.number().int().min(0).max(5),
|
||||
INT: z.number().int().min(0).max(5),
|
||||
WIS: z.number().int().min(0).max(5),
|
||||
CHA: z.number().int().min(0).max(5),
|
||||
}),
|
||||
pointBuy: z.record(z.enum(ABILITIES), z.number().int().min(8).max(15)).optional(),
|
||||
assignments: z
|
||||
.object({
|
||||
STR: z.number().int().min(0).max(5),
|
||||
DEX: z.number().int().min(0).max(5),
|
||||
CON: z.number().int().min(0).max(5),
|
||||
INT: z.number().int().min(0).max(5),
|
||||
WIS: z.number().int().min(0).max(5),
|
||||
CHA: z.number().int().min(0).max(5),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
// Skill proficiencies are now auto-generated (see autoSelectSkills, applied
|
||||
// when this stage is entered from ASSIGNING_SCORES below) — this payload is
|
||||
@@ -1007,8 +1029,23 @@ router.post('/:id/build/choose', requireCharacterAccess({ write: true }), async
|
||||
buildState.motivationChoice = motivation.key;
|
||||
buildState.motivationText = motivation.label;
|
||||
} else if (stage === 'ASSIGNING_SCORES') {
|
||||
let { assignments } = parsed.data;
|
||||
if (req.campaign.abilityScoreMethod === 'POINT_BUY') {
|
||||
const scores = parsed.data.pointBuy;
|
||||
if (!scores || ABILITIES.some((ability) => scores[ability] === undefined)) {
|
||||
return res.status(400).json({ error: 'Buy a score for each of the six abilities.' });
|
||||
}
|
||||
const cost = pointBuyCost(scores);
|
||||
if (cost === null || cost > POINT_BUY_BUDGET) {
|
||||
return res.status(400).json({ error: `Those scores cost ${cost} points; you have ${POINT_BUY_BUDGET}.` });
|
||||
}
|
||||
// Kept the way rolled scores are, each assigned to its own ability.
|
||||
buildState.abilityRolls = ABILITIES.map((ability) => ({ dice: [], dropped: null, total: scores[ability] }));
|
||||
buildState.pointBuy = scores;
|
||||
assignments = Object.fromEntries(ABILITIES.map((ability, i) => [ability, i]));
|
||||
}
|
||||
if (!buildState.abilityRolls) return res.status(400).json({ error: 'Roll ability scores first.' });
|
||||
const { assignments } = parsed.data;
|
||||
if (!assignments) return res.status(400).json({ error: 'Assign each score to an ability.' });
|
||||
const usedIndexes = Object.values(assignments);
|
||||
const uniqueIndexes = new Set(usedIndexes);
|
||||
if (uniqueIndexes.size !== 6 || Math.max(...usedIndexes) > 5) {
|
||||
|
||||
@@ -17,8 +17,10 @@ router.use(requireAuth);
|
||||
|
||||
async function requireEncounterAccess(req, res, next) {
|
||||
try {
|
||||
const encounter = await prisma.encounter.findUnique({ where: { id: req.params.id } });
|
||||
if (!encounter) return res.status(404).json({ error: 'Encounter not found' });
|
||||
const found = await prisma.encounter.findUnique({ where: { id: req.params.id }, include: { campaign: true } });
|
||||
if (!found) return res.status(404).json({ error: 'Encounter not found' });
|
||||
// The campaign rides along for its settings (req.campaign), not on the encounter.
|
||||
const { campaign, ...encounter } = found;
|
||||
|
||||
const membership = await loadCampaignMembership(req.user.id, encounter.campaignId);
|
||||
if (!membership) return res.status(403).json({ error: 'Not a member of this campaign' });
|
||||
@@ -35,6 +37,7 @@ async function requireEncounterAccess(req, res, next) {
|
||||
}
|
||||
|
||||
req.encounter = encounter;
|
||||
req.campaign = campaign;
|
||||
req.membership = membership;
|
||||
req.isDM = isDM;
|
||||
return next();
|
||||
@@ -793,6 +796,9 @@ router.post('/:id/award-xp', requireEncounterAccess, requireDM, async (req, res,
|
||||
try {
|
||||
const parsed = awardXpSchema.safeParse(req.body || {});
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
if (!req.campaign.trackXp) {
|
||||
return res.status(400).json({ error: 'This campaign does not keep experience points.' });
|
||||
}
|
||||
if (req.encounter.xpAwarded != null) {
|
||||
return res.status(400).json({ error: `XP has already been awarded for this encounter (${req.encounter.xpAwarded}). Change a character's XP on their sheet.` });
|
||||
}
|
||||
|
||||
@@ -180,6 +180,16 @@ const seedSchema = z.strictObject({
|
||||
maxEquipped: z.number().int().min(1).max(20).optional(),
|
||||
maxEquippedWeapons: z.number().int().min(0).max(20).optional(),
|
||||
subclassesEnabled: z.boolean().optional(),
|
||||
multiclassEnabled: z.boolean().optional(),
|
||||
diceMode: z.enum(['DIGITAL', 'PHYSICAL', 'BOTH']).optional(),
|
||||
autoRollMonsters: z.boolean().optional(),
|
||||
autoRollNpcs: z.boolean().optional(),
|
||||
trackXp: z.boolean().optional(),
|
||||
hpOnLevelUp: z.enum(['AVERAGE', 'ROLL']).optional(),
|
||||
abilityScoreMethod: z.enum(['ROLL_4D6', 'STANDARD_ARRAY', 'POINT_BUY']).optional(),
|
||||
rollVisibility: z.enum(['ALL', 'OWN']).optional(),
|
||||
hideMonsterRolls: z.boolean().optional(),
|
||||
critRule: z.enum(['DOUBLE_DICE', 'MAX_PLUS_ROLL']).optional(),
|
||||
}),
|
||||
locations: z.array(z.strictObject({ name: text(80), description: optionalText(2000) })).default([]),
|
||||
weapons: poolSchema,
|
||||
|
||||
Reference in New Issue
Block a user