BlueGit

timfa questward

Scale worn tinderbox prices by remaining uses and show charge frames.

Merchant buyback and shop resale track charge fraction; worn charge tools stay off the CE. Tinderboxes now have six uses matching a 3×2 sprite sheet, with legacy high-charge boxes clamped to six.

timfa Tim Falken · pushed by timfa

Authored 2026-08-30 · 2 months ago

Hash
5491bf1fac5e4bc42cd793eacbfc065fe78c0680
Parent
76851622
13 files changed +278 −21
client/src/render/potionIcon.js +43 −5
@@ -1,5 +1,6 @@
1 1 import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js";
2 2 import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
3 import { chargeSpriteFrameIndex, itemMaxCharges } from "/shared/items/charges.js";
3 4 import
4 5 {
5 6 applyMagentaTintToImageData,
@@ -122,6 +123,29 @@ export async function dualTintedItemIconDataUrl (assetPath, dualTint)
122 123 return url;
123 124 }
124 125
126 /** Crop one frame from a multi-frame item sheet for UI icons. */
127 export async function framedItemIconDataUrl (assetPath, frameIndex, framesPerRow = 1, frameCount = 1)
128 {
129 const index = Math.max(0, Math.min((frameCount | 0 || 1) - 1, frameIndex | 0));
130 const perRow = Math.max(1, framesPerRow | 0 || 1);
131 const key = `frame|${assetPath}|${index}|${perRow}|${frameCount}`;
132 if (urlCache.has(key))
133 { return urlCache.get(key); }
134 const img = await loadImage(assetPath);
135 const canvas = document.createElement("canvas");
136 canvas.width = SPRITE_SIZE_PX;
137 canvas.height = SPRITE_SIZE_PX;
138 const ctx = canvas.getContext("2d");
139 ctx.imageSmoothingEnabled = false;
140 ctx.clearRect(0, 0, canvas.width, canvas.height);
141 const sx = (index % perRow) * SPRITE_SIZE_PX;
142 const sy = Math.floor(index / perRow) * SPRITE_SIZE_PX;
143 ctx.drawImage(img, sx, sy, SPRITE_SIZE_PX, SPRITE_SIZE_PX, 0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX);
144 const url = canvas.toDataURL("image/png");
145 urlCache.set(key, url);
146 return url;
147 }
148
125 149 export function isPotionStack (stack)
126 150 {
127 151 return (stack?.itemId === ItemId.potion && stack.instance?.kind === "potion")
@@ -133,17 +157,31 @@ export function applyItemIcon (img, stack, fallbackPath)
133 157 {
134 158 if (!img)
135 159 { return; }
136 img.src = fallbackPath;
160 const path = fallbackPath || itemAssetPathForStack(stack);
161 img.src = path;
137 162 const def = itemDefinition(stack?.itemId);
163 const frames = def?.frames | 0 || 1;
164 if (frames > 1 && itemMaxCharges(def) != null)
165 {
166 const frameIndex = chargeSpriteFrameIndex(stack);
167 framedItemIconDataUrl(path, frameIndex, def.framesPerRow ?? 1, frames).then((url) =>
168 {
169 img.src = url;
170 }).catch(() =>
171 {
172 img.src = path;
173 });
174 return;
175 }
138 176 const dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
139 177 if (dualTint)
140 178 {
141 dualTintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), dualTint).then((url) =>
179 dualTintedItemIconDataUrl(path, dualTint).then((url) =>
142 180 {
143 181 img.src = url;
144 182 }).catch(() =>
145 183 {
146 img.src = fallbackPath;
184 img.src = path;
147 185 });
148 186 return;
149 187 }
@@ -152,12 +190,12 @@ export function applyItemIcon (img, stack, fallbackPath)
152 190 { return; }
153 191 const work = isPotionStack(stack)
154 192 ? potionIconDataUrl(stack.instance)
155 : tintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), tint);
193 : tintedItemIconDataUrl(path, tint);
156 194 work.then((url) =>
157 195 {
158 196 img.src = url;
159 197 }).catch(() =>
160 198 {
161 img.src = fallbackPath;
199 img.src = path;
162 200 });
163 201 }
@@ -1,5 +1,6 @@
1import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js";
1import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js";
2import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
3import { chargeSpriteFrameIndex, itemMaxCharges } from "/shared/items/charges.js";
3import
4import
4 {
5 {
5 applyMagentaTintToImageData,
6 applyMagentaTintToImageData,
@@ -122,6 +123,29 @@ export async function dualTintedItemIconDataUrl (assetPath, dualTint)
122 return url;
123 return url;
123}
124}
124
125
126/** Crop one frame from a multi-frame item sheet for UI icons. */
127export async function framedItemIconDataUrl (assetPath, frameIndex, framesPerRow = 1, frameCount = 1)
128{
129 const index = Math.max(0, Math.min((frameCount | 0 || 1) - 1, frameIndex | 0));
130 const perRow = Math.max(1, framesPerRow | 0 || 1);
131 const key = `frame|${assetPath}|${index}|${perRow}|${frameCount}`;
132 if (urlCache.has(key))
133 { return urlCache.get(key); }
134 const img = await loadImage(assetPath);
135 const canvas = document.createElement("canvas");
136 canvas.width = SPRITE_SIZE_PX;
137 canvas.height = SPRITE_SIZE_PX;
138 const ctx = canvas.getContext("2d");
139 ctx.imageSmoothingEnabled = false;
140 ctx.clearRect(0, 0, canvas.width, canvas.height);
141 const sx = (index % perRow) * SPRITE_SIZE_PX;
142 const sy = Math.floor(index / perRow) * SPRITE_SIZE_PX;
143 ctx.drawImage(img, sx, sy, SPRITE_SIZE_PX, SPRITE_SIZE_PX, 0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX);
144 const url = canvas.toDataURL("image/png");
145 urlCache.set(key, url);
146 return url;
147}
148
125export function isPotionStack (stack)
149export function isPotionStack (stack)
126{
150{
127 return (stack?.itemId === ItemId.potion && stack.instance?.kind === "potion")
151 return (stack?.itemId === ItemId.potion && stack.instance?.kind === "potion")
@@ -133,17 +157,31 @@ export function applyItemIcon (img, stack, fallbackPath)
133{
157{
134 if (!img)
158 if (!img)
135 { return; }
159 { return; }
136 img.src = fallbackPath;
160 const path = fallbackPath || itemAssetPathForStack(stack);
161 img.src = path;
137 const def = itemDefinition(stack?.itemId);
162 const def = itemDefinition(stack?.itemId);
163 const frames = def?.frames | 0 || 1;
164 if (frames > 1 && itemMaxCharges(def) != null)
165 {
166 const frameIndex = chargeSpriteFrameIndex(stack);
167 framedItemIconDataUrl(path, frameIndex, def.framesPerRow ?? 1, frames).then((url) =>
168 {
169 img.src = url;
170 }).catch(() =>
171 {
172 img.src = path;
173 });
174 return;
175 }
138 const dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
176 const dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
139 if (dualTint)
177 if (dualTint)
140 {
178 {
141 dualTintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), dualTint).then((url) =>
179 dualTintedItemIconDataUrl(path, dualTint).then((url) =>
142 {
180 {
143 img.src = url;
181 img.src = url;
144 }).catch(() =>
182 }).catch(() =>
145 {
183 {
146 img.src = fallbackPath;
184 img.src = path;
147 });
185 });
148 return;
186 return;
149 }
187 }
@@ -152,12 +190,12 @@ export function applyItemIcon (img, stack, fallbackPath)
152 { return; }
190 { return; }
153 const work = isPotionStack(stack)
191 const work = isPotionStack(stack)
154 ? potionIconDataUrl(stack.instance)
192 ? potionIconDataUrl(stack.instance)
155 : tintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), tint);
193 : tintedItemIconDataUrl(path, tint);
156 work.then((url) =>
194 work.then((url) =>
157 {
195 {
158 img.src = url;
196 img.src = url;
159 }).catch(() =>
197 }).catch(() =>
160 {
198 {
161 img.src = fallbackPath;
199 img.src = path;
162 });
200 });
163}
201}
client/src/render/worldView.js +3 −0
@@ -6,6 +6,7 @@ import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSpr
6 6 import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7 7 import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
8 8 import { layersFromSpawnerConfig } from "/shared/appearance/layers.js";
9 import { chargeSpriteFrameIndex, itemMaxCharges } from "/shared/items/charges.js";
9 10 import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight, LightOcclusion } from "/shared/definitions/tiles.js";
10 11 import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
11 12 import { bakeChunkAtlases, tileBlockSpriteEntry } from "./chunkBake.js";
@@ -1191,6 +1192,8 @@ export class WorldView
1191 1192 const def = itemDefinition(payload.itemId);
1192 1193 item.dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
1193 1194 item.tintColor = item.dualTint ? null : resolveItemTint(stack, def);
1195 if (item.object?.renderer && itemMaxCharges(def) != null)
1196 { item.object.renderer.setSubImage?.(chargeSpriteFrameIndex(stack)); }
1194 1197 this.#placePivoted(item.object, pivot.x, pivot.y, pivot.y - 0.01, SORT_BLOCK);
1195 1198 this.#syncGroundItemSprite(item, this.tiles.get(`${payload.tileX},${payload.tileY}`)?.shadeAlpha ?? 0);
1196 1199 }
@@ -6,6 +6,7 @@ import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSpr
6import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
6import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
7import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
8import { layersFromSpawnerConfig } from "/shared/appearance/layers.js";
8import { layersFromSpawnerConfig } from "/shared/appearance/layers.js";
9import { chargeSpriteFrameIndex, itemMaxCharges } from "/shared/items/charges.js";
9import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight, LightOcclusion } from "/shared/definitions/tiles.js";
10import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight, LightOcclusion } from "/shared/definitions/tiles.js";
10import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
11import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
11import { bakeChunkAtlases, tileBlockSpriteEntry } from "./chunkBake.js";
12import { bakeChunkAtlases, tileBlockSpriteEntry } from "./chunkBake.js";
@@ -1191,6 +1192,8 @@ export class WorldView
1191 const def = itemDefinition(payload.itemId);
1192 const def = itemDefinition(payload.itemId);
1192 item.dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
1193 item.dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def);
1193 item.tintColor = item.dualTint ? null : resolveItemTint(stack, def);
1194 item.tintColor = item.dualTint ? null : resolveItemTint(stack, def);
1195 if (item.object?.renderer && itemMaxCharges(def) != null)
1196 { item.object.renderer.setSubImage?.(chargeSpriteFrameIndex(stack)); }
1194 this.#placePivoted(item.object, pivot.x, pivot.y, pivot.y - 0.01, SORT_BLOCK);
1197 this.#placePivoted(item.object, pivot.x, pivot.y, pivot.y - 0.01, SORT_BLOCK);
1195 this.#syncGroundItemSprite(item, this.tiles.get(`${payload.tileX},${payload.tileY}`)?.shadeAlpha ?? 0);
1198 this.#syncGroundItemSprite(item, this.tiles.get(`${payload.tileX},${payload.tileY}`)?.shadeAlpha ?? 0);
1196 }
1199 }
server/src/shop/service.js +10 −2
@@ -395,7 +395,10 @@ function shopStockView (world, shop, now)
395 395 listingId: `bought:${lot.stockKey}`,
396 396 itemId,
397 397 displayName: itemDisplayName(stack, def),
398 unitPrice: shopBoughtResalePrice(itemId, lastSaleOpts(itemId)),
398 unitPrice: shopBoughtResalePrice(itemId, {
399 ...lastSaleOpts(itemId),
400 stack
401 }),
399 402 available: lot.quantity,
400 403 source: "bought",
401 404 instance: lot.instance ? structuredClone(lot.instance) : null
@@ -538,7 +541,12 @@ export function buyFromShop (world, sessionId, characterId, npcEntityId, itemId,
538 541 lastSale: lastSaleFor(world, buyItemId),
539 542 now,
540 543 lerpDurationMs: lerpDuration(world),
541 settings: world.settings ?? null
544 settings: world.settings ?? null,
545 stack: {
546 itemId: buyItemId,
547 quantity: 1,
548 instance: boughtLot(shop, listing.stockKey)?.instance ?? null
549 }
542 550 })
543 551 : shopUnitPrice(buyItemId, {
544 552 lastSale: lastSaleFor(world, buyItemId),
@@ -395,7 +395,10 @@ function shopStockView (world, shop, now)
395 listingId: `bought:${lot.stockKey}`,
395 listingId: `bought:${lot.stockKey}`,
396 itemId,
396 itemId,
397 displayName: itemDisplayName(stack, def),
397 displayName: itemDisplayName(stack, def),
398 unitPrice: shopBoughtResalePrice(itemId, lastSaleOpts(itemId)),
398 unitPrice: shopBoughtResalePrice(itemId, {
399 ...lastSaleOpts(itemId),
400 stack
401 }),
399 available: lot.quantity,
402 available: lot.quantity,
400 source: "bought",
403 source: "bought",
401 instance: lot.instance ? structuredClone(lot.instance) : null
404 instance: lot.instance ? structuredClone(lot.instance) : null
@@ -538,7 +541,12 @@ export function buyFromShop (world, sessionId, characterId, npcEntityId, itemId,
538 lastSale: lastSaleFor(world, buyItemId),
541 lastSale: lastSaleFor(world, buyItemId),
539 now,
542 now,
540 lerpDurationMs: lerpDuration(world),
543 lerpDurationMs: lerpDuration(world),
541 settings: world.settings ?? null
544 settings: world.settings ?? null,
545 stack: {
546 itemId: buyItemId,
547 quantity: 1,
548 instance: boughtLot(shop, listing.stockKey)?.instance ?? null
549 }
542 })
550 })
543 : shopUnitPrice(buyItemId, {
551 : shopUnitPrice(buyItemId, {
544 lastSale: lastSaleFor(world, buyItemId),
552 lastSale: lastSaleFor(world, buyItemId),
server/tests/charges.test.js added +49 −0
@@ -0,0 +1,49 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
4 import
5 {
6 chargeFractionRemaining,
7 chargeSpriteFrameIndex,
8 isChargeWorn,
9 normalizeChargeStack,
10 stackChargesRemaining
11 } from "../../shared/items/charges.js";
12 import { normalizeInventorySlots } from "../../shared/items/inventory.js";
13
14 test("tinderbox max charges is 6 and frames map 1:1 to uses left", () =>
15 {
16 assert.equal(TINDERBOX_MAX_CHARGES, 6);
17 assert.equal(chargeSpriteFrameIndex({
18 itemId: CraftingItemId.tinderbox,
19 instance: tinderboxItemInstance(6)
20 }), 5);
21 assert.equal(chargeSpriteFrameIndex({
22 itemId: CraftingItemId.tinderbox,
23 instance: tinderboxItemInstance(5)
24 }), 4);
25 assert.equal(chargeSpriteFrameIndex({
26 itemId: CraftingItemId.tinderbox,
27 instance: tinderboxItemInstance(1)
28 }), 0);
29 });
30
31 test("legacy tinderboxes with more than max charges clamp to max", () =>
32 {
33 const raw = {
34 itemId: CraftingItemId.tinderbox,
35 quantity: 1,
36 instance: { kind: "tinderbox", charges: 20 }
37 };
38 const normalized = normalizeChargeStack(raw);
39 assert.equal(normalized.instance.charges, 6);
40 assert.equal(stackChargesRemaining(normalized), 6);
41 assert.equal(isChargeWorn(normalized), false);
42 assert.equal(chargeFractionRemaining({
43 itemId: CraftingItemId.tinderbox,
44 instance: tinderboxItemInstance(3)
45 }), 0.5);
46
47 const slots = normalizeInventorySlots([raw]);
48 assert.equal(slots[0].instance.charges, 6);
49 });
@@ -0,0 +1,49 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
4import
5 {
6 chargeFractionRemaining,
7 chargeSpriteFrameIndex,
8 isChargeWorn,
9 normalizeChargeStack,
10 stackChargesRemaining
11 } from "../../shared/items/charges.js";
12import { normalizeInventorySlots } from "../../shared/items/inventory.js";
13
14test("tinderbox max charges is 6 and frames map 1:1 to uses left", () =>
15{
16 assert.equal(TINDERBOX_MAX_CHARGES, 6);
17 assert.equal(chargeSpriteFrameIndex({
18 itemId: CraftingItemId.tinderbox,
19 instance: tinderboxItemInstance(6)
20 }), 5);
21 assert.equal(chargeSpriteFrameIndex({
22 itemId: CraftingItemId.tinderbox,
23 instance: tinderboxItemInstance(5)
24 }), 4);
25 assert.equal(chargeSpriteFrameIndex({
26 itemId: CraftingItemId.tinderbox,
27 instance: tinderboxItemInstance(1)
28 }), 0);
29});
30
31test("legacy tinderboxes with more than max charges clamp to max", () =>
32{
33 const raw = {
34 itemId: CraftingItemId.tinderbox,
35 quantity: 1,
36 instance: { kind: "tinderbox", charges: 20 }
37 };
38 const normalized = normalizeChargeStack(raw);
39 assert.equal(normalized.instance.charges, 6);
40 assert.equal(stackChargesRemaining(normalized), 6);
41 assert.equal(isChargeWorn(normalized), false);
42 assert.equal(chargeFractionRemaining({
43 itemId: CraftingItemId.tinderbox,
44 instance: tinderboxItemInstance(3)
45 }), 0.5);
46
47 const slots = normalizeInventorySlots([raw]);
48 assert.equal(slots[0].instance.charges, 6);
49});
server/tests/firestarting-cooking.test.js +2 −2
@@ -124,7 +124,7 @@ test("start-fire places campfire and expiry drops ash", () =>
124 124 worker.world.rng = () => 0.999;
125 125 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
126 126 giveInventory(harness, playerId, [
127 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(20) }
127 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
128 128 ]);
129 129
130 130 const groundItemId = 9001;
@@ -152,7 +152,7 @@ test("start-fire places campfire and expiry drops ash", () =>
152 152
153 153 const bag = harness.simulation.bagOf(playerId);
154 154 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
155 assert.equal(tinder?.instance?.charges, 19);
155 assert.equal(tinder?.instance?.charges, 5);
156 156
157 157 const entry = worker.world.campfireExpires.get("9,8");
158 158 entry.expiresAt = 0;
@@ -124,7 +124,7 @@ test("start-fire places campfire and expiry drops ash", () =>
124 worker.world.rng = () => 0.999;
124 worker.world.rng = () => 0.999;
125 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
125 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
126 giveInventory(harness, playerId, [
126 giveInventory(harness, playerId, [
127 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(20) }
127 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
128 ]);
128 ]);
129
129
130 const groundItemId = 9001;
130 const groundItemId = 9001;
@@ -152,7 +152,7 @@ test("start-fire places campfire and expiry drops ash", () =>
152
152
153 const bag = harness.simulation.bagOf(playerId);
153 const bag = harness.simulation.bagOf(playerId);
154 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
154 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
155 assert.equal(tinder?.instance?.charges, 19);
155 assert.equal(tinder?.instance?.charges, 5);
156
156
157 const entry = worker.world.campfireExpires.get("9,8");
157 const entry = worker.world.campfireExpires.get("9,8");
158 entry.expiresAt = 0;
158 entry.expiresAt = 0;
server/tests/marketplace.test.js +21 −0
@@ -1,6 +1,7 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { ItemId } from "../../shared/definitions/items.js";
4 import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
4 5 import { BlockType } from "../../shared/definitions/tiles.js";
5 6 import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
6 7 import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
@@ -38,6 +39,26 @@ test("normal items are marketplace sellable by default", () =>
38 39 assert.equal(canSellOnMarketplace({ itemId: ItemId.apple, quantity: 3 }), true);
39 40 });
40 41
42 test("fully charged tinderbox can list on CE; worn cannot", () =>
43 {
44 const fresh = {
45 itemId: CraftingItemId.tinderbox,
46 quantity: 1,
47 instance: tinderboxItemInstance(6)
48 };
49 const worn = {
50 itemId: CraftingItemId.tinderbox,
51 quantity: 1,
52 instance: tinderboxItemInstance(1)
53 };
54 assert.equal(canSellOnMarketplace(fresh), true);
55 assert.equal(canSellOnMarketplace(worn), false);
56 assert.throws(
57 () => assertMarketplaceSellable(worn),
58 (err) => /partially used/i.test(err.message)
59 );
60 });
61
41 62 test("letters cannot be listed on the marketplace", () =>
42 63 {
43 64 const own = {
@@ -1,6 +1,7 @@
1import test from "node:test";
1import 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 { BlockType } from "../../shared/definitions/tiles.js";
5import { BlockType } from "../../shared/definitions/tiles.js";
5import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
6import { canSellOnMarketplace, assertMarketplaceSellable } from "../../shared/items/marketplace.js";
6import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
7import { midpointTradePrice, MARKET_ORDER_MAX_AGE_MS, soldLetterBody, marketOrderListingFee, orderCancelledLetterBody, orderExpiredLetterBody } from "../../shared/market/exchange.js";
@@ -38,6 +39,26 @@ test("normal items are marketplace sellable by default", () =>
38 assert.equal(canSellOnMarketplace({ itemId: ItemId.apple, quantity: 3 }), true);
39 assert.equal(canSellOnMarketplace({ itemId: ItemId.apple, quantity: 3 }), true);
39});
40});
40
41
42test("fully charged tinderbox can list on CE; worn cannot", () =>
43{
44 const fresh = {
45 itemId: CraftingItemId.tinderbox,
46 quantity: 1,
47 instance: tinderboxItemInstance(6)
48 };
49 const worn = {
50 itemId: CraftingItemId.tinderbox,
51 quantity: 1,
52 instance: tinderboxItemInstance(1)
53 };
54 assert.equal(canSellOnMarketplace(fresh), true);
55 assert.equal(canSellOnMarketplace(worn), false);
56 assert.throws(
57 () => assertMarketplaceSellable(worn),
58 (err) => /partially used/i.test(err.message)
59 );
60});
61
41test("letters cannot be listed on the marketplace", () =>
62test("letters cannot be listed on the marketplace", () =>
42{
63{
43 const own = {
64 const own = {
server/tests/shop.test.js +29 −1
@@ -16,7 +16,7 @@ import
16 16 lerp
17 17 } from "../../shared/shop/pricing.js";
18 18 import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js";
19 import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
19 import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
20 20 import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
21 21 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
22 22 import { Component } from "../src/ecs/components.js";
@@ -175,6 +175,34 @@ test("shop prices fall back to seedValue and apply hidden discount", () =>
175 175 assert.equal(chequeQuote.payout, chequeQuote.baseTotal - chequeQuote.adminFee);
176 176 });
177 177
178 test("charge-worn tools scale merchant buy and resale prices by remaining uses", () =>
179 {
180 const full = {
181 itemId: CraftingItemId.tinderbox,
182 quantity: 1,
183 instance: tinderboxItemInstance(6)
184 };
185 const worn = {
186 itemId: CraftingItemId.tinderbox,
187 quantity: 1,
188 instance: tinderboxItemInstance(1)
189 };
190 const fullBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: full });
191 const wornBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: worn });
192 assert.equal(fullBuy, shopBuyFromPlayerPrice(CraftingItemId.tinderbox));
193 assert.equal(wornBuy, marketValueAsCoins(
194 undiscountedMarketValue(CraftingItemId.tinderbox) * 0.8 * (1 / 6)
195 ));
196 const fullResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: full });
197 const wornResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: worn });
198 assert.equal(fullResale, shopBoughtResalePrice(CraftingItemId.tinderbox));
199 assert.equal(wornResale, marketValueAsCoins(
200 undiscountedMarketValue(CraftingItemId.tinderbox) * 1.2 * (1 / 6)
201 ));
202 const quote = resolveShopSellQuote(worn);
203 assert.equal(quote.unitPrice, wornBuy);
204 });
205
178 206 test("market value display and shop use ceil fractional lerps", () =>
179 207 {
180 208 const now = 1_000_000;
@@ -16,7 +16,7 @@ import
16 lerp
16 lerp
17 } from "../../shared/shop/pricing.js";
17 } from "../../shared/shop/pricing.js";
18import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js";
18import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js";
19import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
19import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
20import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
20import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js";
21import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
21import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
22import { Component } from "../src/ecs/components.js";
22import { Component } from "../src/ecs/components.js";
@@ -175,6 +175,34 @@ test("shop prices fall back to seedValue and apply hidden discount", () =>
175 assert.equal(chequeQuote.payout, chequeQuote.baseTotal - chequeQuote.adminFee);
175 assert.equal(chequeQuote.payout, chequeQuote.baseTotal - chequeQuote.adminFee);
176});
176});
177
177
178test("charge-worn tools scale merchant buy and resale prices by remaining uses", () =>
179{
180 const full = {
181 itemId: CraftingItemId.tinderbox,
182 quantity: 1,
183 instance: tinderboxItemInstance(6)
184 };
185 const worn = {
186 itemId: CraftingItemId.tinderbox,
187 quantity: 1,
188 instance: tinderboxItemInstance(1)
189 };
190 const fullBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: full });
191 const wornBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: worn });
192 assert.equal(fullBuy, shopBuyFromPlayerPrice(CraftingItemId.tinderbox));
193 assert.equal(wornBuy, marketValueAsCoins(
194 undiscountedMarketValue(CraftingItemId.tinderbox) * 0.8 * (1 / 6)
195 ));
196 const fullResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: full });
197 const wornResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: worn });
198 assert.equal(fullResale, shopBoughtResalePrice(CraftingItemId.tinderbox));
199 assert.equal(wornResale, marketValueAsCoins(
200 undiscountedMarketValue(CraftingItemId.tinderbox) * 1.2 * (1 / 6)
201 ));
202 const quote = resolveShopSellQuote(worn);
203 assert.equal(quote.unitPrice, wornBuy);
204});
205
178test("market value display and shop use ceil fractional lerps", () =>
206test("market value display and shop use ceil fractional lerps", () =>
179{
207{
180 const now = 1_000_000;
208 const now = 1_000_000;
shared/definitions/craftingItems.js +5 −1
@@ -5,7 +5,7 @@ import { StatusEffectType } from "../statusEffects/rules.js";
5 5 const DEFAULT_STACK = 99;
6 6
7 7 /** Default spark charges on a fresh tinderbox. */
8 export const TINDERBOX_MAX_CHARGES = 20;
8 export const TINDERBOX_MAX_CHARGES = 6;
9 9
10 10 /**
11 11 * Reserved numeric IDs for gathering, crafting, and cooking content (9–25).
@@ -81,6 +81,10 @@ export const CRAFTING_ITEM_STUBS = Object.freeze({
81 81 category: ItemCategory.tool,
82 82 iconKey: "tinderbox",
83 83 defaultState: "tinderbox",
84 /** Sheet: 3×2 of 16px — frame index matches remaining uses (1…6). */
85 frames: 6,
86 framesPerRow: 3,
87 maxCharges: TINDERBOX_MAX_CHARGES,
84 88 /** Show tinderbox-equip while lighting a ground log (not worn gear). */
85 89 gatherEquipSprite: true,
86 90 npcShopAllowed: true,
@@ -5,7 +5,7 @@ import { StatusEffectType } from "../statusEffects/rules.js";
5const DEFAULT_STACK = 99;
5const DEFAULT_STACK = 99;
6
6
7/** Default spark charges on a fresh tinderbox. */
7/** Default spark charges on a fresh tinderbox. */
8export const TINDERBOX_MAX_CHARGES = 20;
8export const TINDERBOX_MAX_CHARGES = 6;
9
9
10/**
10/**
11 * Reserved numeric IDs for gathering, crafting, and cooking content (9–25).
11 * Reserved numeric IDs for gathering, crafting, and cooking content (9–25).
@@ -81,6 +81,10 @@ export const CRAFTING_ITEM_STUBS = Object.freeze({
81 category: ItemCategory.tool,
81 category: ItemCategory.tool,
82 iconKey: "tinderbox",
82 iconKey: "tinderbox",
83 defaultState: "tinderbox",
83 defaultState: "tinderbox",
84 /** Sheet: 3×2 of 16px — frame index matches remaining uses (1…6). */
85 frames: 6,
86 framesPerRow: 3,
87 maxCharges: TINDERBOX_MAX_CHARGES,
84 /** Show tinderbox-equip while lighting a ground log (not worn gear). */
88 /** Show tinderbox-equip while lighting a ground log (not worn gear). */
85 gatherEquipSprite: true,
89 gatherEquipSprite: true,
86 npcShopAllowed: true,
90 npcShopAllowed: true,
shared/items/charges.js added +84 −0
@@ -0,0 +1,84 @@
1 import { itemDefinition } from "../definitions/items.js";
2 import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
3
4 /** Max charges for items that wear down (null = not charge-based). */
5 export function itemMaxCharges (itemIdOrDef)
6 {
7 const def = itemIdOrDef && typeof itemIdOrDef === "object"
8 ? itemIdOrDef
9 : itemDefinition(itemIdOrDef);
10 const max = def?.maxCharges | 0;
11 return max > 0 ? max : null;
12 }
13
14 /** Remaining charges on a stack, or null if the item is not charge-based. */
15 export function stackChargesRemaining (stack)
16 {
17 const max = itemMaxCharges(stack?.itemId);
18 if (max == null)
19 { return null; }
20 if (stack?.itemId === CraftingItemId.tinderbox)
21 {
22 if (stack.instance?.kind === "tinderbox")
23 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
24 return max;
25 }
26 if (stack?.instance && typeof stack.instance.charges === "number")
27 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
28 return max;
29 }
30
31 /** 0–1 fraction of charges left (1 if not charge-based). */
32 export function chargeFractionRemaining (stack)
33 {
34 const max = itemMaxCharges(stack?.itemId);
35 if (max == null)
36 { return 1; }
37 return Math.min(1, Math.max(0, stackChargesRemaining(stack) / max));
38 }
39
40 /** True when a charge-based item has fewer than max charges left. */
41 export function isChargeWorn (stack)
42 {
43 const max = itemMaxCharges(stack?.itemId);
44 if (max == null)
45 { return false; }
46 return stackChargesRemaining(stack) < max;
47 }
48
49 /**
50 * Sprite sub-image index for charge wear (frame N for N uses left).
51 * 6 charges → frame index 5; 1 charge → frame index 0.
52 */
53 export function chargeSpriteFrameIndex (stack)
54 {
55 const max = itemMaxCharges(stack?.itemId) ?? 1;
56 const charges = Math.max(1, Math.min(max, stackChargesRemaining(stack) ?? max));
57 return charges - 1;
58 }
59
60 /**
61 * Clamp / restore tinderbox (and future charge tools) instance on a stack.
62 * Charges above max migrate down to max. Returns null if charges hit 0.
63 */
64 export function normalizeChargeStack (stack)
65 {
66 if (!stack?.itemId)
67 { return stack ?? null; }
68 if (stack.itemId !== CraftingItemId.tinderbox)
69 { return stack; }
70 const max = itemMaxCharges(stack.itemId) ?? TINDERBOX_MAX_CHARGES;
71 let charges;
72 if (stack.instance?.kind === "tinderbox")
73 { charges = stack.instance.charges | 0; }
74 else
75 { charges = max; }
76 if (charges > max)
77 { charges = max; }
78 if (charges <= 0)
79 { return null; }
80 return {
81 ...stack,
82 instance: tinderboxItemInstance(charges)
83 };
84 }
@@ -0,0 +1,84 @@
1import { itemDefinition } from "../definitions/items.js";
2import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
3
4/** Max charges for items that wear down (null = not charge-based). */
5export function itemMaxCharges (itemIdOrDef)
6{
7 const def = itemIdOrDef && typeof itemIdOrDef === "object"
8 ? itemIdOrDef
9 : itemDefinition(itemIdOrDef);
10 const max = def?.maxCharges | 0;
11 return max > 0 ? max : null;
12}
13
14/** Remaining charges on a stack, or null if the item is not charge-based. */
15export function stackChargesRemaining (stack)
16{
17 const max = itemMaxCharges(stack?.itemId);
18 if (max == null)
19 { return null; }
20 if (stack?.itemId === CraftingItemId.tinderbox)
21 {
22 if (stack.instance?.kind === "tinderbox")
23 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
24 return max;
25 }
26 if (stack?.instance && typeof stack.instance.charges === "number")
27 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
28 return max;
29}
30
31/** 0–1 fraction of charges left (1 if not charge-based). */
32export function chargeFractionRemaining (stack)
33{
34 const max = itemMaxCharges(stack?.itemId);
35 if (max == null)
36 { return 1; }
37 return Math.min(1, Math.max(0, stackChargesRemaining(stack) / max));
38}
39
40/** True when a charge-based item has fewer than max charges left. */
41export function isChargeWorn (stack)
42{
43 const max = itemMaxCharges(stack?.itemId);
44 if (max == null)
45 { return false; }
46 return stackChargesRemaining(stack) < max;
47}
48
49/**
50 * Sprite sub-image index for charge wear (frame N for N uses left).
51 * 6 charges → frame index 5; 1 charge → frame index 0.
52 */
53export function chargeSpriteFrameIndex (stack)
54{
55 const max = itemMaxCharges(stack?.itemId) ?? 1;
56 const charges = Math.max(1, Math.min(max, stackChargesRemaining(stack) ?? max));
57 return charges - 1;
58}
59
60/**
61 * Clamp / restore tinderbox (and future charge tools) instance on a stack.
62 * Charges above max migrate down to max. Returns null if charges hit 0.
63 */
64export function normalizeChargeStack (stack)
65{
66 if (!stack?.itemId)
67 { return stack ?? null; }
68 if (stack.itemId !== CraftingItemId.tinderbox)
69 { return stack; }
70 const max = itemMaxCharges(stack.itemId) ?? TINDERBOX_MAX_CHARGES;
71 let charges;
72 if (stack.instance?.kind === "tinderbox")
73 { charges = stack.instance.charges | 0; }
74 else
75 { charges = max; }
76 if (charges > max)
77 { charges = max; }
78 if (charges <= 0)
79 { return null; }
80 return {
81 ...stack,
82 instance: tinderboxItemInstance(charges)
83 };
84}
shared/items/inventory.js +11 −4
@@ -4,6 +4,7 @@ export const EQUIPMENT_SLOTS = Object.freeze(["head", "chest", "legs", "boots",
4 4 import { attachOrigins, mergeOriginLists, normalizeOrigins, splitOrigins } from "./origin.js";
5 5 import { stackRulesFor } from "./stackRules.js";
6 6 import { BLANK_CHEQUE_CARRY_CAP, ItemId, itemDefinition } from "../definitions/items.js";
7 import { normalizeChargeStack } from "./charges.js";
7 8
8 9 /** Stack counts can exceed 32-bit range (coin stacks up to 999B). */
9 10 export function stackQuantity (value)
@@ -566,7 +567,8 @@ export function migrateInheritanceCoins (inheritance)
566 567 /** Trim excess blank cheques in inventory (e.g. after legacy saves or bugs). */
567 568 export function normalizeInventorySlots (slots)
568 569 {
569 return capItemTotalInSlots(slots ?? emptySlots(), ItemId.blankCheque, BLANK_CHEQUE_CARRY_CAP);
570 const capped = capItemTotalInSlots(slots ?? emptySlots(), ItemId.blankCheque, BLANK_CHEQUE_CARRY_CAP);
571 return capped.map((slot) => normalizeChargeStack(slot));
570 572 }
571 573
572 574 export function capItemTotalInSlots (slots, itemId, cap)
@@ -597,10 +599,15 @@ export function capItemTotalInSlots (slots, itemId, cap)
597 599 export function normalizeVault (raw)
598 600 {
599 601 if (Array.isArray(raw))
600 { return { slots: raw.map(cloneStack), reserved: [] }; }
602 {
603 return {
604 slots: raw.map((slot) => normalizeChargeStack(cloneStack(slot))),
605 reserved: []
606 };
607 }
601 608 return {
602 slots: (raw?.slots ?? []).map(cloneStack),
603 reserved: (raw?.reserved ?? []).map(cloneStack)
609 slots: (raw?.slots ?? []).map((slot) => normalizeChargeStack(cloneStack(slot))),
610 reserved: (raw?.reserved ?? []).map((slot) => normalizeChargeStack(cloneStack(slot)))
604 611 };
605 612 }
606 613
@@ -4,6 +4,7 @@ export const EQUIPMENT_SLOTS = Object.freeze(["head", "chest", "legs", "boots",
4import { attachOrigins, mergeOriginLists, normalizeOrigins, splitOrigins } from "./origin.js";
4import { attachOrigins, mergeOriginLists, normalizeOrigins, splitOrigins } from "./origin.js";
5import { stackRulesFor } from "./stackRules.js";
5import { stackRulesFor } from "./stackRules.js";
6import { BLANK_CHEQUE_CARRY_CAP, ItemId, itemDefinition } from "../definitions/items.js";
6import { BLANK_CHEQUE_CARRY_CAP, ItemId, itemDefinition } from "../definitions/items.js";
7import { normalizeChargeStack } from "./charges.js";
7
8
8/** Stack counts can exceed 32-bit range (coin stacks up to 999B). */
9/** Stack counts can exceed 32-bit range (coin stacks up to 999B). */
9export function stackQuantity (value)
10export function stackQuantity (value)
@@ -566,7 +567,8 @@ export function migrateInheritanceCoins (inheritance)
566/** Trim excess blank cheques in inventory (e.g. after legacy saves or bugs). */
567/** Trim excess blank cheques in inventory (e.g. after legacy saves or bugs). */
567export function normalizeInventorySlots (slots)
568export function normalizeInventorySlots (slots)
568{
569{
569 return capItemTotalInSlots(slots ?? emptySlots(), ItemId.blankCheque, BLANK_CHEQUE_CARRY_CAP);
570 const capped = capItemTotalInSlots(slots ?? emptySlots(), ItemId.blankCheque, BLANK_CHEQUE_CARRY_CAP);
571 return capped.map((slot) => normalizeChargeStack(slot));
570}
572}
571
573
572export function capItemTotalInSlots (slots, itemId, cap)
574export function capItemTotalInSlots (slots, itemId, cap)
@@ -597,10 +599,15 @@ export function capItemTotalInSlots (slots, itemId, cap)
597export function normalizeVault (raw)
599export function normalizeVault (raw)
598{
600{
599 if (Array.isArray(raw))
601 if (Array.isArray(raw))
600 { return { slots: raw.map(cloneStack), reserved: [] }; }
602 {
603 return {
604 slots: raw.map((slot) => normalizeChargeStack(cloneStack(slot))),
605 reserved: []
606 };
607 }
601 return {
608 return {
602 slots: (raw?.slots ?? []).map(cloneStack),
609 slots: (raw?.slots ?? []).map((slot) => normalizeChargeStack(cloneStack(slot))),
603 reserved: (raw?.reserved ?? []).map(cloneStack)
610 reserved: (raw?.reserved ?? []).map((slot) => normalizeChargeStack(cloneStack(slot)))
604 };
611 };
605}
612}
606
613
shared/items/marketplace.js +7 −1
@@ -1,5 +1,6 @@
1 1 import { itemDefinition } from "../definitions/items.js";
2 2 import { isLetterStack } from "./letters.js";
3 import { isChargeWorn } from "./charges.js";
3 4
4 5 /** Whether this stack may be listed as a marketplace sell order. */
5 6 export function canSellOnMarketplace (stack, characterId = null)
@@ -13,6 +14,9 @@ export function canSellOnMarketplace (stack, characterId = null)
13 14 // Belt-and-suspenders: letters never list, even if a def flag is missing.
14 15 if (isLetterStack(stack))
15 16 { return false; }
17 // Partially used charge tools (e.g. worn tinderbox) stay off the CE.
18 if (isChargeWorn(stack))
19 { return false; }
16 20 return true;
17 21 }
18 22
@@ -26,7 +30,9 @@ export function assertMarketplaceSellable (stack, characterId = null)
26 30 def?.marketplaceSellBlockedReason
27 31 ?? (isLetterStack(stack)
28 32 ? "Letters cannot be sold on the marketplace."
29 : "That item cannot be sold on the marketplace.")
33 : (isChargeWorn(stack)
34 ? "Partially used tools cannot be sold on the marketplace."
35 : "That item cannot be sold on the marketplace."))
30 36 );
31 37 error.userFacing = true;
32 38 throw error;
@@ -1,5 +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";
3
4
4/** Whether this stack may be listed as a marketplace sell order. */
5/** Whether this stack may be listed as a marketplace sell order. */
5export function canSellOnMarketplace (stack, characterId = null)
6export function canSellOnMarketplace (stack, characterId = null)
@@ -13,6 +14,9 @@ export function canSellOnMarketplace (stack, characterId = null)
13 // 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.
14 if (isLetterStack(stack))
15 if (isLetterStack(stack))
15 { return false; }
16 { return false; }
17 // Partially used charge tools (e.g. worn tinderbox) stay off the CE.
18 if (isChargeWorn(stack))
19 { return false; }
16 return true;
20 return true;
17}
21}
18
22
@@ -26,7 +30,9 @@ export function assertMarketplaceSellable (stack, characterId = null)
26 def?.marketplaceSellBlockedReason
30 def?.marketplaceSellBlockedReason
27 ?? (isLetterStack(stack)
31 ?? (isLetterStack(stack)
28 ? "Letters cannot be sold on the marketplace."
32 ? "Letters cannot be sold on the marketplace."
29 : "That item cannot be sold on the marketplace.")
33 : (isChargeWorn(stack)
34 ? "Partially used tools cannot be sold on the marketplace."
35 : "That item cannot be sold on the marketplace."))
30 );
36 );
31 error.userFacing = true;
37 error.userFacing = true;
32 throw error;
38 throw error;
shared/market/funds.js +3 −0
@@ -1,5 +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 4
4 5 /** Filled cheque that reserves coins (usable as market payment, whole only). */
5 6 export function isCoinChequeStack (stack)
@@ -152,6 +153,8 @@ export function resolveMarketSellListing (stack)
152 153 const def = itemDefinition(stack.itemId);
153 154 if (!def || def.marketplaceSellable === false)
154 155 { return null; }
156 if (isChargeWorn(stack))
157 { return null; }
155 158 return {
156 159 isCheque: false,
157 160 itemId: stack.itemId | 0,
@@ -1,5 +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";
3
4
4/** Filled cheque that reserves coins (usable as market payment, whole only). */
5/** Filled cheque that reserves coins (usable as market payment, whole only). */
5export function isCoinChequeStack (stack)
6export function isCoinChequeStack (stack)
@@ -152,6 +153,8 @@ export function resolveMarketSellListing (stack)
152 const def = itemDefinition(stack.itemId);
153 const def = itemDefinition(stack.itemId);
153 if (!def || def.marketplaceSellable === false)
154 if (!def || def.marketplaceSellable === false)
154 { return null; }
155 { return null; }
156 if (isChargeWorn(stack))
157 { return null; }
155 return {
158 return {
156 isCheque: false,
159 isCheque: false,
157 itemId: stack.itemId | 0,
160 itemId: stack.itemId | 0,
shared/shop/pricing.js +11 −5
@@ -6,6 +6,7 @@
6 6 import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js";
7 7 import { ItemCategory } from "../definitions/itemCategories.js";
8 8 import { DEFAULT_WORLD_SETTINGS } from "../protocol/constants.js";
9 import { chargeFractionRemaining } from "../items/charges.js";
9 10
10 11 export function lerp (a, b, t)
11 12 {
@@ -129,11 +130,13 @@ export function shopBuyFromPlayerPrice (itemId, {
129 130 lastSale = null,
130 131 now = Date.now(),
131 132 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
132 settings = null
133 settings = null,
134 stack = null
133 135 } = {})
134 136 {
137 const frac = stack ? chargeFractionRemaining(stack) : 1;
135 138 return marketValueAsCoins(
136 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 0.8
139 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 0.8 * frac
137 140 );
138 141 }
139 142
@@ -142,11 +145,13 @@ export function shopBoughtResalePrice (itemId, {
142 145 lastSale = null,
143 146 now = Date.now(),
144 147 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
145 settings = null
148 settings = null,
149 stack = null
146 150 } = {})
147 151 {
152 const frac = stack ? chargeFractionRemaining(stack) : 1;
148 153 return marketValueAsCoins(
149 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 1.2
154 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 1.2 * frac
150 155 );
151 156 }
152 157
@@ -303,7 +308,8 @@ export function resolveShopSellQuote (stack, {
303 308 lastSale,
304 309 now,
305 310 lerpDurationMs,
306 settings
311 settings,
312 stack
307 313 });
308 314 const baseTotal = unitPrice * goodsQuantity;
309 315 return {
@@ -6,6 +6,7 @@
6import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js";
6import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "../definitions/items.js";
7import { ItemCategory } from "../definitions/itemCategories.js";
7import { ItemCategory } from "../definitions/itemCategories.js";
8import { DEFAULT_WORLD_SETTINGS } from "../protocol/constants.js";
8import { DEFAULT_WORLD_SETTINGS } from "../protocol/constants.js";
9import { chargeFractionRemaining } from "../items/charges.js";
9
10
10export function lerp (a, b, t)
11export function lerp (a, b, t)
11{
12{
@@ -129,11 +130,13 @@ export function shopBuyFromPlayerPrice (itemId, {
129 lastSale = null,
130 lastSale = null,
130 now = Date.now(),
131 now = Date.now(),
131 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
132 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
132 settings = null
133 settings = null,
134 stack = null
133} = {})
135} = {})
134{
136{
137 const frac = stack ? chargeFractionRemaining(stack) : 1;
135 return marketValueAsCoins(
138 return marketValueAsCoins(
136 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 0.8
139 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 0.8 * frac
137 );
140 );
138}
141}
139
142
@@ -142,11 +145,13 @@ export function shopBoughtResalePrice (itemId, {
142 lastSale = null,
145 lastSale = null,
143 now = Date.now(),
146 now = Date.now(),
144 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
147 lerpDurationMs = DEFAULT_WORLD_SETTINGS.shopPriceLerpDurationMs,
145 settings = null
148 settings = null,
149 stack = null
146} = {})
150} = {})
147{
151{
152 const frac = stack ? chargeFractionRemaining(stack) : 1;
148 return marketValueAsCoins(
153 return marketValueAsCoins(
149 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 1.2
154 undiscountedMarketValue(itemId, lastSale, now, lerpDurationMs, settings) * 1.2 * frac
150 );
155 );
151}
156}
152
157
@@ -303,7 +308,8 @@ export function resolveShopSellQuote (stack, {
303 lastSale,
308 lastSale,
304 now,
309 now,
305 lerpDurationMs,
310 lerpDurationMs,
306 settings
311 settings,
312 stack
307 });
313 });
308 const baseTotal = unitPrice * goodsQuantity;
314 const baseTotal = unitPrice * goodsQuantity;
309 return {
315 return {