BlueGit

timfa questward

Files

questward / server / src / shop / service.js

service.js

service.js 20.8 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { bagView, findReservedChequeIndex } from "../items/bags.js";
3import { ItemId, itemDefinition } from "../../../shared/definitions/items.js";
4import {
5 ItemOriginKind,
6 attachOrigins,
7 mergeOriginLists,
8 normalizeOrigins,
9 splitOrigins,
10 shopOrigin
11} from "../../../shared/items/origin.js";
12import { isLetterStack } from "../../../shared/items/letters.js";
13import {
14 addCoinsToSlots,
15 cloneStack,
16 countCoinsInSlots,
17 insertStack,
18 insertStackGrowable,
19 takeCoinsFromSlots,
20 takeFromSlot
21} from "../../../shared/items/inventory.js";
22import {
23 canSellInNpcShop,
24 canPlayerSellToNpc,
25 resolveShopSellQuote,
26 shopUnitPrice,
27 shopBoughtResalePrice,
28 shopRestockDayKey
29} from "../../../shared/shop/pricing.js";
30import { DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
31import { chebyshev } from "../combat/service.js";
32import { objectPronoun } from "../../../shared/speech/pronouns.js";
33
34function userError (message)
35{
36 const error = new Error(message);
37 error.userFacing = true;
38 return error;
39}
40
41function npcSex (world, npcHandle)
42{
43 const appearance = world.ecs.get(npcHandle, Component.appearance);
44 return appearance?.sex === "female" ? "female" : "male";
45}
46
47/** Runtime map of characterId → true; plain object so entity transfer can clone it. */
48function ensureBrowsers (shop)
49{
50 if (!shop.browsers || typeof shop.browsers !== "object" || Array.isArray(shop.browsers))
51 { shop.browsers = Object.create(null); }
52 return shop.browsers;
53}
54
55export function shopHasActiveBrowsers (shop)
56{
57 const browsers = shop?.browsers;
58 if (!browsers || typeof browsers !== "object")
59 { return false; }
60 return Object.keys(browsers).length > 0;
61}
62
63function stopNpcMovement (world, npcHandle)
64{
65 const path = world.ecs.get(npcHandle, Component.path);
66 if (path)
67 { path.tiles = []; }
68}
69
70function addShopBrowser (world, shop, npcHandle, characterId)
71{
72 ensureBrowsers(shop)[characterId] = true;
73 stopNpcMovement(world, npcHandle);
74}
75
76function removeShopBrowser (shop, characterId)
77{
78 if (!shop?.browsers)
79 { return; }
80 delete shop.browsers[characterId];
81}
82
83export function releaseShopBrowsersForCharacter (world, characterId)
84{
85 if (characterId == null)
86 { return; }
87 for (const entity of world.ecs.query(Component.shop))
88 { removeShopBrowser(entity.shop, characterId); }
89}
90
91/**
92 * Drop browsers who left / walked away, pin paths while anyone browses,
93 * and emit shop-closed so clients dismiss the UI.
94 */
95export function pruneShopBrowsers (world)
96{
97 const events = [];
98 for (const entity of world.ecs.query(Component.shop, Component.position))
99 {
100 const shop = entity.shop;
101 const browsers = ensureBrowsers(shop);
102 const ids = Object.keys(browsers).map((id) => Number(id));
103 if (!ids.length)
104 { continue; }
105 stopNpcMovement(world, entity.handle);
106 for (const characterId of ids)
107 {
108 const playerEntityId = world.characterEntities.get(characterId);
109 if (playerEntityId == null)
110 {
111 removeShopBrowser(shop, characterId);
112 continue;
113 }
114 const playerHandle = world.ecs.handleOf(playerEntityId);
115 const playerPos = world.ecs.get(playerHandle, Component.position);
116 const stillNear = playerPos
117 && chebyshev(
118 playerPos.tileX,
119 playerPos.tileY,
120 entity.position.tileX,
121 entity.position.tileY
122 ) <= 1;
123 if (stillNear)
124 { continue; }
125 removeShopBrowser(shop, characterId);
126 const identity = world.ecs.get(playerHandle, Component.networkIdentity);
127 if (identity?.sessionId != null)
128 {
129 events.push({
130 type: "shop-closed",
131 sessionId: identity.sessionId,
132 characterId,
133 npcEntityId: entity.entityId
134 });
135 }
136 }
137 }
138 return events;
139}
140
141function lastSaleFor (world, itemId)
142{
143 return world.itemLastSales?.get(itemId) ?? null;
144}
145
146function lerpDuration (world)
147{
148 return world.settings?.shopPriceLerpDurationMs
149 ?? DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs;
150}
151
152function shopDisplayName (shop, npcHandle, world)
153{
154 const appearance = world.ecs.get(npcHandle, Component.appearance);
155 return shop.displayName
156 || [appearance?.firstName, appearance?.lastName].filter(Boolean).join(" ")
157 || "Shopkeeper";
158}
159
160function ensureVault (ecs, handle)
161{
162 let vault = ecs.get(handle, Component.vault);
163 if (!vault)
164 { vault = ecs.add(handle, Component.vault, { slots: [], reserved: [] }); }
165 if (!Array.isArray(vault.slots))
166 { vault.slots = []; }
167 if (!Array.isArray(vault.reserved))
168 { vault.reserved = []; }
169 return vault;
170}
171
172/** Refresh catalog remaining and clear bought-from-players stock at local midnight. */
173export function ensureShopRestocked (shop, now = Date.now())
174{
175 if (!shop)
176 { return; }
177 if (!Array.isArray(shop.catalog))
178 { shop.catalog = []; }
179 if (Array.isArray(shop.stock) && !shop.catalog.length)
180 {
181 shop.catalog = shop.stock
182 .map((id) => ({ itemId: id | 0, quantity: 0 }))
183 .filter((line) => line.itemId > 0);
184 }
185 if (!shop.remaining || typeof shop.remaining !== "object")
186 { shop.remaining = Object.create(null); }
187 if (!shop.boughtStock || typeof shop.boughtStock !== "object")
188 { shop.boughtStock = Object.create(null); }
189 const day = shopRestockDayKey(now);
190 if (shop.restockDay === day)
191 { return; }
192 shop.restockDay = day;
193 shop.boughtStock = Object.create(null);
194 shop.remaining = Object.create(null);
195 for (const line of shop.catalog)
196 {
197 const itemId = line.itemId | 0;
198 const quantity = Math.max(0, line.quantity | 0 || 0);
199 if (itemId > 0 && quantity > 0)
200 { shop.remaining[itemId] = quantity; }
201 }
202}
203
204function catalogLine (shop, itemId)
205{
206 return (shop.catalog ?? []).find((line) => (line.itemId | 0) === (itemId | 0)) ?? null;
207}
208
209function catalogAvailable (shop, itemId)
210{
211 const line = catalogLine(shop, itemId);
212 if (!line)
213 { return 0; }
214 if ((line.quantity | 0) <= 0)
215 { return Number.POSITIVE_INFINITY; }
216 return Math.max(0, shop.remaining?.[itemId] | 0 || 0);
217}
218
219/** Normalize legacy qty-only boughtStock entries into `{ quantity, origins }`. */
220function boughtLot (shop, itemId)
221{
222 const raw = shop.boughtStock?.[itemId];
223 if (raw == null)
224 { return null; }
225 if (typeof raw === "number")
226 {
227 const quantity = Math.max(0, raw | 0);
228 if (quantity <= 0)
229 { return null; }
230 return {
231 quantity,
232 origins: [{ origin: shopOrigin("a shop"), quantity }]
233 };
234 }
235 const quantity = Math.max(0, raw.quantity | 0 || 0);
236 if (quantity <= 0)
237 { return null; }
238 const origins = normalizeOrigins({ quantity, origins: raw.origins });
239 const originQty = origins.reduce((sum, entry) => sum + entry.quantity, 0);
240 if (originQty < quantity)
241 {
242 origins.push({ origin: shopOrigin("a shop"), quantity: quantity - originQty });
243 }
244 return { quantity, origins };
245}
246
247function boughtAvailable (shop, itemId)
248{
249 return boughtLot(shop, itemId)?.quantity ?? 0;
250}
251
252function addBoughtLot (shop, itemId, quantity, origins)
253{
254 const qty = Math.max(0, quantity | 0 || 0);
255 if (qty <= 0)
256 { return; }
257 const existing = boughtLot(shop, itemId);
258 const nextOrigins = mergeOriginLists(
259 existing?.origins ?? [],
260 normalizeOrigins({ quantity: qty, origins })
261 );
262 shop.boughtStock[itemId] = {
263 quantity: (existing?.quantity ?? 0) + qty,
264 origins: nextOrigins
265 };
266}
267
268/** Pull FIFO origins from bought stock; returns null if insufficient. */
269function takeBoughtLot (shop, itemId, quantity)
270{
271 const qty = Math.max(0, quantity | 0 || 0);
272 const lot = boughtLot(shop, itemId);
273 if (!lot || lot.quantity < qty)
274 { return null; }
275 const split = splitOrigins(lot.origins, qty);
276 const remainQty = lot.quantity - qty;
277 if (remainQty <= 0)
278 { delete shop.boughtStock[itemId]; }
279 else
280 {
281 shop.boughtStock[itemId] = {
282 quantity: remainQty,
283 origins: split.remain
284 };
285 }
286 return { quantity: qty, origins: split.taken };
287}
288
289function shopStockView (world, shop, now)
290{
291 ensureShopRestocked(shop, now);
292 const lastSaleOpts = (itemId) => ({
293 lastSale: lastSaleFor(world, itemId),
294 now,
295 lerpDurationMs: lerpDuration(world),
296 settings: world.settings ?? null
297 });
298 const stock = [];
299 for (const line of shop.catalog ?? [])
300 {
301 const itemId = line.itemId | 0;
302 if (!canSellInNpcShop(itemId))
303 { continue; }
304 const def = itemDefinition(itemId);
305 if (!def)
306 { continue; }
307 const available = catalogAvailable(shop, itemId);
308 if (available === 0)
309 { continue; }
310 stock.push({
311 listingId: `stock:${itemId}`,
312 itemId,
313 displayName: def.displayName,
314 unitPrice: shopUnitPrice(itemId, {
315 ...lastSaleOpts(itemId),
316 hiddenDiscount: shop.hiddenDiscount ?? 1
317 }),
318 available: Number.isFinite(available) ? available : null,
319 source: "stock"
320 });
321 }
322 for (const key of Object.keys(shop.boughtStock ?? {}))
323 {
324 const itemId = key | 0;
325 const available = boughtAvailable(shop, itemId);
326 if (available <= 0 || !canSellInNpcShop(itemId))
327 { continue; }
328 const def = itemDefinition(itemId);
329 if (!def)
330 { continue; }
331 stock.push({
332 listingId: `bought:${itemId}`,
333 itemId,
334 displayName: def.displayName,
335 unitPrice: shopBoughtResalePrice(itemId, lastSaleOpts(itemId)),
336 available,
337 source: "bought"
338 });
339 }
340 return stock;
341}
342
343function shopOpenEvent (world, sessionId, characterId, npcEntityId, shop, displayName, now)
344{
345 const npcHandle = world.ecs.handleOf(npcEntityId);
346 return {
347 type: "shop-open",
348 sessionId,
349 characterId,
350 npcEntityId,
351 displayName,
352 sex: npcHandle != null ? npcSex(world, npcHandle) : "male",
353 buysItems: !!shop.buysItems,
354 stock: shopStockView(world, shop, now)
355 };
356}
357
358function requireShopAccess (world, sessionId, characterId, npcEntityId)
359{
360 const entityId = world.characterEntities.get(characterId);
361 if (entityId == null)
362 { throw userError("Character is not in the world."); }
363 const handle = world.ecs.handleOf(entityId);
364 const identity = world.ecs.get(handle, Component.networkIdentity);
365 if (!identity || identity.sessionId !== sessionId)
366 { throw userError("Character is not in the world."); }
367 if (world.ecs.has(handle, Component.dead))
368 { throw userError("You are dead."); }
369
370 const npcHandle = world.ecs.handleOf(npcEntityId);
371 if (npcHandle == null)
372 { throw userError("Target is not here."); }
373 const shop = world.ecs.get(npcHandle, Component.shop);
374 const sex = npcSex(world, npcHandle);
375 if (!shop || (!(shop.catalog?.length) && !shop.buysItems))
376 {
377 const who = sex === "female" ? "She" : "He";
378 throw userError(`${who} is not selling anything.`);
379 }
380
381 const playerPos = world.ecs.get(handle, Component.position);
382 const npcPos = world.ecs.get(npcHandle, Component.position);
383 if (!playerPos || !npcPos || chebyshev(playerPos.tileX, playerPos.tileY, npcPos.tileX, npcPos.tileY) > 1)
384 { throw userError(`You need to stand next to ${objectPronoun(sex)}.`); }
385
386 return { handle, npcHandle, shop, entityId };
387}
388
389export function openShop (world, sessionId, characterId, npcEntityId, now = Date.now())
390{
391 const { shop, npcHandle } = requireShopAccess(world, sessionId, characterId, npcEntityId);
392 ensureShopRestocked(shop, now);
393 addShopBrowser(world, shop, npcHandle, characterId);
394 const displayName = shopDisplayName(shop, npcHandle, world);
395 return {
396 events: [shopOpenEvent(world, sessionId, characterId, npcEntityId, shop, displayName, now)]
397 };
398}
399
400export function closeShop (world, sessionId, characterId, npcEntityId)
401{
402 const entityId = world.characterEntities.get(characterId);
403 if (entityId == null)
404 { return { events: [] }; }
405 const handle = world.ecs.handleOf(entityId);
406 const identity = world.ecs.get(handle, Component.networkIdentity);
407 if (!identity || identity.sessionId !== sessionId)
408 { return { events: [] }; }
409 const npcHandle = world.ecs.handleOf(npcEntityId | 0);
410 if (npcHandle == null)
411 { return { events: [] }; }
412 const shop = world.ecs.get(npcHandle, Component.shop);
413 removeShopBrowser(shop, characterId);
414 return { events: [] };
415}
416
417function parseListing (listingId, itemId, source)
418{
419 if (typeof listingId === "string" && listingId.includes(":"))
420 {
421 const [kind, id] = listingId.split(":");
422 return { source: kind === "bought" ? "bought" : "stock", itemId: id | 0 };
423 }
424 if (source === "bought")
425 { return { source: "bought", itemId: itemId | 0 }; }
426 return { source: "stock", itemId: itemId | 0 };
427}
428
429export function buyFromShop (world, sessionId, characterId, npcEntityId, itemId, quantity = 1, now = Date.now(), listingId = null, source = null)
430{
431 const { handle, npcHandle, shop } = requireShopAccess(world, sessionId, characterId, npcEntityId);
432 ensureShopRestocked(shop, now);
433 const listing = parseListing(listingId, itemId, source);
434 const buyItemId = listing.itemId;
435 if (!canSellInNpcShop(buyItemId))
436 { throw userError("That item cannot be sold here."); }
437
438 const qty = Math.max(1, quantity | 0 || 1);
439 const def = itemDefinition(buyItemId);
440 if (!def)
441 { throw userError("Unknown item."); }
442
443 const available = listing.source === "bought"
444 ? boughtAvailable(shop, buyItemId)
445 : catalogAvailable(shop, buyItemId);
446 if (available < qty)
447 { throw userError("Not enough stock."); }
448
449 const unitPrice = listing.source === "bought"
450 ? shopBoughtResalePrice(buyItemId, {
451 lastSale: lastSaleFor(world, buyItemId),
452 now,
453 lerpDurationMs: lerpDuration(world),
454 settings: world.settings ?? null
455 })
456 : shopUnitPrice(buyItemId, {
457 lastSale: lastSaleFor(world, buyItemId),
458 now,
459 lerpDurationMs: lerpDuration(world),
460 hiddenDiscount: shop.hiddenDiscount ?? 1,
461 settings: world.settings ?? null
462 });
463 const total = unitPrice * qty;
464 const inventory = world.ecs.get(handle, Component.inventory);
465 if (!inventory)
466 { throw userError("No inventory."); }
467 if (countCoinsInSlots(inventory.slots) < total)
468 { throw userError("You cannot afford that."); }
469
470 const paid = takeCoinsFromSlots(inventory.slots, total);
471 if (!paid.ok)
472 { throw userError("You cannot afford that."); }
473
474 const displayName = shopDisplayName(shop, npcHandle, world);
475 let stack;
476 if (listing.source === "bought")
477 {
478 const lot = boughtLot(shop, buyItemId);
479 if (!lot || lot.quantity < qty)
480 { throw userError("Not enough stock."); }
481 const split = splitOrigins(lot.origins, qty);
482 stack = attachOrigins({
483 itemId: buyItemId,
484 quantity: qty,
485 origins: split.taken
486 });
487 }
488 else
489 {
490 const shopOriginEntry = { kind: ItemOriginKind.shop, displayName };
491 stack = attachOrigins({
492 itemId: buyItemId,
493 quantity: qty,
494 origins: [{ origin: shopOriginEntry, quantity: qty }]
495 });
496 }
497 const added = insertStack(paid.slots, stack, def);
498 inventory.slots = added.slots;
499 if (added.remainder > 0)
500 {
501 const overflowOrigins = splitOrigins(normalizeOrigins(stack), qty - added.remainder).remain;
502 const overflow = attachOrigins({
503 itemId: buyItemId,
504 quantity: added.remainder,
505 origins: overflowOrigins
506 });
507 const vault = ensureVault(world.ecs, handle);
508 const vaulted = insertStackGrowable(vault.slots, overflow, def);
509 if (!vaulted.ok || vaulted.remainder > 0)
510 { throw userError("Your bank cannot hold that purchase."); }
511 vault.slots = vaulted.slots;
512 }
513
514 if (listing.source === "bought")
515 {
516 if (!takeBoughtLot(shop, buyItemId, qty))
517 { throw userError("Not enough stock."); }
518 }
519 else
520 {
521 const line = catalogLine(shop, buyItemId);
522 if (line && (line.quantity | 0) > 0)
523 { shop.remaining[buyItemId] = catalogAvailable(shop, buyItemId) - qty; }
524 }
525
526 return {
527 events: [shopOpenEvent(world, sessionId, characterId, npcEntityId, shop, displayName, now)],
528 private: bagView(world.ecs, handle)
529 };
530}
531
532/**
533 * Player sells to NPC (requires buysItems).
534 * Buys any transferable bag goods except blank cheques.
535 * Filled cheques sell as their reserved goods with a 5% admin fee.
536 * Limited catalog lines refill first; remainder goes into boughtStock
537 * (cleared at midnight). Infinite catalog lines absorb goods without resale stock.
538 * Letters pay out but are destroyed — never restocked or put in boughtStock.
539 */
540export function sellToShop (world, sessionId, characterId, npcEntityId, slotIndex, quantity = 1, now = Date.now())
541{
542 const { handle, npcHandle, shop } = requireShopAccess(world, sessionId, characterId, npcEntityId);
543 ensureShopRestocked(shop, now);
544 if (!shop.buysItems)
545 { throw userError("They are not buying anything."); }
546
547 const inventory = world.ecs.get(handle, Component.inventory);
548 if (!inventory)
549 { throw userError("No inventory."); }
550 const slot = inventory.slots[slotIndex | 0];
551 if (!slot)
552 { throw userError("That slot is empty."); }
553 if (!canPlayerSellToNpc(slot))
554 { throw userError("They will not buy that."); }
555
556 const isCheque = (slot.itemId | 0) === ItemId.filledCheque;
557 const isLetter = isLetterStack(slot);
558 const quote = resolveShopSellQuote(slot, {
559 lastSale: lastSaleFor(world, isCheque ? (slot.instance.itemId | 0) : (slot.itemId | 0)),
560 now,
561 lerpDurationMs: lerpDuration(world),
562 settings: world.settings ?? null,
563 quantity: isCheque ? 1 : (quantity | 0 || 1)
564 });
565 if (!quote)
566 { throw userError("They will not buy that."); }
567
568 const goodsItemId = quote.goodsItemId;
569 const qty = quote.goodsQuantity;
570 const line = catalogLine(shop, goodsItemId);
571 let refill = 0;
572 let leftover = qty;
573 if (isLetter)
574 {
575 // Destroyed on sale — never enter shop stock.
576 refill = 0;
577 leftover = 0;
578 }
579 else if (line)
580 {
581 if ((line.quantity | 0) <= 0)
582 {
583 refill = qty;
584 leftover = 0;
585 }
586 else
587 {
588 const space = Math.max(0, (line.quantity | 0) - catalogAvailable(shop, goodsItemId));
589 refill = Math.min(qty, space);
590 leftover = qty - refill;
591 }
592 }
593
594 const total = quote.payout;
595 let reservedStack = null;
596 let chequeTaken = null;
597 let taken = null;
598
599 if (isCheque)
600 {
601 const instance = slot.instance;
602 const issuerId = instance.issuerCharacterId;
603 chequeTaken = takeFromSlot(inventory.slots, slotIndex | 0, 1);
604 if (!chequeTaken.ok)
605 { throw userError("Could not take that item."); }
606 inventory.slots = chequeTaken.slots;
607
608 if (Number(issuerId) === Number(characterId))
609 {
610 const vault = world.ecs.get(handle, Component.vault);
611 if (!vault)
612 {
613 inventory.slots[slotIndex | 0] = chequeTaken.taken;
614 throw userError("That cheque cannot be sold.");
615 }
616 if (!Array.isArray(vault.reserved))
617 { vault.reserved = []; }
618 const index = findReservedChequeIndex(vault.reserved, instance);
619 if (index < 0)
620 {
621 inventory.slots[slotIndex | 0] = chequeTaken.taken;
622 throw userError("That cheque cannot be sold.");
623 }
624 reservedStack = cloneStack(vault.reserved[index]);
625 vault.reserved = vault.reserved.filter((_, i) => i !== index);
626 }
627 else
628 {
629 const release = world.releaseChequeReservation?.(issuerId, instance.chequeId, instance);
630 if (!release?.ok)
631 {
632 inventory.slots[slotIndex | 0] = chequeTaken.taken;
633 throw userError(release?.error ?? "That cheque cannot be sold.");
634 }
635 reservedStack = release.stack;
636 }
637 }
638 else
639 {
640 taken = takeFromSlot(inventory.slots, slotIndex | 0, qty);
641 if (!taken.ok)
642 { throw userError("Could not take that item."); }
643 }
644
645 const slotsAfterTake = isCheque ? inventory.slots : taken.slots;
646 const paid = addCoinsToSlots(slotsAfterTake, total);
647 if (!paid.ok)
648 {
649 if (isCheque)
650 {
651 inventory.slots[slotIndex | 0] = chequeTaken.taken;
652 if (Number(slot.instance.issuerCharacterId) === Number(characterId))
653 {
654 const vault = world.ecs.get(handle, Component.vault);
655 if (vault && reservedStack)
656 { vault.reserved = [...(vault.reserved ?? []), reservedStack]; }
657 }
658 else if (reservedStack)
659 { world.restoreChequeReservation?.(slot.instance.issuerCharacterId, reservedStack); }
660 }
661 else
662 {
663 const def = itemDefinition(goodsItemId);
664 inventory.slots = insertStack(taken.slots, taken.taken, def).slots;
665 }
666 throw userError("Not enough bag space for coins.");
667 }
668 inventory.slots = paid.slots;
669
670 if (line && (line.quantity | 0) > 0 && refill > 0)
671 { shop.remaining[goodsItemId] = catalogAvailable(shop, goodsItemId) + refill; }
672 if (leftover > 0)
673 {
674 const soldStack = isCheque ? reservedStack : taken.taken;
675 const soldOrigins = normalizeOrigins(soldStack);
676 // Catalog refill absorbs first; leftover keeps remaining origins for resale.
677 const { remain: resaleOrigins } = splitOrigins(soldOrigins, refill);
678 addBoughtLot(shop, goodsItemId, leftover, resaleOrigins);
679 }
680
681 const displayName = shopDisplayName(shop, npcHandle, world);
682 return {
683 events: [shopOpenEvent(world, sessionId, characterId, npcEntityId, shop, displayName, now)],
684 private: bagView(world.ecs, handle)
685 };
686}
687
688export function handleShopCommand (world, sessionId, command, now)
689{
690 switch (command.type)
691 {
692 case "buy-from-shop":
693 return buyFromShop(
694 world,
695 sessionId,
696 command.characterId,
697 command.npcEntityId | 0,
698 command.itemId | 0,
699 command.quantity | 0 || 1,
700 now,
701 command.listingId ?? null,
702 command.source ?? null
703 );
704 case "sell-to-shop":
705 return sellToShop(
706 world,
707 sessionId,
708 command.characterId,
709 command.npcEntityId | 0,
710 command.slotIndex | 0,
711 command.quantity | 0 || 1,
712 now
713 );
714 case "close-shop":
715 return closeShop(world, sessionId, command.characterId, command.npcEntityId | 0);
716 default:
717 return null;
718 }
719}
720
721export function shopHasInterface (shop)
722{
723 if (!shop)
724 { return false; }
725 if (shop.buysItems)
726 { return true; }
727 if ((shop.catalog?.length ?? 0) > 0)
728 { return true; }
729 if (Object.keys(shop.boughtStock ?? {}).length > 0)
730 { return true; }
731 // Legacy shops spawned before catalog migration.
732 if ((shop.stock?.length ?? 0) > 0)
733 { return true; }
734 return false;
735}