| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import |
| 4 | { |
| 5 | shopUnitPrice, |
| 6 | undiscountedMarketValue, |
| 7 | undiscountedMarketCoins, |
| 8 | marketValueAsCoins, |
| 9 | canSellInNpcShop, |
| 10 | canPlayerSellToNpc, |
| 11 | shopBuyFromPlayerPrice, |
| 12 | shopBoughtResalePrice, |
| 13 | shopChequeAdminFee, |
| 14 | resolveShopSellQuote, |
| 15 | shopRestockDayKey, |
| 16 | lerp |
| 17 | } from "../../shared/shop/pricing.js"; |
| 18 | import { ItemId, listAllItemPickerEntries } from "../../shared/definitions/items.js"; |
| 19 | import { JewelryItemId } from "../../shared/definitions/jewelryItems.js"; |
| 20 | import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js"; |
| 21 | import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js"; |
| 22 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 23 | import { Component } from "../src/ecs/components.js"; |
| 24 | import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; |
| 25 | import { ItemOriginKind } from "../../shared/items/origin.js"; |
| 26 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 27 | import { ensureShopRestocked, boughtStockKey } from "../src/shop/service.js"; |
| 28 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 29 | import { GatherOriginVerb, gatherOrigin } from "../../shared/items/origin.js"; |
| 30 | import { dyeItemInstance, DEFAULT_DYE_FILL, FlowerColorId } from "../../shared/definitions/dyes.js"; |
| 31 | |
| 32 | function boughtQty (shop, itemId) |
| 33 | { |
| 34 | const raw = shop?.boughtStock?.[itemId]; |
| 35 | if (raw == null) |
| 36 | { return 0; } |
| 37 | if (typeof raw === "number") |
| 38 | { return raw | 0; } |
| 39 | return raw.quantity | 0 || 0; |
| 40 | } |
| 41 | |
| 42 | test("listAllItemPickerEntries includes paper and bronze gather tools", () => |
| 43 | { |
| 44 | const ids = new Set(listAllItemPickerEntries().map((entry) => entry.itemId)); |
| 45 | assert.ok(ids.has(ItemId.paper)); |
| 46 | assert.ok(ids.has(CraftingItemId.bronzeAxe)); |
| 47 | assert.ok(ids.has(CraftingItemId.bronzePickaxe)); |
| 48 | assert.equal(canSellInNpcShop(ItemId.paper), false); |
| 49 | assert.equal(canSellInNpcShop(CraftingItemId.bronzeAxe), false); |
| 50 | }); |
| 51 | |
| 52 | test("buy paper from a shopkeeper with explicit catalog", () => |
| 53 | { |
| 54 | const harness = makeSimulation({ workerCount: 1 }); |
| 55 | try |
| 56 | { |
| 57 | const data = createEmptyChunkData(); |
| 58 | data.open = true; |
| 59 | data.spawners = [{ |
| 60 | localX: 9, |
| 61 | localY: 8, |
| 62 | config: { |
| 63 | mobTypeId: MobId.humanoid, |
| 64 | maxAlive: 1, |
| 65 | spawnRangeTiles: 1, |
| 66 | maxWanderRangeTiles: 1, |
| 67 | sex: "male", |
| 68 | displayName: "Stationer", |
| 69 | sells: [{ itemId: ItemId.paper, quantity: 0 }], |
| 70 | hiddenDiscount: 1, |
| 71 | equipment: { chest: ItemId.clothShirt } |
| 72 | } |
| 73 | }]; |
| 74 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 75 | harness.simulation.reloadWorld(); |
| 76 | |
| 77 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 78 | const funded = addCoinsToSlots(emptySlots(), 20); |
| 79 | assert.equal(funded.ok, true); |
| 80 | harness.stateDb.saveBag(character.character_id, { |
| 81 | inventory: funded.slots, |
| 82 | equipment: emptyEquipment(), |
| 83 | vault: [], |
| 84 | vaultReserved: [] |
| 85 | }); |
| 86 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 87 | harness.simulation.step(); |
| 88 | harness.advance(50); |
| 89 | harness.simulation.step(); |
| 90 | |
| 91 | const record = harness.simulation.entityRecord(playerId); |
| 92 | const worker = harness.simulation.workers[record.workerId]; |
| 93 | let npcId = null; |
| 94 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 95 | { |
| 96 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 97 | { |
| 98 | npcId = entity.entityId; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | assert.ok(npcId, "expected a humanoid shopkeeper"); |
| 103 | |
| 104 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 105 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 106 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 107 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 108 | playerPos.tileX = npcPos.tileX - 1; |
| 109 | playerPos.tileY = npcPos.tileY; |
| 110 | |
| 111 | harness.simulation.enqueue(1, { |
| 112 | type: "interact", |
| 113 | characterId: character.character_id, |
| 114 | targetEntityId: npcId |
| 115 | }); |
| 116 | const open = harness.simulation.step(); |
| 117 | const shopOpen = open.events.find((event) => event.type === "shop-open"); |
| 118 | assert.ok(shopOpen, JSON.stringify(open.events)); |
| 119 | assert.ok(shopOpen.stock.some((row) => row.itemId === ItemId.paper)); |
| 120 | |
| 121 | harness.simulation.enqueue(1, { |
| 122 | type: "buy-from-shop", |
| 123 | characterId: character.character_id, |
| 124 | npcEntityId: npcId, |
| 125 | itemId: ItemId.paper, |
| 126 | quantity: 1, |
| 127 | listingId: `stock:${ItemId.paper}` |
| 128 | }); |
| 129 | const buy = harness.simulation.step(); |
| 130 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 131 | const bag = harness.simulation.bagOf(playerId); |
| 132 | const paper = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.paper); |
| 133 | assert.ok(paper, JSON.stringify(bag.inventory)); |
| 134 | } |
| 135 | finally |
| 136 | { harness.close(); } |
| 137 | }); |
| 138 | |
| 139 | test("shop prices fall back to seedValue and apply hidden discount", () => |
| 140 | { |
| 141 | assert.equal(undiscountedMarketValue(ItemId.copperOre), 5); |
| 142 | assert.equal(undiscountedMarketValue(ItemId.tinOre), 5); |
| 143 | assert.equal(undiscountedMarketValue(ItemId.pineLog), 5); |
| 144 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.9 }), 5); |
| 145 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.5 }), 3); |
| 146 | assert.equal(shopBuyFromPlayerPrice(ItemId.copperOre), 3); |
| 147 | assert.equal(shopBoughtResalePrice(ItemId.copperOre), 6); |
| 148 | assert.equal(lerp(10, 20, 0.5), 15); |
| 149 | assert.equal(canSellInNpcShop(ItemId.copperOre), true); |
| 150 | assert.equal(canSellInNpcShop(ItemId.tinOre), true); |
| 151 | assert.equal(canSellInNpcShop(ItemId.stick), false); |
| 152 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.copperOre, quantity: 1 }), true); |
| 153 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.stick, quantity: 1 }), true); |
| 154 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.blankCheque, quantity: 1 }), false); |
| 155 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.coins, quantity: 10 }), false); |
| 156 | assert.equal(canPlayerSellToNpc({ |
| 157 | itemId: ItemId.recipePaper, |
| 158 | quantity: 1, |
| 159 | instance: { kind: "recipe-paper", recipeId: "lesser-health-tonic" } |
| 160 | }), false); |
| 161 | assert.equal(canPlayerSellToNpc({ |
| 162 | itemId: ItemId.filledCheque, |
| 163 | quantity: 1, |
| 164 | instance: { kind: "filled-cheque", itemId: ItemId.copperOre, quantity: 3 } |
| 165 | }), true); |
| 166 | assert.equal(shopChequeAdminFee(100), 5); |
| 167 | assert.equal(shopChequeAdminFee(10), 1); |
| 168 | assert.equal(shopChequeAdminFee(1), 1); |
| 169 | const chequeQuote = resolveShopSellQuote({ |
| 170 | itemId: ItemId.filledCheque, |
| 171 | quantity: 1, |
| 172 | instance: { kind: "filled-cheque", itemId: ItemId.copperOre, quantity: 2 } |
| 173 | }); |
| 174 | assert.equal(chequeQuote.baseTotal, shopBuyFromPlayerPrice(ItemId.copperOre) * 2); |
| 175 | assert.equal(chequeQuote.adminFee, shopChequeAdminFee(chequeQuote.baseTotal)); |
| 176 | assert.equal(chequeQuote.payout, chequeQuote.baseTotal - chequeQuote.adminFee); |
| 177 | }); |
| 178 | |
| 179 | test("charge-worn tools scale merchant buy and resale prices by remaining uses", () => |
| 180 | { |
| 181 | const full = { |
| 182 | itemId: CraftingItemId.tinderbox, |
| 183 | quantity: 1, |
| 184 | instance: tinderboxItemInstance(6) |
| 185 | }; |
| 186 | const worn = { |
| 187 | itemId: CraftingItemId.tinderbox, |
| 188 | quantity: 1, |
| 189 | instance: tinderboxItemInstance(1) |
| 190 | }; |
| 191 | const fullBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: full }); |
| 192 | const wornBuy = shopBuyFromPlayerPrice(CraftingItemId.tinderbox, { stack: worn }); |
| 193 | assert.equal(fullBuy, shopBuyFromPlayerPrice(CraftingItemId.tinderbox)); |
| 194 | assert.equal(wornBuy, marketValueAsCoins( |
| 195 | undiscountedMarketValue(CraftingItemId.tinderbox) * 0.5 * (1 / 6) |
| 196 | )); |
| 197 | const fullResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: full }); |
| 198 | const wornResale = shopBoughtResalePrice(CraftingItemId.tinderbox, { stack: worn }); |
| 199 | assert.equal(fullResale, shopBoughtResalePrice(CraftingItemId.tinderbox)); |
| 200 | assert.equal(wornResale, marketValueAsCoins( |
| 201 | undiscountedMarketValue(CraftingItemId.tinderbox) * 1.2 * (1 / 6) |
| 202 | )); |
| 203 | const quote = resolveShopSellQuote(worn); |
| 204 | assert.equal(quote.unitPrice, wornBuy); |
| 205 | }); |
| 206 | |
| 207 | test("market value display and shop use ceil fractional lerps", () => |
| 208 | { |
| 209 | const now = 1_000_000; |
| 210 | const lastSale = { |
| 211 | lastTradePrice: 10, |
| 212 | lastTradeAt: now - 500, |
| 213 | priceFrom: 5 |
| 214 | }; |
| 215 | // Halfway from 5 → 10 over 1000ms → 7.5 internally. |
| 216 | const raw = undiscountedMarketValue(ItemId.copperOre, lastSale, now, 1000); |
| 217 | assert.equal(raw, 7.5); |
| 218 | assert.equal(marketValueAsCoins(raw), 8); |
| 219 | assert.equal(undiscountedMarketCoins(ItemId.copperOre, lastSale, now, 1000), 8); |
| 220 | assert.equal(shopBuyFromPlayerPrice(ItemId.copperOre, { lastSale, now, lerpDurationMs: 1000 }), 4); |
| 221 | assert.equal(shopBoughtResalePrice(ItemId.copperOre, { lastSale, now, lerpDurationMs: 1000 }), 9); |
| 222 | assert.equal(shopUnitPrice(ItemId.copperOre, { |
| 223 | lastSale, |
| 224 | now, |
| 225 | lerpDurationMs: 1000, |
| 226 | hiddenDiscount: 1 |
| 227 | }), 8); |
| 228 | }); |
| 229 | |
| 230 | test("humanoid spawner config keeps sex equipment sells quantities and buysItems", () => |
| 231 | { |
| 232 | assert.equal(isHumanoidMob(MobId.humanoid), true); |
| 233 | const config = normalizeSpawnerConfig({ |
| 234 | mobTypeId: MobId.humanoid, |
| 235 | sex: "female", |
| 236 | displayName: "Ore trader", |
| 237 | equipment: { chest: ItemId.clothShirt, weapon: ItemId.stick }, |
| 238 | sells: [ |
| 239 | { itemId: ItemId.copperOre, quantity: 10 }, |
| 240 | { itemId: ItemId.tinOre, quantity: 0 }, |
| 241 | ItemId.copperOre |
| 242 | ], |
| 243 | buysItems: true, |
| 244 | hiddenDiscount: 0.85 |
| 245 | }, "shop-1"); |
| 246 | assert.equal(config.sex, "female"); |
| 247 | assert.equal(config.displayName, "Ore trader"); |
| 248 | assert.equal(config.equipment.chest, ItemId.clothShirt); |
| 249 | assert.deepEqual(config.sells, [ |
| 250 | { itemId: ItemId.copperOre, quantity: 10 }, |
| 251 | { itemId: ItemId.tinOre, quantity: 0 } |
| 252 | ]); |
| 253 | assert.equal(config.buysItems, true); |
| 254 | assert.equal(config.hiddenDiscount, 0.85); |
| 255 | assert.equal(config.maxAlive, 1); |
| 256 | }); |
| 257 | |
| 258 | test("humanoid spawner config keeps jewelry equipment slots", () => |
| 259 | { |
| 260 | const config = normalizeSpawnerConfig({ |
| 261 | mobTypeId: MobId.humanoid, |
| 262 | equipment: { |
| 263 | ring: JewelryItemId.opalRing, |
| 264 | bracelet: JewelryItemId.diamondBracelet, |
| 265 | amulet: JewelryItemId.rubyAmulet |
| 266 | } |
| 267 | }, "jeweler-npc"); |
| 268 | assert.equal(config.equipment.ring, JewelryItemId.opalRing); |
| 269 | assert.equal(config.equipment.bracelet, JewelryItemId.diamondBracelet); |
| 270 | assert.equal(config.equipment.amulet, JewelryItemId.rubyAmulet); |
| 271 | }); |
| 272 | |
| 273 | test("shopkeeper with buysItems accepts paper from the player", () => |
| 274 | { |
| 275 | const harness = makeSimulation({ workerCount: 1 }); |
| 276 | try |
| 277 | { |
| 278 | const data = createEmptyChunkData(); |
| 279 | data.open = true; |
| 280 | data.spawners = [{ |
| 281 | localX: 9, |
| 282 | localY: 8, |
| 283 | config: { |
| 284 | mobTypeId: MobId.humanoid, |
| 285 | maxAlive: 1, |
| 286 | spawnRangeTiles: 0, |
| 287 | maxWanderRangeTiles: 0, |
| 288 | displayName: "Stationer", |
| 289 | buysItems: true |
| 290 | } |
| 291 | }]; |
| 292 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 293 | harness.simulation.reloadWorld(); |
| 294 | |
| 295 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 296 | const slots = emptySlots(INVENTORY_SLOTS); |
| 297 | slots[0] = { itemId: ItemId.paper, quantity: 3 }; |
| 298 | harness.stateDb.saveBag(character.character_id, { |
| 299 | inventory: slots, |
| 300 | equipment: emptyEquipment(), |
| 301 | vault: [], |
| 302 | vaultReserved: [] |
| 303 | }); |
| 304 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 305 | harness.simulation.step(); |
| 306 | harness.advance(50); |
| 307 | harness.simulation.step(); |
| 308 | |
| 309 | const record = harness.simulation.entityRecord(playerId); |
| 310 | const worker = harness.simulation.workers[record.workerId]; |
| 311 | let npcId = null; |
| 312 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 313 | { |
| 314 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 315 | { |
| 316 | npcId = entity.entityId; |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | assert.ok(npcId); |
| 321 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 322 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 323 | assert.equal(shop.buysItems, true); |
| 324 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 325 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 326 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 327 | playerPos.tileX = npcPos.tileX - 1; |
| 328 | playerPos.tileY = npcPos.tileY; |
| 329 | |
| 330 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 331 | harness.simulation.enqueue(1, { |
| 332 | type: "sell-to-shop", |
| 333 | characterId: character.character_id, |
| 334 | npcEntityId: npcId, |
| 335 | slotIndex: 0, |
| 336 | quantity: 1 |
| 337 | }); |
| 338 | const sold = harness.simulation.step(); |
| 339 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 340 | const inv = worker.world.ecs.get(playerHandle, Component.inventory); |
| 341 | assert.equal(inv.slots[0]?.quantity, 2); |
| 342 | assert.ok(countCoinsInSlots(inv.slots) >= beforeCoins + shopBuyFromPlayerPrice(ItemId.paper)); |
| 343 | } |
| 344 | finally |
| 345 | { harness.close(); } |
| 346 | }); |
| 347 | |
| 348 | test("humanoid spawner equipment can store clothing dye color", () => |
| 349 | { |
| 350 | const dyeColor = { r: 0.2, g: 0.4, b: 0.8 }; |
| 351 | const config = normalizeSpawnerConfig({ |
| 352 | mobTypeId: MobId.humanoid, |
| 353 | equipment: { |
| 354 | chest: { itemId: ItemId.clothShirt, dyeColor }, |
| 355 | weapon: ItemId.stick |
| 356 | } |
| 357 | }, "shop-dye"); |
| 358 | assert.deepEqual(config.equipment.chest, { |
| 359 | itemId: ItemId.clothShirt, |
| 360 | dyeColor |
| 361 | }); |
| 362 | assert.equal(config.equipment.weapon, ItemId.stick); |
| 363 | }); |
| 364 | |
| 365 | test("buy copper ore from a spawned shopkeeper", () => |
| 366 | { |
| 367 | const harness = makeSimulation({ workerCount: 1 }); |
| 368 | try |
| 369 | { |
| 370 | const data = createEmptyChunkData(); |
| 371 | data.open = true; |
| 372 | data.spawners = [{ |
| 373 | localX: 9, |
| 374 | localY: 8, |
| 375 | config: { |
| 376 | mobTypeId: MobId.humanoid, |
| 377 | maxAlive: 1, |
| 378 | spawnRangeTiles: 1, |
| 379 | maxWanderRangeTiles: 1, |
| 380 | sex: "male", |
| 381 | displayName: "Ore trader", |
| 382 | sells: [ |
| 383 | { itemId: ItemId.copperOre, quantity: 0 }, |
| 384 | { itemId: ItemId.tinOre, quantity: 0 } |
| 385 | ], |
| 386 | hiddenDiscount: 1, |
| 387 | equipment: { chest: ItemId.clothShirt } |
| 388 | } |
| 389 | }]; |
| 390 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 391 | harness.simulation.reloadWorld(); |
| 392 | |
| 393 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 394 | const funded = addCoinsToSlots(emptySlots(), 50); |
| 395 | assert.equal(funded.ok, true); |
| 396 | harness.stateDb.saveBag(character.character_id, { |
| 397 | inventory: funded.slots, |
| 398 | equipment: emptyEquipment(), |
| 399 | vault: [], |
| 400 | vaultReserved: [] |
| 401 | }); |
| 402 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 403 | harness.simulation.step(); |
| 404 | harness.advance(50); |
| 405 | harness.simulation.step(); |
| 406 | |
| 407 | const record = harness.simulation.entityRecord(playerId); |
| 408 | const worker = harness.simulation.workers[record.workerId]; |
| 409 | let npcId = null; |
| 410 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 411 | { |
| 412 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 413 | { |
| 414 | npcId = entity.entityId; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | assert.ok(npcId, "expected a humanoid shopkeeper"); |
| 419 | |
| 420 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 421 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 422 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 423 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 424 | playerPos.tileX = npcPos.tileX - 1; |
| 425 | playerPos.tileY = npcPos.tileY; |
| 426 | |
| 427 | harness.simulation.enqueue(1, { |
| 428 | type: "interact", |
| 429 | characterId: character.character_id, |
| 430 | targetEntityId: npcId |
| 431 | }); |
| 432 | const open = harness.simulation.step(); |
| 433 | const shopOpen = open.events.find((event) => event.type === "shop-open"); |
| 434 | assert.ok(shopOpen, JSON.stringify(open.events)); |
| 435 | assert.ok(shopOpen.stock.some((row) => row.itemId === ItemId.copperOre)); |
| 436 | |
| 437 | harness.simulation.enqueue(1, { |
| 438 | type: "buy-from-shop", |
| 439 | characterId: character.character_id, |
| 440 | npcEntityId: npcId, |
| 441 | itemId: ItemId.copperOre, |
| 442 | quantity: 1, |
| 443 | listingId: `stock:${ItemId.copperOre}` |
| 444 | }); |
| 445 | const buy = harness.simulation.step(); |
| 446 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 447 | const bag = harness.simulation.bagOf(playerId); |
| 448 | const ore = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.copperOre); |
| 449 | assert.ok(ore, JSON.stringify(bag.inventory)); |
| 450 | assert.equal(ore.origins?.[0]?.origin?.kind, ItemOriginKind.shop); |
| 451 | assert.equal(ore.origins?.[0]?.origin?.displayName, "Ore trader"); |
| 452 | } |
| 453 | finally |
| 454 | { harness.close(); } |
| 455 | }); |
| 456 | |
| 457 | test("limited shop stock depletes and restocks on a new day", () => |
| 458 | { |
| 459 | const shop = { |
| 460 | catalog: [{ itemId: ItemId.copperOre, quantity: 2 }], |
| 461 | buysItems: false, |
| 462 | hiddenDiscount: 1, |
| 463 | remaining: Object.create(null), |
| 464 | boughtStock: Object.create(null), |
| 465 | restockDay: null |
| 466 | }; |
| 467 | const dayA = new Date(2026, 7, 25, 12, 0, 0, 0).getTime(); |
| 468 | ensureShopRestocked(shop, dayA); |
| 469 | assert.equal(shop.restockDay, shopRestockDayKey(dayA)); |
| 470 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 471 | shop.remaining[ItemId.copperOre] = 0; |
| 472 | shop.boughtStock[ItemId.tinOre] = 3; |
| 473 | ensureShopRestocked(shop, dayA); |
| 474 | assert.equal(shop.remaining[ItemId.copperOre], 0); |
| 475 | assert.equal(shop.boughtStock[ItemId.tinOre], 3); |
| 476 | const dayB = new Date(2026, 7, 26, 0, 0, 1, 0).getTime(); |
| 477 | ensureShopRestocked(shop, dayB); |
| 478 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 479 | assert.equal(shop.boughtStock[ItemId.tinOre], undefined); |
| 480 | }); |
| 481 | |
| 482 | test("shop overflow goes to bank vault when inventory is full", () => |
| 483 | { |
| 484 | const harness = makeSimulation({ workerCount: 1 }); |
| 485 | try |
| 486 | { |
| 487 | const data = createEmptyChunkData(); |
| 488 | data.open = true; |
| 489 | data.spawners = [{ |
| 490 | localX: 9, |
| 491 | localY: 8, |
| 492 | config: { |
| 493 | mobTypeId: MobId.humanoid, |
| 494 | maxAlive: 1, |
| 495 | spawnRangeTiles: 0, |
| 496 | maxWanderRangeTiles: 0, |
| 497 | displayName: "Ore trader", |
| 498 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 499 | hiddenDiscount: 1 |
| 500 | } |
| 501 | }]; |
| 502 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 503 | harness.simulation.reloadWorld(); |
| 504 | |
| 505 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 506 | const slots = emptySlots(); |
| 507 | const funded = addCoinsToSlots(slots, 50); |
| 508 | assert.equal(funded.ok, true); |
| 509 | for (let i = 0; i < funded.slots.length; i++) |
| 510 | { |
| 511 | if (!funded.slots[i]) |
| 512 | { funded.slots[i] = { itemId: ItemId.stick, quantity: 1 }; } |
| 513 | } |
| 514 | harness.stateDb.saveBag(character.character_id, { |
| 515 | inventory: funded.slots, |
| 516 | equipment: emptyEquipment(), |
| 517 | vault: [], |
| 518 | vaultReserved: [] |
| 519 | }); |
| 520 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 521 | harness.simulation.step(); |
| 522 | harness.advance(50); |
| 523 | harness.simulation.step(); |
| 524 | |
| 525 | const record = harness.simulation.entityRecord(playerId); |
| 526 | const worker = harness.simulation.workers[record.workerId]; |
| 527 | let npcId = null; |
| 528 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 529 | { |
| 530 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 531 | { |
| 532 | npcId = entity.entityId; |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | assert.ok(npcId); |
| 537 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 538 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 539 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 540 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 541 | playerPos.tileX = npcPos.tileX - 1; |
| 542 | playerPos.tileY = npcPos.tileY; |
| 543 | |
| 544 | harness.simulation.enqueue(1, { |
| 545 | type: "buy-from-shop", |
| 546 | characterId: character.character_id, |
| 547 | npcEntityId: npcId, |
| 548 | itemId: ItemId.copperOre, |
| 549 | quantity: 1, |
| 550 | listingId: `stock:${ItemId.copperOre}` |
| 551 | }); |
| 552 | const buy = harness.simulation.step(); |
| 553 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 554 | const bag = harness.simulation.bagOf(playerId); |
| 555 | assert.equal((bag.inventory ?? []).some((slot) => slot?.itemId === ItemId.copperOre), false); |
| 556 | assert.ok((bag.vault ?? []).some((slot) => slot?.itemId === ItemId.copperOre)); |
| 557 | } |
| 558 | finally |
| 559 | { harness.close(); } |
| 560 | }); |
| 561 | |
| 562 | test("buysItems refills limited catalog then stores extras for 120% resale", () => |
| 563 | { |
| 564 | const harness = makeSimulation({ workerCount: 1 }); |
| 565 | try |
| 566 | { |
| 567 | const data = createEmptyChunkData(); |
| 568 | data.open = true; |
| 569 | data.spawners = [{ |
| 570 | localX: 9, |
| 571 | localY: 8, |
| 572 | config: { |
| 573 | mobTypeId: MobId.humanoid, |
| 574 | maxAlive: 1, |
| 575 | spawnRangeTiles: 0, |
| 576 | maxWanderRangeTiles: 0, |
| 577 | displayName: "Buyer", |
| 578 | sells: [{ itemId: ItemId.copperOre, quantity: 2 }], |
| 579 | buysItems: true, |
| 580 | hiddenDiscount: 1 |
| 581 | } |
| 582 | }]; |
| 583 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 584 | harness.simulation.reloadWorld(); |
| 585 | |
| 586 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 587 | harness.stateDb.saveBag(character.character_id, { |
| 588 | inventory: emptySlots(INVENTORY_SLOTS), |
| 589 | equipment: emptyEquipment(), |
| 590 | vault: [], |
| 591 | vaultReserved: [] |
| 592 | }); |
| 593 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 594 | harness.simulation.step(); |
| 595 | harness.advance(50); |
| 596 | harness.simulation.step(); |
| 597 | |
| 598 | const record = harness.simulation.entityRecord(playerId); |
| 599 | const worker = harness.simulation.workers[record.workerId]; |
| 600 | let npcId = null; |
| 601 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 602 | { |
| 603 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 604 | { |
| 605 | npcId = entity.entityId; |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | assert.ok(npcId); |
| 610 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 611 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 612 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 613 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 614 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 615 | playerPos.tileX = npcPos.tileX - 1; |
| 616 | playerPos.tileY = npcPos.tileY; |
| 617 | |
| 618 | // Fund coins and buy out limited stock first. |
| 619 | const inv = worker.world.ecs.get(playerHandle, Component.inventory); |
| 620 | const funded = addCoinsToSlots(inv.slots, 50); |
| 621 | assert.equal(funded.ok, true); |
| 622 | inv.slots = funded.slots; |
| 623 | inv.slots[1] = { itemId: ItemId.copperOre, quantity: 5 }; |
| 624 | |
| 625 | harness.simulation.enqueue(1, { |
| 626 | type: "buy-from-shop", |
| 627 | characterId: character.character_id, |
| 628 | npcEntityId: npcId, |
| 629 | itemId: ItemId.copperOre, |
| 630 | quantity: 2, |
| 631 | listingId: `stock:${ItemId.copperOre}` |
| 632 | }); |
| 633 | const boughtOut = harness.simulation.step(); |
| 634 | assert.equal(boughtOut.events.some((event) => event.type === "command-error"), false, JSON.stringify(boughtOut.events)); |
| 635 | assert.equal(shop.remaining[ItemId.copperOre], 0); |
| 636 | |
| 637 | const oreSlot = inv.slots.findIndex((slot) => slot?.itemId === ItemId.copperOre && (slot.quantity | 0) >= 5); |
| 638 | assert.ok(oreSlot >= 0); |
| 639 | |
| 640 | harness.simulation.enqueue(1, { |
| 641 | type: "sell-to-shop", |
| 642 | characterId: character.character_id, |
| 643 | npcEntityId: npcId, |
| 644 | slotIndex: oreSlot, |
| 645 | quantity: 5 |
| 646 | }); |
| 647 | const sold = harness.simulation.step(); |
| 648 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 649 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 650 | assert.equal(boughtQty(shop, ItemId.copperOre), 3); |
| 651 | |
| 652 | const open = sold.events.find((event) => event.type === "shop-open"); |
| 653 | assert.ok(open.stock.some((row) => row.source === "stock" && row.available === 2)); |
| 654 | assert.ok(open.stock.some((row) => row.source === "bought" && row.available === 3)); |
| 655 | } |
| 656 | finally |
| 657 | { harness.close(); } |
| 658 | }); |
| 659 | |
| 660 | test("shop adjacency errors use gendered pronouns", () => |
| 661 | { |
| 662 | const harness = makeSimulation({ workerCount: 1 }); |
| 663 | try |
| 664 | { |
| 665 | const data = createEmptyChunkData(); |
| 666 | data.open = true; |
| 667 | data.spawners = [{ |
| 668 | localX: 9, |
| 669 | localY: 8, |
| 670 | config: { |
| 671 | mobTypeId: MobId.humanoid, |
| 672 | maxAlive: 1, |
| 673 | spawnRangeTiles: 0, |
| 674 | maxWanderRangeTiles: 0, |
| 675 | sex: "female", |
| 676 | displayName: "Trader", |
| 677 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 678 | hiddenDiscount: 1 |
| 679 | } |
| 680 | }]; |
| 681 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 682 | harness.simulation.reloadWorld(); |
| 683 | |
| 684 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 2 }); |
| 685 | harness.stateDb.saveBag(character.character_id, { |
| 686 | inventory: emptySlots(INVENTORY_SLOTS), |
| 687 | equipment: emptyEquipment(), |
| 688 | vault: [], |
| 689 | vaultReserved: [] |
| 690 | }); |
| 691 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 692 | harness.simulation.step(); |
| 693 | harness.advance(50); |
| 694 | harness.simulation.step(); |
| 695 | |
| 696 | const record = harness.simulation.entityRecord(playerId); |
| 697 | const worker = harness.simulation.workers[record.workerId]; |
| 698 | let npcId = null; |
| 699 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 700 | { |
| 701 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 702 | { |
| 703 | npcId = entity.entityId; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | assert.ok(npcId); |
| 708 | |
| 709 | harness.simulation.enqueue(1, { |
| 710 | type: "interact", |
| 711 | characterId: character.character_id, |
| 712 | targetEntityId: npcId |
| 713 | }); |
| 714 | const open = harness.simulation.step(); |
| 715 | const err = open.events.find((event) => event.type === "command-error"); |
| 716 | assert.ok(err, JSON.stringify(open.events)); |
| 717 | assert.match(err.message, /stand next to her/i); |
| 718 | assert.doesNotMatch(err.message, /\bthem\b/i); |
| 719 | } |
| 720 | finally |
| 721 | { harness.close(); } |
| 722 | }); |
| 723 | |
| 724 | test("shopkeeper stops wandering while a player has the shop open", () => |
| 725 | { |
| 726 | const harness = makeSimulation({ workerCount: 1 }); |
| 727 | try |
| 728 | { |
| 729 | const data = createEmptyChunkData(); |
| 730 | data.open = true; |
| 731 | data.spawners = [{ |
| 732 | localX: 8, |
| 733 | localY: 8, |
| 734 | config: { |
| 735 | mobTypeId: MobId.humanoid, |
| 736 | maxAlive: 1, |
| 737 | spawnRangeTiles: 4, |
| 738 | maxWanderRangeTiles: 8, |
| 739 | sex: "male", |
| 740 | displayName: "Stay put", |
| 741 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 742 | hiddenDiscount: 1 |
| 743 | } |
| 744 | }]; |
| 745 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 746 | harness.simulation.reloadWorld(); |
| 747 | |
| 748 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 749 | harness.stateDb.saveBag(character.character_id, { |
| 750 | inventory: emptySlots(INVENTORY_SLOTS), |
| 751 | equipment: emptyEquipment(), |
| 752 | vault: [], |
| 753 | vaultReserved: [] |
| 754 | }); |
| 755 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 756 | harness.simulation.step(); |
| 757 | harness.advance(50); |
| 758 | harness.simulation.step(); |
| 759 | |
| 760 | const record = harness.simulation.entityRecord(playerId); |
| 761 | const worker = harness.simulation.workers[record.workerId]; |
| 762 | let npc = null; |
| 763 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature, Component.position, Component.path)) |
| 764 | { |
| 765 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 766 | { |
| 767 | npc = entity; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | assert.ok(npc); |
| 772 | |
| 773 | const npcStartX = npc.position.tileX; |
| 774 | const npcStartY = npc.position.tileY; |
| 775 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 776 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 777 | playerPos.tileX = npcStartX - 1; |
| 778 | playerPos.tileY = npcStartY; |
| 779 | |
| 780 | harness.simulation.enqueue(1, { |
| 781 | type: "interact", |
| 782 | characterId: character.character_id, |
| 783 | targetEntityId: npc.entityId |
| 784 | }); |
| 785 | const open = harness.simulation.step(); |
| 786 | assert.ok(open.events.some((event) => event.type === "shop-open")); |
| 787 | assert.equal(!!npc.shop.browsers?.[character.character_id], true); |
| 788 | |
| 789 | npc.path.tiles = [{ x: npc.position.tileX + 1, y: npc.position.tileY }]; |
| 790 | for (let i = 0; i < 20; i++) |
| 791 | { harness.simulation.step(); } |
| 792 | assert.equal(npc.path.tiles.length, 0); |
| 793 | assert.equal(npc.position.tileX, npcStartX); |
| 794 | assert.equal(npc.position.tileY, npcStartY); |
| 795 | |
| 796 | harness.simulation.enqueue(1, { |
| 797 | type: "close-shop", |
| 798 | characterId: character.character_id, |
| 799 | npcEntityId: npc.entityId |
| 800 | }); |
| 801 | harness.simulation.step(); |
| 802 | assert.equal(!!npc.shop.browsers?.[character.character_id], false); |
| 803 | } |
| 804 | finally |
| 805 | { harness.close(); } |
| 806 | }); |
| 807 | |
| 808 | test("buysItems accepts non-ingredient tools like sticks", () => |
| 809 | { |
| 810 | const harness = makeSimulation({ workerCount: 1 }); |
| 811 | try |
| 812 | { |
| 813 | const data = createEmptyChunkData(); |
| 814 | data.open = true; |
| 815 | data.spawners = [{ |
| 816 | localX: 9, |
| 817 | localY: 8, |
| 818 | config: { |
| 819 | mobTypeId: MobId.humanoid, |
| 820 | maxAlive: 1, |
| 821 | spawnRangeTiles: 0, |
| 822 | maxWanderRangeTiles: 0, |
| 823 | displayName: "Buyer", |
| 824 | buysItems: true, |
| 825 | hiddenDiscount: 1 |
| 826 | } |
| 827 | }]; |
| 828 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 829 | harness.simulation.reloadWorld(); |
| 830 | |
| 831 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 832 | const slots = emptySlots(INVENTORY_SLOTS); |
| 833 | slots[0] = { itemId: ItemId.stick, quantity: 1 }; |
| 834 | harness.stateDb.saveBag(character.character_id, { |
| 835 | inventory: slots, |
| 836 | equipment: emptyEquipment(), |
| 837 | vault: [], |
| 838 | vaultReserved: [] |
| 839 | }); |
| 840 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 841 | harness.simulation.step(); |
| 842 | harness.advance(50); |
| 843 | harness.simulation.step(); |
| 844 | |
| 845 | const record = harness.simulation.entityRecord(playerId); |
| 846 | const worker = harness.simulation.workers[record.workerId]; |
| 847 | let npcId = null; |
| 848 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 849 | { |
| 850 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 851 | { |
| 852 | npcId = entity.entityId; |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | assert.ok(npcId); |
| 857 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 858 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 859 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 860 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 861 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 862 | playerPos.tileX = npcPos.tileX - 1; |
| 863 | playerPos.tileY = npcPos.tileY; |
| 864 | |
| 865 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 866 | harness.simulation.enqueue(1, { |
| 867 | type: "sell-to-shop", |
| 868 | characterId: character.character_id, |
| 869 | npcEntityId: npcId, |
| 870 | slotIndex: 0, |
| 871 | quantity: 1 |
| 872 | }); |
| 873 | const sold = harness.simulation.step(); |
| 874 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 875 | const inv = worker.world.ecs.get(playerHandle, Component.inventory); |
| 876 | assert.equal(inv.slots[0]?.itemId === ItemId.stick, false); |
| 877 | assert.ok(countCoinsInSlots(inv.slots) >= beforeCoins + shopBuyFromPlayerPrice(ItemId.stick)); |
| 878 | assert.equal(boughtQty(shop, ItemId.stick), 1); |
| 879 | } |
| 880 | finally |
| 881 | { harness.close(); } |
| 882 | }); |
| 883 | |
| 884 | test("selling a filled cheque pays reserved-goods price minus 5% admin fee", () => |
| 885 | { |
| 886 | const harness = makeSimulation({ workerCount: 1 }); |
| 887 | try |
| 888 | { |
| 889 | const data = createEmptyChunkData(); |
| 890 | data.open = true; |
| 891 | data.blocks[8 * 16 + 8] = { typeId: BlockType.bank, state: 0 }; |
| 892 | data.spawners = [{ |
| 893 | localX: 9, |
| 894 | localY: 8, |
| 895 | config: { |
| 896 | mobTypeId: MobId.humanoid, |
| 897 | maxAlive: 1, |
| 898 | spawnRangeTiles: 0, |
| 899 | maxWanderRangeTiles: 0, |
| 900 | displayName: "Buyer", |
| 901 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 902 | buysItems: true, |
| 903 | hiddenDiscount: 1 |
| 904 | } |
| 905 | }]; |
| 906 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 907 | harness.simulation.reloadWorld(); |
| 908 | |
| 909 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 910 | const slots = emptySlots(INVENTORY_SLOTS); |
| 911 | slots[0] = { itemId: ItemId.copperOre, quantity: 2 }; |
| 912 | harness.stateDb.saveBag(character.character_id, { |
| 913 | inventory: slots, |
| 914 | equipment: emptyEquipment(), |
| 915 | vault: [], |
| 916 | vaultReserved: [] |
| 917 | }); |
| 918 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 919 | harness.simulation.step(); |
| 920 | |
| 921 | harness.simulation.enqueue(1, { |
| 922 | type: "get-blank-cheques", |
| 923 | characterId: character.character_id |
| 924 | }); |
| 925 | harness.simulation.step(); |
| 926 | harness.simulation.enqueue(1, { |
| 927 | type: "bank-deposit", |
| 928 | characterId: character.character_id, |
| 929 | slotIndex: 0, |
| 930 | quantity: null |
| 931 | }); |
| 932 | harness.simulation.step(); |
| 933 | let bag = harness.simulation.bagOf(playerId); |
| 934 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 935 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.copperOre); |
| 936 | harness.simulation.enqueue(1, { |
| 937 | type: "write-cheque", |
| 938 | characterId: character.character_id, |
| 939 | slotIndex: blankIndex, |
| 940 | vaultSlotIndex: vaultIndex, |
| 941 | quantity: 2 |
| 942 | }); |
| 943 | harness.simulation.step(); |
| 944 | bag = harness.simulation.bagOf(playerId); |
| 945 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 946 | assert.ok(chequeIndex >= 0); |
| 947 | |
| 948 | const record = harness.simulation.entityRecord(playerId); |
| 949 | const worker = harness.simulation.workers[record.workerId]; |
| 950 | let npcId = null; |
| 951 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 952 | { |
| 953 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 954 | { |
| 955 | npcId = entity.entityId; |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | assert.ok(npcId); |
| 960 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 961 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 962 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 963 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 964 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 965 | playerPos.tileX = npcPos.tileX - 1; |
| 966 | playerPos.tileY = npcPos.tileY; |
| 967 | |
| 968 | const quote = resolveShopSellQuote(bag.inventory[chequeIndex]); |
| 969 | assert.ok(quote?.isCheque); |
| 970 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 971 | harness.simulation.enqueue(1, { |
| 972 | type: "sell-to-shop", |
| 973 | characterId: character.character_id, |
| 974 | npcEntityId: npcId, |
| 975 | slotIndex: chequeIndex, |
| 976 | quantity: 1 |
| 977 | }); |
| 978 | const sold = harness.simulation.step(); |
| 979 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 980 | bag = harness.simulation.bagOf(playerId); |
| 981 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 982 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 983 | const afterCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 984 | assert.equal(afterCoins - beforeCoins, quote.payout); |
| 985 | // Infinite catalog absorbs goods without bought-from-player resale stock. |
| 986 | assert.equal(boughtQty(shop, ItemId.copperOre), 0); |
| 987 | } |
| 988 | finally |
| 989 | { harness.close(); } |
| 990 | }); |
| 991 | |
| 992 | test("resale stock keeps the seller origin instead of Bought from NPC", () => |
| 993 | { |
| 994 | const harness = makeSimulation({ workerCount: 1 }); |
| 995 | try |
| 996 | { |
| 997 | const data = createEmptyChunkData(); |
| 998 | data.open = true; |
| 999 | data.spawners = [{ |
| 1000 | localX: 9, |
| 1001 | localY: 8, |
| 1002 | config: { |
| 1003 | mobTypeId: MobId.humanoid, |
| 1004 | maxAlive: 1, |
| 1005 | spawnRangeTiles: 0, |
| 1006 | maxWanderRangeTiles: 0, |
| 1007 | displayName: "Fence", |
| 1008 | sells: [], |
| 1009 | buysItems: true, |
| 1010 | hiddenDiscount: 1 |
| 1011 | } |
| 1012 | }]; |
| 1013 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 1014 | harness.simulation.reloadWorld(); |
| 1015 | |
| 1016 | const minerOrigin = gatherOrigin(GatherOriginVerb.mined, "Ada Miner"); |
| 1017 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1018 | const funded = addCoinsToSlots(emptySlots(), 50); |
| 1019 | assert.equal(funded.ok, true); |
| 1020 | funded.slots[1] = { |
| 1021 | itemId: ItemId.copperOre, |
| 1022 | quantity: 2, |
| 1023 | origins: [{ origin: minerOrigin, quantity: 2 }] |
| 1024 | }; |
| 1025 | harness.stateDb.saveBag(character.character_id, { |
| 1026 | inventory: funded.slots, |
| 1027 | equipment: emptyEquipment(), |
| 1028 | vault: [], |
| 1029 | vaultReserved: [] |
| 1030 | }); |
| 1031 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1032 | harness.simulation.step(); |
| 1033 | harness.advance(50); |
| 1034 | harness.simulation.step(); |
| 1035 | |
| 1036 | const record = harness.simulation.entityRecord(playerId); |
| 1037 | const worker = harness.simulation.workers[record.workerId]; |
| 1038 | let npcId = null; |
| 1039 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 1040 | { |
| 1041 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 1042 | { |
| 1043 | npcId = entity.entityId; |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | assert.ok(npcId); |
| 1048 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 1049 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 1050 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 1051 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1052 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 1053 | playerPos.tileX = npcPos.tileX - 1; |
| 1054 | playerPos.tileY = npcPos.tileY; |
| 1055 | |
| 1056 | harness.simulation.enqueue(1, { |
| 1057 | type: "sell-to-shop", |
| 1058 | characterId: character.character_id, |
| 1059 | npcEntityId: npcId, |
| 1060 | slotIndex: 1, |
| 1061 | quantity: 2 |
| 1062 | }); |
| 1063 | const sold = harness.simulation.step(); |
| 1064 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 1065 | assert.equal(boughtQty(shop, ItemId.copperOre), 2); |
| 1066 | assert.deepEqual(shop.boughtStock[ItemId.copperOre].origins, [ |
| 1067 | { origin: minerOrigin, quantity: 2 } |
| 1068 | ]); |
| 1069 | |
| 1070 | harness.simulation.enqueue(1, { |
| 1071 | type: "buy-from-shop", |
| 1072 | characterId: character.character_id, |
| 1073 | npcEntityId: npcId, |
| 1074 | itemId: ItemId.copperOre, |
| 1075 | quantity: 2, |
| 1076 | listingId: `bought:${ItemId.copperOre}`, |
| 1077 | source: "bought" |
| 1078 | }); |
| 1079 | const buy = harness.simulation.step(); |
| 1080 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 1081 | const bag = harness.simulation.bagOf(playerId); |
| 1082 | const ore = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.copperOre); |
| 1083 | assert.ok(ore); |
| 1084 | assert.equal(ore.origins?.[0]?.origin?.kind, ItemOriginKind.gathered); |
| 1085 | assert.equal(ore.origins?.[0]?.origin?.displayName, "Ada Miner"); |
| 1086 | assert.equal(ore.origins?.[0]?.origin?.kind === ItemOriginKind.shop, false); |
| 1087 | } |
| 1088 | finally |
| 1089 | { harness.close(); } |
| 1090 | }); |
| 1091 | |
| 1092 | test("NPC letter sales pay out and destroy the letter without stock", () => |
| 1093 | { |
| 1094 | const harness = makeSimulation({ workerCount: 1 }); |
| 1095 | try |
| 1096 | { |
| 1097 | const data = createEmptyChunkData(); |
| 1098 | data.open = true; |
| 1099 | data.spawners = [{ |
| 1100 | localX: 9, |
| 1101 | localY: 8, |
| 1102 | config: { |
| 1103 | mobTypeId: MobId.humanoid, |
| 1104 | maxAlive: 1, |
| 1105 | spawnRangeTiles: 0, |
| 1106 | maxWanderRangeTiles: 0, |
| 1107 | displayName: "Fence", |
| 1108 | sells: [], |
| 1109 | buysItems: true, |
| 1110 | hiddenDiscount: 1 |
| 1111 | } |
| 1112 | }]; |
| 1113 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 1114 | harness.simulation.reloadWorld(); |
| 1115 | |
| 1116 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1117 | const funded = addCoinsToSlots(emptySlots(), 10); |
| 1118 | assert.equal(funded.ok, true); |
| 1119 | funded.slots[1] = { |
| 1120 | itemId: ItemId.letter, |
| 1121 | quantity: 1, |
| 1122 | instance: { |
| 1123 | kind: "letter", |
| 1124 | letterId: "sell-me", |
| 1125 | authorCharacterId: character.character_id, |
| 1126 | authorDisplayName: "Ada", |
| 1127 | body: "For sale", |
| 1128 | opened: true, |
| 1129 | writtenAt: Date.now() |
| 1130 | } |
| 1131 | }; |
| 1132 | harness.stateDb.saveBag(character.character_id, { |
| 1133 | inventory: funded.slots, |
| 1134 | equipment: emptyEquipment(), |
| 1135 | vault: [], |
| 1136 | vaultReserved: [] |
| 1137 | }); |
| 1138 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1139 | harness.simulation.step(); |
| 1140 | harness.advance(50); |
| 1141 | harness.simulation.step(); |
| 1142 | |
| 1143 | const record = harness.simulation.entityRecord(playerId); |
| 1144 | const worker = harness.simulation.workers[record.workerId]; |
| 1145 | let npcId = null; |
| 1146 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 1147 | { |
| 1148 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 1149 | { |
| 1150 | npcId = entity.entityId; |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
| 1154 | assert.ok(npcId); |
| 1155 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 1156 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 1157 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 1158 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1159 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 1160 | playerPos.tileX = npcPos.tileX - 1; |
| 1161 | playerPos.tileY = npcPos.tileY; |
| 1162 | |
| 1163 | const letterSlot = harness.simulation.bagOf(playerId).inventory |
| 1164 | .findIndex((slot) => slot?.itemId === ItemId.letter); |
| 1165 | assert.ok(letterSlot >= 0); |
| 1166 | const quote = resolveShopSellQuote(harness.simulation.bagOf(playerId).inventory[letterSlot]); |
| 1167 | assert.ok(quote); |
| 1168 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 1169 | |
| 1170 | harness.simulation.enqueue(1, { |
| 1171 | type: "sell-to-shop", |
| 1172 | characterId: character.character_id, |
| 1173 | npcEntityId: npcId, |
| 1174 | slotIndex: letterSlot, |
| 1175 | quantity: 1 |
| 1176 | }); |
| 1177 | const sold = harness.simulation.step(); |
| 1178 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 1179 | const bag = harness.simulation.bagOf(playerId); |
| 1180 | assert.equal(bag.inventory.some((slot) => slot?.itemId === ItemId.letter), false); |
| 1181 | assert.equal(boughtQty(shop, ItemId.letter), 0); |
| 1182 | assert.equal(shop.boughtStock?.[ItemId.letter], undefined); |
| 1183 | const afterCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 1184 | assert.equal(afterCoins - beforeCoins, quote.payout); |
| 1185 | } |
| 1186 | finally |
| 1187 | { harness.close(); } |
| 1188 | }); |
| 1189 | |
| 1190 | test("vendor bought dye stays split by colour and resells that colour", () => |
| 1191 | { |
| 1192 | const harness = makeSimulation({ workerCount: 1 }); |
| 1193 | try |
| 1194 | { |
| 1195 | const data = createEmptyChunkData(); |
| 1196 | data.open = true; |
| 1197 | data.spawners = [{ |
| 1198 | localX: 9, |
| 1199 | localY: 8, |
| 1200 | config: { |
| 1201 | mobTypeId: MobId.humanoid, |
| 1202 | maxAlive: 1, |
| 1203 | spawnRangeTiles: 0, |
| 1204 | maxWanderRangeTiles: 0, |
| 1205 | displayName: "Dyer", |
| 1206 | sells: [], |
| 1207 | buysItems: true, |
| 1208 | hiddenDiscount: 1 |
| 1209 | } |
| 1210 | }]; |
| 1211 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 1212 | harness.simulation.reloadWorld(); |
| 1213 | |
| 1214 | const yellow = dyeItemInstance( |
| 1215 | { r: 240 / 255, g: 210 / 255, b: 40 / 255 }, |
| 1216 | DEFAULT_DYE_FILL, |
| 1217 | FlowerColorId.yellow |
| 1218 | ); |
| 1219 | const magenta = dyeItemInstance( |
| 1220 | { r: 220 / 255, g: 40 / 255, b: 180 / 255 }, |
| 1221 | DEFAULT_DYE_FILL, |
| 1222 | FlowerColorId.magenta |
| 1223 | ); |
| 1224 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 1225 | const funded = addCoinsToSlots(emptySlots(), 200); |
| 1226 | assert.equal(funded.ok, true); |
| 1227 | funded.slots[1] = { itemId: ItemId.dye, quantity: 1, instance: structuredClone(yellow) }; |
| 1228 | funded.slots[2] = { itemId: ItemId.dye, quantity: 1, instance: structuredClone(magenta) }; |
| 1229 | harness.stateDb.saveBag(character.character_id, { |
| 1230 | inventory: funded.slots, |
| 1231 | equipment: emptyEquipment(), |
| 1232 | vault: [], |
| 1233 | vaultReserved: [] |
| 1234 | }); |
| 1235 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1236 | harness.simulation.step(); |
| 1237 | harness.advance(50); |
| 1238 | harness.simulation.step(); |
| 1239 | |
| 1240 | const record = harness.simulation.entityRecord(playerId); |
| 1241 | const worker = harness.simulation.workers[record.workerId]; |
| 1242 | let npcId = null; |
| 1243 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 1244 | { |
| 1245 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 1246 | { |
| 1247 | npcId = entity.entityId; |
| 1248 | break; |
| 1249 | } |
| 1250 | } |
| 1251 | assert.ok(npcId); |
| 1252 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 1253 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 1254 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 1255 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1256 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 1257 | playerPos.tileX = npcPos.tileX - 1; |
| 1258 | playerPos.tileY = npcPos.tileY; |
| 1259 | |
| 1260 | for (const slotIndex of [1, 2]) |
| 1261 | { |
| 1262 | harness.simulation.enqueue(1, { |
| 1263 | type: "sell-to-shop", |
| 1264 | characterId: character.character_id, |
| 1265 | npcEntityId: npcId, |
| 1266 | slotIndex, |
| 1267 | quantity: 1 |
| 1268 | }); |
| 1269 | const sold = harness.simulation.step(); |
| 1270 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 1271 | } |
| 1272 | |
| 1273 | const yellowKey = boughtStockKey(ItemId.dye, yellow); |
| 1274 | const magentaKey = boughtStockKey(ItemId.dye, magenta); |
| 1275 | assert.notEqual(yellowKey, magentaKey); |
| 1276 | assert.equal(shop.boughtStock[yellowKey]?.quantity, 1); |
| 1277 | assert.equal(shop.boughtStock[magentaKey]?.quantity, 1); |
| 1278 | assert.equal(shop.boughtStock[yellowKey]?.instance?.colorId, FlowerColorId.yellow); |
| 1279 | assert.equal(shop.boughtStock[magentaKey]?.instance?.colorId, FlowerColorId.magenta); |
| 1280 | assert.equal(shop.boughtStock[ItemId.dye], undefined); |
| 1281 | |
| 1282 | harness.simulation.enqueue(1, { |
| 1283 | type: "buy-from-shop", |
| 1284 | characterId: character.character_id, |
| 1285 | npcEntityId: npcId, |
| 1286 | itemId: ItemId.dye, |
| 1287 | quantity: 1, |
| 1288 | listingId: `bought:${magentaKey}`, |
| 1289 | source: "bought" |
| 1290 | }); |
| 1291 | const bought = harness.simulation.step(); |
| 1292 | assert.equal(bought.events.some((event) => event.type === "command-error"), false, JSON.stringify(bought.events)); |
| 1293 | const bag = harness.simulation.bagOf(playerId); |
| 1294 | const dye = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.dye); |
| 1295 | assert.ok(dye); |
| 1296 | assert.equal(dye.instance?.colorId, FlowerColorId.magenta); |
| 1297 | assert.equal(shop.boughtStock[magentaKey], undefined); |
| 1298 | assert.equal(shop.boughtStock[yellowKey]?.quantity, 1); |
| 1299 | } |
| 1300 | finally |
| 1301 | { harness.close(); } |
| 1302 | }); |
| 1303 | |
| 1304 | test("shop opens across a table when adjacent tiles to the NPC are blocked", () => |
| 1305 | { |
| 1306 | const harness = makeSimulation({ workerCount: 1 }); |
| 1307 | try |
| 1308 | { |
| 1309 | const data = createEmptyChunkData(); |
| 1310 | data.open = true; |
| 1311 | // NPC at 9,8; table south at 9,9; player further south at 9,10. |
| 1312 | data.blocks[9 * 16 + 9] = { typeId: BlockType.table, state: 0, config: null }; |
| 1313 | data.spawners = [{ |
| 1314 | localX: 9, |
| 1315 | localY: 8, |
| 1316 | config: { |
| 1317 | mobTypeId: MobId.humanoid, |
| 1318 | maxAlive: 1, |
| 1319 | spawnRangeTiles: 0, |
| 1320 | maxWanderRangeTiles: 0, |
| 1321 | sex: "female", |
| 1322 | displayName: "Barmaid", |
| 1323 | sells: [{ itemId: ItemId.paper, quantity: 0 }], |
| 1324 | hiddenDiscount: 1, |
| 1325 | equipment: { chest: ItemId.clothShirt } |
| 1326 | } |
| 1327 | }]; |
| 1328 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 1329 | harness.simulation.reloadWorld(); |
| 1330 | |
| 1331 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 10 }); |
| 1332 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 1333 | harness.simulation.step(); |
| 1334 | harness.advance(50); |
| 1335 | harness.simulation.step(); |
| 1336 | |
| 1337 | const record = harness.simulation.entityRecord(playerId); |
| 1338 | const worker = harness.simulation.workers[record.workerId]; |
| 1339 | let npcId = null; |
| 1340 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 1341 | { |
| 1342 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 1343 | { |
| 1344 | npcId = entity.entityId; |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
| 1348 | assert.ok(npcId, "expected a humanoid shopkeeper"); |
| 1349 | |
| 1350 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 1351 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 1352 | npcPos.tileX = 9; |
| 1353 | npcPos.tileY = 8; |
| 1354 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 1355 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 1356 | playerPos.tileX = 9; |
| 1357 | playerPos.tileY = 10; |
| 1358 | |
| 1359 | assert.equal(worker.world.blockTypeAt(9, 9), BlockType.table); |
| 1360 | |
| 1361 | harness.simulation.enqueue(1, { |
| 1362 | type: "interact", |
| 1363 | characterId: character.character_id, |
| 1364 | targetEntityId: npcId, |
| 1365 | purpose: "shop" |
| 1366 | }); |
| 1367 | const open = harness.simulation.step(); |
| 1368 | assert.ok( |
| 1369 | open.events.some((event) => event.type === "shop-open"), |
| 1370 | JSON.stringify(open.events) |
| 1371 | ); |
| 1372 | } |
| 1373 | finally |
| 1374 | { harness.close(); } |
| 1375 | }); |