| 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 | /** |
| 62 | * Editor / gift picker rows: one entry per alchemy recipe (same as CE buy catalog). |
| 63 | * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>} |
| 64 | */ |
| 65 | export function listRecipePaperPickerEntries () |
| 66 | { |
| 67 | const entries = []; |
| 68 | for (const potion of listAlchemyPotions()) |
| 69 | { |
| 70 | const listingKey = recipeListingKey(potion.potionKey); |
| 71 | if (!listingKey) |
| 72 | { continue; } |
| 73 | entries.push({ |
| 74 | itemId: ItemId.recipePaper, |
| 75 | listingKey, |
| 76 | potionKey: potion.potionKey, |
| 77 | displayName: recipePaperDisplayName(potion.potionKey), |
| 78 | key: `recipe-paper:${potion.potionKey}` |
| 79 | }); |
| 80 | } |
| 81 | return entries; |
| 82 | } |
| 83 | |
| 84 | export function marketOrderDisplayName (order) |
| 85 | { |
| 86 | const listingKey = listingKeyFromOrder(order); |
| 87 | if (listingKey?.startsWith(RECIPE_LISTING_PREFIX)) |
| 88 | { |
| 89 | return recipePaperDisplayName(potionKeyFromListingKey(listingKey)); |
| 90 | } |
| 91 | return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`; |
| 92 | } |
| 93 | |
| 94 | export function normalizeBuyListingKey (itemId, listingKey) |
| 95 | { |
| 96 | const id = itemId | 0; |
| 97 | if (id !== ItemId.recipePaper) |
| 98 | { return null; } |
| 99 | const key = String(listingKey ?? "").trim(); |
| 100 | if (!key.startsWith(RECIPE_LISTING_PREFIX)) |
| 101 | { return null; } |
| 102 | const potionKey = potionKeyFromListingKey(key); |
| 103 | if (!potionKey || !alchemyPotionDefinition(potionKey)) |
| 104 | { return null; } |
| 105 | return key; |
| 106 | } |
| 107 | |
| 108 | /** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */ |
| 109 | export function buildMarketCatalogEntries ({ |
| 110 | lastSaleFor, |
| 111 | undiscountedMarketCoins, |
| 112 | now, |
| 113 | lerpDurationMs, |
| 114 | settings |
| 115 | }) |
| 116 | { |
| 117 | const catalog = []; |
| 118 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 119 | { |
| 120 | if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 })) |
| 121 | { continue; } |
| 122 | if (def.itemId === ItemId.coins) |
| 123 | { continue; } |
| 124 | if (def.itemId === ItemId.recipePaper) |
| 125 | { continue; } |
| 126 | catalog.push({ |
| 127 | itemId: def.itemId, |
| 128 | listingKey: null, |
| 129 | displayName: def.displayName, |
| 130 | hintPrice: undiscountedMarketCoins( |
| 131 | def.itemId, |
| 132 | lastSaleFor(def.itemId), |
| 133 | now, |
| 134 | lerpDurationMs, |
| 135 | settings |
| 136 | ) |
| 137 | }); |
| 138 | } |
| 139 | const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper]; |
| 140 | if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 })) |
| 141 | { |
| 142 | const recipeHint = undiscountedMarketCoins( |
| 143 | ItemId.recipePaper, |
| 144 | lastSaleFor(ItemId.recipePaper), |
| 145 | now, |
| 146 | lerpDurationMs, |
| 147 | settings |
| 148 | ); |
| 149 | for (const potion of listAlchemyPotions()) |
| 150 | { |
| 151 | const listingKey = recipeListingKey(potion.potionKey); |
| 152 | if (!listingKey) |
| 153 | { continue; } |
| 154 | catalog.push({ |
| 155 | itemId: ItemId.recipePaper, |
| 156 | listingKey, |
| 157 | displayName: recipePaperDisplayName(potion.potionKey), |
| 158 | hintPrice: recipeHint |
| 159 | }); |
| 160 | } |
| 161 | } |
| 162 | catalog.sort((a, b) => a.displayName.localeCompare(b.displayName)); |
| 163 | return catalog; |
| 164 | } |