✨Feature: implement town market functionality with treasure selling and stashing
This commit is contained in:
@@ -10,6 +10,7 @@ import { findCreatureById } from "@/data/contentHelpers";
|
||||
|
||||
import { startCombatFromRoom } from "./combat";
|
||||
import { resolveCombatLoot } from "./loot";
|
||||
import { createInitialTownState } from "./town";
|
||||
import {
|
||||
resolveEnemyTurn,
|
||||
resolvePlayerAttack,
|
||||
@@ -164,6 +165,13 @@ function cloneRun(run: RunState): RunState {
|
||||
globalFlags: [...run.dungeon.globalFlags],
|
||||
},
|
||||
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],
|
||||
xpGained: run.xpGained,
|
||||
goldGained: run.goldGained,
|
||||
@@ -336,6 +344,7 @@ export function createRunState(options: CreateRunOptions): RunState {
|
||||
globalFlags: [],
|
||||
},
|
||||
adventurerSnapshot: options.adventurer,
|
||||
townState: createInitialTownState(),
|
||||
defeatedCreatureIds: [],
|
||||
xpGained: 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] };
|
||||
}
|
||||
Reference in New Issue
Block a user