V1.12.0: worlds from a code, shops and cash in the seeds, up-to-date packages
- A DM can start a campaign from a ready-made world by typing its six-character code: "+ New Campaign" on the Campaigns page, then "Or start from a ready-made world". The whole world is built in one go with them as its DM: locations, weapons, items, spells, monsters, NPCs, encounters and notes. ALDENF is Aldenfell, HOLLOW is Hollowcrown. - A world is one file in /backend/seeds. Drop another in and its code works straight away, with no restart. "npm run seeds:check" lists every world, or what is wrong with one: a misspelt field, a name used twice, an encounter pointing at something the file does not have. A file with a mistake in it is never offered. docs/SEEDS.md has the format. - A seed can now hold everything a campaign can: gear effects, an NPC's wares and exchange rate, things for sale at an encounter, and rewards in silver, copper and cash. - The database seed creates the admin account and nothing else. The Aldenfell and Hollowcrown seed commands are gone: worlds come from codes. - Hollowcrown follows the V3 book: 4 starting items and 3 things equipped, the book's effects on every weapon and item, armor as items (library, modern and magic), worn gear at the book's levels, damage types on monsters, ten shop scenes with prices and stock, five money changers, Meridian loot as cash and mixed hoards as gold, silver and copper. - Aldenfell: the Shield and Leather Armor change AC, Mireille and the Founding Fair stalls sell things, and the book's missing scenes are in (E5, E7, E15, E16, E18, E23-24, E26, the Drowned Shrine, Sevrisk's Hollow, a wyvern pass). The locket is found with Investigation, as the book has it. - Packages are up to date: Prisma 7 (with the pg driver adapter and a prisma.config.js that holds the database URL), Express 5, zod 4, dotenv 18, jest 30, bcrypt 6 and TypeScript 7. "npm audit" finds nothing. Install scripts are approved for Prisma and bcrypt. On a server: "npm install", then "npm run deploy". - The frontend's five lint warnings are fixed; useAuth and usePagination have files of their own. - All packages at version 1.12.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2720,3 +2720,89 @@ describe('equipping', () => {
|
||||
expect((await wear('weapons', sword.id, true)).status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('a campaign from a world seed', () => {
|
||||
async function registerDm() {
|
||||
const res = await request(app).post('/auth/register').send({ email: uniqueEmail('seed-dm'), password: 'password123', displayName: 'Seed DM' });
|
||||
cleanupUserIds.push(res.body.user.id);
|
||||
return { Authorization: `Bearer ${res.body.token}` };
|
||||
}
|
||||
const fromSeed = (auth, code) => request(app).post('/campaigns/from-seed').set(auth).send({ code });
|
||||
|
||||
itIfDb('builds the whole world for whoever enters its code, as its DM', async () => {
|
||||
const auth = await registerDm();
|
||||
const res = await fromSeed(auth, ' aldenf ');
|
||||
expect(res.status).toBe(201);
|
||||
const campaign = res.body.campaign;
|
||||
cleanupCampaignIds.push(campaign.id);
|
||||
expect(campaign).toMatchObject({ name: 'Aldenfell', role: 'DM', startingGold: 15 });
|
||||
expect(campaign.inviteCode).toMatch(/^[A-Z0-9]{6}$/);
|
||||
|
||||
const get = async (path) => (await request(app).get(`/campaigns/${campaign.id}/${path}`).set(auth)).body;
|
||||
const { weapons } = await get('weapons');
|
||||
const { items } = await get('items');
|
||||
const { npcs } = await get('npcs');
|
||||
const { encounters } = await get('encounters');
|
||||
expect((await get('locations')).locations.length).toBeGreaterThan(10);
|
||||
expect((await get('monsters')).monsters.length).toBeGreaterThan(10);
|
||||
expect((await get('notes')).notes.length).toBeGreaterThan(10);
|
||||
|
||||
// Library copies and the seed's own rows, with what the seed set on them.
|
||||
expect(weapons.find((w) => w.name === 'Longsword')).toMatchObject({ isStartingOption: true });
|
||||
expect(weapons.find((w) => w.name === 'Hollowbane')).toMatchObject({ isStartingOption: false, minLevel: 7 });
|
||||
expect(items.find((i) => i.name === 'Shield').effects).toEqual([{ stat: 'AC', amount: 2 }]);
|
||||
|
||||
// An NPC's wares, and a stall's, point at the campaign's own pool entries.
|
||||
const mireille = npcs.find((n) => n.name === 'Mireille Costa');
|
||||
const potion = mireille.wares.find((w) => w.item && w.item.name === 'Potion of Healing');
|
||||
expect(potion).toMatchObject({ price: 50, currency: 'GOLD', itemId: items.find((i) => i.name === 'Potion of Healing').id });
|
||||
const greater = mireille.wares.find((w) => w.item && w.item.name === 'Potion of Greater Healing');
|
||||
expect(greater.level).toBe(3); // sold at the item's own starting level
|
||||
|
||||
// Encounters keep the seed's order, with their people, creatures and loot.
|
||||
expect(encounters[0].name).toBe('E1 — The Founding Fair');
|
||||
const fair = (await request(app).get(`/encounters/${encounters[0].id}`).set(auth)).body.encounter;
|
||||
expect(fair.wares.find((w) => w.item && w.item.name === 'Torches (bundle of 5)')).toMatchObject({ price: 5, currency: 'COPPER' });
|
||||
expect(fair.participants.map((p) => p.displayName)).toEqual(expect.arrayContaining(['Mireille Costa', 'Ser Kavan Thorne']));
|
||||
const ossuary = encounters.find((e) => e.name === 'E3 — Sunken Vaults: The Restless Ossuary');
|
||||
const fight = (await request(app).get(`/encounters/${ossuary.id}`).set(auth)).body.encounter;
|
||||
expect(fight.participants.map((p) => p.displayName).sort()).toEqual(['Skeleton #1', 'Skeleton #2']);
|
||||
expect(fight.rewards[0]).toMatchObject({ type: 'GOLD', gold: 25, currency: 'GOLD' });
|
||||
});
|
||||
|
||||
itIfDb('seeds cash, the other coins and money changers', async () => {
|
||||
const auth = await registerDm();
|
||||
const res = await fromSeed(auth, 'HOLLOW');
|
||||
expect(res.status).toBe(201);
|
||||
const campaign = res.body.campaign;
|
||||
cleanupCampaignIds.push(campaign.id);
|
||||
expect(campaign).toMatchObject({ startingItemCount: 4, maxEquipped: 3, maxEquippedWeapons: 1 });
|
||||
|
||||
const npcs = (await request(app).get(`/campaigns/${campaign.id}/npcs`).set(auth)).body.npcs;
|
||||
expect(npcs.find((n) => n.name === 'Ray')).toMatchObject({ exchangeGold: 1, exchangeCash: 50 });
|
||||
const encounters = (await request(app).get(`/campaigns/${campaign.id}/encounters`).set(auth)).body.encounters;
|
||||
const detail = async (name) => (await request(app).get(`/encounters/${encounters.find((e) => e.name === name).id}`).set(auth)).body.encounter;
|
||||
expect((await detail('Security Response')).rewards).toEqual(expect.arrayContaining([expect.objectContaining({ type: 'GOLD', gold: 300, currency: 'CASH' })]));
|
||||
expect((await detail('The Goblin Den')).rewards.filter((r) => r.type === 'GOLD').map((r) => `${r.gold} ${r.currency}`)).toEqual(['12 GOLD', '50 SILVER', '100 COPPER']);
|
||||
const shops = await detail('Port Meridian: Ray’s Cash for Gold and the Shops');
|
||||
expect(shops.wares.find((w) => w.weapon && w.weapon.name === 'Chainsaw')).toMatchObject({ price: 300, currency: 'CASH', level: 5, stock: 1 });
|
||||
});
|
||||
|
||||
itIfDb('the same code can be used again, by anyone, for a copy of their own', async () => {
|
||||
const first = await fromSeed(await registerDm(), 'ALDENF');
|
||||
const second = await fromSeed(await registerDm(), 'ALDENF');
|
||||
cleanupCampaignIds.push(first.body.campaign.id, second.body.campaign.id);
|
||||
expect(second.status).toBe(201);
|
||||
expect(second.body.campaign.id).not.toBe(first.body.campaign.id);
|
||||
expect(second.body.campaign.inviteCode).not.toBe(first.body.campaign.inviteCode);
|
||||
});
|
||||
|
||||
itIfDb('refuses a code that is no world, and needs a login', async () => {
|
||||
const auth = await registerDm();
|
||||
expect((await fromSeed(auth, 'ZZZZZZ')).status).toBe(404);
|
||||
expect((await fromSeed(auth, 'abc')).status).toBe(400);
|
||||
expect((await request(app).post('/campaigns/from-seed').set(auth).send({})).status).toBe(400);
|
||||
expect((await request(app).post('/campaigns/from-seed').send({ code: 'ALDENF' })).status).toBe(401);
|
||||
expect((await request(app).get('/campaigns').set(auth)).body.campaigns).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,6 +10,8 @@ const { WEAPON_LIBRARY, ITEM_LIBRARY, SPELL_LIBRARY, MONSTER_LIBRARY } = require
|
||||
const { visitedLocationIds } = require('../utils/locations');
|
||||
const { deathSaveInfo } = require('../utils/hp');
|
||||
const { CURRENCIES } = require('../utils/grants');
|
||||
const { findSeed, normalizeCode, CODE_LENGTH } = require('../seeds');
|
||||
const { createCampaignFromSeed } = require('../seeds/createCampaign');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -96,6 +98,28 @@ router.post('/', async (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
// POST /campaigns/from-seed — a ready-made world (a file in /backend/seeds)
|
||||
// as a new campaign, by its code. As with a blank campaign, whoever creates
|
||||
// it is its DM and owner; the same code can be used again for another copy.
|
||||
router.post('/from-seed', async (req, res, next) => {
|
||||
try {
|
||||
const parsed = z.object({ code: z.string().max(40) }).safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: parsed.error.flatten() });
|
||||
if (normalizeCode(parsed.data.code).length !== CODE_LENGTH) {
|
||||
return res.status(400).json({ error: `A world code is ${CODE_LENGTH} letters or numbers` });
|
||||
}
|
||||
const seed = findSeed(parsed.data.code);
|
||||
if (!seed) return res.status(404).json({ error: 'There is no world with that code' });
|
||||
|
||||
// One transaction, so a failure doesn't leave half a world behind. A big
|
||||
// world is thousands of rows, which is more than the default 5 seconds.
|
||||
const campaign = await prisma.$transaction((tx) => createCampaignFromSeed(tx, req.user, seed), { timeout: 120000, maxWait: 20000 });
|
||||
res.status(201).json({ campaign: campaignSummary(campaign, 'DM') });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
// POST /campaigns/join/:inviteCode — must be before requireCampaignMember-gated routes below
|
||||
router.post('/join/:inviteCode', async (req, res, next) => {
|
||||
try {
|
||||
|
||||
@@ -603,8 +603,8 @@ const chooseSchemas = {
|
||||
.optional(),
|
||||
}),
|
||||
CHOOSING_ABILITIES: z.object({ talents: z.array(z.string()) }),
|
||||
CHOOSING_BACKGROUND: z.object({ picks: z.record(z.array(z.string())) }),
|
||||
CHOOSING_APPEARANCE: z.object({ picks: z.record(z.array(z.string())) }),
|
||||
CHOOSING_BACKGROUND: z.object({ picks: z.record(z.string(), z.array(z.string())) }),
|
||||
CHOOSING_APPEARANCE: z.object({ picks: z.record(z.string(), z.array(z.string())) }),
|
||||
CHOOSING_EQUIPMENT: z.object({
|
||||
weaponIds: z.array(z.string()).default([]),
|
||||
itemIds: z.array(z.string()).default([]),
|
||||
|
||||
Reference in New Issue
Block a user