Expand CE buy/sell listings for specific potion brews.
Mirror recipe papers: catalog each drinkable alchemy potion under a potion: listing key so buy orders match the right bottle.
@@ -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/defini2
import {2
import {3
listRecipePaperPickerEntries,3
listRecipePaperPickerEntries,4
listPotionPickerEntries,4
listPotionPickerEntries,5
potionKeyFromListingKey5
potionKeyFromListingKey,6
potionKeyFromPotionListingKey6
} from "/shared/market/listingKey.js";7
} from "/shared/market/listingKey.js";7
import {8
import {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.potionKey95
const potionKey = item.potionKey95
?? 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);@@ -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/rec5
import {5
import {6
normalizeBuyListingKey,6
normalizeBuyListingKey,7
potionKeyFromListingKey,7
potionKeyFromListingKey,8
potionKeyFromPotionListingKey,8
recipePaperDisplayName9
recipePaperDisplayName9
} from "../../../shared/market/listingKey.js";10
} from "../../../shared/market/listingKey.js";10
import { insertStackGrowable, normalizeVault } from "../../../shared/items/inventory.js";11
import { 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; }@@ -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
normalizeBuyListingKey35
normalizeBuyListingKey@@ -311,8 +312,14 @@ export class MarketService311
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.recipePaper319
? "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);@@ -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
recipePaperDisplayName17
recipePaperDisplayName,18
potionListingKey18
} from "../../shared/market/listingKey.js";19
} from "../../shared/market/listingKey.js";20
import { alchemyPotionDefinition } from "../../shared/definitions/alchemy.js";19
import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";21
import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";20
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";22
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";21
import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";23
import { 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
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
test("recipe paper sell listings carry listing keys", () =>288
test("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
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?.type311
})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
test("recipe paper buy and sell orders only match the same recipe", () =>320
test("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
405
test("matching potion buy and sell orders trade the specific brew", () =>406
{407
const harness = makeSimulation({ workerCount: 1 });408
try409
{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?.type428
})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: 8455
});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: 12466
});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
finally481
{ harness.close(); }482
});483
359
test("recipe paper buy orders for a different recipe do not match", () =>484
test("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 });