- 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>
122 lines
4.9 KiB
JavaScript
122 lines
4.9 KiB
JavaScript
// Creates one campaign, fully built, from a checked seed (seeds/index.js,
|
|
// buildSeed), with `owner` as its DM. `db` is a transaction client, so a
|
|
// failure leaves nothing behind.
|
|
const { generateInviteCode } = require('../utils/inviteCode');
|
|
|
|
// Notes list newest-first on screen, so they are stamped a second apart,
|
|
// backwards: the first entry in the seed is the one at the top.
|
|
function bookOrder(index) {
|
|
return new Date(Date.now() - index * 1000);
|
|
}
|
|
|
|
function byName(rows) {
|
|
return new Map(rows.map((row) => [row.name, row]));
|
|
}
|
|
|
|
const MONEY = { gold: 'GOLD', silver: 'SILVER', copper: 'COPPER', cash: 'CASH' };
|
|
|
|
async function createCampaignFromSeed(db, owner, seed) {
|
|
let inviteCode = generateInviteCode();
|
|
while (await db.campaign.findUnique({ where: { inviteCode } })) inviteCode = generateInviteCode();
|
|
|
|
const campaign = await db.campaign.create({
|
|
data: { ...seed.campaign, inviteCode, ownerId: owner.id, members: { create: { userId: owner.id, role: 'DM' } } },
|
|
});
|
|
const campaignId = campaign.id;
|
|
const inCampaign = (rows) => rows.map((row) => ({ ...row, campaignId }));
|
|
|
|
await db.location.createMany({ data: inCampaign(seed.locations) });
|
|
await db.weapon.createMany({ data: inCampaign(seed.weapons) });
|
|
await db.item.createMany({ data: inCampaign(seed.items) });
|
|
await db.spell.createMany({ data: inCampaign(seed.spells) });
|
|
await db.monster.createMany({ data: inCampaign(seed.monsters) });
|
|
await db.npc.createMany({ data: inCampaign(seed.npcs.map(({ wares, ...npc }) => npc)) });
|
|
|
|
const where = { where: { campaignId } };
|
|
const monsters = byName(await db.monster.findMany(where));
|
|
const npcs = byName(await db.npc.findMany(where));
|
|
const weapons = byName(await db.weapon.findMany(where));
|
|
const items = byName(await db.item.findMany(where));
|
|
const locations = byName(await db.location.findMany(where));
|
|
|
|
// A weapon or item named in a reward or a ware, with the level it is handed
|
|
// over at: its own starting level unless the seed asks for higher.
|
|
const gear = ({ weapon, item, level }) => {
|
|
const entry = weapon ? weapons.get(weapon) : items.get(item);
|
|
return { [weapon ? 'weaponId' : 'itemId']: entry.id, level: level ?? entry.minLevel };
|
|
};
|
|
const wares = (list, seller) =>
|
|
(list || []).map(({ price, currency, stock, ...ware }, i) => ({
|
|
...seller,
|
|
...gear(ware),
|
|
campaignId,
|
|
price,
|
|
currency: currency || 'GOLD',
|
|
stock: stock ?? null,
|
|
createdAt: new Date(Date.now() + i), // wares list in the order written
|
|
}));
|
|
|
|
for (const npc of seed.npcs) {
|
|
if (npc.wares) await db.saleOffer.createMany({ data: wares(npc.wares, { npcId: npcs.get(npc.name).id }) });
|
|
}
|
|
|
|
for (const [index, enc] of seed.encounters.entries()) {
|
|
const participants = [];
|
|
for (const [name, count, displayName, hp] of enc.monsters || []) {
|
|
const monster = monsters.get(name);
|
|
const label = displayName || monster.name;
|
|
const maxHp = hp ?? monster.maxHp;
|
|
for (let i = 1; i <= count; i++) {
|
|
participants.push({
|
|
monsterId: monster.id,
|
|
displayName: count > 1 ? `${label} #${i}` : label,
|
|
currentHp: maxHp,
|
|
maxHp,
|
|
isMonster: true,
|
|
});
|
|
}
|
|
}
|
|
for (const entry of enc.npcs || []) {
|
|
const { name, hp } = typeof entry === 'string' ? { name: entry } : entry;
|
|
const npc = npcs.get(name);
|
|
// An NPC with no combat stats takes part with no hit points to track.
|
|
const maxHp = hp ?? npc.maxHp ?? 0;
|
|
participants.push({ npcId: npc.id, displayName: npc.name, currentHp: maxHp, maxHp, isMonster: false });
|
|
}
|
|
|
|
// Rewards list oldest-first, so these are stamped forwards.
|
|
const rewards = (enc.rewards || []).map(({ quantity, note, ...reward }, i) => {
|
|
const createdAt = new Date(Date.now() + i);
|
|
const coin = Object.keys(MONEY).find((key) => reward[key]);
|
|
if (coin) return { type: 'GOLD', gold: reward[coin], currency: MONEY[coin], note, createdAt };
|
|
return { type: reward.weapon ? 'WEAPON' : 'ITEM', ...gear(reward), quantity: quantity ?? 1, note, createdAt };
|
|
});
|
|
|
|
const encounter = await db.encounter.create({
|
|
data: {
|
|
campaignId,
|
|
name: enc.name,
|
|
mode: enc.mode,
|
|
description: enc.description,
|
|
dmNotes: enc.dmNotes,
|
|
locationId: enc.location ? locations.get(enc.location).id : null,
|
|
sortOrder: index,
|
|
participants: { create: participants },
|
|
rewards: { create: rewards },
|
|
},
|
|
});
|
|
if (enc.wares) await db.saleOffer.createMany({ data: wares(enc.wares, { encounterId: encounter.id }) });
|
|
}
|
|
|
|
await db.note.createMany({
|
|
data: [
|
|
...seed.sharedNotes.map((body) => ({ body, visibility: 'SHARED' })),
|
|
...seed.dmNotes.map((body) => ({ body, visibility: 'DM_ONLY' })),
|
|
].map((note, i) => ({ ...note, campaignId, authorId: owner.id, createdAt: bookOrder(i) })),
|
|
});
|
|
|
|
return campaign;
|
|
}
|
|
|
|
module.exports = { createCampaignFromSeed };
|