// Loading a character so that its sheet comes out right. The sheet depends on // what the character has equipped (game/calculations.js, activeGear), so every // query that feeds computeCharacterSheet includes the same relations. const { computeCharacterSheet } = require('../game/calculations'); const SHEET_INCLUDE = { skillProficiencies: true, saveProficiencies: true, inventoryItems: { include: { item: true } }, weapons: { include: { weapon: true } }, }; function sheetOf(character) { return computeCharacterSheet(character, character.skillProficiencies, character.saveProficiencies); } // Equipping, unequipping or changing gear can lower a character's maximum HP. // Brings current HP back down to the new maximum where it is now above it, // and returns the ids of the characters that changed. Takes a Prisma client or // transaction client as `db`. async function settleHp(db, characterIds) { const changed = []; const characters = await db.character.findMany({ where: { id: { in: [...new Set(characterIds)] }, buildStage: 'COMPLETE' }, include: SHEET_INCLUDE }); for (const character of characters) { const max = sheetOf(character).hp.max; if (character.currentHp > max) { await db.character.update({ where: { id: character.id }, data: { currentHp: max } }); changed.push(character.id); } } return changed; } module.exports = { SHEET_INCLUDE, sheetOf, settleHp };