| 1 | import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js"; |
| 2 | import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alchemy.js"; |
| 3 | import { canSellOnMarketplace } from "../items/marketplace.js"; |
| 4 | |
| 5 | export const RECIPE_LISTING_PREFIX = "recipe:"; |
| 6 | |
| 7 | /** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */ |
| 8 | export function recipeListingKey (potionKey) |
| 9 | { |
| 10 | const key = String(potionKey ?? "").trim(); |
| 11 | if (!key) |
| 12 | { return null; } |
| 13 | return `${RECIPE_LISTING_PREFIX}${key}`; |
| 14 | } |
| 15 | |
| 16 | export function potionKeyFromListingKey (listingKey) |
| 17 | { |
| 18 | const text = String(listingKey ?? ""); |
| 19 | if (!text.startsWith(RECIPE_LISTING_PREFIX)) |
| 20 | { return null; } |
| 21 | return text.slice(RECIPE_LISTING_PREFIX.length) || null; |
| 22 | } |
| 23 | |
| 24 | /** Listing key carried on a bag stack (sell escrow) or null for normal goods. */ |
| 25 | export function listingKeyFromStack (stack) |
| 26 | { |
| 27 | if ((stack?.itemId | 0) !== ItemId.recipePaper) |
| 28 | { return null; } |
| 29 | if (stack.instance?.kind !== "recipe-paper") |
| 30 | { return null; } |
| 31 | return recipeListingKey(stack.instance.potionKey); |
| 32 | } |
| 33 | |
| 34 | /** Resolved listing key on an open order (column or sell escrow). */ |
| 35 | export function listingKeyFromOrder (order) |
| 36 | { |
| 37 | if (order?.listingKey) |
| 38 | { return order.listingKey; } |
| 39 | return listingKeyFromStack(order?.escrowStack) ?? null; |
| 40 | } |
| 41 | |
| 42 | export function marketOrdersCompatible (buy, sell) |
| 43 | { |
| 44 | if ((buy.itemId | 0) !== (sell.itemId | 0)) |
| 45 | { return false; } |
| 46 | const buyKey = listingKeyFromOrder(buy); |
| 47 | const sellKey = listingKeyFromOrder(sell); |
| 48 | if (!buyKey && !sellKey) |
| 49 | { return true; } |
| 50 | return buyKey === sellKey; |
| 51 | } |
| 52 | |
| 53 | export function recipePaperDisplayName (potionKey) |
| 54 | { |
| 55 | const def = alchemyPotionDefinition(potionKey); |
| 56 | if (!def) |
| 57 | { return "Recipe paper"; } |
| 58 | return `${def.displayName} recipe`; |
| 59 | } |
| 60 | |
| 61 | export function marketOrderDisplayName (order) |
| 62 | { |
| 63 | const listingKey = listingKeyFromOrder(order); |
| 64 | if (listingKey?.startsWith(RECIPE_LISTING_PREFIX)) |
| 65 | { |
| 66 | return recipePaperDisplayName(potionKeyFromListingKey(listingKey)); |
| 67 | } |
| 68 | return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`; |
| 69 | } |
| 70 | |
| 71 | export function normalizeBuyListingKey (itemId, listingKey) |
| 72 | { |
| 73 | const id = itemId | 0; |
| 74 | if (id !== ItemId.recipePaper) |
| 75 | { return null; } |
| 76 | const key = String(listingKey ?? "").trim(); |
| 77 | if (!key.startsWith(RECIPE_LISTING_PREFIX)) |
| 78 | { return null; } |
| 79 | const potionKey = potionKeyFromListingKey(key); |
| 80 | if (!potionKey || !alchemyPotionDefinition(potionKey)) |
| 81 | { return null; } |
| 82 | return key; |
| 83 | } |
| 84 | |
| 85 | /** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */ |
| 86 | export function buildMarketCatalogEntries ({ |
| 87 | lastSaleFor, |
| 88 | undiscountedMarketCoins, |
| 89 | now, |
| 90 | lerpDurationMs, |
| 91 | settings |
| 92 | }) |
| 93 | { |
| 94 | const catalog = []; |
| 95 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 96 | { |
| 97 | if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 })) |
| 98 | { continue; } |
| 99 | if (def.itemId === ItemId.coins) |
| 100 | { continue; } |
| 101 | if (def.itemId === ItemId.recipePaper) |
| 102 | { continue; } |
| 103 | catalog.push({ |
| 104 | itemId: def.itemId, |
| 105 | listingKey: null, |
| 106 | displayName: def.displayName, |
| 107 | hintPrice: undiscountedMarketCoins( |
| 108 | def.itemId, |
| 109 | lastSaleFor(def.itemId), |
| 110 | now, |
| 111 | lerpDurationMs, |
| 112 | settings |
| 113 | ) |
| 114 | }); |
| 115 | } |
| 116 | const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper]; |
| 117 | if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 })) |
| 118 | { |
| 119 | const recipeHint = undiscountedMarketCoins( |
| 120 | ItemId.recipePaper, |
| 121 | lastSaleFor(ItemId.recipePaper), |
| 122 | now, |
| 123 | lerpDurationMs, |
| 124 | settings |
| 125 | ); |
| 126 | for (const potion of listAlchemyPotions()) |
| 127 | { |
| 128 | const listingKey = recipeListingKey(potion.potionKey); |
| 129 | if (!listingKey) |
| 130 | { continue; } |
| 131 | catalog.push({ |
| 132 | itemId: ItemId.recipePaper, |
| 133 | listingKey, |
| 134 | displayName: recipePaperDisplayName(potion.potionKey), |
| 135 | hintPrice: recipeHint |
| 136 | }); |
| 137 | } |
| 138 | } |
| 139 | catalog.sort((a, b) => a.displayName.localeCompare(b.displayName)); |
| 140 | return catalog; |
| 141 | } |