Compare commits
6 Commits
cbb3efafd7
...
feature/to
| Author | SHA1 | Date | |
|---|---|---|---|
| 1613acd39b | |||
| 2c58c3f1a5 | |||
| 967766956f | |||
| 9eef50e0c9 | |||
|
|
8597b4fded | ||
|
|
71bdc6d031 |
272
src/App.tsx
272
src/App.tsx
@@ -12,6 +12,7 @@ import {
|
|||||||
startCombatInCurrentRoom,
|
startCombatInCurrentRoom,
|
||||||
travelCurrentExit,
|
travelCurrentExit,
|
||||||
} from "@/rules/runState";
|
} from "@/rules/runState";
|
||||||
|
import { queueTreasureForSale, sellPendingTreasure, sendTreasureToStash } from "@/rules/town";
|
||||||
import type { RunState } from "@/types/state";
|
import type { RunState } from "@/types/state";
|
||||||
|
|
||||||
function createDemoRun() {
|
function createDemoRun() {
|
||||||
@@ -48,12 +49,74 @@ function getRoomTitle(run: RunState, roomId?: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDefinitionName(definitionId: string) {
|
||||||
|
const item =
|
||||||
|
sampleContentPack.items.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.weapons.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.armour.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.scrolls.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.potions.find((candidate) => candidate.id === definitionId);
|
||||||
|
|
||||||
|
return item?.name ?? definitionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatInventoryEntry(definitionId: string, quantity: number) {
|
||||||
|
const name = getDefinitionName(definitionId);
|
||||||
|
return quantity > 1 ? `${quantity}x ${name}` : name;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTreasureItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.itemType === "treasure")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSupportItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.itemType !== "treasure")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getConsumableItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.consumable || item.itemType === "ration")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const treasureItemIds = getTreasureItemIds();
|
||||||
|
const supportItemIds = getSupportItemIds();
|
||||||
|
const consumableItemIds = getConsumableItemIds();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [run, setRun] = React.useState<RunState>(() => createDemoRun());
|
const [run, setRun] = React.useState<RunState>(() => createDemoRun());
|
||||||
const currentLevel = run.dungeon.levels[run.currentLevel];
|
const currentLevel = run.dungeon.levels[run.currentLevel];
|
||||||
const currentRoom = run.currentRoomId ? currentLevel?.rooms[run.currentRoomId] : undefined;
|
const currentRoom = run.currentRoomId ? currentLevel?.rooms[run.currentRoomId] : undefined;
|
||||||
const availableMoves = getAvailableMoves(run);
|
const availableMoves = getAvailableMoves(run);
|
||||||
const combatReadyEncounter = isCurrentRoomCombatReady(run);
|
const combatReadyEncounter = isCurrentRoomCombatReady(run);
|
||||||
|
const carriedTreasure = run.adventurerSnapshot.inventory.carried.filter((entry) =>
|
||||||
|
treasureItemIds.has(entry.definitionId),
|
||||||
|
);
|
||||||
|
const carriedConsumables = run.adventurerSnapshot.inventory.carried.filter(
|
||||||
|
(entry) =>
|
||||||
|
consumableItemIds.has(entry.definitionId) ||
|
||||||
|
entry.definitionId.startsWith("potion.") ||
|
||||||
|
entry.definitionId.startsWith("scroll."),
|
||||||
|
);
|
||||||
|
const carriedGear = run.adventurerSnapshot.inventory.carried.filter(
|
||||||
|
(entry) =>
|
||||||
|
supportItemIds.has(entry.definitionId) &&
|
||||||
|
!consumableItemIds.has(entry.definitionId),
|
||||||
|
);
|
||||||
|
const equippedItems = run.adventurerSnapshot.inventory.equipped;
|
||||||
|
const latestLoot = run.lootedItems.slice(-4).reverse();
|
||||||
|
const pendingSales = run.townState.pendingSales;
|
||||||
|
const stash = run.townState.stash;
|
||||||
|
|
||||||
const handleReset = () => {
|
const handleReset = () => {
|
||||||
setRun(createDemoRun());
|
setRun(createDemoRun());
|
||||||
@@ -96,6 +159,18 @@ function App() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleQueueSale = (definitionId: string) => {
|
||||||
|
setRun((previous) => queueTreasureForSale(sampleContentPack, previous, definitionId).run);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleStashTreasure = (definitionId: string) => {
|
||||||
|
setRun((previous) => sendTreasureToStash(sampleContentPack, previous, definitionId).run);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSellPending = () => {
|
||||||
|
setRun((previous) => sellPendingTreasure(sampleContentPack, previous).run);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="app-shell">
|
<main className="app-shell">
|
||||||
<section className="hero">
|
<section className="hero">
|
||||||
@@ -176,23 +251,188 @@ function App() {
|
|||||||
Run rewards: {run.xpGained} XP, {run.goldGained} gold,{" "}
|
Run rewards: {run.xpGained} XP, {run.goldGained} gold,{" "}
|
||||||
{run.defeatedCreatureIds.length} foes defeated.
|
{run.defeatedCreatureIds.length} foes defeated.
|
||||||
</p>
|
</p>
|
||||||
<p className="supporting-text">
|
</article>
|
||||||
Carried gold: {run.adventurerSnapshot.inventory.currency.gold}. Looted items:{" "}
|
|
||||||
{run.lootedItems.length === 0
|
|
||||||
? "none yet"
|
|
||||||
: run.lootedItems
|
|
||||||
.map((entry) => {
|
|
||||||
const item = sampleContentPack.items.find(
|
|
||||||
(candidate) => candidate.id === entry.definitionId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return entry.quantity > 1
|
<article className="panel panel-inventory">
|
||||||
? `${entry.quantity}x ${item?.name ?? entry.definitionId}`
|
<div className="panel-header">
|
||||||
: item?.name ?? entry.definitionId;
|
<h2>Inventory</h2>
|
||||||
})
|
<span>{run.adventurerSnapshot.inventory.carried.length} carried entries</span>
|
||||||
.join(", ")}
|
</div>
|
||||||
.
|
<div className="inventory-summary">
|
||||||
</p>
|
<div className="inventory-badge">
|
||||||
|
<span>Gold</span>
|
||||||
|
<strong>{run.adventurerSnapshot.inventory.currency.gold}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Rations</span>
|
||||||
|
<strong>{run.adventurerSnapshot.inventory.rationCount}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Treasure</span>
|
||||||
|
<strong>{carriedTreasure.length}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Latest Loot</span>
|
||||||
|
<strong>{run.lootedItems.reduce((total, entry) => total + entry.quantity, 0)}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-grid">
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Equipped</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{equippedItems.map((entry) => (
|
||||||
|
<article key={`equipped-${entry.definitionId}`} className="inventory-card inventory-card-equipped">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Ready for use</span>
|
||||||
|
</article>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Consumables</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedConsumables.length === 0 ? (
|
||||||
|
<p className="supporting-text">No consumables carried.</p>
|
||||||
|
) : (
|
||||||
|
carriedConsumables.map((entry) => (
|
||||||
|
<article key={`consumable-${entry.definitionId}`} className="inventory-card">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Combat or run utility</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Pack Gear</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedGear.length === 0 ? (
|
||||||
|
<p className="supporting-text">No general gear carried.</p>
|
||||||
|
) : (
|
||||||
|
carriedGear.map((entry) => (
|
||||||
|
<article key={`gear-${entry.definitionId}`} className="inventory-card">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Travel and exploration kit</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Treasure Stash</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedTreasure.length === 0 ? (
|
||||||
|
<p className="supporting-text">No treasure recovered yet.</p>
|
||||||
|
) : (
|
||||||
|
carriedTreasure.map((entry) => (
|
||||||
|
<article key={`treasure-${entry.definitionId}`} className="inventory-card inventory-card-treasure">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Sellable dungeon spoils</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
<div className="loot-ribbon">
|
||||||
|
<span className="inventory-label">Recent Spoils</span>
|
||||||
|
{latestLoot.length === 0 ? (
|
||||||
|
<p className="supporting-text">Win a fight to populate the loot ribbon.</p>
|
||||||
|
) : (
|
||||||
|
<div className="loot-ribbon-list">
|
||||||
|
{latestLoot.map((entry) => (
|
||||||
|
<article key={`recent-${entry.definitionId}`} className="loot-pill">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
</article>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article className="panel panel-town">
|
||||||
|
<div className="panel-header">
|
||||||
|
<h2>Town Market</h2>
|
||||||
|
<span>{run.townState.visits} town actions</span>
|
||||||
|
</div>
|
||||||
|
<div className="town-summary">
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Known Services</span>
|
||||||
|
<strong>{run.townState.knownServices.length}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Queued Sales</span>
|
||||||
|
<strong>{pendingSales.length}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Stash</span>
|
||||||
|
<strong>{stash.length}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="town-actions">
|
||||||
|
<button
|
||||||
|
className="button button-primary"
|
||||||
|
onClick={handleSellPending}
|
||||||
|
disabled={pendingSales.length === 0}
|
||||||
|
>
|
||||||
|
Sell Queued Treasure
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<section className="town-section">
|
||||||
|
<span className="inventory-label">Treasure In Pack</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedTreasure.length === 0 ? (
|
||||||
|
<p className="supporting-text">No treasure available for town actions.</p>
|
||||||
|
) : (
|
||||||
|
carriedTreasure.map((entry) => (
|
||||||
|
<article key={`town-pack-${entry.definitionId}`} className="town-card">
|
||||||
|
<div>
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Choose whether to sell or stash this treasure.</span>
|
||||||
|
</div>
|
||||||
|
<div className="enemy-actions">
|
||||||
|
<button className="button" onClick={() => handleStashTreasure(entry.definitionId)}>
|
||||||
|
Send To Stash
|
||||||
|
</button>
|
||||||
|
<button className="button button-primary" onClick={() => handleQueueSale(entry.definitionId)}>
|
||||||
|
Queue For Sale
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="town-section">
|
||||||
|
<span className="inventory-label">Pending Sales</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{pendingSales.length === 0 ? (
|
||||||
|
<p className="supporting-text">Nothing queued at the market.</p>
|
||||||
|
) : (
|
||||||
|
pendingSales.map((entry) => (
|
||||||
|
<article key={`pending-${entry.definitionId}`} className="inventory-card inventory-card-treasure">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Ready to convert into gold</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="town-section">
|
||||||
|
<span className="inventory-label">Town Stash</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{stash.length === 0 ? (
|
||||||
|
<p className="supporting-text">The stash is empty.</p>
|
||||||
|
) : (
|
||||||
|
stash.map((entry) => (
|
||||||
|
<article key={`stash-${entry.definitionId}`} className="inventory-card">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Held safely in town storage</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<article className="panel">
|
<article className="panel">
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { findCreatureById } from "@/data/contentHelpers";
|
|||||||
|
|
||||||
import { startCombatFromRoom } from "./combat";
|
import { startCombatFromRoom } from "./combat";
|
||||||
import { resolveCombatLoot } from "./loot";
|
import { resolveCombatLoot } from "./loot";
|
||||||
|
import { createInitialTownState } from "./town";
|
||||||
import {
|
import {
|
||||||
resolveEnemyTurn,
|
resolveEnemyTurn,
|
||||||
resolvePlayerAttack,
|
resolvePlayerAttack,
|
||||||
@@ -164,6 +165,13 @@ function cloneRun(run: RunState): RunState {
|
|||||||
globalFlags: [...run.dungeon.globalFlags],
|
globalFlags: [...run.dungeon.globalFlags],
|
||||||
},
|
},
|
||||||
activeCombat: run.activeCombat ? cloneCombat(run.activeCombat) : undefined,
|
activeCombat: run.activeCombat ? cloneCombat(run.activeCombat) : undefined,
|
||||||
|
townState: {
|
||||||
|
...run.townState,
|
||||||
|
knownServices: [...run.townState.knownServices],
|
||||||
|
stash: run.townState.stash.map((entry) => ({ ...entry })),
|
||||||
|
pendingSales: run.townState.pendingSales.map((entry) => ({ ...entry })),
|
||||||
|
serviceFlags: [...run.townState.serviceFlags],
|
||||||
|
},
|
||||||
defeatedCreatureIds: [...run.defeatedCreatureIds],
|
defeatedCreatureIds: [...run.defeatedCreatureIds],
|
||||||
xpGained: run.xpGained,
|
xpGained: run.xpGained,
|
||||||
goldGained: run.goldGained,
|
goldGained: run.goldGained,
|
||||||
@@ -336,6 +344,7 @@ export function createRunState(options: CreateRunOptions): RunState {
|
|||||||
globalFlags: [],
|
globalFlags: [],
|
||||||
},
|
},
|
||||||
adventurerSnapshot: options.adventurer,
|
adventurerSnapshot: options.adventurer,
|
||||||
|
townState: createInitialTownState(),
|
||||||
defeatedCreatureIds: [],
|
defeatedCreatureIds: [],
|
||||||
xpGained: 0,
|
xpGained: 0,
|
||||||
goldGained: 0,
|
goldGained: 0,
|
||||||
|
|||||||
74
src/rules/town.test.ts
Normal file
74
src/rules/town.test.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import { createRunState } from "./runState";
|
||||||
|
import { queueTreasureForSale, sellPendingTreasure, sendTreasureToStash } from "./town";
|
||||||
|
|
||||||
|
function createAdventurer() {
|
||||||
|
return createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Aster",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("town flow", () => {
|
||||||
|
it("moves treasure from carried inventory into the stash", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
});
|
||||||
|
|
||||||
|
run.adventurerSnapshot.inventory.carried.push({
|
||||||
|
definitionId: "item.silver-clasp",
|
||||||
|
quantity: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = sendTreasureToStash(
|
||||||
|
sampleContentPack,
|
||||||
|
run,
|
||||||
|
"item.silver-clasp",
|
||||||
|
"2026-03-15T15:00:00.000Z",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.run.townState.stash).toEqual([
|
||||||
|
{ definitionId: "item.silver-clasp", quantity: 1 },
|
||||||
|
]);
|
||||||
|
expect(result.run.adventurerSnapshot.inventory.carried).not.toEqual(
|
||||||
|
expect.arrayContaining([expect.objectContaining({ definitionId: "item.silver-clasp" })]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("queues treasure and sells it for item value", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
});
|
||||||
|
|
||||||
|
run.adventurerSnapshot.inventory.carried.push({
|
||||||
|
definitionId: "item.keeper-keyring",
|
||||||
|
quantity: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
const queued = queueTreasureForSale(
|
||||||
|
sampleContentPack,
|
||||||
|
run,
|
||||||
|
"item.keeper-keyring",
|
||||||
|
"2026-03-15T15:05:00.000Z",
|
||||||
|
).run;
|
||||||
|
const sold = sellPendingTreasure(
|
||||||
|
sampleContentPack,
|
||||||
|
queued,
|
||||||
|
"2026-03-15T15:06:00.000Z",
|
||||||
|
).run;
|
||||||
|
|
||||||
|
expect(sold.townState.pendingSales).toEqual([]);
|
||||||
|
expect(sold.adventurerSnapshot.inventory.currency.gold).toBe(5);
|
||||||
|
expect(sold.log.at(-1)?.text).toContain("for 5 gold");
|
||||||
|
});
|
||||||
|
});
|
||||||
165
src/rules/town.ts
Normal file
165
src/rules/town.ts
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
import { findItemById } from "@/data/contentHelpers";
|
||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type { InventoryEntry, RunState, TownState } from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
export type TownActionResult = {
|
||||||
|
run: RunState;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
};
|
||||||
|
|
||||||
|
function cloneTownState(townState: TownState): TownState {
|
||||||
|
return {
|
||||||
|
...townState,
|
||||||
|
knownServices: [...townState.knownServices],
|
||||||
|
stash: townState.stash.map((entry) => ({ ...entry })),
|
||||||
|
pendingSales: townState.pendingSales.map((entry) => ({ ...entry })),
|
||||||
|
serviceFlags: [...townState.serviceFlags],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneRun(run: RunState): RunState {
|
||||||
|
return {
|
||||||
|
...run,
|
||||||
|
adventurerSnapshot: {
|
||||||
|
...run.adventurerSnapshot,
|
||||||
|
hp: { ...run.adventurerSnapshot.hp },
|
||||||
|
stats: { ...run.adventurerSnapshot.stats },
|
||||||
|
favour: { ...run.adventurerSnapshot.favour },
|
||||||
|
statuses: run.adventurerSnapshot.statuses.map((status) => ({ ...status })),
|
||||||
|
inventory: {
|
||||||
|
...run.adventurerSnapshot.inventory,
|
||||||
|
carried: run.adventurerSnapshot.inventory.carried.map((entry) => ({ ...entry })),
|
||||||
|
equipped: run.adventurerSnapshot.inventory.equipped.map((entry) => ({ ...entry })),
|
||||||
|
stored: run.adventurerSnapshot.inventory.stored.map((entry) => ({ ...entry })),
|
||||||
|
currency: { ...run.adventurerSnapshot.inventory.currency },
|
||||||
|
lightSources: run.adventurerSnapshot.inventory.lightSources.map((entry) => ({ ...entry })),
|
||||||
|
},
|
||||||
|
progressionFlags: [...run.adventurerSnapshot.progressionFlags],
|
||||||
|
manoeuvreIds: [...run.adventurerSnapshot.manoeuvreIds],
|
||||||
|
},
|
||||||
|
townState: cloneTownState(run.townState),
|
||||||
|
defeatedCreatureIds: [...run.defeatedCreatureIds],
|
||||||
|
lootedItems: run.lootedItems.map((entry) => ({ ...entry })),
|
||||||
|
log: run.log.map((entry) => ({ ...entry, relatedIds: entry.relatedIds ? [...entry.relatedIds] : undefined })),
|
||||||
|
pendingEffects: run.pendingEffects.map((effect) => ({ ...effect })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeEntry(entries: InventoryEntry[], entry: InventoryEntry) {
|
||||||
|
const existing = entries.find((candidate) => candidate.definitionId === entry.definitionId);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
existing.quantity += entry.quantity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
entries.push({ ...entry });
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractEntry(entries: InventoryEntry[], definitionId: string) {
|
||||||
|
const index = entries.findIndex((entry) => entry.definitionId === definitionId);
|
||||||
|
|
||||||
|
if (index === -1) {
|
||||||
|
throw new Error(`No inventory entry found for ${definitionId}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [removed] = entries.splice(index, 1);
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTownLog(id: string, at: string, text: string, relatedIds: string[]): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type: "town",
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createInitialTownState(): TownState {
|
||||||
|
return {
|
||||||
|
visits: 0,
|
||||||
|
knownServices: ["service.market"],
|
||||||
|
stash: [],
|
||||||
|
pendingSales: [],
|
||||||
|
serviceFlags: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendTreasureToStash(
|
||||||
|
content: ContentPack,
|
||||||
|
run: RunState,
|
||||||
|
definitionId: string,
|
||||||
|
at = new Date().toISOString(),
|
||||||
|
): TownActionResult {
|
||||||
|
const nextRun = cloneRun(run);
|
||||||
|
const removed = extractEntry(nextRun.adventurerSnapshot.inventory.carried, definitionId);
|
||||||
|
mergeEntry(nextRun.townState.stash, removed);
|
||||||
|
nextRun.townState.visits += 1;
|
||||||
|
const item = findItemById(content, definitionId);
|
||||||
|
const logEntry = createTownLog(
|
||||||
|
`town.stash.${definitionId}.${nextRun.log.length + 1}`,
|
||||||
|
at,
|
||||||
|
`Moved ${item.name} into the town stash.`,
|
||||||
|
[definitionId],
|
||||||
|
);
|
||||||
|
nextRun.log.push(logEntry);
|
||||||
|
|
||||||
|
return { run: nextRun, logEntries: [logEntry] };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function queueTreasureForSale(
|
||||||
|
content: ContentPack,
|
||||||
|
run: RunState,
|
||||||
|
definitionId: string,
|
||||||
|
at = new Date().toISOString(),
|
||||||
|
): TownActionResult {
|
||||||
|
const nextRun = cloneRun(run);
|
||||||
|
const removed = extractEntry(nextRun.adventurerSnapshot.inventory.carried, definitionId);
|
||||||
|
mergeEntry(nextRun.townState.pendingSales, removed);
|
||||||
|
nextRun.townState.visits += 1;
|
||||||
|
const item = findItemById(content, definitionId);
|
||||||
|
const logEntry = createTownLog(
|
||||||
|
`town.queue.${definitionId}.${nextRun.log.length + 1}`,
|
||||||
|
at,
|
||||||
|
`Queued ${item.name} for sale at the market.`,
|
||||||
|
[definitionId],
|
||||||
|
);
|
||||||
|
nextRun.log.push(logEntry);
|
||||||
|
|
||||||
|
return { run: nextRun, logEntries: [logEntry] };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sellPendingTreasure(
|
||||||
|
content: ContentPack,
|
||||||
|
run: RunState,
|
||||||
|
at = new Date().toISOString(),
|
||||||
|
): TownActionResult {
|
||||||
|
const nextRun = cloneRun(run);
|
||||||
|
const soldEntries = nextRun.townState.pendingSales;
|
||||||
|
const goldEarned = soldEntries.reduce((total, entry) => {
|
||||||
|
const item = findItemById(content, entry.definitionId);
|
||||||
|
return total + (item.valueGp ?? 0) * entry.quantity;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
nextRun.adventurerSnapshot.inventory.currency.gold += goldEarned;
|
||||||
|
nextRun.townState.pendingSales = [];
|
||||||
|
nextRun.townState.visits += 1;
|
||||||
|
|
||||||
|
const soldText =
|
||||||
|
soldEntries.length === 0
|
||||||
|
? "No treasure was queued for sale."
|
||||||
|
: `Sold ${soldEntries.reduce((total, entry) => total + entry.quantity, 0)} treasure item${soldEntries.reduce((total, entry) => total + entry.quantity, 0) === 1 ? "" : "s"} for ${goldEarned} gold.`;
|
||||||
|
|
||||||
|
const logEntry = createTownLog(
|
||||||
|
`town.sell.${nextRun.log.length + 1}`,
|
||||||
|
at,
|
||||||
|
soldText,
|
||||||
|
soldEntries.map((entry) => entry.definitionId),
|
||||||
|
);
|
||||||
|
nextRun.log.push(logEntry);
|
||||||
|
|
||||||
|
return { run: nextRun, logEntries: [logEntry] };
|
||||||
|
}
|
||||||
@@ -214,6 +214,7 @@ export const runStateSchema = z.object({
|
|||||||
dungeon: dungeonStateSchema,
|
dungeon: dungeonStateSchema,
|
||||||
adventurerSnapshot: adventurerStateSchema,
|
adventurerSnapshot: adventurerStateSchema,
|
||||||
activeCombat: combatStateSchema.optional(),
|
activeCombat: combatStateSchema.optional(),
|
||||||
|
townState: townStateSchema,
|
||||||
defeatedCreatureIds: z.array(z.string()),
|
defeatedCreatureIds: z.array(z.string()),
|
||||||
xpGained: z.number().int().nonnegative(),
|
xpGained: z.number().int().nonnegative(),
|
||||||
goldGained: z.number().int().nonnegative(),
|
goldGained: z.number().int().nonnegative(),
|
||||||
|
|||||||
131
src/styles.css
131
src/styles.css
@@ -152,6 +152,20 @@ select {
|
|||||||
rgba(25, 19, 16, 0.9);
|
rgba(25, 19, 16, 0.9);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.panel-inventory {
|
||||||
|
grid-column: span 8;
|
||||||
|
background:
|
||||||
|
linear-gradient(180deg, rgba(43, 32, 24, 0.92), rgba(22, 17, 14, 0.92)),
|
||||||
|
rgba(25, 19, 16, 0.9);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-town {
|
||||||
|
grid-column: span 4;
|
||||||
|
background:
|
||||||
|
linear-gradient(180deg, rgba(32, 37, 24, 0.92), rgba(20, 22, 14, 0.92)),
|
||||||
|
rgba(25, 19, 16, 0.9);
|
||||||
|
}
|
||||||
|
|
||||||
.panel-log {
|
.panel-log {
|
||||||
grid-column: span 12;
|
grid-column: span 12;
|
||||||
}
|
}
|
||||||
@@ -184,6 +198,7 @@ select {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-strip div,
|
.stat-strip div,
|
||||||
|
.inventory-badge,
|
||||||
.encounter-box,
|
.encounter-box,
|
||||||
.combat-status {
|
.combat-status {
|
||||||
padding: 0.9rem;
|
padding: 0.9rem;
|
||||||
@@ -192,6 +207,8 @@ select {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-strip span,
|
.stat-strip span,
|
||||||
|
.inventory-badge span,
|
||||||
|
.inventory-label,
|
||||||
.encounter-label,
|
.encounter-label,
|
||||||
.combat-status span,
|
.combat-status span,
|
||||||
.room-meta span,
|
.room-meta span,
|
||||||
@@ -204,6 +221,7 @@ select {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.stat-strip strong,
|
.stat-strip strong,
|
||||||
|
.inventory-badge strong,
|
||||||
.encounter-box strong,
|
.encounter-box strong,
|
||||||
.combat-status strong {
|
.combat-status strong {
|
||||||
display: block;
|
display: block;
|
||||||
@@ -217,6 +235,113 @@ select {
|
|||||||
color: rgba(244, 239, 227, 0.76);
|
color: rgba(244, 239, 227, 0.76);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inventory-summary,
|
||||||
|
.inventory-grid,
|
||||||
|
.town-summary {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-summary,
|
||||||
|
.town-summary {
|
||||||
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-grid {
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-section {
|
||||||
|
padding: 1rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background:
|
||||||
|
linear-gradient(180deg, rgba(255, 245, 223, 0.04), rgba(255, 245, 223, 0.02));
|
||||||
|
}
|
||||||
|
|
||||||
|
.town-section {
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.town-actions {
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.town-card {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
padding: 0.95rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background: rgba(255, 245, 223, 0.04);
|
||||||
|
}
|
||||||
|
|
||||||
|
.town-card strong {
|
||||||
|
display: block;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.town-card span {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
color: rgba(244, 239, 227, 0.62);
|
||||||
|
font-size: 0.84rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 0.7rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-list,
|
||||||
|
.loot-ribbon-list {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.65rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card,
|
||||||
|
.loot-pill {
|
||||||
|
padding: 0.85rem 0.9rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background: rgba(11, 8, 7, 0.32);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card strong,
|
||||||
|
.loot-pill strong {
|
||||||
|
display: block;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card span {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
color: rgba(244, 239, 227, 0.62);
|
||||||
|
font-size: 0.84rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card-equipped {
|
||||||
|
border-color: rgba(113, 176, 152, 0.35);
|
||||||
|
background: rgba(56, 86, 73, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card-treasure,
|
||||||
|
.loot-pill {
|
||||||
|
border-color: rgba(214, 168, 86, 0.35);
|
||||||
|
background: rgba(111, 76, 20, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
|
.loot-ribbon {
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.loot-ribbon-list {
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||||
|
margin-top: 0.7rem;
|
||||||
|
}
|
||||||
|
|
||||||
.room-title {
|
.room-title {
|
||||||
margin: 0 0 0.35rem;
|
margin: 0 0 0.35rem;
|
||||||
font-size: 1.5rem;
|
font-size: 1.5rem;
|
||||||
@@ -393,4 +518,10 @@ select {
|
|||||||
.stat-strip {
|
.stat-strip {
|
||||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inventory-summary,
|
||||||
|
.inventory-grid,
|
||||||
|
.town-summary {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -215,6 +215,7 @@ export type RunState = {
|
|||||||
dungeon: DungeonState;
|
dungeon: DungeonState;
|
||||||
adventurerSnapshot: AdventurerState;
|
adventurerSnapshot: AdventurerState;
|
||||||
activeCombat?: CombatState;
|
activeCombat?: CombatState;
|
||||||
|
townState: TownState;
|
||||||
defeatedCreatureIds: string[];
|
defeatedCreatureIds: string[];
|
||||||
xpGained: number;
|
xpGained: number;
|
||||||
goldGained: number;
|
goldGained: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user