V1.11.0: gear that applies itself, equipping that matters, a character panel
- Weapons and items can carry effects: bonuses and penalties to AC, speed, initiative, max HP, passive Perception, ability scores, saving throws and skills, plus a note. They apply on their own while equipped and stop when the thing comes off. Armor is an item with an AC effect. The DM sets them with "Effects" on a row in the weapon or item pool. - The sheet's "armor bonus" is now "Extra AC", the DM's own adjustment on top of gear, and only the DM can set it. - Only what is equipped counts: a weapon has to be equipped to be used in an encounter. A character can have two things equipped at once, one of them a weapon (campaign settings). Equipping at the limit is refused with a message, and can be changed at any time, mid-encounter included. - A built-in item library: armor, shields, worn gear and supplies, with their effects set. The weapon library gains paired weapons (a pair of daggers is one weapon). - A floating character button on every page opens a panel with a character's numbers and everything they hold, with equip ticks. Players get their own characters, the DM everyone's, and each character card in an encounter has an Inventory button. It replaces the quick stats drawer. - A player's own characters stand out: marked and highlighted in encounters, with a button that jumps to their card; highlighted on the party screen; and listed once, not twice, on the campaign page. - All packages at version 1.11.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
import { Link, useMatch } from 'react-router-dom';
|
||||
import { useCampaign, useCampaignCharacters, usePatchCharacterWeapon, usePatchInventoryItem, useQuickStats } from '../api/queries';
|
||||
import { useAuth } from '../auth/AuthContext';
|
||||
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>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user