import { Component } from "../ecs/components.js"; import { bagView } from "../items/bags.js"; import { itemDefinition } from "../../../shared/definitions/items.js"; import { ItemOriginKind, attachOrigins } from "../../../shared/items/origin.js"; import { countCoinsInSlots, insertStack, takeCoinsFromSlots } from "../../../shared/items/inventory.js"; import { canSellInNpcShop, shopUnitPrice } from "../../../shared/shop/pricing.js"; import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js"; import { chebyshev } from "../combat/service.js"; function userError (message) { const error = new Error(message); error.userFacing = true; return error; } function lastSaleFor (world, itemId) { return world.itemLastSales?.get(itemId) ?? null; } function shopStockView (world, shop, now) { const lerpDurationMs = world.settings?.shopPriceLerpDurationMs ?? DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs; const stock = []; for (const itemId of shop.stock ?? []) { if (!canSellInNpcShop(itemId)) { continue; } const def = itemDefinition(itemId); if (!def) { continue; } stock.push({ itemId, displayName: def.displayName, unitPrice: shopUnitPrice(itemId, { lastSale: lastSaleFor(world, itemId), now, lerpDurationMs, hiddenDiscount: shop.hiddenDiscount ?? 1 }) }); } return stock; } export function openShop (world, sessionId, characterId, npcEntityId, now = Date.now()) { const entityId = world.characterEntities.get(characterId); if (entityId == null) { throw userError("Character is not in the world."); } const handle = world.ecs.handleOf(entityId); const identity = world.ecs.get(handle, Component.networkIdentity); if (!identity || identity.sessionId !== sessionId) { throw userError("Character is not in the world."); } if (world.ecs.has(handle, Component.dead)) { throw userError("You are dead."); } const npcHandle = world.ecs.handleOf(npcEntityId); if (npcHandle == null) { throw userError("Target is not here."); } const shop = world.ecs.get(npcHandle, Component.shop); if (!shop?.stock?.length) { throw userError("They are not selling anything."); } const playerPos = world.ecs.get(handle, Component.position); const npcPos = world.ecs.get(npcHandle, Component.position); if (!playerPos || !npcPos || chebyshev(playerPos.tileX, playerPos.tileY, npcPos.tileX, npcPos.tileY) > 1) { throw userError("You need to stand next to them."); } const appearance = world.ecs.get(npcHandle, Component.appearance); const displayName = shop.displayName || [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ") || "Shopkeeper"; return { events: [{ type: "shop-open", sessionId, characterId, npcEntityId, displayName, stock: shopStockView(world, shop, now) }] }; } export function buyFromShop (world, sessionId, characterId, npcEntityId, itemId, quantity = 1, now = Date.now()) { const entityId = world.characterEntities.get(characterId); if (entityId == null) { throw userError("Character is not in the world."); } const handle = world.ecs.handleOf(entityId); const identity = world.ecs.get(handle, Component.networkIdentity); if (!identity || identity.sessionId !== sessionId) { throw userError("Character is not in the world."); } if (world.ecs.has(handle, Component.dead)) { throw userError("You are dead."); } const npcHandle = world.ecs.handleOf(npcEntityId); if (npcHandle == null) { throw userError("Target is not here."); } const shop = world.ecs.get(npcHandle, Component.shop); if (!shop?.stock?.includes(itemId | 0)) { throw userError("They do not sell that."); } if (!canSellInNpcShop(itemId)) { throw userError("That item cannot be sold here."); } const playerPos = world.ecs.get(handle, Component.position); const npcPos = world.ecs.get(npcHandle, Component.position); if (!playerPos || !npcPos || chebyshev(playerPos.tileX, playerPos.tileY, npcPos.tileX, npcPos.tileY) > 1) { throw userError("You need to stand next to them."); } const qty = Math.max(1, Math.min(99, quantity | 0 || 1)); const def = itemDefinition(itemId); if (!def) { throw userError("Unknown item."); } const lerpDurationMs = world.settings?.shopPriceLerpDurationMs ?? DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs; const unitPrice = shopUnitPrice(itemId, { lastSale: lastSaleFor(world, itemId), now, lerpDurationMs, hiddenDiscount: shop.hiddenDiscount ?? 1 }); const total = unitPrice * qty; const inventory = world.ecs.get(handle, Component.inventory); if (!inventory) { throw userError("No inventory."); } if (countCoinsInSlots(inventory.slots) < total) { throw userError("You cannot afford that."); } const paid = takeCoinsFromSlots(inventory.slots, total); if (!paid.ok) { throw userError("You cannot afford that."); } const appearance = world.ecs.get(npcHandle, Component.appearance); const displayName = shop.displayName || [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ") || "Shopkeeper"; const stack = attachOrigins({ itemId, quantity: qty, origins: [{ origin: { kind: ItemOriginKind.shop, displayName }, quantity: qty }] }); const added = insertStack(paid.slots, stack, def); if (!added.ok || added.remainder > 0) { throw userError("Not enough bag space."); } inventory.slots = added.slots; return { events: [{ type: "shop-open", sessionId, characterId, npcEntityId, displayName, stock: shopStockView(world, shop, now) }], private: bagView(world.ecs, handle) }; } export function handleShopCommand (world, sessionId, command, now) { switch (command.type) { case "buy-from-shop": return buyFromShop( world, sessionId, command.characterId, command.npcEntityId | 0, command.itemId | 0, command.quantity | 0 || 1, now ); default: return null; } }