import { useState } from 'react'; import { Modal } from './Modal'; import { NumberInput } from './NumberInput'; import { ABILITY_LABELS, SKILL_LABELS, fmtMod } from '../lib/format'; import { rollD20, type RollMode } from '../lib/diceRoller'; import type { Ability, AbilityDetail, SaveBonuses, SkillBonuses } from '../api/types'; const ABILITIES: Ability[] = ['STR', 'DEX', 'CON', 'INT', 'WIS', 'CHA']; // What the modal needs to know about each character who might roll. export interface Checker { id: string; name: string; abilities: Record; saves: SaveBonuses; skills: SkillBonuses; } interface Result { id: string; name: string; dice: number[]; kept: number; bonus: number; total: number; } // A check is "SKILL:STEALTH", "SAVE:DEX" or "ABILITY:STR". function describe(check: string): string { const [kind, key] = check.split(':'); if (kind === 'SKILL') return SKILL_LABELS[key]; return `${ABILITY_LABELS[key]} ${kind === 'SAVE' ? 'save' : 'check'}`; } function bonusFor(c: Checker, check: string): number { const [kind, key] = check.split(':'); if (kind === 'SKILL') return c.skills[key].bonus; if (kind === 'SAVE') return c.saves[key as Ability].bonus; return c.abilities[key as Ability].modifier; } // The DM calling for a roll from several characters at once: everyone makes // a Stealth check, or a Dexterity save against a fireball. Pick the roll and // who makes it; each gets their own d20 with their own bonus, and with a DC // the list says who made it. Rolled in the browser, like every table roll. export function GroupCheckModal({ characters, onClose }: { characters: Checker[]; onClose: () => void }) { const [check, setCheck] = useState('SKILL:PERCEPTION'); const [dc, setDc] = useState(''); const [mode, setMode] = useState('NORMAL'); const [left, setLeft] = useState>(new Set()); // who sits this one out const [results, setResults] = useState<{ check: string; dc: number | null; rows: Result[] } | null>(null); const rolling = characters.filter((c) => !left.has(c.id)); const target = dc === '' ? null : Number(dc); function roll() { setResults({ check, dc: target, rows: rolling.map((c) => { const { kept, dice } = rollD20(mode); const bonus = bonusFor(c, check); return { id: c.id, name: c.name, dice, kept, bonus, total: kept + bonus }; }), }); } const passed = results && results.dc != null ? results.rows.filter((r) => r.total >= results.dc!).length : 0; return (
setDc(e.target.value)} style={{ width: 70 }} />
{characters.map((c) => ( ))}
{results && (
{describe(results.check)} {results.dc != null && ` · DC ${results.dc} · ${passed} of ${results.rows.length} made it`}
{results.rows.map((r) => { const made = results.dc == null ? null : r.total >= results.dc; return (
{r.name}{' '} {r.dice.length > 1 ? `${r.dice.join(' / ')} → ${r.kept}` : r.kept} {r.bonus >= 0 ? '+' : '−'} {Math.abs(r.bonus)} {r.kept === 20 && ' · natural 20'} {r.kept === 1 && ' · natural 1'} {r.total} {made != null && ( {made ? 'made it' : 'failed'} )}
); })} {results.dc != null && (

A group check succeeds when at least half make it{passed * 2 >= results.rows.length ? ': this one does.' : ': this one does not.'}

)}
)}
); }