import { ItemId, itemDefinition, ITEM_DEFINITIONS, itemDisplayName } from "../definitions/items.js"; import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alchemy.js"; import { MAGIC_ELEMENT_ORDER } from "../definitions/attackTypes.js"; import { FLOWER_COLOR_ORDER, FLOWER_COLOR_DISPLAY, DyeItemId } from "../definitions/dyes.js"; import { ItemFamilyId } from "../definitions/tierFamilies.js"; import { canSellOnMarketplace } from "../items/marketplace.js"; export const RECIPE_LISTING_PREFIX = "recipe:"; export const POTION_LISTING_PREFIX = "potion:"; export const FLOWER_LISTING_PREFIX = "flower:"; export const ARROW_LISTING_PREFIX = "arrow:"; /** Shared CE key for any full unstable draught (buyer gets a surprise effect). */ export const UNSTABLE_POTION_LISTING_KEY = `${POTION_LISTING_PREFIX}unstable`; /** 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}`; } /** Stable CE sub-type key for a bottled brew (`potion:lesserHealthTonic`, …). */ export function potionListingKey (potionKey) { const key = String(potionKey ?? "").trim(); if (!key) { return null; } return `${POTION_LISTING_PREFIX}${key}`; } export function flowerListingKey (colorId) { const id = String(colorId ?? "").trim(); if (!FLOWER_COLOR_ORDER.includes(id)) { return null; } return `${FLOWER_LISTING_PREFIX}${id}`; } export function arrowListingKey (element) { const el = String(element ?? "").trim(); if (!MAGIC_ELEMENT_ORDER.includes(el)) { return null; } return `${ARROW_LISTING_PREFIX}${el}`; } /** Potion key from a `recipe:…` listing key (recipe papers). */ export function potionKeyFromListingKey (listingKey) { const text = String(listingKey ?? ""); if (!text.startsWith(RECIPE_LISTING_PREFIX)) { return null; } return text.slice(RECIPE_LISTING_PREFIX.length) || null; } /** Potion key from a `potion:…` listing key (bottled brews). */ export function potionKeyFromPotionListingKey (listingKey) { const text = String(listingKey ?? ""); if (!text.startsWith(POTION_LISTING_PREFIX)) { return null; } return text.slice(POTION_LISTING_PREFIX.length) || null; } export function flowerColorIdFromListingKey (listingKey) { const text = String(listingKey ?? ""); if (!text.startsWith(FLOWER_LISTING_PREFIX)) { return null; } const colorId = text.slice(FLOWER_LISTING_PREFIX.length); return FLOWER_COLOR_ORDER.includes(colorId) ? colorId : null; } export function arrowElementFromListingKey (listingKey) { const text = String(listingKey ?? ""); if (!text.startsWith(ARROW_LISTING_PREFIX)) { return null; } const element = text.slice(ARROW_LISTING_PREFIX.length); return MAGIC_ELEMENT_ORDER.includes(element) ? element : null; } export function isArrowItemId (itemId) { return itemDefinition(itemId)?.familyId === ItemFamilyId.arrow; } export function isFlowerItemId (itemId) { return (itemId | 0) === ItemId.flower || (itemId | 0) === DyeItemId.flower; } /** Drinkable alchemy brew suitable for CE / gifts (not solid outputs, not unstable). */ export function isMarketPotionBrew (potionKey) { const potion = alchemyPotionDefinition(potionKey); if (!potion || potion.outputItemId) { return false; } const key = String(potion.potionKey ?? "").trim(); return !!key && !key.startsWith("unstable:"); } export function isUnstablePotionStack (stack) { if ((stack?.itemId | 0) !== ItemId.potion || stack?.instance?.kind !== "potion") { return false; } if (stack.instance.unstable) { return true; } return String(stack.instance.potionKey ?? "").startsWith("unstable:"); } /** Listing key carried on a bag stack (sell escrow) or null for normal goods. */ export function listingKeyFromStack (stack) { const itemId = stack?.itemId | 0; if (itemId === ItemId.recipePaper) { if (stack.instance?.kind !== "recipe-paper") { return null; } return recipeListingKey(stack.instance.potionKey); } if (itemId === ItemId.potion) { if (stack.instance?.kind !== "potion") { return null; } if (isUnstablePotionStack(stack)) { return UNSTABLE_POTION_LISTING_KEY; } const potionKey = String(stack.instance.potionKey ?? "").trim(); if (!isMarketPotionBrew(potionKey)) { return null; } return potionListingKey(potionKey); } if (isFlowerItemId(itemId)) { if (stack.instance?.kind !== "flower") { return null; } return flowerListingKey(stack.instance.colorId); } if (isArrowItemId(itemId) && stack.instance?.kind === "tipped-arrow") { return arrowListingKey(stack.instance.element); } // Dyeable clothing keeps listingKey null so dyed and undyed match (buyer colour surprise). return null; } /** 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`; } export function flowerListingDisplayName (colorId) { const label = FLOWER_COLOR_DISPLAY[colorId] ?? "Flower"; return `${label} flower`; } export function tippedArrowListingDisplayName (itemId, element) { return itemDisplayName({ itemId, quantity: 1, instance: { kind: "tipped-arrow", element } }); } /** * 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; } /** * Gift / CE picker rows: one drinkable bottle per alchemy potion * (skips solid outputs like tanning brew). * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>} */ export function listPotionPickerEntries () { const entries = []; for (const potion of listAlchemyPotions()) { if (!isMarketPotionBrew(potion.potionKey)) { continue; } const listingKey = potionListingKey(potion.potionKey); if (!listingKey) { continue; } entries.push({ itemId: ItemId.potion, listingKey, potionKey: potion.potionKey, displayName: potion.displayName ?? potion.potionKey, key: `potion:${potion.potionKey}` }); } return entries; } export function listFlowerPickerEntries () { return FLOWER_COLOR_ORDER.map((colorId) => ({ itemId: ItemId.flower, listingKey: flowerListingKey(colorId), colorId, displayName: flowerListingDisplayName(colorId), key: `flower:${colorId}` })); } export function marketOrderDisplayName (order) { const listingKey = listingKeyFromOrder(order); if (listingKey?.startsWith(RECIPE_LISTING_PREFIX)) { return recipePaperDisplayName(potionKeyFromListingKey(listingKey)); } if (listingKey === UNSTABLE_POTION_LISTING_KEY) { return "Unstable draught"; } if (listingKey?.startsWith(POTION_LISTING_PREFIX)) { const potionKey = potionKeyFromPotionListingKey(listingKey); return alchemyPotionDefinition(potionKey)?.displayName ?? itemDefinition(ItemId.potion)?.displayName ?? "Potion"; } if (listingKey?.startsWith(FLOWER_LISTING_PREFIX)) { const colorId = flowerColorIdFromListingKey(listingKey); return colorId ? flowerListingDisplayName(colorId) : "Flower"; } if (listingKey?.startsWith(ARROW_LISTING_PREFIX)) { const element = arrowElementFromListingKey(listingKey); if (element && order?.itemId) { return tippedArrowListingDisplayName(order.itemId, element); } } if (order?.escrowStack) { return itemDisplayName(order.escrowStack); } return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`; } export function normalizeBuyListingKey (itemId, listingKey) { const id = itemId | 0; const key = String(listingKey ?? "").trim(); if (!key) { return null; } if (id === ItemId.recipePaper) { if (!key.startsWith(RECIPE_LISTING_PREFIX)) { return null; } const potionKey = potionKeyFromListingKey(key); if (!potionKey || !alchemyPotionDefinition(potionKey)) { return null; } return key; } if (id === ItemId.potion) { if (key === UNSTABLE_POTION_LISTING_KEY) { return UNSTABLE_POTION_LISTING_KEY; } if (!key.startsWith(POTION_LISTING_PREFIX)) { return null; } const potionKey = potionKeyFromPotionListingKey(key); if (!isMarketPotionBrew(potionKey)) { return null; } return key; } if (isFlowerItemId(id)) { const colorId = flowerColorIdFromListingKey(key); return colorId ? flowerListingKey(colorId) : null; } if (isArrowItemId(id)) { const element = arrowElementFromListingKey(key); return element ? arrowListingKey(element) : null; } return null; } /** Whether this itemId requires a specific listing key on buy orders. */ export function marketBuyRequiresListingKey (itemId) { const id = itemId | 0; return id === ItemId.recipePaper || id === ItemId.potion || isFlowerItemId(id); } export function marketBuyListingKeyError (itemId) { const id = itemId | 0; if (id === ItemId.recipePaper) { return "Choose which recipe to buy."; } if (id === ItemId.potion) { return "Choose which potion to buy."; } if (isFlowerItemId(id)) { return "Choose which flower colour to buy."; } return "Choose a specific listing to buy."; } function catalogHint (itemId, lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings) { return undiscountedMarketCoins( itemId, lastSaleFor(itemId), now, lerpDurationMs, settings ); } /** Buy-tab catalog rows (subtype expansions for recipes, potions, flowers, tipped arrows). */ 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 || def.itemId === ItemId.potion || isFlowerItemId(def.itemId) ) { continue; } const hint = catalogHint( def.itemId, lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings ); if (isArrowItemId(def.itemId)) { catalog.push({ itemId: def.itemId, listingKey: null, displayName: def.displayName, hintPrice: hint }); for (const element of MAGIC_ELEMENT_ORDER) { catalog.push({ itemId: def.itemId, listingKey: arrowListingKey(element), displayName: tippedArrowListingDisplayName(def.itemId, element), hintPrice: hint }); } continue; } catalog.push({ itemId: def.itemId, listingKey: null, displayName: def.displayName, hintPrice: hint }); } const recipeDef = ITEM_DEFINITIONS[ItemId.recipePaper]; if (recipeDef && canSellOnMarketplace({ itemId: ItemId.recipePaper, quantity: 1 })) { const recipeHint = catalogHint( ItemId.recipePaper, lastSaleFor, undiscountedMarketCoins, 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 }); } } const potionDef = ITEM_DEFINITIONS[ItemId.potion]; if (potionDef && canSellOnMarketplace({ itemId: ItemId.potion, quantity: 1 })) { const potionHint = catalogHint( ItemId.potion, lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings ); for (const entry of listPotionPickerEntries()) { catalog.push({ itemId: ItemId.potion, listingKey: entry.listingKey, displayName: entry.displayName, hintPrice: potionHint }); } catalog.push({ itemId: ItemId.potion, listingKey: UNSTABLE_POTION_LISTING_KEY, displayName: "Unstable draught", hintPrice: potionHint }); } const flowerDef = ITEM_DEFINITIONS[ItemId.flower]; if (flowerDef && canSellOnMarketplace({ itemId: ItemId.flower, quantity: 1 })) { const flowerHint = catalogHint( ItemId.flower, lastSaleFor, undiscountedMarketCoins, now, lerpDurationMs, settings ); for (const entry of listFlowerPickerEntries()) { catalog.push({ itemId: ItemId.flower, listingKey: entry.listingKey, displayName: entry.displayName, hintPrice: flowerHint }); } } catalog.sort((a, b) => a.displayName.localeCompare(b.displayName)); return catalog; }