import { ABILITY_ORDER } from './previewCalc'; import { formatMoney } from './format'; import type { Ability, Monster, Npc } from '../api/types'; // One thing in a campaign's lists that opens in a pop-out. export type EntryKind = 'weapon' | 'item' | 'spell' | 'monster' | 'npc'; export interface EntryRef { kind: EntryKind; id: string; // It was added by hand a moment ago: its pop-out offers to add another. isNew?: boolean; } export type MonsterInput = Omit; export type AbilityScores = Pick; export const scoreField = (ability: string) => `${ability.toLowerCase()}Score` as keyof AbilityScores; // "STR 8 · DEX 14 · …", in stat-block order. export function abilityLine(scores: AbilityScores): string { return ABILITY_ORDER.map((a) => `${a} ${scores[scoreField(a)]}`).join(' · '); } // "1 gp = $5": the rate an NPC changes gold and cash at, or null if they don't. export function rateLabel(npc: Pick): string | null { if (!npc.exchangeGold || !npc.exchangeCash) return null; return `${formatMoney(npc.exchangeGold, 'GOLD')} = ${formatMoney(npc.exchangeCash, 'CASH')}`; } // Whether every word of a search is somewhere in the text given: "fire bolt" // finds "Fire Bolt", and "bolt fire" does too. export function matchesSearch(query: string, ...fields: (string | number | null | undefined)[]): boolean { const words = query.toLowerCase().split(/\s+/).filter(Boolean); const text = fields.filter((f) => f !== null && f !== undefined).join(' ').toLowerCase(); return words.every((word) => text.includes(word)); } // How a spell lands in a battle, as one choice: it just takes effect, the // caster rolls a spell attack, or each target makes a save with an ability // (taking half damage on a success, or none). `landingValue` and // `landingFrom` turn it into the value of a select and back. export interface SpellLanding { attackRoll: boolean; saveAbility: Ability | null; halfOnSave: boolean; } export function landingValue(spell: SpellLanding): string { if (spell.attackRoll) return 'ATTACK'; return spell.saveAbility ? `${spell.saveAbility}:${spell.halfOnSave ? 'HALF' : 'NONE'}` : ''; } export function landingFrom(value: string): SpellLanding { if (value === 'ATTACK') return { attackRoll: true, saveAbility: null, halfOnSave: false }; if (!value) return { attackRoll: false, saveAbility: null, halfOnSave: false }; const [ability, half] = value.split(':'); return { attackRoll: false, saveAbility: ability as Ability, halfOnSave: half === 'HALF' }; }