| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | shopUnitPrice, |
| 5 | undiscountedMarketValue, |
| 6 | canSellInNpcShop, |
| 7 | canPlayerSellToNpc, |
| 8 | shopBuyFromPlayerPrice, |
| 9 | shopBoughtResalePrice, |
| 10 | shopChequeAdminFee, |
| 11 | resolveShopSellQuote, |
| 12 | shopRestockDayKey, |
| 13 | lerp |
| 14 | } from "../../shared/shop/pricing.js"; |
| 15 | import { ItemId } from "../../shared/definitions/items.js"; |
| 16 | import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js"; |
| 17 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 18 | import { Component } from "../src/ecs/components.js"; |
| 19 | import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; |
| 20 | import { ItemOriginKind } from "../../shared/items/origin.js"; |
| 21 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 22 | import { ensureShopRestocked } from "../src/shop/service.js"; |
| 23 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 24 | |
| 25 | test("shop prices fall back to seedValue and apply hidden discount", () => |
| 26 | { |
| 27 | assert.equal(undiscountedMarketValue(ItemId.copperOre), 5); |
| 28 | assert.equal(undiscountedMarketValue(ItemId.tinOre), 5); |
| 29 | assert.equal(undiscountedMarketValue(ItemId.pineLog), 5); |
| 30 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.9 }), 5); |
| 31 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.5 }), 3); |
| 32 | assert.equal(shopBuyFromPlayerPrice(ItemId.copperOre), 4); |
| 33 | assert.equal(shopBoughtResalePrice(ItemId.copperOre), 6); |
| 34 | assert.equal(lerp(10, 20, 0.5), 15); |
| 35 | assert.equal(canSellInNpcShop(ItemId.copperOre), true); |
| 36 | assert.equal(canSellInNpcShop(ItemId.tinOre), true); |
| 37 | assert.equal(canSellInNpcShop(ItemId.stick), false); |
| 38 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.copperOre, quantity: 1 }), true); |
| 39 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.stick, quantity: 1 }), true); |
| 40 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.blankCheque, quantity: 1 }), false); |
| 41 | assert.equal(canPlayerSellToNpc({ itemId: ItemId.coins, quantity: 10 }), false); |
| 42 | assert.equal(canPlayerSellToNpc({ |
| 43 | itemId: ItemId.filledCheque, |
| 44 | quantity: 1, |
| 45 | instance: { kind: "filled-cheque", itemId: ItemId.copperOre, quantity: 3 } |
| 46 | }), true); |
| 47 | assert.equal(shopChequeAdminFee(100), 5); |
| 48 | assert.equal(shopChequeAdminFee(10), 1); |
| 49 | assert.equal(shopChequeAdminFee(1), 1); |
| 50 | const chequeQuote = resolveShopSellQuote({ |
| 51 | itemId: ItemId.filledCheque, |
| 52 | quantity: 1, |
| 53 | instance: { kind: "filled-cheque", itemId: ItemId.copperOre, quantity: 2 } |
| 54 | }); |
| 55 | assert.equal(chequeQuote.baseTotal, shopBuyFromPlayerPrice(ItemId.copperOre) * 2); |
| 56 | assert.equal(chequeQuote.adminFee, shopChequeAdminFee(chequeQuote.baseTotal)); |
| 57 | assert.equal(chequeQuote.payout, chequeQuote.baseTotal - chequeQuote.adminFee); |
| 58 | }); |
| 59 | |
| 60 | test("humanoid spawner config keeps sex equipment sells quantities and buysItems", () => |
| 61 | { |
| 62 | assert.equal(isHumanoidMob(MobId.humanoid), true); |
| 63 | const config = normalizeSpawnerConfig({ |
| 64 | mobTypeId: MobId.humanoid, |
| 65 | sex: "female", |
| 66 | displayName: "Ore trader", |
| 67 | equipment: { chest: ItemId.clothShirt, weapon: ItemId.stick }, |
| 68 | sells: [ |
| 69 | { itemId: ItemId.copperOre, quantity: 10 }, |
| 70 | { itemId: ItemId.tinOre, quantity: 0 }, |
| 71 | ItemId.copperOre |
| 72 | ], |
| 73 | buysItems: true, |
| 74 | hiddenDiscount: 0.85 |
| 75 | }, "shop-1"); |
| 76 | assert.equal(config.sex, "female"); |
| 77 | assert.equal(config.displayName, "Ore trader"); |
| 78 | assert.equal(config.equipment.chest, ItemId.clothShirt); |
| 79 | assert.deepEqual(config.sells, [ |
| 80 | { itemId: ItemId.copperOre, quantity: 10 }, |
| 81 | { itemId: ItemId.tinOre, quantity: 0 } |
| 82 | ]); |
| 83 | assert.equal(config.buysItems, true); |
| 84 | assert.equal(config.hiddenDiscount, 0.85); |
| 85 | assert.equal(config.maxAlive, 1); |
| 86 | }); |
| 87 | |
| 88 | test("buy copper ore from a spawned shopkeeper", () => |
| 89 | { |
| 90 | const harness = makeSimulation({ workerCount: 1 }); |
| 91 | try |
| 92 | { |
| 93 | const data = createEmptyChunkData(); |
| 94 | data.open = true; |
| 95 | data.spawners = [{ |
| 96 | localX: 9, |
| 97 | localY: 8, |
| 98 | config: { |
| 99 | mobTypeId: MobId.humanoid, |
| 100 | maxAlive: 1, |
| 101 | spawnRangeTiles: 1, |
| 102 | maxWanderRangeTiles: 1, |
| 103 | sex: "male", |
| 104 | displayName: "Ore trader", |
| 105 | sells: [ |
| 106 | { itemId: ItemId.copperOre, quantity: 0 }, |
| 107 | { itemId: ItemId.tinOre, quantity: 0 } |
| 108 | ], |
| 109 | hiddenDiscount: 1, |
| 110 | equipment: { chest: ItemId.clothShirt } |
| 111 | } |
| 112 | }]; |
| 113 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 114 | harness.simulation.reloadWorld(); |
| 115 | |
| 116 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 117 | const funded = addCoinsToSlots(emptySlots(), 50); |
| 118 | assert.equal(funded.ok, true); |
| 119 | harness.stateDb.saveBag(character.character_id, { |
| 120 | inventory: funded.slots, |
| 121 | equipment: emptyEquipment(), |
| 122 | vault: [], |
| 123 | vaultReserved: [] |
| 124 | }); |
| 125 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 126 | harness.simulation.step(); |
| 127 | harness.advance(50); |
| 128 | harness.simulation.step(); |
| 129 | |
| 130 | const record = harness.simulation.entityRecord(playerId); |
| 131 | const worker = harness.simulation.workers[record.workerId]; |
| 132 | let npcId = null; |
| 133 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 134 | { |
| 135 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 136 | { |
| 137 | npcId = entity.entityId; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | assert.ok(npcId, "expected a humanoid shopkeeper"); |
| 142 | |
| 143 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 144 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 145 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 146 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 147 | playerPos.tileX = npcPos.tileX; |
| 148 | playerPos.tileY = npcPos.tileY; |
| 149 | |
| 150 | harness.simulation.enqueue(1, { |
| 151 | type: "interact", |
| 152 | characterId: character.character_id, |
| 153 | targetEntityId: npcId |
| 154 | }); |
| 155 | const open = harness.simulation.step(); |
| 156 | const shopOpen = open.events.find((event) => event.type === "shop-open"); |
| 157 | assert.ok(shopOpen, JSON.stringify(open.events)); |
| 158 | assert.ok(shopOpen.stock.some((row) => row.itemId === ItemId.copperOre)); |
| 159 | |
| 160 | harness.simulation.enqueue(1, { |
| 161 | type: "buy-from-shop", |
| 162 | characterId: character.character_id, |
| 163 | npcEntityId: npcId, |
| 164 | itemId: ItemId.copperOre, |
| 165 | quantity: 1, |
| 166 | listingId: `stock:${ItemId.copperOre}` |
| 167 | }); |
| 168 | const buy = harness.simulation.step(); |
| 169 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 170 | const bag = harness.simulation.bagOf(playerId); |
| 171 | const ore = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.copperOre); |
| 172 | assert.ok(ore, JSON.stringify(bag.inventory)); |
| 173 | assert.equal(ore.origins?.[0]?.origin?.kind, ItemOriginKind.shop); |
| 174 | } |
| 175 | finally |
| 176 | { harness.close(); } |
| 177 | }); |
| 178 | |
| 179 | test("limited shop stock depletes and restocks on a new day", () => |
| 180 | { |
| 181 | const shop = { |
| 182 | catalog: [{ itemId: ItemId.copperOre, quantity: 2 }], |
| 183 | buysItems: false, |
| 184 | hiddenDiscount: 1, |
| 185 | remaining: Object.create(null), |
| 186 | boughtStock: Object.create(null), |
| 187 | restockDay: null |
| 188 | }; |
| 189 | const dayA = Date.parse("2026-08-25T12:00:00Z"); |
| 190 | ensureShopRestocked(shop, dayA); |
| 191 | assert.equal(shop.restockDay, shopRestockDayKey(dayA)); |
| 192 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 193 | shop.remaining[ItemId.copperOre] = 0; |
| 194 | shop.boughtStock[ItemId.tinOre] = 3; |
| 195 | ensureShopRestocked(shop, dayA); |
| 196 | assert.equal(shop.remaining[ItemId.copperOre], 0); |
| 197 | assert.equal(shop.boughtStock[ItemId.tinOre], 3); |
| 198 | const dayB = Date.parse("2026-08-26T00:00:01Z"); |
| 199 | ensureShopRestocked(shop, dayB); |
| 200 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 201 | assert.equal(shop.boughtStock[ItemId.tinOre], undefined); |
| 202 | }); |
| 203 | |
| 204 | test("shop overflow goes to bank vault when inventory is full", () => |
| 205 | { |
| 206 | const harness = makeSimulation({ workerCount: 1 }); |
| 207 | try |
| 208 | { |
| 209 | const data = createEmptyChunkData(); |
| 210 | data.open = true; |
| 211 | data.spawners = [{ |
| 212 | localX: 9, |
| 213 | localY: 8, |
| 214 | config: { |
| 215 | mobTypeId: MobId.humanoid, |
| 216 | maxAlive: 1, |
| 217 | spawnRangeTiles: 0, |
| 218 | maxWanderRangeTiles: 0, |
| 219 | displayName: "Ore trader", |
| 220 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 221 | hiddenDiscount: 1 |
| 222 | } |
| 223 | }]; |
| 224 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 225 | harness.simulation.reloadWorld(); |
| 226 | |
| 227 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 228 | const slots = emptySlots(); |
| 229 | const funded = addCoinsToSlots(slots, 50); |
| 230 | assert.equal(funded.ok, true); |
| 231 | for (let i = 0; i < funded.slots.length; i++) |
| 232 | { |
| 233 | if (!funded.slots[i]) |
| 234 | { funded.slots[i] = { itemId: ItemId.stick, quantity: 1 }; } |
| 235 | } |
| 236 | harness.stateDb.saveBag(character.character_id, { |
| 237 | inventory: funded.slots, |
| 238 | equipment: emptyEquipment(), |
| 239 | vault: [], |
| 240 | vaultReserved: [] |
| 241 | }); |
| 242 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 243 | harness.simulation.step(); |
| 244 | harness.advance(50); |
| 245 | harness.simulation.step(); |
| 246 | |
| 247 | const record = harness.simulation.entityRecord(playerId); |
| 248 | const worker = harness.simulation.workers[record.workerId]; |
| 249 | let npcId = null; |
| 250 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 251 | { |
| 252 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 253 | { |
| 254 | npcId = entity.entityId; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | assert.ok(npcId); |
| 259 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 260 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 261 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 262 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 263 | playerPos.tileX = npcPos.tileX; |
| 264 | playerPos.tileY = npcPos.tileY; |
| 265 | |
| 266 | harness.simulation.enqueue(1, { |
| 267 | type: "buy-from-shop", |
| 268 | characterId: character.character_id, |
| 269 | npcEntityId: npcId, |
| 270 | itemId: ItemId.copperOre, |
| 271 | quantity: 1, |
| 272 | listingId: `stock:${ItemId.copperOre}` |
| 273 | }); |
| 274 | const buy = harness.simulation.step(); |
| 275 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 276 | const bag = harness.simulation.bagOf(playerId); |
| 277 | assert.equal((bag.inventory ?? []).some((slot) => slot?.itemId === ItemId.copperOre), false); |
| 278 | assert.ok((bag.vault ?? []).some((slot) => slot?.itemId === ItemId.copperOre)); |
| 279 | } |
| 280 | finally |
| 281 | { harness.close(); } |
| 282 | }); |
| 283 | |
| 284 | test("buysItems refills limited catalog then stores extras for 120% resale", () => |
| 285 | { |
| 286 | const harness = makeSimulation({ workerCount: 1 }); |
| 287 | try |
| 288 | { |
| 289 | const data = createEmptyChunkData(); |
| 290 | data.open = true; |
| 291 | data.spawners = [{ |
| 292 | localX: 9, |
| 293 | localY: 8, |
| 294 | config: { |
| 295 | mobTypeId: MobId.humanoid, |
| 296 | maxAlive: 1, |
| 297 | spawnRangeTiles: 0, |
| 298 | maxWanderRangeTiles: 0, |
| 299 | displayName: "Buyer", |
| 300 | sells: [{ itemId: ItemId.copperOre, quantity: 2 }], |
| 301 | buysItems: true, |
| 302 | hiddenDiscount: 1 |
| 303 | } |
| 304 | }]; |
| 305 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 306 | harness.simulation.reloadWorld(); |
| 307 | |
| 308 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 309 | harness.stateDb.saveBag(character.character_id, { |
| 310 | inventory: emptySlots(INVENTORY_SLOTS), |
| 311 | equipment: emptyEquipment(), |
| 312 | vault: [], |
| 313 | vaultReserved: [] |
| 314 | }); |
| 315 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 316 | harness.simulation.step(); |
| 317 | harness.advance(50); |
| 318 | harness.simulation.step(); |
| 319 | |
| 320 | const record = harness.simulation.entityRecord(playerId); |
| 321 | const worker = harness.simulation.workers[record.workerId]; |
| 322 | let npcId = null; |
| 323 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 324 | { |
| 325 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 326 | { |
| 327 | npcId = entity.entityId; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | assert.ok(npcId); |
| 332 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 333 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 334 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 335 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 336 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 337 | playerPos.tileX = npcPos.tileX; |
| 338 | playerPos.tileY = npcPos.tileY; |
| 339 | |
| 340 | // Fund coins and buy out limited stock first. |
| 341 | const inv = worker.world.ecs.get(playerHandle, Component.inventory); |
| 342 | const funded = addCoinsToSlots(inv.slots, 50); |
| 343 | assert.equal(funded.ok, true); |
| 344 | inv.slots = funded.slots; |
| 345 | inv.slots[1] = { itemId: ItemId.copperOre, quantity: 5 }; |
| 346 | |
| 347 | harness.simulation.enqueue(1, { |
| 348 | type: "buy-from-shop", |
| 349 | characterId: character.character_id, |
| 350 | npcEntityId: npcId, |
| 351 | itemId: ItemId.copperOre, |
| 352 | quantity: 2, |
| 353 | listingId: `stock:${ItemId.copperOre}` |
| 354 | }); |
| 355 | const boughtOut = harness.simulation.step(); |
| 356 | assert.equal(boughtOut.events.some((event) => event.type === "command-error"), false, JSON.stringify(boughtOut.events)); |
| 357 | assert.equal(shop.remaining[ItemId.copperOre], 0); |
| 358 | |
| 359 | const oreSlot = inv.slots.findIndex((slot) => slot?.itemId === ItemId.copperOre && (slot.quantity | 0) >= 5); |
| 360 | assert.ok(oreSlot >= 0); |
| 361 | |
| 362 | harness.simulation.enqueue(1, { |
| 363 | type: "sell-to-shop", |
| 364 | characterId: character.character_id, |
| 365 | npcEntityId: npcId, |
| 366 | slotIndex: oreSlot, |
| 367 | quantity: 5 |
| 368 | }); |
| 369 | const sold = harness.simulation.step(); |
| 370 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 371 | assert.equal(shop.remaining[ItemId.copperOre], 2); |
| 372 | assert.equal(shop.boughtStock[ItemId.copperOre], 3); |
| 373 | |
| 374 | const open = sold.events.find((event) => event.type === "shop-open"); |
| 375 | assert.ok(open.stock.some((row) => row.source === "stock" && row.available === 2)); |
| 376 | assert.ok(open.stock.some((row) => row.source === "bought" && row.available === 3)); |
| 377 | } |
| 378 | finally |
| 379 | { harness.close(); } |
| 380 | }); |
| 381 | |
| 382 | test("shop adjacency errors use gendered pronouns", () => |
| 383 | { |
| 384 | const harness = makeSimulation({ workerCount: 1 }); |
| 385 | try |
| 386 | { |
| 387 | const data = createEmptyChunkData(); |
| 388 | data.open = true; |
| 389 | data.spawners = [{ |
| 390 | localX: 9, |
| 391 | localY: 8, |
| 392 | config: { |
| 393 | mobTypeId: MobId.humanoid, |
| 394 | maxAlive: 1, |
| 395 | spawnRangeTiles: 0, |
| 396 | maxWanderRangeTiles: 0, |
| 397 | sex: "female", |
| 398 | displayName: "Trader", |
| 399 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 400 | hiddenDiscount: 1 |
| 401 | } |
| 402 | }]; |
| 403 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 404 | harness.simulation.reloadWorld(); |
| 405 | |
| 406 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 2 }); |
| 407 | harness.stateDb.saveBag(character.character_id, { |
| 408 | inventory: emptySlots(INVENTORY_SLOTS), |
| 409 | equipment: emptyEquipment(), |
| 410 | vault: [], |
| 411 | vaultReserved: [] |
| 412 | }); |
| 413 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 414 | harness.simulation.step(); |
| 415 | harness.advance(50); |
| 416 | harness.simulation.step(); |
| 417 | |
| 418 | const record = harness.simulation.entityRecord(playerId); |
| 419 | const worker = harness.simulation.workers[record.workerId]; |
| 420 | let npcId = null; |
| 421 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 422 | { |
| 423 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 424 | { |
| 425 | npcId = entity.entityId; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | assert.ok(npcId); |
| 430 | |
| 431 | harness.simulation.enqueue(1, { |
| 432 | type: "interact", |
| 433 | characterId: character.character_id, |
| 434 | targetEntityId: npcId |
| 435 | }); |
| 436 | const open = harness.simulation.step(); |
| 437 | const err = open.events.find((event) => event.type === "command-error"); |
| 438 | assert.ok(err, JSON.stringify(open.events)); |
| 439 | assert.match(err.message, /stand next to her/i); |
| 440 | assert.doesNotMatch(err.message, /\bthem\b/i); |
| 441 | } |
| 442 | finally |
| 443 | { harness.close(); } |
| 444 | }); |
| 445 | |
| 446 | test("shopkeeper stops wandering while a player has the shop open", () => |
| 447 | { |
| 448 | const harness = makeSimulation({ workerCount: 1 }); |
| 449 | try |
| 450 | { |
| 451 | const data = createEmptyChunkData(); |
| 452 | data.open = true; |
| 453 | data.spawners = [{ |
| 454 | localX: 8, |
| 455 | localY: 8, |
| 456 | config: { |
| 457 | mobTypeId: MobId.humanoid, |
| 458 | maxAlive: 1, |
| 459 | spawnRangeTiles: 4, |
| 460 | maxWanderRangeTiles: 8, |
| 461 | sex: "male", |
| 462 | displayName: "Stay put", |
| 463 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 464 | hiddenDiscount: 1 |
| 465 | } |
| 466 | }]; |
| 467 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 468 | harness.simulation.reloadWorld(); |
| 469 | |
| 470 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 471 | harness.stateDb.saveBag(character.character_id, { |
| 472 | inventory: emptySlots(INVENTORY_SLOTS), |
| 473 | equipment: emptyEquipment(), |
| 474 | vault: [], |
| 475 | vaultReserved: [] |
| 476 | }); |
| 477 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 478 | harness.simulation.step(); |
| 479 | harness.advance(50); |
| 480 | harness.simulation.step(); |
| 481 | |
| 482 | const record = harness.simulation.entityRecord(playerId); |
| 483 | const worker = harness.simulation.workers[record.workerId]; |
| 484 | let npc = null; |
| 485 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature, Component.position, Component.path)) |
| 486 | { |
| 487 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 488 | { |
| 489 | npc = entity; |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | assert.ok(npc); |
| 494 | |
| 495 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 496 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 497 | playerPos.tileX = npc.position.tileX; |
| 498 | playerPos.tileY = npc.position.tileY; |
| 499 | |
| 500 | harness.simulation.enqueue(1, { |
| 501 | type: "interact", |
| 502 | characterId: character.character_id, |
| 503 | targetEntityId: npc.entityId |
| 504 | }); |
| 505 | const open = harness.simulation.step(); |
| 506 | assert.ok(open.events.some((event) => event.type === "shop-open")); |
| 507 | assert.equal(!!npc.shop.browsers?.[character.character_id], true); |
| 508 | |
| 509 | npc.path.tiles = [{ x: npc.position.tileX + 1, y: npc.position.tileY }]; |
| 510 | for (let i = 0; i < 20; i++) |
| 511 | { harness.simulation.step(); } |
| 512 | assert.equal(npc.path.tiles.length, 0); |
| 513 | assert.equal(npc.position.tileX, playerPos.tileX); |
| 514 | assert.equal(npc.position.tileY, playerPos.tileY); |
| 515 | |
| 516 | harness.simulation.enqueue(1, { |
| 517 | type: "close-shop", |
| 518 | characterId: character.character_id, |
| 519 | npcEntityId: npc.entityId |
| 520 | }); |
| 521 | harness.simulation.step(); |
| 522 | assert.equal(!!npc.shop.browsers?.[character.character_id], false); |
| 523 | } |
| 524 | finally |
| 525 | { harness.close(); } |
| 526 | }); |
| 527 | |
| 528 | test("buysItems accepts non-ingredient tools like sticks", () => |
| 529 | { |
| 530 | const harness = makeSimulation({ workerCount: 1 }); |
| 531 | try |
| 532 | { |
| 533 | const data = createEmptyChunkData(); |
| 534 | data.open = true; |
| 535 | data.spawners = [{ |
| 536 | localX: 9, |
| 537 | localY: 8, |
| 538 | config: { |
| 539 | mobTypeId: MobId.humanoid, |
| 540 | maxAlive: 1, |
| 541 | spawnRangeTiles: 0, |
| 542 | maxWanderRangeTiles: 0, |
| 543 | displayName: "Buyer", |
| 544 | buysItems: true, |
| 545 | hiddenDiscount: 1 |
| 546 | } |
| 547 | }]; |
| 548 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 549 | harness.simulation.reloadWorld(); |
| 550 | |
| 551 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 552 | const slots = emptySlots(INVENTORY_SLOTS); |
| 553 | slots[0] = { itemId: ItemId.stick, quantity: 1 }; |
| 554 | harness.stateDb.saveBag(character.character_id, { |
| 555 | inventory: slots, |
| 556 | equipment: emptyEquipment(), |
| 557 | vault: [], |
| 558 | vaultReserved: [] |
| 559 | }); |
| 560 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 561 | harness.simulation.step(); |
| 562 | harness.advance(50); |
| 563 | harness.simulation.step(); |
| 564 | |
| 565 | const record = harness.simulation.entityRecord(playerId); |
| 566 | const worker = harness.simulation.workers[record.workerId]; |
| 567 | let npcId = null; |
| 568 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 569 | { |
| 570 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 571 | { |
| 572 | npcId = entity.entityId; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | assert.ok(npcId); |
| 577 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 578 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 579 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 580 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 581 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 582 | playerPos.tileX = npcPos.tileX; |
| 583 | playerPos.tileY = npcPos.tileY; |
| 584 | |
| 585 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 586 | harness.simulation.enqueue(1, { |
| 587 | type: "sell-to-shop", |
| 588 | characterId: character.character_id, |
| 589 | npcEntityId: npcId, |
| 590 | slotIndex: 0, |
| 591 | quantity: 1 |
| 592 | }); |
| 593 | const sold = harness.simulation.step(); |
| 594 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 595 | const inv = worker.world.ecs.get(playerHandle, Component.inventory); |
| 596 | assert.equal(inv.slots[0]?.itemId === ItemId.stick, false); |
| 597 | assert.ok(countCoinsInSlots(inv.slots) >= beforeCoins + shopBuyFromPlayerPrice(ItemId.stick)); |
| 598 | assert.equal(shop.boughtStock[ItemId.stick], 1); |
| 599 | } |
| 600 | finally |
| 601 | { harness.close(); } |
| 602 | }); |
| 603 | |
| 604 | test("selling a filled cheque pays reserved-goods price minus 5% admin fee", () => |
| 605 | { |
| 606 | const harness = makeSimulation({ workerCount: 1 }); |
| 607 | try |
| 608 | { |
| 609 | const data = createEmptyChunkData(); |
| 610 | data.open = true; |
| 611 | data.blocks[8 * 16 + 8] = { typeId: BlockType.bank, state: 0 }; |
| 612 | data.spawners = [{ |
| 613 | localX: 9, |
| 614 | localY: 8, |
| 615 | config: { |
| 616 | mobTypeId: MobId.humanoid, |
| 617 | maxAlive: 1, |
| 618 | spawnRangeTiles: 0, |
| 619 | maxWanderRangeTiles: 0, |
| 620 | displayName: "Buyer", |
| 621 | sells: [{ itemId: ItemId.copperOre, quantity: 0 }], |
| 622 | buysItems: true, |
| 623 | hiddenDiscount: 1 |
| 624 | } |
| 625 | }]; |
| 626 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 627 | harness.simulation.reloadWorld(); |
| 628 | |
| 629 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 630 | const slots = emptySlots(INVENTORY_SLOTS); |
| 631 | slots[0] = { itemId: ItemId.copperOre, quantity: 2 }; |
| 632 | harness.stateDb.saveBag(character.character_id, { |
| 633 | inventory: slots, |
| 634 | equipment: emptyEquipment(), |
| 635 | vault: [], |
| 636 | vaultReserved: [] |
| 637 | }); |
| 638 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 639 | harness.simulation.step(); |
| 640 | |
| 641 | harness.simulation.enqueue(1, { |
| 642 | type: "get-blank-cheques", |
| 643 | characterId: character.character_id |
| 644 | }); |
| 645 | harness.simulation.step(); |
| 646 | harness.simulation.enqueue(1, { |
| 647 | type: "bank-deposit", |
| 648 | characterId: character.character_id, |
| 649 | slotIndex: 0, |
| 650 | quantity: null |
| 651 | }); |
| 652 | harness.simulation.step(); |
| 653 | let bag = harness.simulation.bagOf(playerId); |
| 654 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 655 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.copperOre); |
| 656 | harness.simulation.enqueue(1, { |
| 657 | type: "write-cheque", |
| 658 | characterId: character.character_id, |
| 659 | slotIndex: blankIndex, |
| 660 | vaultSlotIndex: vaultIndex, |
| 661 | quantity: 2 |
| 662 | }); |
| 663 | harness.simulation.step(); |
| 664 | bag = harness.simulation.bagOf(playerId); |
| 665 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 666 | assert.ok(chequeIndex >= 0); |
| 667 | |
| 668 | const record = harness.simulation.entityRecord(playerId); |
| 669 | const worker = harness.simulation.workers[record.workerId]; |
| 670 | let npcId = null; |
| 671 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 672 | { |
| 673 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 674 | { |
| 675 | npcId = entity.entityId; |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | assert.ok(npcId); |
| 680 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 681 | const shop = worker.world.ecs.get(npcHandle, Component.shop); |
| 682 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 683 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 684 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 685 | playerPos.tileX = npcPos.tileX; |
| 686 | playerPos.tileY = npcPos.tileY; |
| 687 | |
| 688 | const quote = resolveShopSellQuote(bag.inventory[chequeIndex]); |
| 689 | assert.ok(quote?.isCheque); |
| 690 | const beforeCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 691 | harness.simulation.enqueue(1, { |
| 692 | type: "sell-to-shop", |
| 693 | characterId: character.character_id, |
| 694 | npcEntityId: npcId, |
| 695 | slotIndex: chequeIndex, |
| 696 | quantity: 1 |
| 697 | }); |
| 698 | const sold = harness.simulation.step(); |
| 699 | assert.equal(sold.events.some((event) => event.type === "command-error"), false, JSON.stringify(sold.events)); |
| 700 | bag = harness.simulation.bagOf(playerId); |
| 701 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 702 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 703 | const afterCoins = countCoinsInSlots(worker.world.ecs.get(playerHandle, Component.inventory).slots); |
| 704 | assert.equal(afterCoins - beforeCoins, quote.payout); |
| 705 | // Infinite catalog absorbs goods without bought-from-player resale stock. |
| 706 | assert.equal(shop.boughtStock?.[ItemId.copperOre] | 0, 0); |
| 707 | } |
| 708 | finally |
| 709 | { harness.close(); } |
| 710 | }); |