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(null); const [amounts, setAmounts] = useState>({}); const [sides, setSides] = useState>({}); 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) => (
{wareName(ware)} Β· {stockLabel(ware)}
{ware.item?.description &&
{ware.item.description}
}
); return (
🧭 Places to go {!open && {encounter.status === 'PLANNING' ? 'Not begun yet' : 'Over'}}
{roamers.length > 0 ? (
πŸ“ {whereabouts ? whereabouts.name : 'not at any of these places yet'} {purse && purse.gold !== null && {formatPurse(purse)}}
) : (

{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.'}

)} {done &&

βœ“ {done}

} {places.length === 0 && (

{isDM ? 'No places yet. Tick the locations the party can go to, below.' : 'The DM has not opened any places yet.'}

)} {isDM && (
Places in this scene ({places.length} of {locations.length} locations) {locations.length === 0 ? (

This campaign has no locations yet. Add some under Locations on the campaign page.

) : (
{locations.map((loc) => { const included = placeIds.includes(loc.id); return ( ); })}
)}
)}
{places.map((place) => { const atPlace = Boolean(roamer) && place.here.some((h) => h.characterId === roamer?.id); return (
πŸ“ {place.name} {atPlace ? ( {roamer?.name} is here ) : ( )}
{place.description &&

{place.description}

}
{place.here.length > 0 ? `Here now: ${place.here.map((h) => h.name).join(', ')}` : 'Nobody from the party is here.'}
{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 (
πŸ§‘ {npc.name} {npc.role ? `, ${npc.role}` : ''}
{npc.description &&

{npc.description}

} {npc.dmNotes && (

πŸ”’ {npc.dmNotes}

)} {(npc.wares ?? []).map((ware) => wareRow(ware, atPlace))} {changes && (
πŸ’± Changes {formatMoney(rate.GOLD, 'GOLD', true)} for {formatMoney(rate.CASH, 'CASH')}, either way.
setAmounts((a) => ({ ...a, [npc.id]: e.target.value }))} style={{ width: 90 }} />
)} {(npc.wares ?? []).length === 0 && !changes &&
Nothing to sell: someone to talk to.
}
); })} {place.wares.length > 0 && (
πŸ›’ For sale here
{place.wares.map((ware) => wareRow(ware, atPlace))}
)} {place.npcs.length === 0 && place.wares.length === 0 && !isDM &&
Nothing to buy here.
} {isDM && (
Set up this place’s own shop

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).

)}
); })}
); }