// 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 };