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