BlueGit

timfa questward

Files

questward / shared / shop / pricing.js

pricing.js

pricing.js 2.0 KiB Download Raw
1/**
2 * Shop / tooltip pricing from seedValue and optional player-market last sale.
3 * Matches design §27.6 — lerp toward lastTradePrice over shopPriceLerpDurationMs.
4 */
5
6import { itemDefinition } from "../definitions/items.js";
7import { ItemCategory } from "../definitions/itemCategories.js";
8import { DEFAULT_WORLD_SETTINGS } from "../protocol/constants.js";
9
10export function lerp (a, b, t)
11{
12 return a + (b - a) * t;
13}
14
15/**
16 * @param {object|null} lastSale `{ lastTradePrice, lastTradeAt, priceFrom }`
17 */
18export function undiscountedMarketValue (itemId, lastSale = null, now = Date.now(), lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs)
19{
20 const def = itemDefinition(itemId);
21 const seed = Math.max(1, Number(def?.seedValue) || 1);
22 if (!lastSale?.lastTradeAt || lastSale.lastTradePrice == null)
23 { return seed; }
24 const duration = Math.max(1, Number(lerpDurationMs) || DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs);
25 const t = Math.min(1, Math.max(0, (now - lastSale.lastTradeAt) / duration));
26 const from = lastSale.priceFrom != null ? Number(lastSale.priceFrom) : seed;
27 const to = Number(lastSale.lastTradePrice);
28 if (!Number.isFinite(from) || !Number.isFinite(to))
29 { return seed; }
30 return lerp(from, to, t);
31}
32
33/** Integer shop purchase price (server authority). */
34export function shopUnitPrice (itemId, {
35 lastSale = null,
36 now = Date.now(),
37 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
38 hiddenDiscount = 1
39} = {})
40{
41 const undiscounted = undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs);
42 const discount = Math.min(1, Math.max(0.01, Number(hiddenDiscount) || 1));
43 return Math.max(1, Math.round(undiscounted * discount));
44}
45
46/** NPC shops sell ingredients (and items explicitly flagged). */
47export function canSellInNpcShop (itemId)
48{
49 const def = itemDefinition(itemId);
50 if (!def)
51 { return false; }
52 if (def.npcShopAllowed === false)
53 { return false; }
54 if (def.npcShopAllowed === true)
55 { return true; }
56 return def.category === ItemCategory.ingredient || def.category === "ingredient";
57}