- A character's purse holds gold, silver and copper as separate coins: 1 gold = 10 silver = 100 copper. The money log records each entry's coin. - Spending and giving are paid from the whole purse: coins of the kind named go first, then the nearest bigger coin is broken (change comes back in as few coins as possible), then smaller coins are counted out. A payment is refused only when the whole purse isn't worth enough. - Exchange converts one coin into another at the fixed worth. - Encounter rewards and gifts between characters can be in any coin; the party overview shows the whole purse. - All packages at version 1.4.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
4.6 KiB
TypeScript
135 lines
4.6 KiB
TypeScript
import type { Currency } from '../api/types';
|
|
|
|
export const SPECIES_LABELS: Record<string, string> = {
|
|
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<string, string> = {
|
|
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<string, string> = {
|
|
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<string, string> = {
|
|
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<Currency, string> = { GOLD: 'Gold', SILVER: 'Silver', COPPER: 'Copper' };
|
|
export const COIN_SHORT: Record<Currency, string> = { GOLD: 'gp', SILVER: 'sp', COPPER: 'cp' };
|
|
export const COIN_FIELD: Record<Currency, 'gold' | 'silver' | 'copper'> = { GOLD: 'gold', SILVER: 'silver', COPPER: 'copper' };
|
|
// What one coin is worth, counted in copper.
|
|
export const COIN_VALUE: Record<Currency, number> = { 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<string, string[]> };
|
|
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'];
|