| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 6 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 7 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 8 | |
| 9 | function placeBlock (worldDb, tileX, tileY, typeId) |
| 10 | { |
| 11 | const chunkX = Math.floor(tileX / 16); |
| 12 | const chunkY = Math.floor(tileY / 16); |
| 13 | const data = createEmptyChunkData(); |
| 14 | data.open = true; |
| 15 | const localX = tileX - chunkX * 16; |
| 16 | const localY = tileY - chunkY * 16; |
| 17 | data.blocks[localY * 16 + localX] = { typeId, state: 0 }; |
| 18 | worldDb.saveChunkVersion( |
| 19 | chunkX, |
| 20 | chunkY, |
| 21 | worldDb.getActiveRevisionId(), |
| 22 | true, |
| 23 | encodeChunkBlob(data) |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | function countCoins (slots) |
| 28 | { |
| 29 | return (slots ?? []) |
| 30 | .filter((slot) => slot?.itemId === ItemId.coins) |
| 31 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 32 | } |
| 33 | |
| 34 | function giveCoins (harness, entityId, amount) |
| 35 | { |
| 36 | const bag = harness.simulation.bagOf(entityId); |
| 37 | bag.inventory = emptySlots(); |
| 38 | bag.inventory[0] = { itemId: ItemId.coins, quantity: amount }; |
| 39 | bag.equipment = emptyEquipment(); |
| 40 | harness.simulation.applyBag(entityId, bag); |
| 41 | harness.stateDb.saveBag( |
| 42 | harness.simulation.directory.get(entityId).characterId, |
| 43 | bag |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | test("raise bounty spends inventory coins and tracks ledger", () => |
| 48 | { |
| 49 | const harness = makeSimulation({ workerCount: 1 }); |
| 50 | try |
| 51 | { |
| 52 | placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); |
| 53 | harness.simulation.reloadWorld(); |
| 54 | const placer = makeCharacter(harness.stateDb, { |
| 55 | firstName: "Place", |
| 56 | lastName: "R", |
| 57 | tileX: 8, |
| 58 | tileY: 9 |
| 59 | }); |
| 60 | const target = makeCharacter(harness.stateDb, { |
| 61 | firstName: "Mark", |
| 62 | lastName: "Ed", |
| 63 | tileX: 10, |
| 64 | tileY: 10 |
| 65 | }); |
| 66 | const placerId = harness.simulation.spawnCharacter(1, placer); |
| 67 | giveCoins(harness, placerId, 500); |
| 68 | |
| 69 | harness.simulation.enqueue(1, { |
| 70 | type: "raise-bounty", |
| 71 | characterId: placer.character_id, |
| 72 | firstName: "Mark", |
| 73 | lastName: "Ed", |
| 74 | amountCopper: 250 |
| 75 | }); |
| 76 | const result = harness.simulation.step(); |
| 77 | const snap = result.events.find((event) => event.type === "bounty-snapshot"); |
| 78 | assert.ok(snap?.raised); |
| 79 | assert.equal(snap.raised.amountCopper, 250); |
| 80 | assert.equal(harness.stateDb.getCharacterBounty(target.character_id), 250); |
| 81 | assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 250); |
| 82 | |
| 83 | harness.simulation.enqueue(1, { |
| 84 | type: "raise-bounty", |
| 85 | characterId: placer.character_id, |
| 86 | firstName: "Mark", |
| 87 | lastName: "Ed", |
| 88 | amountCopper: 100 |
| 89 | }); |
| 90 | harness.simulation.step(); |
| 91 | assert.equal(harness.stateDb.getCharacterBounty(target.character_id), 350); |
| 92 | assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 150); |
| 93 | } |
| 94 | finally |
| 95 | { harness.close(); } |
| 96 | }); |
| 97 | |
| 98 | test("open bounty requires standing next to a bounty office", () => |
| 99 | { |
| 100 | const harness = makeSimulation({ workerCount: 1 }); |
| 101 | try |
| 102 | { |
| 103 | placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); |
| 104 | harness.simulation.reloadWorld(); |
| 105 | const character = makeCharacter(harness.stateDb, { tileX: 12, tileY: 12 }); |
| 106 | harness.simulation.spawnCharacter(1, character); |
| 107 | harness.simulation.enqueue(1, { |
| 108 | type: "open-bounty", |
| 109 | characterId: character.character_id |
| 110 | }); |
| 111 | const result = harness.simulation.step(); |
| 112 | const error = result.events.find((event) => event.type === "command-error"); |
| 113 | assert.ok(error); |
| 114 | assert.match(error.message, /Bounty Office/i); |
| 115 | } |
| 116 | finally |
| 117 | { harness.close(); } |
| 118 | }); |
| 119 | |
| 120 | test("PvP kill pays bounty into killer vault and clears ledger", () => |
| 121 | { |
| 122 | const harness = makeSimulation({ workerCount: 1 }); |
| 123 | try |
| 124 | { |
| 125 | placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); |
| 126 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 127 | harness.simulation.reloadWorld(); |
| 128 | |
| 129 | const victim = makeCharacter(harness.stateDb, { |
| 130 | firstName: "Vic", |
| 131 | lastName: "Tim", |
| 132 | tileX: 9, |
| 133 | tileY: 9 |
| 134 | }); |
| 135 | const killer = makeCharacter(harness.stateDb, { |
| 136 | firstName: "Kill", |
| 137 | lastName: "Er", |
| 138 | tileX: 10, |
| 139 | tileY: 9 |
| 140 | }); |
| 141 | harness.stateDb.addCharacterBounty(victim.character_id, 400); |
| 142 | |
| 143 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 144 | const killerId = harness.simulation.spawnCharacter(2, killer); |
| 145 | |
| 146 | const settled = harness.simulation.bounty.settleKill( |
| 147 | victim.character_id, |
| 148 | killerId, |
| 149 | 9, |
| 150 | 9 |
| 151 | ); |
| 152 | assert.equal(settled?.paidCopper, 400); |
| 153 | assert.equal(harness.stateDb.getCharacterBounty(victim.character_id), 0); |
| 154 | assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 400); |
| 155 | |
| 156 | harness.stateDb.addKillCounter(killer.character_id, 500, harness.now()); |
| 157 | harness.simulation.refreshCharacterSkull(killer.character_id); |
| 158 | assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), 500); |
| 159 | assert.equal(harness.simulation.directory.get(killerId).skullValue, 500); |
| 160 | assert.equal(harness.simulation.directory.get(victimId).skullValue, 0); |
| 161 | } |
| 162 | finally |
| 163 | { harness.close(); } |
| 164 | }); |
| 165 | |
| 166 | test("friend PvP kill does not pay or clear bounty", () => |
| 167 | { |
| 168 | const harness = makeSimulation({ workerCount: 1 }); |
| 169 | try |
| 170 | { |
| 171 | placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice); |
| 172 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 173 | harness.simulation.reloadWorld(); |
| 174 | |
| 175 | const victim = makeCharacter(harness.stateDb, { |
| 176 | firstName: "Vic", |
| 177 | lastName: "Friend", |
| 178 | tileX: 9, |
| 179 | tileY: 9 |
| 180 | }); |
| 181 | const killer = makeCharacter(harness.stateDb, { |
| 182 | firstName: "Kill", |
| 183 | lastName: "Buddy", |
| 184 | tileX: 10, |
| 185 | tileY: 9 |
| 186 | }); |
| 187 | harness.stateDb.addCharacterBounty(victim.character_id, 400); |
| 188 | harness.stateDb.insertAccountFriendship(victim.account_id, killer.account_id); |
| 189 | |
| 190 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 191 | const killerId = harness.simulation.spawnCharacter(2, killer); |
| 192 | |
| 193 | const settled = harness.simulation.bounty.settleKill( |
| 194 | victim.character_id, |
| 195 | killerId, |
| 196 | 9, |
| 197 | 9 |
| 198 | ); |
| 199 | assert.equal(settled, null); |
| 200 | assert.equal(harness.stateDb.getCharacterBounty(victim.character_id), 400); |
| 201 | assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 0); |
| 202 | assert.equal(harness.simulation.directory.get(victimId).skullValue, 400); |
| 203 | } |
| 204 | finally |
| 205 | { harness.close(); } |
| 206 | }); |