| 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 | /** |
| 85 | * Gift / editor picker rows: one drinkable bottle per alchemy potion |
| 86 | * (skips solid outputs like tanning brew). |
| 87 | * @returns {Array<{ itemId: number, listingKey: null, potionKey: string, displayName: string, key: string }>} |
| 88 | */ |
| 89 | export function listPotionPickerEntries () |
| 90 | { |
| 91 | const entries = []; |
| 92 | for (const potion of listAlchemyPotions()) |
| 93 | { |
| 94 | if (potion.outputItemId) |
| 95 | { continue; } |
| 96 | const potionKey = String(potion.potionKey ?? "").trim(); |
| 97 | if (!potionKey) |
| 98 | { continue; } |
| 99 | entries.push({ |
| 100 | itemId: ItemId.potion, |
| 101 | listingKey: null, |
| 102 | potionKey, |
| 103 | displayName: potion.displayName ?? potionKey, |
| 104 | key: `potion:${potionKey}` |
| 105 | }); |
| 106 | } |
| 107 | return entries; |
| 108 | } |
| 109 | |
| 110 | export function marketOrderDisplayName (order) |
| 111 | { |
| 112 | const listingKey = listingKeyFromOrder(order); |
| 113 | if (listingKey?.startsWith(RECIPE_LISTING_PREFIX)) |
| 114 | { |
| 115 | return recipePaperDisplayName(potionKeyFromListingKey(listingKey)); |
| 116 | } |
| 117 | return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`; |
| 118 | } |
| 119 | |
| 120 | export function normalizeBuyListingKey (itemId, listingKey) |
| 121 | { |
| 122 | const id = itemId | 0; |
| 123 | if (id !== ItemId.recipePaper) |
| 124 | { return null; } |
| 125 | const key = String(listingKey ?? "").trim(); |
| 126 | if (!key.startsWith(RECIPE_LISTING_PREFIX)) |
| 127 | { return null; } |
| 128 | const potionKey = potionKeyFromListingKey(key); |
| 129 | if (!potionKey || !alchemyPotionDefinition(potionKey)) |
| 130 | { return null; } |
| 131 | return key; |
| 132 | } |
| 133 | |
| 134 | /** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */ |
| 135 | export function buildMarketCatalogEntries ({ |
| 136 | lastSaleFor, |
| 137 | undiscountedMarketCoins, |
| 138 | now, |
| 139 | lerpDurationMs, |
| 140 | settings |
| 141 | }) |
| 142 | { |
| 143 | const catalog = []; |
| 144 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 145 | { |
| 146 | if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 })) |
| 147 | { continue; } |
| 148 | if (def.itemId === ItemId.coins) |
| 149 | { continue; } |
| 150 | if (def.itemId === ItemId.recipePaper) |
| 151 | { continue; } |
| 152 | catalog.push({ |
| 153 | itemId: def.itemId, |
| 154 | listingKey: null, |
| 155 | displayName: def.displayName, |
| 156 | hintPrice: undiscountedMarketCoins( |
| 157 | def.itemId, |
| 158 | lastSaleFor(def.itemId), |
| 159 | now, |
| 160 | lerpDurationMs, |
| 161 | settings |
| 162 | ) |
| 163 | }); |
| 164 | } |
| 165 | const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper]; |
| 166 | if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 })) |
| 167 | { |
| 168 | const recipeHint = undiscountedMarketCoins( |
| 169 | ItemId.recipePaper, |
| 170 | lastSaleFor(ItemId.recipePaper), |
| 171 | now, |
| 172 | lerpDurationMs, |
| 173 | settings |
| 174 | ); |
| 175 | for (const potion of listAlchemyPotions()) |
| 176 | { |
| 177 | const listingKey = recipeListingKey(potion.potionKey); |
| 178 | if (!listingKey) |
| 179 | { continue; } |
| 180 | catalog.push({ |
| 181 | itemId: ItemId.recipePaper, |
| 182 | listingKey, |
| 183 | displayName: recipePaperDisplayName(potion.potionKey), |
| 184 | hintPrice: recipeHint |
| 185 | }); |
| 186 | } |
| 187 | } |
| 188 | catalog.sort((a, b) => a.displayName.localeCompare(b.displayName)); |
| 189 | return catalog; |
| 190 | } |