- Free roam: a third kind of encounter. The DM ticks the nearby locations, the whole party is in it from the start, and each player sends their own character to a place - A place shows who is found there and what is for sale; a character shops (a place's own shop, or an NPC's wares and exchange) only where they stand - Locations can have a shop of their own; an NPC can be "found at" a location - Completed encounters are hidden from the campaign's list until "Show completed" is ticked; moving an encounter works on the list as shown Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
267 lines
13 KiB
TypeScript
267 lines
13 KiB
TypeScript
import { useState } from 'react';
|
||
import { Link } from 'react-router-dom';
|
||
import { useBuyWare, useExchangeWithNpc, useSetPlaces, useVisitPlace } from '../../api/queries';
|
||
import { NumberInput } from '../NumberInput';
|
||
import { WaresEditor } from '../Wares';
|
||
import { stockLabel, wareName } from '../../lib/wares';
|
||
import { formatMoney, formatPurse } from '../../lib/format';
|
||
import type { CharacterSummary, Encounter, FreeRoamPlace, Item, Location, SaleOffer, Weapon } from '../../api/types';
|
||
|
||
type ExchangeSide = 'GOLD' | 'CASH';
|
||
|
||
// A free-roam scene: the places the DM has laid out, each with who is found
|
||
// there and what is for sale, and who of the party is there now. A player
|
||
// sends their own character to a place and shops where they stand; the DM
|
||
// can send anyone, picks which of the campaign's locations are part of the
|
||
// scene, and stocks each place's own shop here. (What an NPC sells, and where
|
||
// they are found, is set on the NPC, on the campaign page.)
|
||
export function FreeRoamPanel({
|
||
encounter,
|
||
isDM,
|
||
userId,
|
||
characters,
|
||
locations,
|
||
weapons,
|
||
items,
|
||
onError,
|
||
}: {
|
||
encounter: Encounter;
|
||
isDM: boolean;
|
||
userId: string | undefined;
|
||
characters: CharacterSummary[];
|
||
locations: Location[]; // the campaign's, for the DM to pick places from
|
||
weapons: Weapon[];
|
||
items: Item[];
|
||
onError: (err: unknown) => void;
|
||
}) {
|
||
const setPlaces = useSetPlaces(encounter.id);
|
||
const visit = useVisitPlace(encounter.id);
|
||
const buy = useBuyWare(encounter.id);
|
||
const exchange = useExchangeWithNpc(encounter.id);
|
||
const [roamerId, setRoamerId] = useState('');
|
||
const [done, setDone] = useState<string | null>(null);
|
||
const [amounts, setAmounts] = useState<Record<string, string>>({});
|
||
const [sides, setSides] = useState<Record<string, ExchangeSide>>({});
|
||
|
||
const places = encounter.places ?? [];
|
||
const open = encounter.status === 'ACTIVE';
|
||
// Who can be moved and can shop: a player's own characters in the scene; for the DM, anyone's.
|
||
const roamers = (encounter.participants ?? []).flatMap((p) => (p.character && !p.hidden && (isDM || p.character.userId === userId) ? [p.character] : []));
|
||
const roamer = roamers.find((c) => c.id === roamerId) ?? roamers[0];
|
||
const purse = characters.find((c) => c.id === roamer?.id);
|
||
const whereabouts = places.find((place) => place.here.some((h) => h.characterId === roamer?.id));
|
||
const busy = visit.isPending || buy.isPending || exchange.isPending;
|
||
const placeIds = places.map((p) => p.id);
|
||
|
||
function go(place: FreeRoamPlace) {
|
||
if (!roamer) return;
|
||
setDone(null);
|
||
visit.mutate({ characterId: roamer.id, locationId: place.id }, { onError, onSuccess: () => setDone(`${roamer.name} goes to ${place.name}.`) });
|
||
}
|
||
|
||
function handleBuy(ware: SaleOffer) {
|
||
if (!roamer) return;
|
||
setDone(null);
|
||
buy.mutate(
|
||
{ wareId: ware.id, characterId: roamer.id },
|
||
{ onError, onSuccess: () => setDone(`${roamer.name} bought ${(ware.weapon ?? ware.item)?.name} for ${formatMoney(ware.price, ware.currency, true)}.`) }
|
||
);
|
||
}
|
||
|
||
const wareRow = (ware: SaleOffer, atPlace: boolean) => (
|
||
<div key={ware.id} className="reward-row">
|
||
<div className="flex-between">
|
||
<span>
|
||
<strong>{wareName(ware)}</strong>
|
||
<span className="combatant-sub"> · {stockLabel(ware)}</span>
|
||
</span>
|
||
<button
|
||
className="btn"
|
||
disabled={!open || !roamer || !atPlace || ware.stock === 0 || busy}
|
||
title={!open ? 'The scene has not begun' : !atPlace ? `${roamer?.name ?? 'Your character'} has to go here first` : undefined}
|
||
onClick={() => handleBuy(ware)}
|
||
>
|
||
Buy · {formatMoney(ware.price, ware.currency)}
|
||
</button>
|
||
</div>
|
||
{ware.item?.description && <div className="combatant-sub">{ware.item.description}</div>}
|
||
</div>
|
||
);
|
||
|
||
return (
|
||
<div>
|
||
<div className="card">
|
||
<div className="card-title">
|
||
<span>🧭 Places to go</span>
|
||
{!open && <span className="badge">{encounter.status === 'PLANNING' ? 'Not begun yet' : 'Over'}</span>}
|
||
</div>
|
||
{roamers.length > 0 ? (
|
||
<div className="flex-row flex-wrap gap-sm">
|
||
<label className="flex-row gap-sm" style={{ flex: 1, minWidth: 180 }}>
|
||
{roamers.length > 1 ? 'Moving' : 'You are'}
|
||
<select aria-label="Which character is moving and shopping" value={roamer?.id ?? ''} onChange={(e) => setRoamerId(e.target.value)} style={{ flex: 1 }}>
|
||
{roamers.map((c) => (
|
||
<option key={c.id} value={c.id}>
|
||
{c.name}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
<span className="combatant-sub">📍 {whereabouts ? whereabouts.name : 'not at any of these places yet'}</span>
|
||
{purse && purse.gold !== null && <span className="mono">{formatPurse(purse)}</span>}
|
||
</div>
|
||
) : (
|
||
<p className="combatant-sub" style={{ margin: 0 }}>
|
||
{isDM ? 'No characters are in this scene yet: add them from "Add to the scene".' : 'Your character has to be in the scene to go anywhere.'}
|
||
</p>
|
||
)}
|
||
{done && <p className="combatant-sub" style={{ marginBottom: 0 }}>✓ {done}</p>}
|
||
{places.length === 0 && (
|
||
<p className="combatant-sub" style={{ marginBottom: 0 }}>
|
||
{isDM ? 'No places yet. Tick the locations the party can go to, below.' : 'The DM has not opened any places yet.'}
|
||
</p>
|
||
)}
|
||
|
||
{isDM && (
|
||
<details className="mt-sm" open={places.length === 0}>
|
||
<summary className="section-label" style={{ cursor: 'pointer' }}>
|
||
Places in this scene ({places.length} of {locations.length} locations)
|
||
</summary>
|
||
{locations.length === 0 ? (
|
||
<p className="combatant-sub">
|
||
This campaign has no locations yet. Add some under Locations on the <Link to={`/campaigns/${encounter.campaignId}`}>campaign page</Link>.
|
||
</p>
|
||
) : (
|
||
<div className="flex-row flex-wrap gap-sm mt-sm">
|
||
{locations.map((loc) => {
|
||
const included = placeIds.includes(loc.id);
|
||
return (
|
||
<label key={loc.id} className="checkbox-row" title={loc.description ?? undefined}>
|
||
<input
|
||
type="checkbox"
|
||
checked={included}
|
||
disabled={setPlaces.isPending}
|
||
onChange={() => setPlaces.mutate(included ? placeIds.filter((id) => id !== loc.id) : [...placeIds, loc.id], { onError })}
|
||
/>
|
||
📍 {loc.name}
|
||
</label>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</details>
|
||
)}
|
||
</div>
|
||
|
||
<div className="place-grid">
|
||
{places.map((place) => {
|
||
const atPlace = Boolean(roamer) && place.here.some((h) => h.characterId === roamer?.id);
|
||
return (
|
||
<div key={place.id} className={`card place-card${atPlace ? ' is-here' : ''}`}>
|
||
<div className="card-title">
|
||
<span>📍 {place.name}</span>
|
||
{atPlace ? (
|
||
<span className="badge badge-live">{roamer?.name} is here</span>
|
||
) : (
|
||
<button className="btn btn-primary" disabled={!open || !roamer || busy} onClick={() => go(place)}>
|
||
Go here
|
||
</button>
|
||
)}
|
||
</div>
|
||
{place.description && <p style={{ margin: '0 0 0.4rem', whiteSpace: 'pre-wrap' }}>{place.description}</p>}
|
||
<div className="combatant-sub">{place.here.length > 0 ? `Here now: ${place.here.map((h) => h.name).join(', ')}` : 'Nobody from the party is here.'}</div>
|
||
|
||
{place.npcs.map((npc) => {
|
||
const rate = { GOLD: npc.exchangeGold ?? 0, CASH: npc.exchangeCash ?? 0 };
|
||
const changes = rate.GOLD > 0 && rate.CASH > 0;
|
||
const from = sides[npc.id] ?? 'GOLD';
|
||
const to = from === 'GOLD' ? 'CASH' : 'GOLD';
|
||
const amount = Math.floor(Number(amounts[npc.id]));
|
||
const whole = changes && amount > 0 && amount % rate[from] === 0;
|
||
return (
|
||
<div key={npc.id} className="mt-sm">
|
||
<div className="section-label">
|
||
🧑 {npc.name}
|
||
{npc.role ? `, ${npc.role}` : ''}
|
||
</div>
|
||
{npc.description && <p className="combatant-sub" style={{ margin: 0, whiteSpace: 'pre-wrap' }}>{npc.description}</p>}
|
||
{npc.dmNotes && (
|
||
<p className="combatant-sub" style={{ margin: 0 }} title="Only you can see this">
|
||
🔒 {npc.dmNotes}
|
||
</p>
|
||
)}
|
||
{(npc.wares ?? []).map((ware) => wareRow(ware, atPlace))}
|
||
{changes && (
|
||
<div className="reward-row">
|
||
<span className="combatant-sub">
|
||
💱 Changes {formatMoney(rate.GOLD, 'GOLD', true)} for {formatMoney(rate.CASH, 'CASH')}, either way.
|
||
</span>
|
||
<div className="flex-row flex-wrap gap-sm mt-sm">
|
||
<NumberInput
|
||
min={1}
|
||
max={1000000}
|
||
placeholder="Amount"
|
||
aria-label={`How much to change with ${npc.name}`}
|
||
value={amounts[npc.id] ?? ''}
|
||
onChange={(e) => setAmounts((a) => ({ ...a, [npc.id]: e.target.value }))}
|
||
style={{ width: 90 }}
|
||
/>
|
||
<select aria-label={`What to hand ${npc.name}`} value={from} onChange={(e) => setSides((s) => ({ ...s, [npc.id]: e.target.value as ExchangeSide }))}>
|
||
<option value="GOLD">gold into cash</option>
|
||
<option value="CASH">cash into gold</option>
|
||
</select>
|
||
<button
|
||
className="btn"
|
||
disabled={!open || !roamer || !atPlace || !whole || busy}
|
||
onClick={() => {
|
||
if (!roamer) return;
|
||
setDone(null);
|
||
exchange.mutate(
|
||
{ characterId: roamer.id, npcId: npc.id, from, amount },
|
||
{
|
||
onError,
|
||
onSuccess: ({ received, currency }) => {
|
||
setDone(`${roamer.name} changed ${formatMoney(amount, from, true)} for ${formatMoney(received, currency, true)} with ${npc.name}.`);
|
||
setAmounts((a) => ({ ...a, [npc.id]: '' }));
|
||
},
|
||
}
|
||
);
|
||
}}
|
||
>
|
||
{whole ? `Get ${formatMoney((amount / rate[from]) * rate[to], to, true)}` : 'Exchange'}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
)}
|
||
{(npc.wares ?? []).length === 0 && !changes && <div className="combatant-sub">Nothing to sell: someone to talk to.</div>}
|
||
</div>
|
||
);
|
||
})}
|
||
|
||
{place.wares.length > 0 && (
|
||
<div className="mt-sm">
|
||
<div className="section-label">🛒 For sale here</div>
|
||
{place.wares.map((ware) => wareRow(ware, atPlace))}
|
||
</div>
|
||
)}
|
||
{place.npcs.length === 0 && place.wares.length === 0 && !isDM && <div className="combatant-sub mt-sm">Nothing to buy here.</div>}
|
||
|
||
{isDM && (
|
||
<details className="mt-sm">
|
||
<summary className="section-label" style={{ cursor: 'pointer' }}>
|
||
Set up this place’s own shop
|
||
</summary>
|
||
<p className="combatant-sub" style={{ marginTop: '0.3rem' }}>
|
||
Sold here whenever this place is part of a free-roam scene. To have an NPC found here, set their location on the NPC (campaign page).
|
||
</p>
|
||
<WaresEditor campaignId={encounter.campaignId} seller={{ locationId: place.id }} wares={place.wares} weapons={weapons} items={items} onError={onError} />
|
||
</details>
|
||
)}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|