From 6b6fd8970dd6aae2ce2684209a9dbbc1547b3de5 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Mon, 27 Apr 2026 13:55:18 +0000 Subject: [PATCH] Inventory slot ids can be either strings or numbers. --- src/common/rpg/inventory.ts | 16 ++++++++-------- src/common/rpg/types.ts | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/common/rpg/inventory.ts b/src/common/rpg/inventory.ts index 9e1f1a5..fce9586 100644 --- a/src/common/rpg/inventory.ts +++ b/src/common/rpg/inventory.ts @@ -1,20 +1,20 @@ -import type { InventoryOptions, InventorySlotInput } from "./types"; +import type { InventoryOptions, InventorySlotInput, SlotId } from "./types"; interface SlotEntry { - readonly slotId: string; + readonly slotId: SlotId; readonly limit: number | undefined; state: { itemId: string; amount: number } | null; } export class Inventory { - private readonly slots: Map; + private readonly slots: Map; private readonly maxAmountPerItem: Record; constructor(slotDefs: Array, options?: InventoryOptions) { this.slots = new Map( slotDefs.map(def => { - const slotId = typeof def === 'string' ? def : def.slotId; - const limit = typeof def === 'string' ? undefined : def.limit; + const slotId = typeof def === 'object' ? def.slotId : def; + const limit = typeof def === 'object' ? def.limit : undefined; return [slotId, { slotId, limit, state: null }]; }) ); @@ -34,7 +34,7 @@ export class Inventory { return this.slotCapFor(slot, itemId) - (slot.state?.amount ?? 0); } - addItem(itemId: string, amount: number, slotId?: string): boolean { + addItem(itemId: string, amount: number, slotId?: SlotId): boolean { if (amount < 0) return false; if (amount === 0) return true; @@ -78,7 +78,7 @@ export class Inventory { return remaining === 0; } - removeItem(itemId: string, amount: number, slotId?: string): boolean { + removeItem(itemId: string, amount: number, slotId?: SlotId): boolean { if (amount < 0) return false; if (amount === 0) return true; @@ -107,7 +107,7 @@ export class Inventory { return remaining === 0; } - getAmount(itemId: string, slotId?: string): number { + getAmount(itemId: string, slotId?: SlotId): number { if (slotId !== undefined) { const slot = this.slots.get(slotId); return slot?.state?.itemId === itemId ? slot.state.amount : 0; diff --git a/src/common/rpg/types.ts b/src/common/rpg/types.ts index b120b40..91f0d6a 100644 --- a/src/common/rpg/types.ts +++ b/src/common/rpg/types.ts @@ -97,12 +97,14 @@ function isQuestStage(v: unknown): v is QuestStage { && Array.isArray(s.actions) && s.actions.every(isRPGAction); } +export type SlotId = string | number; + export interface InventorySlotDefinition { - slotId: string; + slotId: SlotId; limit?: number; } -export type InventorySlotInput = string | InventorySlotDefinition; +export type InventorySlotInput = SlotId | InventorySlotDefinition; export interface InventoryOptions { maxAmountPerItem?: Record;