import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js"; import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alchemy.js"; import { canSellOnMarketplace } from "../items/marketplace.js"; export const RECIPE_LISTING_PREFIX = "recipe:"; /** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */ export function recipeListingKey (potionKey) { const key = String(potionKey ?? "").trim(); if (!key) { return null; } return `${RECIPE_LISTING_PREFIX}${key}`; } export function potionKeyFromListingKey (listingKey) { const text = String(listingKey ?? ""); if (!text.startsWith(RECIPE_LISTING_PREFIX)) { return null; } return text.slice(RECIPE_LISTING_PREFIX.length) || null; } /** Listing key carried on a bag stack (sell escrow) or null for normal goods. */ export function listingKeyFromStack (stack) { if ((stack?.itemId | 0) !== ItemId.recipePaper) { return null; } if (stack.instance?.kind !== "recipe-paper") { return null; } return recipeListingKey(stack.instance.potionKey); } /** Resolved listing key on an open order (column or sell escrow). */ export function listingKeyFromOrder (order) { if (order?.listingKey) { return order.listingKey; } return listingKeyFromStack(order?.escrowStack) ?? null; } export function marketOrdersCompatible (buy, sell) { if ((buy.itemId | 0) !== (sell.itemId | 0)) { return false; } const buyKey = listingKeyFromOrder(buy); const sellKey = listingKeyFromOrder(sell); if (!buyKey && !sellKey) { return true; } return buyKey === sellKey; } export function recipePaperDisplayName (potionKey) { const def = alchemyPotionDefinition(potionKey); if (!def) { return "Recipe paper"; } return `${def.displayName} recipe`; } /** * Editor / gift picker rows: one entry per alchemy recipe (same as CE buy catalog). * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>} */ export function listRecipePaperPickerEntries () { const entries = []; for (const potion of listAlchemyPotions()) { const listingKey = recipeListingKey(potion.potionKey); if (!listingKey) { continue; } entries.push({ itemId: ItemId.recipePaper, listingKey, potionKey: potion.potionKey, displayName: recipePaperDisplayName(potion.potionKey), key: `recipe-paper:${potion.potionKey}` }); } return entries; } export function marketOrderDisplayName (order) { const listingKey = listingKeyFromOrder(order); if (listingKey?.startsWith(RECIPE_LISTING_PREFIX)) { return recipePaperDisplayName(potionKeyFromListingKey(listingKey)); } return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`; } export function normalizeBuyListingKey (itemId, listingKey) { const id = itemId | 0; if (id !== ItemId.recipePaper) { return null; } const key = String(listingKey ?? "").trim(); if (!key.startsWith(RECIPE_LISTING_PREFIX)) { return null; } const potionKey = potionKeyFromListingKey(key); if (!potionKey || !alchemyPotionDefinition(potionKey)) { return null; } return key; } /** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */ export function buildMarketCatalogEntries ({ lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings }) { const catalog = []; for (const def of Object.values(ITEM_DEFINITIONS)) { if (!canSellOnMarketplace({ itemId: def.itemId, quantity: 1 })) { continue; } if (def.itemId === ItemId.coins) { continue; } if (def.itemId === ItemId.recipePaper) { continue; } catalog.push({ itemId: def.itemId, listingKey: null, displayName: def.displayName, hintPrice: undiscountedMarketCoins( def.itemId, lastSaleFor(def.itemId), now, lerpDurationMs, settings ) }); } const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper]; if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 })) { const recipeHint = undiscountedMarketCoins( ItemId.recipePaper, lastSaleFor(ItemId.recipePaper), now, lerpDurationMs, settings ); for (const potion of listAlchemyPotions()) { const listingKey = recipeListingKey(potion.potionKey); if (!listingKey) { continue; } catalog.push({ itemId: ItemId.recipePaper, listingKey, displayName: recipePaperDisplayName(potion.potionKey), hintPrice: recipeHint }); } } catalog.sort((a, b) => a.displayName.localeCompare(b.displayName)); return catalog; }