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