import type { Currency } from '../api/types'; export const SPECIES_LABELS: Record = { HUMAN: 'Human', HALF_ELF: 'Half-Elf', ELF: 'Elf', GNOME: 'Gnome', DWARF: 'Dwarf', HALFLING: 'Halfling', DRAGONBORN: 'Dragonborn', TIEFLING: 'Tiefling', HALF_ORC: 'Half-Orc', ORC: 'Orc', }; export const CLASS_LABELS: Record = { FIGHTER: 'Fighter', BARBARIAN: 'Barbarian', ROGUE: 'Rogue', RANGER: 'Ranger', WIZARD: 'Wizard', SORCERER: 'Sorcerer', CLERIC: 'Cleric', PALADIN: 'Paladin', BARD: 'Bard', WARLOCK: 'Warlock', DRUID: 'Druid', MONK: 'Monk', }; export const SKILL_LABELS: Record = { ACROBATICS: 'Acrobatics', ANIMAL_HANDLING: 'Animal Handling', ARCANA: 'Arcana', ATHLETICS: 'Athletics', DECEPTION: 'Deception', HISTORY: 'History', INSIGHT: 'Insight', INTIMIDATION: 'Intimidation', INVESTIGATION: 'Investigation', MEDICINE: 'Medicine', NATURE: 'Nature', PERCEPTION: 'Perception', PERFORMANCE: 'Performance', PERSUASION: 'Persuasion', RELIGION: 'Religion', SLEIGHT_OF_HAND: 'Sleight of Hand', STEALTH: 'Stealth', SURVIVAL: 'Survival', }; export const ABILITY_LABELS: Record = { STR: 'Strength', DEX: 'Dexterity', CON: 'Constitution', INT: 'Intelligence', WIS: 'Wisdom', CHA: 'Charisma', }; export function fmtMod(mod: number): string { return mod >= 0 ? `+${mod}` : `${mod}`; } // Healthy / bloodied (<=50%) / critical (<=25%) — matches .hp-bar-fill's // modifier classes in index.css. export function hpBarClass(current: number, max: number): string { if (max <= 0) return 'hp-bar-fill'; const pct = current / max; if (pct <= 0.25) return 'hp-bar-fill hp-critical'; if (pct <= 0.5) return 'hp-bar-fill hp-bloodied'; return 'hp-bar-fill'; } // Coins. Silver is worth 0.1 gold and copper 0.01 gold. export const CURRENCIES: Currency[] = ['GOLD', 'SILVER', 'COPPER']; export const COIN_LABELS: Record = { GOLD: 'Gold', SILVER: 'Silver', COPPER: 'Copper' }; export const COIN_SHORT: Record = { GOLD: 'gp', SILVER: 'sp', COPPER: 'cp' }; export const COIN_FIELD: Record = { GOLD: 'gold', SILVER: 'silver', COPPER: 'copper' }; // What one coin is worth, counted in copper. export const COIN_VALUE: Record = { GOLD: 100, SILVER: 10, COPPER: 1 }; type Purse = { gold: number | null; silver: number | null; copper: number | null }; // "12 gp · 5 sp · 3 cp". Gold always shows; silver and copper only when held. export function formatPurse(purse: Purse): string { return CURRENCIES.filter((c) => c === 'GOLD' || (purse[COIN_FIELD[c]] ?? 0) > 0) .map((c) => `${purse[COIN_FIELD[c]] ?? 0} ${COIN_SHORT[c]}`) .join(' · '); } // The whole purse as one figure in gold, e.g. 12.65. export function purseWorthInGold(purse: Purse): number { const copper = CURRENCIES.reduce((sum, c) => sum + (purse[COIN_FIELD[c]] ?? 0) * COIN_VALUE[c], 0); return copper / COIN_VALUE.GOLD; } export function extractErrorMessage(err: unknown): string { const anyErr = err as { response?: { data?: { error?: unknown } } }; const apiError = anyErr?.response?.data?.error; if (typeof apiError === 'string') return apiError; if (apiError && typeof apiError === 'object') { const flat = apiError as { formErrors?: string[]; fieldErrors?: Record }; const messages = [ ...(flat.formErrors || []), ...Object.values(flat.fieldErrors || {}).flat(), ]; if (messages.length) return messages.join(', '); return JSON.stringify(apiError); } if (err instanceof Error) return err.message; return 'Something went wrong.'; } // A cantrip is spell level 0. It is always called a cantrip, never "level 0" // or "level 1". export function spellLevelLabel(level: number): string { return level === 0 ? 'Cantrip' : `Level ${level}`; } // Who can use a spell: a character of at least its level; cantrips, anyone. export function spellRequirementLabel(level: number): string { return level === 0 ? 'any character can use it' : `needs character level ${level}`; } // The damage values offered for a spell, grouped by how many dice are rolled. const SPELL_DIE_SIZES = [4, 6, 8, 10, 12]; const SPELL_DIE_COUNTS = [1, 2, 3, 4, 5, 6, 8, 10]; export const SPELL_DAMAGE_GROUPS: { label: string; options: string[] }[] = SPELL_DIE_COUNTS.map((count) => ({ label: count === 1 ? '1 die' : `${count} dice`, options: SPELL_DIE_SIZES.map((size) => `${count}d${size}`), })); // Values the spell library uses that don't fit the pattern above. export const SPELL_DAMAGE_SPECIALS = ['3d4+3', '2d8+4d6', '20'];