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