BlueGit

timfa questward

Files

questward / server / tests / bounty.test.js

bounty.test.js

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