BlueGit

timfa questward

Block partially used potions and dyes from the Central Exchange.

CE matching is blind, so any fill or charge below the bottled/full amount is refused alongside worn tools.

timfa Tim Falken · pushed by timfa

Authored 2026-08-30 · 2 months ago

Hash
e5528d66dc668e4b520ba7e7c3867f6af074cd36
Parent
5491bf1f
4 files changed +106 −7
server/tests/marketplace.test.js +55 −0
@@ -2,6 +2,8 @@ import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { ItemId } from "../../shared/definitions/items.js";
4 4 import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
5 import { dyeItemInstance } from "../../shared/definitions/dyes.js";
6 import { potionItemInstance } from "../../shared/alchemy/recipes.js";
5 7 import { BlockType } from "../../shared/definitions/tiles.js";
6 8 import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
7 9 import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
@@ -59,6 +61,59 @@ test("fully charged tinderbox can list on CE; worn cannot", () =>
59 61 );
60 62 });
61 63
64 test("full potions and dyes can list on CE; sipped or used cannot", () =>
65 {
66 const fullPotion = {
67 itemId: ItemId.potion,
68 quantity: 1,
69 instance: potionItemInstance({
70 potionKey: "lesserHealthTonic",
71 displayName: "Lesser health tonic",
72 color: { r: 0.95, g: 0.38, b: 0.32 },
73 fillLevel: 4,
74 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
75 effectKey: "health-regen"
76 })
77 };
78 const sipped = {
79 ...fullPotion,
80 instance: { ...fullPotion.instance, fillLevel: 3 }
81 };
82 const fullDye = {
83 itemId: ItemId.dye,
84 quantity: 1,
85 instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 4)
86 };
87 const usedDye = {
88 itemId: ItemId.dye,
89 quantity: 1,
90 instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 2)
91 };
92 const unstableFull = {
93 itemId: ItemId.potion,
94 quantity: 1,
95 instance: potionItemInstance({
96 potionKey: "unstable:glow",
97 unstable: true,
98 displayName: "Unstable draught",
99 color: { r: 0.95, g: 0.9, b: 0.45 },
100 fillLevel: 3,
101 effectKey: "glow",
102 effect: { type: "glow", lightRadiusTiles: 2, durationMs: 10_000 }
103 })
104 };
105 const unstableSipped = {
106 ...unstableFull,
107 instance: { ...unstableFull.instance, fillLevel: 2 }
108 };
109 assert.equal(canSellOnMarketplace(fullPotion), true);
110 assert.equal(canSellOnMarketplace(sipped), false);
111 assert.equal(canSellOnMarketplace(fullDye), true);
112 assert.equal(canSellOnMarketplace(usedDye), false);
113 assert.equal(canSellOnMarketplace(unstableFull), true);
114 assert.equal(canSellOnMarketplace(unstableSipped), false);
115 });
116
62 117 test("letters cannot be listed on the marketplace", () =>
63 118 {
64 119 const own = {
@@ -2,6 +2,8 @@ import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { ItemId } from "../../shared/definitions/items.js";
3import { ItemId } from "../../shared/definitions/items.js";
4import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
4import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
5import { dyeItemInstance } from "../../shared/definitions/dyes.js";
6import { potionItemInstance } from "../../shared/alchemy/recipes.js";
5import { BlockType } from "../../shared/definitions/tiles.js";
7import { BlockType } from "../../shared/definitions/tiles.js";
6import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
8import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
7import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
9import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
@@ -59,6 +61,59 @@ test("fully charged tinderbox can list on CE; worn cannot", () =>
59 );
61 );
60});
62});
61
63
64test("full potions and dyes can list on CE; sipped or used cannot", () =>
65{
66 const fullPotion = {
67 itemId: ItemId.potion,
68 quantity: 1,
69 instance: potionItemInstance({
70 potionKey: "lesserHealthTonic",
71 displayName: "Lesser health tonic",
72 color: { r: 0.95, g: 0.38, b: 0.32 },
73 fillLevel: 4,
74 effect: { type: "health-regen", amountPerSecond: 1, durationMs: 10_000 },
75 effectKey: "health-regen"
76 })
77 };
78 const sipped = {
79 ...fullPotion,
80 instance: { ...fullPotion.instance, fillLevel: 3 }
81 };
82 const fullDye = {
83 itemId: ItemId.dye,
84 quantity: 1,
85 instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 4)
86 };
87 const usedDye = {
88 itemId: ItemId.dye,
89 quantity: 1,
90 instance: dyeItemInstance({ r: 0.8, g: 0.1, b: 0.1 }, 2)
91 };
92 const unstableFull = {
93 itemId: ItemId.potion,
94 quantity: 1,
95 instance: potionItemInstance({
96 potionKey: "unstable:glow",
97 unstable: true,
98 displayName: "Unstable draught",
99 color: { r: 0.95, g: 0.9, b: 0.45 },
100 fillLevel: 3,
101 effectKey: "glow",
102 effect: { type: "glow", lightRadiusTiles: 2, durationMs: 10_000 }
103 })
104 };
105 const unstableSipped = {
106 ...unstableFull,
107 instance: { ...unstableFull.instance, fillLevel: 2 }
108 };
109 assert.equal(canSellOnMarketplace(fullPotion), true);
110 assert.equal(canSellOnMarketplace(sipped), false);
111 assert.equal(canSellOnMarketplace(fullDye), true);
112 assert.equal(canSellOnMarketplace(usedDye), false);
113 assert.equal(canSellOnMarketplace(unstableFull), true);
114 assert.equal(canSellOnMarketplace(unstableSipped), false);
115});
116
62test("letters cannot be listed on the marketplace", () =>
117test("letters cannot be listed on the marketplace", () =>
63{
118{
64 const own = {
119 const own = {
shared/items/charges.js +44 −0
@@ -1,5 +1,7 @@
1 1 import { itemDefinition } from "../definitions/items.js";
2 2 import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
3 import { alchemyPotionDefinition, UNSTABLE_POTION_EFFECTS } from "../definitions/alchemy.js";
4 import { DEFAULT_DYE_FILL } from "../definitions/dyes.js";
3 5
4 6 /** Max charges for items that wear down (null = not charge-based). */
5 7 export function itemMaxCharges (itemIdOrDef)
@@ -46,6 +48,48 @@ export function isChargeWorn (stack)
46 48 return stackChargesRemaining(stack) < max;
47 49 }
48 50
51 /**
52 * Full bottle fill for a potion/dye instance (as bottled), or null if not a fill bottle.
53 * Unstable draughts use their effect's bottled fill — not the success-potion default.
54 */
55 export function bottleFullFillLevel (instance)
56 {
57 if (!instance)
58 { return null; }
59 if (instance.kind === "dye")
60 { return DEFAULT_DYE_FILL; }
61 if (instance.kind !== "potion")
62 { return null; }
63 const key = String(instance.potionKey ?? "");
64 if (instance.unstable || key.startsWith("unstable:"))
65 {
66 const effectKey = key.startsWith("unstable:")
67 ? key.slice("unstable:".length)
68 : String(instance.effectKey ?? "");
69 const entry = UNSTABLE_POTION_EFFECTS.find((row) => row.effectKey === effectKey);
70 return entry?.fillLevel ?? Math.max(1, instance.fillLevel | 0 || 1);
71 }
72 const def = alchemyPotionDefinition(instance.potionKey);
73 return def?.fillLevel ?? 4;
74 }
75
76 /** True when a potion/dye has been sipped or used below its bottled fill. */
77 export function isFillWorn (stack)
78 {
79 if (stack?.instance?.fillLevel == null)
80 { return false; }
81 const full = bottleFullFillLevel(stack.instance);
82 if (full == null)
83 { return false; }
84 return (stack.instance.fillLevel | 0) < full;
85 }
86
87 /** Any partially consumed charge tool, potion, or dye — unsafe for blind CE matching. */
88 export function isPartiallyConsumed (stack)
89 {
90 return isChargeWorn(stack) || isFillWorn(stack);
91 }
92
49 93 /**
50 94 * Sprite sub-image index for charge wear (frame N for N uses left).
51 95 * 6 charges → frame index 5; 1 charge → frame index 0.
@@ -1,5 +1,7 @@
1import { itemDefinition } from "../definitions/items.js";
1import { itemDefinition } from "../definitions/items.js";
2import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
2import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
3import { alchemyPotionDefinition, UNSTABLE_POTION_EFFECTS } from "../definitions/alchemy.js";
4import { DEFAULT_DYE_FILL } from "../definitions/dyes.js";
3
5
4/** Max charges for items that wear down (null = not charge-based). */
6/** Max charges for items that wear down (null = not charge-based). */
5export function itemMaxCharges (itemIdOrDef)
7export function itemMaxCharges (itemIdOrDef)
@@ -46,6 +48,48 @@ export function isChargeWorn (stack)
46 return stackChargesRemaining(stack) < max;
48 return stackChargesRemaining(stack) < max;
47}
49}
48
50
51/**
52 * Full bottle fill for a potion/dye instance (as bottled), or null if not a fill bottle.
53 * Unstable draughts use their effect's bottled fill — not the success-potion default.
54 */
55export function bottleFullFillLevel (instance)
56{
57 if (!instance)
58 { return null; }
59 if (instance.kind === "dye")
60 { return DEFAULT_DYE_FILL; }
61 if (instance.kind !== "potion")
62 { return null; }
63 const key = String(instance.potionKey ?? "");
64 if (instance.unstable || key.startsWith("unstable:"))
65 {
66 const effectKey = key.startsWith("unstable:")
67 ? key.slice("unstable:".length)
68 : String(instance.effectKey ?? "");
69 const entry = UNSTABLE_POTION_EFFECTS.find((row) => row.effectKey === effectKey);
70 return entry?.fillLevel ?? Math.max(1, instance.fillLevel | 0 || 1);
71 }
72 const def = alchemyPotionDefinition(instance.potionKey);
73 return def?.fillLevel ?? 4;
74}
75
76/** True when a potion/dye has been sipped or used below its bottled fill. */
77export function isFillWorn (stack)
78{
79 if (stack?.instance?.fillLevel == null)
80 { return false; }
81 const full = bottleFullFillLevel(stack.instance);
82 if (full == null)
83 { return false; }
84 return (stack.instance.fillLevel | 0) < full;
85}
86
87/** Any partially consumed charge tool, potion, or dye — unsafe for blind CE matching. */
88export function isPartiallyConsumed (stack)
89{
90 return isChargeWorn(stack) || isFillWorn(stack);
91}
92
49/**
93/**
50 * Sprite sub-image index for charge wear (frame N for N uses left).
94 * Sprite sub-image index for charge wear (frame N for N uses left).
51 * 6 charges → frame index 5; 1 charge → frame index 0.
95 * 6 charges → frame index 5; 1 charge → frame index 0.
shared/items/marketplace.js +5 −5
@@ -1,6 +1,6 @@
1 1 import { itemDefinition } from "../definitions/items.js";
2 2 import { isLetterStack } from "./letters.js";
3 import { isChargeWorn } from "./charges.js";
3 import { isPartiallyConsumed } from "./charges.js";
4 4
5 5 /** Whether this stack may be listed as a marketplace sell order. */
6 6 export function canSellOnMarketplace (stack, characterId = null)
@@ -14,8 +14,8 @@ export function canSellOnMarketplace (stack, characterId = null)
14 14 // Belt-and-suspenders: letters never list, even if a def flag is missing.
15 15 if (isLetterStack(stack))
16 16 { return false; }
17 // Partially used charge tools (e.g. worn tinderbox) stay off the CE.
18 if (isChargeWorn(stack))
17 // Buyers do not pick listings — partially used goods must stay off the CE.
18 if (isPartiallyConsumed(stack))
19 19 { return false; }
20 20 return true;
21 21 }
@@ -30,8 +30,8 @@ export function assertMarketplaceSellable (stack, characterId = null)
30 30 def?.marketplaceSellBlockedReason
31 31 ?? (isLetterStack(stack)
32 32 ? "Letters cannot be sold on the marketplace."
33 : (isChargeWorn(stack)
34 ? "Partially used tools cannot be sold on the marketplace."
33 : (isPartiallyConsumed(stack)
34 ? "Partially used items cannot be sold on the marketplace."
35 35 : "That item cannot be sold on the marketplace."))
36 36 );
37 37 error.userFacing = true;
@@ -1,6 +1,6 @@
1import { itemDefinition } from "../definitions/items.js";
1import { itemDefinition } from "../definitions/items.js";
2import { isLetterStack } from "./letters.js";
2import { isLetterStack } from "./letters.js";
3import { isChargeWorn } from "./charges.js";
3import { isPartiallyConsumed } from "./charges.js";
4
4
5/** Whether this stack may be listed as a marketplace sell order. */
5/** Whether this stack may be listed as a marketplace sell order. */
6export function canSellOnMarketplace (stack, characterId = null)
6export function canSellOnMarketplace (stack, characterId = null)
@@ -14,8 +14,8 @@ export function canSellOnMarketplace (stack, characterId = null)
14 // Belt-and-suspenders: letters never list, even if a def flag is missing.
14 // Belt-and-suspenders: letters never list, even if a def flag is missing.
15 if (isLetterStack(stack))
15 if (isLetterStack(stack))
16 { return false; }
16 { return false; }
17 // Partially used charge tools (e.g. worn tinderbox) stay off the CE.
17 // Buyers do not pick listings — partially used goods must stay off the CE.
18 if (isChargeWorn(stack))
18 if (isPartiallyConsumed(stack))
19 { return false; }
19 { return false; }
20 return true;
20 return true;
21}
21}
@@ -30,8 +30,8 @@ export function assertMarketplaceSellable (stack, characterId = null)
30 def?.marketplaceSellBlockedReason
30 def?.marketplaceSellBlockedReason
31 ?? (isLetterStack(stack)
31 ?? (isLetterStack(stack)
32 ? "Letters cannot be sold on the marketplace."
32 ? "Letters cannot be sold on the marketplace."
33 : (isChargeWorn(stack)
33 : (isPartiallyConsumed(stack)
34 ? "Partially used tools cannot be sold on the marketplace."
34 ? "Partially used items cannot be sold on the marketplace."
35 : "That item cannot be sold on the marketplace."))
35 : "That item cannot be sold on the marketplace."))
36 );
36 );
37 error.userFacing = true;
37 error.userFacing = true;
shared/market/funds.js +2 −2
@@ -1,6 +1,6 @@
1 1 import { ItemId, itemDefinition } from "../definitions/items.js";
2 2 import { cloneStack, stackQuantity, takeFromSlot } from "../items/inventory.js";
3 import { isChargeWorn } from "../items/charges.js";
3 import { isPartiallyConsumed } from "../items/charges.js";
4 4
5 5 /** Filled cheque that reserves coins (usable as market payment, whole only). */
6 6 export function isCoinChequeStack (stack)
@@ -153,7 +153,7 @@ export function resolveMarketSellListing (stack)
153 153 const def = itemDefinition(stack.itemId);
154 154 if (!def || def.marketplaceSellable === false)
155 155 { return null; }
156 if (isChargeWorn(stack))
156 if (isPartiallyConsumed(stack))
157 157 { return null; }
158 158 return {
159 159 isCheque: false,
@@ -1,6 +1,6 @@
1import { ItemId, itemDefinition } from "../definitions/items.js";
1import { ItemId, itemDefinition } from "../definitions/items.js";
2import { cloneStack, stackQuantity, takeFromSlot } from "../items/inventory.js";
2import { cloneStack, stackQuantity, takeFromSlot } from "../items/inventory.js";
3import { isChargeWorn } from "../items/charges.js";
3import { isPartiallyConsumed } from "../items/charges.js";
4
4
5/** Filled cheque that reserves coins (usable as market payment, whole only). */
5/** Filled cheque that reserves coins (usable as market payment, whole only). */
6export function isCoinChequeStack (stack)
6export function isCoinChequeStack (stack)
@@ -153,7 +153,7 @@ export function resolveMarketSellListing (stack)
153 const def = itemDefinition(stack.itemId);
153 const def = itemDefinition(stack.itemId);
154 if (!def || def.marketplaceSellable === false)
154 if (!def || def.marketplaceSellable === false)
155 { return null; }
155 { return null; }
156 if (isChargeWorn(stack))
156 if (isPartiallyConsumed(stack))
157 { return null; }
157 { return null; }
158 return {
158 return {
159 isCheque: false,
159 isCheque: false,