BlueGit

timfa questward

Merge branch 'feature/ce-potion-listings'

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
5a1d2bc908a9ab7269bc642fb55b746b6dfc301a
5 files changed +256 −29
client/src/editor/giftsModal.js +3 −1
@@ -2,7 +2,8 @@ import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/defini
2 2 import {
3 3 listRecipePaperPickerEntries,
4 4 listPotionPickerEntries,
5 potionKeyFromListingKey
5 potionKeyFromListingKey,
6 potionKeyFromPotionListingKey
6 7 } from "/shared/market/listingKey.js";
7 8 import {
8 9 formatGiftLetterBody,
@@ -93,6 +94,7 @@ export function initGiftsModal ({ socket, MessageType })
93 94 const listingKey = item.listingKey ?? null;
94 95 const potionKey = item.potionKey
95 96 ?? potionKeyFromListingKey(listingKey)
97 ?? potionKeyFromPotionListingKey(listingKey)
96 98 ?? null;
97 99 const key = giftRowKey({ itemId: item.itemId, listingKey, potionKey });
98 100 const existing = giftItems.find((row) => giftRowKey(row) === key);
@@ -2,7 +2,8 @@ import { listAllItemPickerEntries, itemDefinition, ItemId } from "/shared/defini
2import {
2import {
3 listRecipePaperPickerEntries,
3 listRecipePaperPickerEntries,
4 listPotionPickerEntries,
4 listPotionPickerEntries,
5 potionKeyFromListingKey
5 potionKeyFromListingKey,
6 potionKeyFromPotionListingKey
6} from "/shared/market/listingKey.js";
7} from "/shared/market/listingKey.js";
7import {
8import {
8 formatGiftLetterBody,
9 formatGiftLetterBody,
@@ -93,6 +94,7 @@ export function initGiftsModal ({ socket, MessageType })
93 const listingKey = item.listingKey ?? null;
94 const listingKey = item.listingKey ?? null;
94 const potionKey = item.potionKey
95 const potionKey = item.potionKey
95 ?? potionKeyFromListingKey(listingKey)
96 ?? potionKeyFromListingKey(listingKey)
97 ?? potionKeyFromPotionListingKey(listingKey)
96 ?? null;
98 ?? null;
97 const key = giftRowKey({ itemId: item.itemId, listingKey, potionKey });
99 const key = giftRowKey({ itemId: item.itemId, listingKey, potionKey });
98 const existing = giftItems.find((row) => giftRowKey(row) === key);
100 const existing = giftItems.find((row) => giftRowKey(row) === key);
server/src/gifts/service.js +5 −1
@@ -5,6 +5,7 @@ import { recipePaperBody, potionItemInstance } from "../../../shared/alchemy/rec
5 5 import {
6 6 normalizeBuyListingKey,
7 7 potionKeyFromListingKey,
8 potionKeyFromPotionListingKey,
8 9 recipePaperDisplayName
9 10 } from "../../../shared/market/listingKey.js";
10 11 import { insertStackGrowable, normalizeVault } from "../../../shared/items/inventory.js";
@@ -290,7 +291,10 @@ function normalizeGiftItems (raw)
290 291 }
291 292 if (itemId === ItemId.potion)
292 293 {
293 const potionKey = String(row?.potionKey ?? "").trim();
294 const listingKey = normalizeBuyListingKey(itemId, row?.listingKey ?? null);
295 const potionKey = String(row?.potionKey ?? "").trim()
296 || potionKeyFromPotionListingKey(listingKey)
297 || "";
294 298 const potion = potionKey ? alchemyPotionDefinition(potionKey) : null;
295 299 if (!potion || potion.outputItemId)
296 300 { continue; }
@@ -5,6 +5,7 @@ import { recipePaperBody, potionItemInstance } from "../../../shared/alchemy/rec
5import {
5import {
6 normalizeBuyListingKey,
6 normalizeBuyListingKey,
7 potionKeyFromListingKey,
7 potionKeyFromListingKey,
8 potionKeyFromPotionListingKey,
8 recipePaperDisplayName
9 recipePaperDisplayName
9} from "../../../shared/market/listingKey.js";
10} from "../../../shared/market/listingKey.js";
10import { insertStackGrowable, normalizeVault } from "../../../shared/items/inventory.js";
11import { insertStackGrowable, normalizeVault } from "../../../shared/items/inventory.js";
@@ -290,7 +291,10 @@ function normalizeGiftItems (raw)
290 }
291 }
291 if (itemId === ItemId.potion)
292 if (itemId === ItemId.potion)
292 {
293 {
293 const potionKey = String(row?.potionKey ?? "").trim();
294 const listingKey = normalizeBuyListingKey(itemId, row?.listingKey ?? null);
295 const potionKey = String(row?.potionKey ?? "").trim()
296 || potionKeyFromPotionListingKey(listingKey)
297 || "";
294 const potion = potionKey ? alchemyPotionDefinition(potionKey) : null;
298 const potion = potionKey ? alchemyPotionDefinition(potionKey) : null;
295 if (!potion || potion.outputItemId)
299 if (!potion || potion.outputItemId)
296 { continue; }
300 { continue; }
server/src/market/service.js +9 −2
@@ -29,6 +29,7 @@ import {
29 29 buildMarketCatalogEntries,
30 30 listingKeyFromOrder,
31 31 listingKeyFromStack,
32 marketBuyRequiresListingKey,
32 33 marketOrderDisplayName,
33 34 marketOrdersCompatible,
34 35 normalizeBuyListingKey
@@ -311,8 +312,14 @@ export class MarketService
311 312 if ((itemId | 0) === ItemId.coins)
312 313 { throw userError("Coins cannot be ordered on the marketplace."); }
313 314 const normalizedListingKey = normalizeBuyListingKey(itemId, listingKey);
314 if ((itemId | 0) === ItemId.recipePaper && !normalizedListingKey)
315 { throw userError("Choose which recipe to buy."); }
315 if (marketBuyRequiresListingKey(itemId) && !normalizedListingKey)
316 {
317 throw userError(
318 (itemId | 0) === ItemId.recipePaper
319 ? "Choose which recipe to buy."
320 : "Choose which potion to buy."
321 );
322 }
316 323 const escrow = maxPrice * qty;
317 324 const fee = marketOrderListingFee(maxPrice, qty);
318 325 const bag = this.#loadBag(characterId);
@@ -29,6 +29,7 @@ import {
29 buildMarketCatalogEntries,
29 buildMarketCatalogEntries,
30 listingKeyFromOrder,
30 listingKeyFromOrder,
31 listingKeyFromStack,
31 listingKeyFromStack,
32 marketBuyRequiresListingKey,
32 marketOrderDisplayName,
33 marketOrderDisplayName,
33 marketOrdersCompatible,
34 marketOrdersCompatible,
34 normalizeBuyListingKey
35 normalizeBuyListingKey
@@ -311,8 +312,14 @@ export class MarketService
311 if ((itemId | 0) === ItemId.coins)
312 if ((itemId | 0) === ItemId.coins)
312 { throw userError("Coins cannot be ordered on the marketplace."); }
313 { throw userError("Coins cannot be ordered on the marketplace."); }
313 const normalizedListingKey = normalizeBuyListingKey(itemId, listingKey);
314 const normalizedListingKey = normalizeBuyListingKey(itemId, listingKey);
314 if ((itemId | 0) === ItemId.recipePaper && !normalizedListingKey)
315 if (marketBuyRequiresListingKey(itemId) && !normalizedListingKey)
315 { throw userError("Choose which recipe to buy."); }
316 {
317 throw userError(
318 (itemId | 0) === ItemId.recipePaper
319 ? "Choose which recipe to buy."
320 : "Choose which potion to buy."
321 );
322 }
316 const escrow = maxPrice * qty;
323 const escrow = maxPrice * qty;
317 const fee = marketOrderListingFee(maxPrice, qty);
324 const fee = marketOrderListingFee(maxPrice, qty);
318 const bag = this.#loadBag(characterId);
325 const bag = this.#loadBag(characterId);
server/tests/marketplace.test.js +126 −1
@@ -14,8 +14,10 @@ import {
14 14 buildMarketCatalogEntries,
15 15 marketOrdersCompatible,
16 16 recipeListingKey,
17 recipePaperDisplayName
17 recipePaperDisplayName,
18 potionListingKey
18 19 } from "../../shared/market/listingKey.js";
20 import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js";
19 21 import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";
20 22 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
21 23 import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
@@ -261,6 +263,28 @@ test("market catalog lists each alchemy recipe paper separately", () =>
261 263 assert.equal(recipes.some((entry) => !entry.listingKey), false);
262 264 });
263 265
266 test("market catalog lists each drinkable potion brew separately", () =>
267 {
268 const catalog = buildMarketCatalogEntries({
269 lastSaleFor: () => null,
270 undiscountedMarketCoins: (itemId) => (itemId === ItemId.potion ? 7 : 10),
271 now: Date.now(),
272 lerpDurationMs: 0,
273 settings: {}
274 });
275 const potions = catalog.filter((entry) => entry.itemId === ItemId.potion);
276 assert.ok(potions.length > 1);
277 assert.ok(potions.some((entry) => entry.listingKey === potionListingKey("lesserHealthTonic")));
278 assert.ok(potions.some((entry) => entry.displayName === "Lesser health tonic"));
279 assert.equal(potions.some((entry) => !entry.listingKey), false);
280 assert.equal(potions.some((entry) => entry.displayName === "Potion"), false);
281 assert.equal(
282 potions.some((entry) => entry.listingKey === potionListingKey("tanningBrew")),
283 false,
284 "solid tanning brew output stays its own item id"
285 );
286 });
287
264 288 test("recipe paper sell listings carry listing keys", () =>
265 289 {
266 290 const stack = recipePaperStack("lesserHealthTonic");
@@ -271,6 +295,28 @@ test("recipe paper sell listings carry listing keys", () =>
271 295 assert.equal(listing.quantityLocked, true);
272 296 });
273 297
298 test("potion sell listings carry brew listing keys", () =>
299 {
300 const potion = alchemyPotionDefinition("lesserHealthTonic");
301 const stack = {
302 itemId: ItemId.potion,
303 quantity: 3,
304 instance: potionItemInstance({
305 potionKey: potion.potionKey,
306 displayName: potion.displayName,
307 color: potion.color,
308 fillLevel: potion.fillLevel,
309 effect: potion.effect,
310 effectKey: potion.effect?.type
311 })
312 };
313 const listing = resolveMarketSellListing(stack);
314 assert.ok(listing);
315 assert.equal(listing.listingKey, potionListingKey("lesserHealthTonic"));
316 assert.equal(listing.displayName, "Lesser health tonic");
317 assert.equal(listing.quantityLocked, false);
318 });
319
274 320 test("recipe paper buy and sell orders only match the same recipe", () =>
275 321 {
276 322 const lesserBuy = {
@@ -356,6 +402,85 @@ test("matching recipe paper buy and sell orders trade the specific recipe", () =
356 402 { harness.close(); }
357 403 });
358 404
405 test("matching potion buy and sell orders trade the specific brew", () =>
406 {
407 const harness = makeSimulation({ workerCount: 1 });
408 try
409 {
410 placePlaza(harness.worldDb);
411 harness.simulation.reloadWorld();
412
413 const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Alchem", lastName: "One" });
414 const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buyer", lastName: "Two" });
415 const potion = alchemyPotionDefinition("lesserHealthTonic");
416 const sellerBag = addCoinsToSlots(emptySlots(), 5);
417 assert.equal(sellerBag.ok, true);
418 sellerBag.slots[1] = {
419 itemId: ItemId.potion,
420 quantity: 2,
421 instance: potionItemInstance({
422 potionKey: potion.potionKey,
423 displayName: potion.displayName,
424 color: potion.color,
425 fillLevel: potion.fillLevel,
426 effect: potion.effect,
427 effectKey: potion.effect?.type
428 })
429 };
430 harness.stateDb.saveBag(seller.character_id, {
431 inventory: sellerBag.slots,
432 equipment: emptyEquipment(),
433 vault: [],
434 vaultReserved: []
435 });
436 const buyerBag = addCoinsToSlots(emptySlots(), 100);
437 assert.equal(buyerBag.ok, true);
438 harness.stateDb.saveBag(buyer.character_id, {
439 inventory: buyerBag.slots,
440 equipment: emptyEquipment(),
441 vault: [],
442 vaultReserved: []
443 });
444
445 harness.simulation.spawnCharacter(1, seller);
446 harness.simulation.spawnCharacter(2, buyer);
447 harness.simulation.step();
448
449 harness.simulation.enqueue(1, {
450 type: "place-market-sell",
451 characterId: seller.character_id,
452 slotIndex: 1,
453 quantity: 2,
454 minUnitPrice: 8
455 });
456 const sellResult = harness.simulation.step();
457 assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events));
458
459 harness.simulation.enqueue(2, {
460 type: "place-market-buy",
461 characterId: buyer.character_id,
462 itemId: ItemId.potion,
463 listingKey: potionListingKey("lesserHealthTonic"),
464 quantity: 2,
465 maxUnitPrice: 12
466 });
467 const buyResult = harness.simulation.step();
468 assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events));
469 const trade = buyResult.events.find((event) => event.type === "market-trade");
470 assert.ok(trade, JSON.stringify(buyResult.events));
471 assert.equal(trade.quantity, 2);
472
473 const buyerBagNow = harness.simulation.bagOf(2);
474 const brew = (buyerBagNow.inventory ?? []).find((slot) =>
475 slot?.itemId === ItemId.potion && slot.instance?.potionKey === "lesserHealthTonic"
476 );
477 assert.ok(brew);
478 assert.equal(brew.quantity, 2);
479 }
480 finally
481 { harness.close(); }
482 });
483
359 484 test("recipe paper buy orders for a different recipe do not match", () =>
360 485 {
361 486 const harness = makeSimulation({ workerCount: 1 });
@@ -14,8 +14,10 @@ import {
14 buildMarketCatalogEntries,
14 buildMarketCatalogEntries,
15 marketOrdersCompatible,
15 marketOrdersCompatible,
16 recipeListingKey,
16 recipeListingKey,
17 recipePaperDisplayName
17 recipePaperDisplayName,
18 potionListingKey
18} from "../../shared/market/listingKey.js";
19} from "../../shared/market/listingKey.js";
20import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js";
19import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";
21import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";
20import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
22import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
21import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
23import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
@@ -261,6 +263,28 @@ test("market catalog lists each alchemy recipe paper separately", () =>
261 assert.equal(recipes.some((entry) => !entry.listingKey), false);
263 assert.equal(recipes.some((entry) => !entry.listingKey), false);
262});
264});
263
265
266test("market catalog lists each drinkable potion brew separately", () =>
267{
268 const catalog = buildMarketCatalogEntries({
269 lastSaleFor: () => null,
270 undiscountedMarketCoins: (itemId) => (itemId === ItemId.potion ? 7 : 10),
271 now: Date.now(),
272 lerpDurationMs: 0,
273 settings: {}
274 });
275 const potions = catalog.filter((entry) => entry.itemId === ItemId.potion);
276 assert.ok(potions.length > 1);
277 assert.ok(potions.some((entry) => entry.listingKey === potionListingKey("lesserHealthTonic")));
278 assert.ok(potions.some((entry) => entry.displayName === "Lesser health tonic"));
279 assert.equal(potions.some((entry) => !entry.listingKey), false);
280 assert.equal(potions.some((entry) => entry.displayName === "Potion"), false);
281 assert.equal(
282 potions.some((entry) => entry.listingKey === potionListingKey("tanningBrew")),
283 false,
284 "solid tanning brew output stays its own item id"
285 );
286});
287
264test("recipe paper sell listings carry listing keys", () =>
288test("recipe paper sell listings carry listing keys", () =>
265{
289{
266 const stack = recipePaperStack("lesserHealthTonic");
290 const stack = recipePaperStack("lesserHealthTonic");
@@ -271,6 +295,28 @@ test("recipe paper sell listings carry listing keys", () =>
271 assert.equal(listing.quantityLocked, true);
295 assert.equal(listing.quantityLocked, true);
272});
296});
273
297
298test("potion sell listings carry brew listing keys", () =>
299{
300 const potion = alchemyPotionDefinition("lesserHealthTonic");
301 const stack = {
302 itemId: ItemId.potion,
303 quantity: 3,
304 instance: potionItemInstance({
305 potionKey: potion.potionKey,
306 displayName: potion.displayName,
307 color: potion.color,
308 fillLevel: potion.fillLevel,
309 effect: potion.effect,
310 effectKey: potion.effect?.type
311 })
312 };
313 const listing = resolveMarketSellListing(stack);
314 assert.ok(listing);
315 assert.equal(listing.listingKey, potionListingKey("lesserHealthTonic"));
316 assert.equal(listing.displayName, "Lesser health tonic");
317 assert.equal(listing.quantityLocked, false);
318});
319
274test("recipe paper buy and sell orders only match the same recipe", () =>
320test("recipe paper buy and sell orders only match the same recipe", () =>
275{
321{
276 const lesserBuy = {
322 const lesserBuy = {
@@ -356,6 +402,85 @@ test("matching recipe paper buy and sell orders trade the specific recipe", () =
356 { harness.close(); }
402 { harness.close(); }
357});
403});
358
404
405test("matching potion buy and sell orders trade the specific brew", () =>
406{
407 const harness = makeSimulation({ workerCount: 1 });
408 try
409 {
410 placePlaza(harness.worldDb);
411 harness.simulation.reloadWorld();
412
413 const seller = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Alchem", lastName: "One" });
414 const buyer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Buyer", lastName: "Two" });
415 const potion = alchemyPotionDefinition("lesserHealthTonic");
416 const sellerBag = addCoinsToSlots(emptySlots(), 5);
417 assert.equal(sellerBag.ok, true);
418 sellerBag.slots[1] = {
419 itemId: ItemId.potion,
420 quantity: 2,
421 instance: potionItemInstance({
422 potionKey: potion.potionKey,
423 displayName: potion.displayName,
424 color: potion.color,
425 fillLevel: potion.fillLevel,
426 effect: potion.effect,
427 effectKey: potion.effect?.type
428 })
429 };
430 harness.stateDb.saveBag(seller.character_id, {
431 inventory: sellerBag.slots,
432 equipment: emptyEquipment(),
433 vault: [],
434 vaultReserved: []
435 });
436 const buyerBag = addCoinsToSlots(emptySlots(), 100);
437 assert.equal(buyerBag.ok, true);
438 harness.stateDb.saveBag(buyer.character_id, {
439 inventory: buyerBag.slots,
440 equipment: emptyEquipment(),
441 vault: [],
442 vaultReserved: []
443 });
444
445 harness.simulation.spawnCharacter(1, seller);
446 harness.simulation.spawnCharacter(2, buyer);
447 harness.simulation.step();
448
449 harness.simulation.enqueue(1, {
450 type: "place-market-sell",
451 characterId: seller.character_id,
452 slotIndex: 1,
453 quantity: 2,
454 minUnitPrice: 8
455 });
456 const sellResult = harness.simulation.step();
457 assert.equal(sellResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(sellResult.events));
458
459 harness.simulation.enqueue(2, {
460 type: "place-market-buy",
461 characterId: buyer.character_id,
462 itemId: ItemId.potion,
463 listingKey: potionListingKey("lesserHealthTonic"),
464 quantity: 2,
465 maxUnitPrice: 12
466 });
467 const buyResult = harness.simulation.step();
468 assert.equal(buyResult.events.some((event) => event.type === "command-error"), false, JSON.stringify(buyResult.events));
469 const trade = buyResult.events.find((event) => event.type === "market-trade");
470 assert.ok(trade, JSON.stringify(buyResult.events));
471 assert.equal(trade.quantity, 2);
472
473 const buyerBagNow = harness.simulation.bagOf(2);
474 const brew = (buyerBagNow.inventory ?? []).find((slot) =>
475 slot?.itemId === ItemId.potion && slot.instance?.potionKey === "lesserHealthTonic"
476 );
477 assert.ok(brew);
478 assert.equal(brew.quantity, 2);
479 }
480 finally
481 { harness.close(); }
482});
483
359test("recipe paper buy orders for a different recipe do not match", () =>
484test("recipe paper buy orders for a different recipe do not match", () =>
360{
485{
361 const harness = makeSimulation({ workerCount: 1 });
486 const harness = makeSimulation({ workerCount: 1 });
shared/market/listingKey.js +113 −24
@@ -3,6 +3,7 @@ import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alch
3 3 import { canSellOnMarketplace } from "../items/marketplace.js";
4 4
5 5 export const RECIPE_LISTING_PREFIX = "recipe:";
6 export const POTION_LISTING_PREFIX = "potion:";
6 7
7 8 /** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */
8 9 export function recipeListingKey (potionKey)
@@ -13,6 +14,16 @@ export function recipeListingKey (potionKey)
13 14 return `${RECIPE_LISTING_PREFIX}${key}`;
14 15 }
15 16
17 /** Stable CE sub-type key for a bottled brew (`potion:lesserHealthTonic`, …). */
18 export function potionListingKey (potionKey)
19 {
20 const key = String(potionKey ?? "").trim();
21 if (!key)
22 { return null; }
23 return `${POTION_LISTING_PREFIX}${key}`;
24 }
25
26 /** Potion key from a `recipe:…` listing key (recipe papers). */
16 27 export function potionKeyFromListingKey (listingKey)
17 28 {
18 29 const text = String(listingKey ?? "");
@@ -21,14 +32,47 @@ export function potionKeyFromListingKey (listingKey)
21 32 return text.slice(RECIPE_LISTING_PREFIX.length) || null;
22 33 }
23 34
35 /** Potion key from a `potion:…` listing key (bottled brews). */
36 export function potionKeyFromPotionListingKey (listingKey)
37 {
38 const text = String(listingKey ?? "");
39 if (!text.startsWith(POTION_LISTING_PREFIX))
40 { return null; }
41 return text.slice(POTION_LISTING_PREFIX.length) || null;
42 }
43
44 /** Drinkable alchemy brew suitable for CE / gifts (not solid outputs). */
45 export function isMarketPotionBrew (potionKey)
46 {
47 const potion = alchemyPotionDefinition(potionKey);
48 if (!potion || potion.outputItemId)
49 { return false; }
50 const key = String(potion.potionKey ?? "").trim();
51 return !!key && !key.startsWith("unstable:");
52 }
53
24 54 /** Listing key carried on a bag stack (sell escrow) or null for normal goods. */
25 55 export function listingKeyFromStack (stack)
26 56 {
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);
57 const itemId = stack?.itemId | 0;
58 if (itemId === ItemId.recipePaper)
59 {
60 if (stack.instance?.kind !== "recipe-paper")
61 { return null; }
62 return recipeListingKey(stack.instance.potionKey);
63 }
64 if (itemId === ItemId.potion)
65 {
66 if (stack.instance?.kind !== "potion")
67 { return null; }
68 if (stack.instance.unstable)
69 { return null; }
70 const potionKey = String(stack.instance.potionKey ?? "").trim();
71 if (!isMarketPotionBrew(potionKey))
72 { return null; }
73 return potionListingKey(potionKey);
74 }
75 return null;
32 76 }
33 77
34 78 /** Resolved listing key on an open order (column or sell escrow). */
@@ -82,26 +126,26 @@ export function listRecipePaperPickerEntries ()
82 126 }
83 127
84 128 /**
85 * Gift / editor picker rows: one drinkable bottle per alchemy potion
129 * Gift / CE picker rows: one drinkable bottle per alchemy potion
86 130 * (skips solid outputs like tanning brew).
87 * @returns {Array<{ itemId: number, listingKey: null, potionKey: string, displayName: string, key: string }>}
131 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
88 132 */
89 133 export function listPotionPickerEntries ()
90 134 {
91 135 const entries = [];
92 136 for (const potion of listAlchemyPotions())
93 137 {
94 if (potion.outputItemId)
138 if (!isMarketPotionBrew(potion.potionKey))
95 139 { continue; }
96 const potionKey = String(potion.potionKey ?? "").trim();
97 if (!potionKey)
140 const listingKey = potionListingKey(potion.potionKey);
141 if (!listingKey)
98 142 { continue; }
99 143 entries.push({
100 144 itemId: ItemId.potion,
101 listingKey: null,
102 potionKey,
103 displayName: potion.displayName ?? potionKey,
104 key: `potion:${potionKey}`
145 listingKey,
146 potionKey: potion.potionKey,
147 displayName: potion.displayName ?? potion.potionKey,
148 key: `potion:${potion.potionKey}`
105 149 });
106 150 }
107 151 return entries;
@@ -114,24 +158,49 @@ export function marketOrderDisplayName (order)
114 158 {
115 159 return recipePaperDisplayName(potionKeyFromListingKey(listingKey));
116 160 }
161 if (listingKey?.startsWith(POTION_LISTING_PREFIX))
162 {
163 const potionKey = potionKeyFromPotionListingKey(listingKey);
164 return alchemyPotionDefinition(potionKey)?.displayName
165 ?? itemDefinition(ItemId.potion)?.displayName
166 ?? "Potion";
167 }
117 168 return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`;
118 169 }
119 170
120 171 export function normalizeBuyListingKey (itemId, listingKey)
121 172 {
122 173 const id = itemId | 0;
123 if (id !== ItemId.recipePaper)
124 { return null; }
125 174 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;
175 if (id === ItemId.recipePaper)
176 {
177 if (!key.startsWith(RECIPE_LISTING_PREFIX))
178 { return null; }
179 const potionKey = potionKeyFromListingKey(key);
180 if (!potionKey || !alchemyPotionDefinition(potionKey))
181 { return null; }
182 return key;
183 }
184 if (id === ItemId.potion)
185 {
186 if (!key.startsWith(POTION_LISTING_PREFIX))
187 { return null; }
188 const potionKey = potionKeyFromPotionListingKey(key);
189 if (!isMarketPotionBrew(potionKey))
190 { return null; }
191 return key;
192 }
193 return null;
194 }
195
196 /** Whether this itemId requires a specific listing key on buy orders. */
197 export function marketBuyRequiresListingKey (itemId)
198 {
199 const id = itemId | 0;
200 return id === ItemId.recipePaper || id === ItemId.potion;
132 201 }
133 202
134 /** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */
203 /** Buy-tab catalog rows (recipe papers / potions expand per known subtype). */
135 204 export function buildMarketCatalogEntries ({
136 205 lastSaleFor,
137 206 undiscountedMarketCoins,
@@ -147,7 +216,7 @@ export function buildMarketCatalogEntries ({
147 216 { continue; }
148 217 if (def.itemId === ItemId.coins)
149 218 { continue; }
150 if (def.itemId === ItemId.recipePaper)
219 if (def.itemId === ItemId.recipePaper || def.itemId === ItemId.potion)
151 220 { continue; }
152 221 catalog.push({
153 222 itemId: def.itemId,
@@ -185,6 +254,26 @@ export function buildMarketCatalogEntries ({
185 254 });
186 255 }
187 256 }
257 const potionDef = ITEM_DEFINITIONS[ItemId.potion];
258 if (potionDef && canSellOnMarketplace({ itemId: ItemId.potion, quantity: 1 }))
259 {
260 const potionHint = undiscountedMarketCoins(
261 ItemId.potion,
262 lastSaleFor(ItemId.potion),
263 now,
264 lerpDurationMs,
265 settings
266 );
267 for (const entry of listPotionPickerEntries())
268 {
269 catalog.push({
270 itemId: ItemId.potion,
271 listingKey: entry.listingKey,
272 displayName: entry.displayName,
273 hintPrice: potionHint
274 });
275 }
276 }
188 277 catalog.sort((a, b) => a.displayName.localeCompare(b.displayName));
189 278 return catalog;
190 279 }
@@ -3,6 +3,7 @@ import { alchemyPotionDefinition, listAlchemyPotions } from "../definitions/alch
3import { canSellOnMarketplace } from "../items/marketplace.js";
3import { canSellOnMarketplace } from "../items/marketplace.js";
4
4
5export const RECIPE_LISTING_PREFIX = "recipe:";
5export const RECIPE_LISTING_PREFIX = "recipe:";
6export const POTION_LISTING_PREFIX = "potion:";
6
7
7/** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */
8/** Stable CE sub-type key for a written recipe paper (`recipe:lesserHealthTonic`, …). */
8export function recipeListingKey (potionKey)
9export function recipeListingKey (potionKey)
@@ -13,6 +14,16 @@ export function recipeListingKey (potionKey)
13 return `${RECIPE_LISTING_PREFIX}${key}`;
14 return `${RECIPE_LISTING_PREFIX}${key}`;
14}
15}
15
16
17/** Stable CE sub-type key for a bottled brew (`potion:lesserHealthTonic`, …). */
18export function potionListingKey (potionKey)
19{
20 const key = String(potionKey ?? "").trim();
21 if (!key)
22 { return null; }
23 return `${POTION_LISTING_PREFIX}${key}`;
24}
25
26/** Potion key from a `recipe:…` listing key (recipe papers). */
16export function potionKeyFromListingKey (listingKey)
27export function potionKeyFromListingKey (listingKey)
17{
28{
18 const text = String(listingKey ?? "");
29 const text = String(listingKey ?? "");
@@ -21,14 +32,47 @@ export function potionKeyFromListingKey (listingKey)
21 return text.slice(RECIPE_LISTING_PREFIX.length) || null;
32 return text.slice(RECIPE_LISTING_PREFIX.length) || null;
22}
33}
23
34
35/** Potion key from a `potion:…` listing key (bottled brews). */
36export function potionKeyFromPotionListingKey (listingKey)
37{
38 const text = String(listingKey ?? "");
39 if (!text.startsWith(POTION_LISTING_PREFIX))
40 { return null; }
41 return text.slice(POTION_LISTING_PREFIX.length) || null;
42}
43
44/** Drinkable alchemy brew suitable for CE / gifts (not solid outputs). */
45export function isMarketPotionBrew (potionKey)
46{
47 const potion = alchemyPotionDefinition(potionKey);
48 if (!potion || potion.outputItemId)
49 { return false; }
50 const key = String(potion.potionKey ?? "").trim();
51 return !!key && !key.startsWith("unstable:");
52}
53
24/** Listing key carried on a bag stack (sell escrow) or null for normal goods. */
54/** Listing key carried on a bag stack (sell escrow) or null for normal goods. */
25export function listingKeyFromStack (stack)
55export function listingKeyFromStack (stack)
26{
56{
27 if ((stack?.itemId | 0) !== ItemId.recipePaper)
57 const itemId = stack?.itemId | 0;
28 { return null; }
58 if (itemId === ItemId.recipePaper)
29 if (stack.instance?.kind !== "recipe-paper")
59 {
30 { return null; }
60 if (stack.instance?.kind !== "recipe-paper")
31 return recipeListingKey(stack.instance.potionKey);
61 { return null; }
62 return recipeListingKey(stack.instance.potionKey);
63 }
64 if (itemId === ItemId.potion)
65 {
66 if (stack.instance?.kind !== "potion")
67 { return null; }
68 if (stack.instance.unstable)
69 { return null; }
70 const potionKey = String(stack.instance.potionKey ?? "").trim();
71 if (!isMarketPotionBrew(potionKey))
72 { return null; }
73 return potionListingKey(potionKey);
74 }
75 return null;
32}
76}
33
77
34/** Resolved listing key on an open order (column or sell escrow). */
78/** Resolved listing key on an open order (column or sell escrow). */
@@ -82,26 +126,26 @@ export function listRecipePaperPickerEntries ()
82}
126}
83
127
84/**
128/**
85 * Gift / editor picker rows: one drinkable bottle per alchemy potion
129 * Gift / CE picker rows: one drinkable bottle per alchemy potion
86 * (skips solid outputs like tanning brew).
130 * (skips solid outputs like tanning brew).
87 * @returns {Array<{ itemId: number, listingKey: null, potionKey: string, displayName: string, key: string }>}
131 * @returns {Array<{ itemId: number, listingKey: string, potionKey: string, displayName: string, key: string }>}
88 */
132 */
89export function listPotionPickerEntries ()
133export function listPotionPickerEntries ()
90{
134{
91 const entries = [];
135 const entries = [];
92 for (const potion of listAlchemyPotions())
136 for (const potion of listAlchemyPotions())
93 {
137 {
94 if (potion.outputItemId)
138 if (!isMarketPotionBrew(potion.potionKey))
95 { continue; }
139 { continue; }
96 const potionKey = String(potion.potionKey ?? "").trim();
140 const listingKey = potionListingKey(potion.potionKey);
97 if (!potionKey)
141 if (!listingKey)
98 { continue; }
142 { continue; }
99 entries.push({
143 entries.push({
100 itemId: ItemId.potion,
144 itemId: ItemId.potion,
101 listingKey: null,
145 listingKey,
102 potionKey,
146 potionKey: potion.potionKey,
103 displayName: potion.displayName ?? potionKey,
147 displayName: potion.displayName ?? potion.potionKey,
104 key: `potion:${potionKey}`
148 key: `potion:${potion.potionKey}`
105 });
149 });
106 }
150 }
107 return entries;
151 return entries;
@@ -114,24 +158,49 @@ export function marketOrderDisplayName (order)
114 {
158 {
115 return recipePaperDisplayName(potionKeyFromListingKey(listingKey));
159 return recipePaperDisplayName(potionKeyFromListingKey(listingKey));
116 }
160 }
161 if (listingKey?.startsWith(POTION_LISTING_PREFIX))
162 {
163 const potionKey = potionKeyFromPotionListingKey(listingKey);
164 return alchemyPotionDefinition(potionKey)?.displayName
165 ?? itemDefinition(ItemId.potion)?.displayName
166 ?? "Potion";
167 }
117 return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`;
168 return itemDefinition(order?.itemId)?.displayName ?? `Item ${order?.itemId ?? "?"}`;
118}
169}
119
170
120export function normalizeBuyListingKey (itemId, listingKey)
171export function normalizeBuyListingKey (itemId, listingKey)
121{
172{
122 const id = itemId | 0;
173 const id = itemId | 0;
123 if (id !== ItemId.recipePaper)
124 { return null; }
125 const key = String(listingKey ?? "").trim();
174 const key = String(listingKey ?? "").trim();
126 if (!key.startsWith(RECIPE_LISTING_PREFIX))
175 if (id === ItemId.recipePaper)
127 { return null; }
176 {
128 const potionKey = potionKeyFromListingKey(key);
177 if (!key.startsWith(RECIPE_LISTING_PREFIX))
129 if (!potionKey || !alchemyPotionDefinition(potionKey))
178 { return null; }
130 { return null; }
179 const potionKey = potionKeyFromListingKey(key);
131 return key;
180 if (!potionKey || !alchemyPotionDefinition(potionKey))
181 { return null; }
182 return key;
183 }
184 if (id === ItemId.potion)
185 {
186 if (!key.startsWith(POTION_LISTING_PREFIX))
187 { return null; }
188 const potionKey = potionKeyFromPotionListingKey(key);
189 if (!isMarketPotionBrew(potionKey))
190 { return null; }
191 return key;
192 }
193 return null;
194}
195
196/** Whether this itemId requires a specific listing key on buy orders. */
197export function marketBuyRequiresListingKey (itemId)
198{
199 const id = itemId | 0;
200 return id === ItemId.recipePaper || id === ItemId.potion;
132}
201}
133
202
134/** Buy-tab catalog rows (recipe papers expand to one entry per known potion recipe). */
203/** Buy-tab catalog rows (recipe papers / potions expand per known subtype). */
135export function buildMarketCatalogEntries ({
204export function buildMarketCatalogEntries ({
136 lastSaleFor,
205 lastSaleFor,
137 undiscountedMarketCoins,
206 undiscountedMarketCoins,
@@ -147,7 +216,7 @@ export function buildMarketCatalogEntries ({
147 { continue; }
216 { continue; }
148 if (def.itemId === ItemId.coins)
217 if (def.itemId === ItemId.coins)
149 { continue; }
218 { continue; }
150 if (def.itemId === ItemId.recipePaper)
219 if (def.itemId === ItemId.recipePaper || def.itemId === ItemId.potion)
151 { continue; }
220 { continue; }
152 catalog.push({
221 catalog.push({
153 itemId: def.itemId,
222 itemId: def.itemId,
@@ -185,6 +254,26 @@ export function buildMarketCatalogEntries ({
185 });
254 });
186 }
255 }
187 }
256 }
257 const potionDef = ITEM_DEFINITIONS[ItemId.potion];
258 if (potionDef && canSellOnMarketplace({ itemId: ItemId.potion, quantity: 1 }))
259 {
260 const potionHint = undiscountedMarketCoins(
261 ItemId.potion,
262 lastSaleFor(ItemId.potion),
263 now,
264 lerpDurationMs,
265 settings
266 );
267 for (const entry of listPotionPickerEntries())
268 {
269 catalog.push({
270 itemId: ItemId.potion,
271 listingKey: entry.listingKey,
272 displayName: entry.displayName,
273 hintPrice: potionHint
274 });
275 }
276 }
188 catalog.sort((a, b) => a.displayName.localeCompare(b.displayName));
277 catalog.sort((a, b) => a.displayName.localeCompare(b.displayName));
189 return catalog;
278 return catalog;
190}
279}