- An NPC can change gold and cash at a rate of their own ("2 gold = $15"),
the same both ways. Only gold and cash are taken: no silver, no copper
and no change, and the amount has to be a whole multiple of the rate.
- An NPC can sell weapons and items from the campaign pools, each with a
price in any coin or in cash, a level and an optional stock. The wares
follow the NPC into every quest. An encounter can sell things itself
too, for a stall with nobody behind it.
- Trading is open only in an NPC quest in progress, never in a battle,
with an NPC who is in the scene and standing, for a character who is in
it. Players see every rate and ware and trade for their own character
in a new Trade card; the DM can trade for anyone.
- Only the DM sets trade up: "Trade" on an NPC's row on the campaign page,
and the DM's part of the Trade card for an encounter's own wares. A
ware's price and stock can be edited afterwards.
- Every exchange and purchase is written to the character's money log.
- All packages at version 1.10.0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
7.4 KiB
TypeScript
205 lines
7.4 KiB
TypeScript
import { useState } from 'react';
|
|
import { useCreateWare, useDeleteWare, usePatchWare } from '../api/queries';
|
|
import { NumberInput } from './NumberInput';
|
|
import { COIN_LABELS, CURRENCIES, formatMoney } from '../lib/format';
|
|
import { scaledDamageDice, MAX_GEAR_LEVEL } from '../lib/previewCalc';
|
|
import { stockLabel, wareName } from '../lib/wares';
|
|
import type { Currency, Item, SaleOffer, Weapon } from '../api/types';
|
|
|
|
// One ware in the DM's list. "Edit" swaps the line for its price and stock.
|
|
function WareRow({ campaignId, ware, onError }: { campaignId: string; ware: SaleOffer; onError: (err: unknown) => void }) {
|
|
const patchWare = usePatchWare(campaignId);
|
|
const deleteWare = useDeleteWare(campaignId);
|
|
const [editing, setEditing] = useState(false);
|
|
const [price, setPrice] = useState('');
|
|
const [currency, setCurrency] = useState<Currency>(ware.currency);
|
|
const [stock, setStock] = useState('');
|
|
|
|
function startEditing() {
|
|
setPrice(String(ware.price));
|
|
setCurrency(ware.currency);
|
|
setStock(ware.stock === null ? '' : String(ware.stock));
|
|
setEditing(true);
|
|
}
|
|
|
|
function handleSave() {
|
|
const amount = Math.floor(Number(price));
|
|
if (!(amount > 0)) return;
|
|
patchWare.mutate({ id: ware.id, price: amount, currency, stock: stock === '' ? null : Number(stock) }, { onError, onSuccess: () => setEditing(false) });
|
|
}
|
|
|
|
return (
|
|
<div style={{ padding: '0.3rem 0', borderBottom: '1px solid var(--border-soft)' }}>
|
|
<div className="flex-between">
|
|
<span>
|
|
{wareName(ware)} — <strong className="mono">{formatMoney(ware.price, ware.currency)}</strong>
|
|
<span className="combatant-sub"> · {stockLabel(ware)}</span>
|
|
</span>
|
|
<span className="flex-row">
|
|
<button className="btn btn-ghost" aria-label={`Edit the price and stock of ${wareName(ware)}`} onClick={() => (editing ? setEditing(false) : startEditing())}>
|
|
{editing ? 'Cancel' : 'Edit'}
|
|
</button>
|
|
<button className="btn btn-ghost" aria-label={`Stop selling ${wareName(ware)}`} onClick={() => deleteWare.mutate(ware.id, { onError })}>
|
|
✕
|
|
</button>
|
|
</span>
|
|
</div>
|
|
{editing && (
|
|
<div className="flex-row flex-wrap gap-sm mt-sm">
|
|
<NumberInput min={1} max={1000000} placeholder="Price" aria-label="Price for one" value={price} onChange={(e) => setPrice(e.target.value)} style={{ width: 90 }} />
|
|
<select aria-label="Price is in" value={currency} onChange={(e) => setCurrency(e.target.value as Currency)}>
|
|
{CURRENCIES.map((c) => (
|
|
<option key={c} value={c}>
|
|
{COIN_LABELS[c]}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<NumberInput
|
|
min={0}
|
|
max={9999}
|
|
placeholder="Stock (empty = no limit)"
|
|
aria-label="How many are in stock"
|
|
title="How many can be bought. Leave empty for no limit."
|
|
value={stock}
|
|
onChange={(e) => setStock(e.target.value)}
|
|
style={{ flex: 1, minWidth: 120 }}
|
|
/>
|
|
<button className="btn btn-primary" onClick={handleSave} disabled={!price || patchWare.isPending}>
|
|
Save
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// The DM's list of what one seller has for sale — an NPC, or an encounter
|
|
// itself — with a form to put something from the campaign pools up for sale.
|
|
export function WaresEditor({
|
|
campaignId,
|
|
seller,
|
|
wares,
|
|
weapons,
|
|
items,
|
|
onError,
|
|
}: {
|
|
campaignId: string;
|
|
seller: { npcId: string } | { encounterId: string };
|
|
wares: SaleOffer[];
|
|
weapons: Weapon[];
|
|
items: Item[];
|
|
onError: (err: unknown) => void;
|
|
}) {
|
|
const createWare = useCreateWare(campaignId);
|
|
|
|
const [kind, setKind] = useState<'item' | 'weapon'>('item');
|
|
const [poolId, setPoolId] = useState('');
|
|
const [level, setLevel] = useState('');
|
|
const [price, setPrice] = useState('');
|
|
const [currency, setCurrency] = useState<Currency>('GOLD');
|
|
const [stock, setStock] = useState('');
|
|
|
|
const pool: (Weapon | Item)[] = kind === 'weapon' ? weapons : items;
|
|
const picked = pool.find((entry) => entry.id === poolId);
|
|
|
|
function handleAdd() {
|
|
const amount = Math.floor(Number(price));
|
|
if (!picked || !(amount > 0)) return;
|
|
createWare.mutate(
|
|
{
|
|
...seller,
|
|
...(kind === 'weapon' ? { weaponId: picked.id } : { itemId: picked.id }),
|
|
price: amount,
|
|
currency,
|
|
level: level ? Number(level) : undefined,
|
|
stock: stock === '' ? null : Number(stock),
|
|
},
|
|
{
|
|
onError,
|
|
onSuccess: () => {
|
|
setPoolId('');
|
|
setLevel('');
|
|
setPrice('');
|
|
setStock('');
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
{wares.length === 0 && (
|
|
<p className="combatant-sub" style={{ marginTop: 0 }}>
|
|
Nothing for sale yet.
|
|
</p>
|
|
)}
|
|
{wares.map((ware) => (
|
|
<WareRow key={ware.id} campaignId={campaignId} ware={ware} onError={onError} />
|
|
))}
|
|
<div className="flex-row flex-wrap gap-sm mt-sm">
|
|
<select
|
|
aria-label="What kind of thing to sell"
|
|
value={kind}
|
|
onChange={(e) => {
|
|
setKind(e.target.value as typeof kind);
|
|
setPoolId('');
|
|
setLevel('');
|
|
}}
|
|
>
|
|
<option value="item">Item</option>
|
|
<option value="weapon">Weapon</option>
|
|
</select>
|
|
<select
|
|
aria-label={kind === 'weapon' ? 'Weapon to sell' : 'Item to sell'}
|
|
value={poolId}
|
|
onChange={(e) => {
|
|
setPoolId(e.target.value);
|
|
setLevel('');
|
|
}}
|
|
style={{ flex: 1, minWidth: 110 }}
|
|
>
|
|
<option value="">{pool.length ? 'Choose…' : 'The pool is empty'}</option>
|
|
{pool.map((entry) => (
|
|
<option key={entry.id} value={entry.id}>
|
|
{entry.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
{picked && (
|
|
<select aria-label="Level" value={level || picked.minLevel} onChange={(e) => setLevel(e.target.value)}>
|
|
{Array.from({ length: MAX_GEAR_LEVEL - picked.minLevel + 1 }, (_, i) => picked.minLevel + i).map((l) => (
|
|
<option key={l} value={l}>
|
|
Lv {l}
|
|
{picked.damageDice ? ` · ${scaledDamageDice(picked.damageDice, l, picked.damageScaling, picked.minLevel)}` : ''}
|
|
</option>
|
|
))}
|
|
</select>
|
|
)}
|
|
</div>
|
|
<div className="flex-row flex-wrap gap-sm mt-sm">
|
|
<NumberInput min={1} max={1000000} placeholder="Price" aria-label="Price for one" value={price} onChange={(e) => setPrice(e.target.value)} style={{ width: 90 }} />
|
|
<select aria-label="Price is in" value={currency} onChange={(e) => setCurrency(e.target.value as Currency)}>
|
|
{CURRENCIES.map((c) => (
|
|
<option key={c} value={c}>
|
|
{COIN_LABELS[c]}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<NumberInput
|
|
min={0}
|
|
max={9999}
|
|
placeholder="Stock (empty = no limit)"
|
|
aria-label="How many are in stock"
|
|
title="How many can be bought. Leave empty for no limit."
|
|
value={stock}
|
|
onChange={(e) => setStock(e.target.value)}
|
|
style={{ flex: 1, minWidth: 120 }}
|
|
/>
|
|
<button className="btn btn-primary" onClick={handleAdd} disabled={!picked || !price || createWare.isPending}>
|
|
Put up for sale
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|