| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { shopUnitPrice, undiscountedMarketValue, canSellInNpcShop, lerp } from "../../shared/shop/pricing.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { MobId, normalizeSpawnerConfig, isHumanoidMob } from "../../shared/definitions/mobs.js"; |
| 6 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 7 | import { Component } from "../src/ecs/components.js"; |
| 8 | import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js"; |
| 9 | import { ItemOriginKind } from "../../shared/items/origin.js"; |
| 10 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 11 | |
| 12 | test("shop prices fall back to seedValue and apply hidden discount", () => |
| 13 | { |
| 14 | assert.equal(undiscountedMarketValue(ItemId.copperOre), 2); |
| 15 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.9 }), 2); |
| 16 | assert.equal(shopUnitPrice(ItemId.copperOre, { hiddenDiscount: 0.5 }), 1); |
| 17 | assert.equal(lerp(10, 20, 0.5), 15); |
| 18 | assert.equal(canSellInNpcShop(ItemId.copperOre), true); |
| 19 | assert.equal(canSellInNpcShop(ItemId.tinOre), true); |
| 20 | assert.equal(canSellInNpcShop(ItemId.stick), false); |
| 21 | }); |
| 22 | |
| 23 | test("humanoid spawner config keeps sex equipment and sells", () => |
| 24 | { |
| 25 | assert.equal(isHumanoidMob(MobId.humanoid), true); |
| 26 | const config = normalizeSpawnerConfig({ |
| 27 | mobTypeId: MobId.humanoid, |
| 28 | sex: "female", |
| 29 | displayName: "Ore trader", |
| 30 | equipment: { chest: ItemId.clothShirt, weapon: ItemId.stick }, |
| 31 | sells: [ItemId.copperOre, ItemId.tinOre, ItemId.copperOre], |
| 32 | hiddenDiscount: 0.85 |
| 33 | }, "shop-1"); |
| 34 | assert.equal(config.sex, "female"); |
| 35 | assert.equal(config.displayName, "Ore trader"); |
| 36 | assert.equal(config.equipment.chest, ItemId.clothShirt); |
| 37 | assert.deepEqual(config.sells, [ItemId.copperOre, ItemId.tinOre]); |
| 38 | assert.equal(config.hiddenDiscount, 0.85); |
| 39 | assert.equal(config.maxAlive, 1); |
| 40 | }); |
| 41 | |
| 42 | test("buy copper ore from a spawned shopkeeper", () => |
| 43 | { |
| 44 | const harness = makeSimulation({ workerCount: 1 }); |
| 45 | try |
| 46 | { |
| 47 | const data = createEmptyChunkData(); |
| 48 | data.open = true; |
| 49 | data.spawners = [{ |
| 50 | localX: 9, |
| 51 | localY: 8, |
| 52 | config: { |
| 53 | mobTypeId: MobId.humanoid, |
| 54 | maxAlive: 1, |
| 55 | spawnRangeTiles: 1, |
| 56 | maxWanderRangeTiles: 1, |
| 57 | sex: "male", |
| 58 | displayName: "Ore trader", |
| 59 | sells: [ItemId.copperOre, ItemId.tinOre], |
| 60 | hiddenDiscount: 1, |
| 61 | equipment: { chest: ItemId.clothShirt } |
| 62 | } |
| 63 | }]; |
| 64 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 65 | harness.simulation.reloadWorld(); |
| 66 | |
| 67 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 68 | const funded = addCoinsToSlots(emptySlots(), 50); |
| 69 | assert.equal(funded.ok, true); |
| 70 | harness.stateDb.saveBag(character.character_id, { |
| 71 | inventory: funded.slots, |
| 72 | equipment: emptyEquipment(), |
| 73 | vault: [], |
| 74 | vaultReserved: [] |
| 75 | }); |
| 76 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 77 | harness.simulation.step(); |
| 78 | harness.advance(50); |
| 79 | harness.simulation.step(); |
| 80 | |
| 81 | const record = harness.simulation.entityRecord(playerId); |
| 82 | const worker = harness.simulation.workers[record.workerId]; |
| 83 | let npcId = null; |
| 84 | for (const entity of worker.world.ecs.query(Component.shop, Component.creature)) |
| 85 | { |
| 86 | if (entity.creature.mobTypeId === MobId.humanoid) |
| 87 | { |
| 88 | npcId = entity.entityId; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | assert.ok(npcId, "expected a humanoid shopkeeper"); |
| 93 | |
| 94 | const npcHandle = worker.world.ecs.handleOf(npcId); |
| 95 | const npcPos = worker.world.ecs.get(npcHandle, Component.position); |
| 96 | const playerHandle = worker.world.ecs.handleOf(playerId); |
| 97 | const playerPos = worker.world.ecs.get(playerHandle, Component.position); |
| 98 | playerPos.tileX = npcPos.tileX; |
| 99 | playerPos.tileY = npcPos.tileY; |
| 100 | |
| 101 | harness.simulation.enqueue(1, { |
| 102 | type: "interact", |
| 103 | characterId: character.character_id, |
| 104 | targetEntityId: npcId |
| 105 | }); |
| 106 | const open = harness.simulation.step(); |
| 107 | const shopOpen = open.events.find((event) => event.type === "shop-open"); |
| 108 | assert.ok(shopOpen, JSON.stringify(open.events)); |
| 109 | assert.ok(shopOpen.stock.some((row) => row.itemId === ItemId.copperOre)); |
| 110 | |
| 111 | harness.simulation.enqueue(1, { |
| 112 | type: "buy-from-shop", |
| 113 | characterId: character.character_id, |
| 114 | npcEntityId: npcId, |
| 115 | itemId: ItemId.copperOre, |
| 116 | quantity: 1 |
| 117 | }); |
| 118 | const buy = harness.simulation.step(); |
| 119 | assert.equal(buy.events.some((event) => event.type === "command-error"), false, JSON.stringify(buy.events)); |
| 120 | const bag = harness.simulation.bagOf(playerId); |
| 121 | const ore = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.copperOre); |
| 122 | assert.ok(ore, JSON.stringify(bag.inventory)); |
| 123 | assert.equal(ore.origins?.[0]?.origin?.kind, ItemOriginKind.shop); |
| 124 | } |
| 125 | finally |
| 126 | { harness.close(); } |
| 127 | }); |