- A DM can start a campaign from a ready-made world by typing its six-character code: "+ New Campaign" on the Campaigns page, then "Or start from a ready-made world". The whole world is built in one go with them as its DM: locations, weapons, items, spells, monsters, NPCs, encounters and notes. ALDENF is Aldenfell, HOLLOW is Hollowcrown. - A world is one file in /backend/seeds. Drop another in and its code works straight away, with no restart. "npm run seeds:check" lists every world, or what is wrong with one: a misspelt field, a name used twice, an encounter pointing at something the file does not have. A file with a mistake in it is never offered. docs/SEEDS.md has the format. - A seed can now hold everything a campaign can: gear effects, an NPC's wares and exchange rate, things for sale at an encounter, and rewards in silver, copper and cash. - The database seed creates the admin account and nothing else. The Aldenfell and Hollowcrown seed commands are gone: worlds come from codes. - Hollowcrown follows the V3 book: 4 starting items and 3 things equipped, the book's effects on every weapon and item, armor as items (library, modern and magic), worn gear at the book's levels, damage types on monsters, ten shop scenes with prices and stock, five money changers, Meridian loot as cash and mixed hoards as gold, silver and copper. - Aldenfell: the Shield and Leather Armor change AC, Mireille and the Founding Fair stalls sell things, and the book's missing scenes are in (E5, E7, E15, E16, E18, E23-24, E26, the Drowned Shrine, Sevrisk's Hollow, a wyvern pass). The locket is found with Investigation, as the book has it. - Packages are up to date: Prisma 7 (with the pg driver adapter and a prisma.config.js that holds the database URL), Express 5, zod 4, dotenv 18, jest 30, bcrypt 6 and TypeScript 7. "npm audit" finds nothing. Install scripts are approved for Prisma and bcrypt. On a server: "npm install", then "npm run deploy". - The frontend's five lint warnings are fixed; useAuth and usePagination have files of their own. - All packages at version 1.12.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
299 lines
13 KiB
TypeScript
299 lines
13 KiB
TypeScript
import { Link, useMatch } from 'react-router-dom';
|
||
import { useCampaign, useCampaignCharacters, usePatchCharacterWeapon, usePatchInventoryItem, useQuickStats } from '../api/queries';
|
||
import { useAuth } from '../auth/useAuth';
|
||
import { useLastCampaignId } from '../lib/lastCampaign';
|
||
import { closeCharacterPanel, openCharacterPanel, useCharacterPanelId } from '../lib/characterPanel';
|
||
import { CLASS_LABELS, SPECIES_LABELS, fmtMod, formatPurse, spellLevelLabel } from '../lib/format';
|
||
import { effectLabel, effectsSummary } from '../lib/gear';
|
||
import { speciesIcon } from '../lib/speciesIcons';
|
||
import { Spinner, ErrorBanner } from './Spinner';
|
||
import { HpBar } from './HpBar';
|
||
import { DeathSaves } from './DeathSaves';
|
||
import { useState } from 'react';
|
||
import { extractErrorMessage } from '../lib/format';
|
||
import type { ActiveGear } from '../api/types';
|
||
|
||
// What equipped gear is doing, one line per thing: "Chain Mail: +6 AC · −10 Speed".
|
||
export function GearInForce({ gear }: { gear: ActiveGear }) {
|
||
const sources = [...new Set([...gear.effects.map((e) => e.source), ...gear.notes.map((n) => n.source)])];
|
||
if (sources.length === 0) return null;
|
||
return (
|
||
<>
|
||
{sources.map((source) => (
|
||
<div key={source} className="detail-row">
|
||
<strong>{source}</strong>
|
||
<span>
|
||
{[...gear.effects.filter((e) => e.source === source).map(effectLabel), ...gear.notes.filter((n) => n.source === source).map((n) => n.note)].join(' · ')}
|
||
</span>
|
||
</div>
|
||
))}
|
||
</>
|
||
);
|
||
}
|
||
|
||
function Flags({ equipped, usable, level }: { equipped: boolean; usable: boolean; level: number }) {
|
||
return (
|
||
<>
|
||
{equipped && <span className="badge badge-live" style={{ marginLeft: 6 }}>equipped</span>}
|
||
{!usable && <span className="badge badge-hostile" style={{ marginLeft: 6 }}>needs level {level}</span>}
|
||
</>
|
||
);
|
||
}
|
||
|
||
// One character at a glance: the numbers for a fight, and everything they
|
||
// hold. The pack, spells and purse are there for the character's own player
|
||
// and the DM, who can also put gear on and take it off from here.
|
||
function PanelBody({ characterId }: { characterId: string }) {
|
||
const { data, isLoading, error } = useQuickStats(characterId, true);
|
||
const patchWeapon = usePatchCharacterWeapon(characterId);
|
||
const patchItem = usePatchInventoryItem(characterId);
|
||
// Why something could not be equipped: too much on already.
|
||
const [equipError, setEquipError] = useState<string | null>(null);
|
||
const equip = { onError: (err: unknown) => setEquipError(extractErrorMessage(err)), onSuccess: () => setEquipError(null) };
|
||
|
||
if (isLoading) return <Spinner label="Reading the sheet…" />;
|
||
if (error || !data) return <ErrorBanner message="Could not load this character." />;
|
||
|
||
const canEquip = !data.restricted;
|
||
const stats: [string, string | number][] = [
|
||
['AC', data.armorClass],
|
||
['Initiative', fmtMod(data.initiative)],
|
||
['Speed', data.speed],
|
||
['Prof.', fmtMod(data.proficiencyBonus)],
|
||
['Passive Perc.', data.passivePerception],
|
||
];
|
||
|
||
return (
|
||
<div className="mt-md">
|
||
<div className="flex-between flex-wrap">
|
||
<div>
|
||
<h3 style={{ margin: 0 }}>
|
||
<span aria-hidden="true">{speciesIcon(data.species)}</span> {data.name}
|
||
</h3>
|
||
<div className="combatant-sub">
|
||
{data.species && SPECIES_LABELS[data.species]} {data.characterClass && CLASS_LABELS[data.characterClass]} · Level {data.level}
|
||
</div>
|
||
</div>
|
||
<Link to={`/characters/${data.id}`} className="btn" onClick={closeCharacterPanel}>
|
||
Full sheet
|
||
</Link>
|
||
</div>
|
||
|
||
<div className="stat-chips mt-sm">
|
||
<span className="stat-chip">
|
||
<span>HP</span>
|
||
<b>
|
||
{data.currentHp}/{data.maxHp}
|
||
{data.tempHp > 0 && ` +${data.tempHp}`}
|
||
</b>
|
||
</span>
|
||
{stats.map(([label, value]) => (
|
||
<span key={label} className="stat-chip">
|
||
<span>{label}</span>
|
||
<b>{value}</b>
|
||
</span>
|
||
))}
|
||
</div>
|
||
<div className="mt-sm">
|
||
<HpBar current={data.currentHp} max={data.maxHp} temp={data.tempHp} />
|
||
</div>
|
||
{/* Read-only here; the saves are rolled on the sheet or in the encounter. */}
|
||
<DeathSaves characterId={characterId} name={data.name} deathSaves={data.deathSaves} canAct={false} isDM={false} />
|
||
|
||
<div className="stat-chips mt-sm">
|
||
{(Object.keys(data.abilities) as (keyof typeof data.abilities)[]).map((a) => (
|
||
<span key={a} className="stat-chip">
|
||
<span>{a}</span>
|
||
<b>
|
||
{data.abilities[a].final} ({fmtMod(data.abilities[a].modifier)})
|
||
</b>
|
||
</span>
|
||
))}
|
||
</div>
|
||
|
||
{(data.gear.effects.length > 0 || data.gear.notes.length > 0) && (
|
||
<div className="card mt-md">
|
||
<div className="card-title">From equipped gear</div>
|
||
<GearInForce gear={data.gear} />
|
||
<p className="combatant-sub" style={{ margin: '0.4rem 0 0' }}>Already counted in the numbers above.</p>
|
||
</div>
|
||
)}
|
||
|
||
{data.spellcasting && (
|
||
<div className="card mt-md">
|
||
<div className="card-title">Spellcasting</div>
|
||
<p style={{ margin: 0 }}>
|
||
Save DC {data.spellcasting.saveDC} · Attack {fmtMod(data.spellcasting.attackBonus)}
|
||
</p>
|
||
</div>
|
||
)}
|
||
|
||
{canEquip && (
|
||
<p className="combatant-sub mt-md" style={{ marginBottom: 0 }}>
|
||
Tick what is equipped. Only equipped weapons can be used in an encounter, and only equipped gear gives its effects.
|
||
</p>
|
||
)}
|
||
{equipError && <ErrorBanner message={equipError} />}
|
||
<div className="card mt-md">
|
||
<div className="card-title">Weapons · {data.weapons.length}</div>
|
||
{data.weapons.length === 0 && <p className="combatant-sub" style={{ margin: 0 }}>No weapons.</p>}
|
||
{data.weapons.map((w) => (
|
||
<div key={w.characterWeaponId} className="reward-row">
|
||
<div className="flex-between">
|
||
<div>
|
||
<strong>{w.name}</strong>
|
||
{w.quantity > 1 && <span className="combatant-sub"> ×{w.quantity}</span>}
|
||
<Flags equipped={w.equipped} usable={w.usable} level={w.level} />
|
||
<div className="combatant-sub">
|
||
Lv {w.level} · attack {fmtMod(w.attackBonus)} · {w.damageDice} {fmtMod(w.damageBonus)} {w.damageType}
|
||
</div>
|
||
{effectsSummary(w.effects, w.effectNote) && <div className="combatant-sub">✦ {effectsSummary(w.effects, w.effectNote)}</div>}
|
||
</div>
|
||
{canEquip && (
|
||
<input
|
||
type="checkbox"
|
||
aria-label={`${w.name} is equipped`}
|
||
title="Equipped"
|
||
checked={w.equipped}
|
||
disabled={!w.usable || patchWeapon.isPending}
|
||
onChange={(e) => patchWeapon.mutate({ weaponId: w.weaponId, equipped: e.target.checked }, equip)}
|
||
/>
|
||
)}
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
|
||
{data.restricted ? (
|
||
<p className="combatant-sub mt-md">Their pack, spells and purse are their own business.</p>
|
||
) : (
|
||
<>
|
||
<div className="card mt-md">
|
||
<div className="card-title">Inventory · {data.inventory.length}</div>
|
||
{data.inventory.length === 0 && <p className="combatant-sub" style={{ margin: 0 }}>Nothing carried.</p>}
|
||
{data.inventory.map((item) => (
|
||
<div key={item.inventoryItemId} className="reward-row">
|
||
<div className="flex-between">
|
||
<div>
|
||
<strong>{item.name}</strong>
|
||
<span className="combatant-sub"> ×{item.quantity}</span>
|
||
<Flags equipped={item.equipped} usable={item.usable} level={item.level} />
|
||
<div className="combatant-sub">
|
||
Lv {item.level}
|
||
{item.damageDice && ` · ${item.damageDice}`}
|
||
</div>
|
||
{effectsSummary(item.effects, item.effectNote) && <div className="combatant-sub">✦ {effectsSummary(item.effects, item.effectNote)}</div>}
|
||
{item.description && <div className="combatant-sub">{item.description}</div>}
|
||
</div>
|
||
<input
|
||
type="checkbox"
|
||
aria-label={`${item.name} is equipped`}
|
||
title="Equipped"
|
||
checked={item.equipped}
|
||
disabled={!item.usable || patchItem.isPending}
|
||
onChange={(e) => patchItem.mutate({ itemId: item.itemId, equipped: e.target.checked }, equip)}
|
||
/>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
|
||
<div className="card mt-md">
|
||
<div className="card-title">
|
||
<span>Money</span>
|
||
{data.purse && <span className="mono">{formatPurse(data.purse)}</span>}
|
||
</div>
|
||
</div>
|
||
|
||
{data.spells.length > 0 && (
|
||
<div className="card mt-md">
|
||
<div className="card-title">Spells · {data.spells.length}</div>
|
||
{data.spells.map((s) => (
|
||
<div key={s.characterSpellId} className="reward-row">
|
||
<strong>{s.name}</strong>
|
||
<span className="combatant-sub">
|
||
{' '}
|
||
{spellLevelLabel(s.level)}
|
||
{s.damageDice && ` · ${s.damageDice}`}
|
||
</span>
|
||
{s.prepared && <span className="badge badge-live" style={{ marginLeft: 6 }}>prepared</span>}
|
||
{!s.usable && <span className="badge badge-hostile" style={{ marginLeft: 6 }}>needs level {s.level}</span>}
|
||
{s.description && <div className="combatant-sub">{s.description}</div>}
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
|
||
{data.specialAbilities && (
|
||
<div className="card mt-md">
|
||
<div className="card-title">Special Abilities</div>
|
||
<p style={{ margin: 0, whiteSpace: 'pre-wrap' }}>{data.specialAbilities}</p>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// The floating character button, on every page, and the panel it opens. A
|
||
// player gets their own characters in the campaign they are in; the DM gets
|
||
// everyone's. Other pages can open the panel for a particular character with
|
||
// openCharacterPanel (lib/characterPanel.ts).
|
||
export function CharacterPanel() {
|
||
const { user } = useAuth();
|
||
const campaignId = useLastCampaignId() ?? undefined;
|
||
const { data: campaignData } = useCampaign(campaignId);
|
||
const { data: characters } = useCampaignCharacters(campaignId);
|
||
const onSheet = useMatch('/characters/:characterId');
|
||
const openId = useCharacterPanelId();
|
||
|
||
const isDM = campaignData?.campaign.role === 'DM';
|
||
const choices = (characters ?? []).filter((c) => c.buildStage === 'COMPLETE' && (isDM || c.userId === user?.id));
|
||
if (choices.length === 0 && !openId) return null;
|
||
|
||
// On a character's own sheet the button opens that character; otherwise the first of yours.
|
||
const fallback = choices.find((c) => c.id === onSheet?.params.characterId) ?? choices.find((c) => c.userId === user?.id) ?? choices[0];
|
||
const shown = openId ?? undefined;
|
||
|
||
return (
|
||
<>
|
||
{fallback && (
|
||
<button
|
||
className="fab-quickstats"
|
||
onClick={() => openCharacterPanel(fallback.id)}
|
||
title={isDM ? 'Characters: stats and inventory' : 'Your character: stats and inventory'}
|
||
aria-label="Open the character panel"
|
||
>
|
||
🧙
|
||
</button>
|
||
)}
|
||
{shown && (
|
||
<>
|
||
<div className="drawer-backdrop" onClick={closeCharacterPanel} />
|
||
<div className="drawer" role="dialog" aria-label="Character panel">
|
||
<div className="flex-between">
|
||
{choices.length > 1 && choices.some((c) => c.id === shown) ? (
|
||
<select aria-label="Which character" value={shown} onChange={(e) => openCharacterPanel(e.target.value)} style={{ flex: 1, minWidth: 0 }}>
|
||
{choices.map((c) => (
|
||
<option key={c.id} value={c.id}>
|
||
{c.name}
|
||
{isDM && c.userId === user?.id ? ' (yours)' : ''}
|
||
</option>
|
||
))}
|
||
</select>
|
||
) : (
|
||
<h2 style={{ margin: 0 }}>Character</h2>
|
||
)}
|
||
<button className="btn btn-ghost" onClick={closeCharacterPanel} aria-label="Close the character panel">
|
||
✕
|
||
</button>
|
||
</div>
|
||
<PanelBody key={shown} characterId={shown} />
|
||
</div>
|
||
</>
|
||
)}
|
||
</>
|
||
);
|
||
}
|