BlueGit

timfa questward

Files

questward / server / tests / bounty.test.js

bounty.test.js

bounty.test.js 11.2 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 { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
8import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
9
10function placeBlock (worldDb, tileX, tileY, typeId, dimensionId = OVERWORLD_ID)
11{
12 const chunkX = Math.floor(tileX / 16);
13 const chunkY = Math.floor(tileY / 16);
14 const rev = worldDb.getActiveRevisionId();
15 const existing = worldDb.getChunkVersion(chunkX, chunkY, rev, dimensionId);
16 const data = createEmptyChunkData();
17 data.open = true;
18 if (existing?.data_blob)
19 {
20 const parsed = typeof existing.data_blob === "string"
21 ? JSON.parse(existing.data_blob)
22 : existing.data_blob;
23 data.grounds = parsed.grounds ?? data.grounds;
24 data.blocks = parsed.blocks ?? data.blocks;
25 data.open = parsed.open ?? true;
26 }
27 const localX = tileX - chunkX * 16;
28 const localY = tileY - chunkY * 16;
29 data.blocks[localY * 16 + localX] = { typeId, state: 0 };
30 worldDb.saveChunkVersion(
31 chunkX,
32 chunkY,
33 rev,
34 true,
35 encodeChunkBlob(data),
36 dimensionId
37 );
38}
39
40function countCoins (slots)
41{
42 return (slots ?? [])
43 .filter((slot) => slot?.itemId === ItemId.coins)
44 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
45}
46
47function giveCoins (harness, entityId, amount)
48{
49 const bag = harness.simulation.bagOf(entityId);
50 bag.inventory = emptySlots();
51 bag.inventory[0] = { itemId: ItemId.coins, quantity: amount };
52 bag.equipment = emptyEquipment();
53 harness.simulation.applyBag(entityId, bag);
54 harness.stateDb.saveBag(
55 harness.simulation.directory.get(entityId).characterId,
56 bag
57 );
58}
59
60test("raise bounty spends inventory coins and tracks ledger", () =>
61{
62 const harness = makeSimulation({ workerCount: 1 });
63 try
64 {
65 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
66 harness.simulation.reloadWorld();
67 const placer = makeCharacter(harness.stateDb, {
68 firstName: "Place",
69 lastName: "R",
70 tileX: 8,
71 tileY: 9
72 });
73 const target = makeCharacter(harness.stateDb, {
74 firstName: "Mark",
75 lastName: "Ed",
76 tileX: 10,
77 tileY: 10
78 });
79 const placerId = harness.simulation.spawnCharacter(1, placer);
80 giveCoins(harness, placerId, 500);
81
82 harness.simulation.enqueue(1, {
83 type: "raise-bounty",
84 characterId: placer.character_id,
85 firstName: "Mark",
86 lastName: "Ed",
87 amountCopper: 250
88 });
89 const result = harness.simulation.step();
90 const snap = result.events.find((event) => event.type === "bounty-snapshot");
91 assert.ok(snap?.raised);
92 assert.equal(snap.raised.amountCopper, 250);
93 assert.equal(harness.stateDb.getCharacterBounty(target.character_id, harness.now()), 250);
94 assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 250);
95
96 harness.simulation.enqueue(1, {
97 type: "raise-bounty",
98 characterId: placer.character_id,
99 firstName: "Mark",
100 lastName: "Ed",
101 amountCopper: 100
102 });
103 harness.simulation.step();
104 assert.equal(harness.stateDb.getCharacterBounty(target.character_id, harness.now()), 350);
105 assert.equal(countCoins(harness.simulation.bagOf(placerId).inventory), 150);
106 }
107 finally
108 { harness.close(); }
109});
110
111test("open bounty requires standing next to a bounty office", () =>
112{
113 const harness = makeSimulation({ workerCount: 1 });
114 try
115 {
116 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
117 harness.simulation.reloadWorld();
118 const character = makeCharacter(harness.stateDb, { tileX: 12, tileY: 12 });
119 harness.simulation.spawnCharacter(1, character);
120 harness.simulation.enqueue(1, {
121 type: "open-bounty",
122 characterId: character.character_id
123 });
124 const result = harness.simulation.step();
125 const error = result.events.find((event) => event.type === "command-error");
126 assert.ok(error);
127 assert.match(error.message, /Bounty Office/i);
128 }
129 finally
130 { harness.close(); }
131});
132
133test("PvP kill pays bounty into killer vault and clears ledger", () =>
134{
135 const harness = makeSimulation({ workerCount: 1 });
136 try
137 {
138 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
139 placeBlock(harness.worldDb, 7, 8, BlockType.bank);
140 harness.simulation.reloadWorld();
141
142 const victim = makeCharacter(harness.stateDb, {
143 firstName: "Vic",
144 lastName: "Tim",
145 tileX: 9,
146 tileY: 9
147 });
148 const killer = makeCharacter(harness.stateDb, {
149 firstName: "Kill",
150 lastName: "Er",
151 tileX: 10,
152 tileY: 9
153 });
154 harness.stateDb.addCharacterBounty(victim.character_id, 400);
155
156 const victimId = harness.simulation.spawnCharacter(1, victim);
157 const killerId = harness.simulation.spawnCharacter(2, killer);
158
159 const settled = harness.simulation.bounty.settleKill(
160 victim.character_id,
161 killerId,
162 9,
163 9
164 );
165 assert.equal(settled?.paidCopper, 400);
166 assert.equal(harness.stateDb.getCharacterBounty(victim.character_id, harness.now()), 0);
167 assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 400);
168
169 harness.stateDb.addKillCounter(killer.character_id, 500, harness.now());
170 harness.simulation.refreshCharacterSkull(killer.character_id);
171 assert.equal(harness.stateDb.peekKillCounter(killer.character_id, harness.now()), 500);
172 assert.equal(harness.simulation.directory.get(killerId).skullValue, 500);
173 assert.equal(harness.simulation.directory.get(victimId).skullValue, 0);
174 }
175 finally
176 { harness.close(); }
177});
178
179test("friend PvP kill does not pay or clear bounty", () =>
180{
181 const harness = makeSimulation({ workerCount: 1 });
182 try
183 {
184 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
185 placeBlock(harness.worldDb, 7, 8, BlockType.bank);
186 harness.simulation.reloadWorld();
187
188 const victim = makeCharacter(harness.stateDb, {
189 firstName: "Vic",
190 lastName: "Friend",
191 tileX: 9,
192 tileY: 9
193 });
194 const killer = makeCharacter(harness.stateDb, {
195 firstName: "Kill",
196 lastName: "Buddy",
197 tileX: 10,
198 tileY: 9
199 });
200 harness.stateDb.addCharacterBounty(victim.character_id, 400);
201 harness.stateDb.insertAccountFriendship(victim.account_id, killer.account_id);
202
203 const victimId = harness.simulation.spawnCharacter(1, victim);
204 const killerId = harness.simulation.spawnCharacter(2, killer);
205
206 const settled = harness.simulation.bounty.settleKill(
207 victim.character_id,
208 killerId,
209 9,
210 9
211 );
212 assert.equal(settled, null);
213 assert.equal(harness.stateDb.getCharacterBounty(victim.character_id, harness.now()), 400);
214 assert.equal(countCoins(harness.simulation.bagOf(killerId).vault), 0);
215 assert.equal(harness.simulation.directory.get(victimId).skullValue, 400);
216 }
217 finally
218 { harness.close(); }
219});
220
221test("pay off own bounty spends 10 coins per 1 bounty cleared", () =>
222{
223 const harness = makeSimulation({ workerCount: 1 });
224 try
225 {
226 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
227 harness.simulation.reloadWorld();
228 const character = makeCharacter(harness.stateDb, {
229 firstName: "Self",
230 lastName: "Pay",
231 tileX: 8,
232 tileY: 9
233 });
234 harness.stateDb.addCharacterBounty(character.character_id, 25);
235 const entityId = harness.simulation.spawnCharacter(1, character);
236 giveCoins(harness, entityId, 100);
237
238 harness.simulation.enqueue(1, {
239 type: "pay-bounty",
240 characterId: character.character_id,
241 amountCopper: 100
242 });
243 const result = harness.simulation.step();
244 const snap = result.events.find((event) => event.type === "bounty-snapshot");
245 assert.ok(snap?.paidOff);
246 assert.equal(snap.paidOff.clearedCopper, 10);
247 assert.equal(snap.paidOff.spentCopper, 100);
248 assert.equal(snap.myAmountCopper, 15);
249 assert.equal(harness.stateDb.getCharacterBounty(character.character_id, harness.now()), 15);
250 assert.equal(countCoins(harness.simulation.bagOf(entityId).inventory), 0);
251 assert.equal(harness.simulation.directory.get(entityId).skullValue, 15);
252 }
253 finally
254 { harness.close(); }
255});
256
257test("pay off caps at remaining bounty and spends only matching tens", () =>
258{
259 const harness = makeSimulation({ workerCount: 1 });
260 try
261 {
262 placeBlock(harness.worldDb, 8, 8, BlockType.bountyOffice);
263 harness.simulation.reloadWorld();
264 const character = makeCharacter(harness.stateDb, {
265 firstName: "Cap",
266 lastName: "Pay",
267 tileX: 8,
268 tileY: 9
269 });
270 harness.stateDb.addCharacterBounty(character.character_id, 3);
271 const entityId = harness.simulation.spawnCharacter(1, character);
272 giveCoins(harness, entityId, 100);
273
274 harness.simulation.enqueue(1, {
275 type: "pay-bounty",
276 characterId: character.character_id,
277 amountCopper: 100
278 });
279 harness.simulation.step();
280 assert.equal(harness.stateDb.getCharacterBounty(character.character_id, harness.now()), 0);
281 assert.equal(countCoins(harness.simulation.bagOf(entityId).inventory), 70);
282 }
283 finally
284 { harness.close(); }
285});
286
287test("bounty decays one coin per calendar day", () =>
288{
289 const harness = makeSimulation({ workerCount: 1 });
290 try
291 {
292 const character = makeCharacter(harness.stateDb, {
293 firstName: "Decay",
294 lastName: "Ing",
295 tileX: 5,
296 tileY: 5
297 });
298 const dayA = Date.parse("2026-03-01T12:00:00");
299 const dayC = Date.parse("2026-03-03T12:00:00");
300 harness.stateDb.addCharacterBounty(character.character_id, 10, dayA);
301 assert.equal(harness.stateDb.getCharacterBounty(character.character_id, dayA), 10);
302 assert.equal(harness.stateDb.getCharacterBounty(character.character_id, dayC), 8);
303 }
304 finally
305 { harness.close(); }
306});
307
308
309test("bounty payout letter prefers office in death dimension", () =>
310{
311 const harness = makeSimulation({ workerCount: 1 });
312 const CAVE_ID = "bounty-cave";
313 try
314 {
315 const rev = harness.worldDb.getActiveRevisionId();
316 harness.worldDb.saveDimension(rev, {
317 dimensionId: CAVE_ID,
318 displayName: "Bounty Cave",
319 lightingMode: "alwaysLit",
320 seasonMode: "summer",
321 defaultGroundTypeId: 1
322 });
323 const cave = createEmptyChunkData();
324 cave.open = true;
325 harness.worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID);
326
327 // Overworld office farther from death tile than cave office
328 placeBlock(harness.worldDb, 2, 2, BlockType.bountyOffice, OVERWORLD_ID);
329 placeBlock(harness.worldDb, 1, 2, BlockType.bank, OVERWORLD_ID);
330 placeBlock(harness.worldDb, 9, 9, BlockType.bountyOffice, CAVE_ID);
331 placeBlock(harness.worldDb, 8, 9, BlockType.bank, CAVE_ID);
332 harness.simulation.reloadWorld();
333
334 const victim = makeCharacter(harness.stateDb, {
335 firstName: "Vic",
336 lastName: "DimVictim",
337 tileX: 10,
338 tileY: 9
339 });
340 const killer = makeCharacter(harness.stateDb, {
341 firstName: "Kill",
342 lastName: "DimKiller",
343 tileX: 11,
344 tileY: 9
345 });
346 harness.stateDb.addCharacterBounty(victim.character_id, 400);
347
348 const killerId = harness.simulation.spawnCharacter(2, killer);
349 const settled = harness.simulation.bounty.settleKill(
350 victim.character_id,
351 killerId,
352 10,
353 9,
354 CAVE_ID
355 );
356 assert.equal(settled?.paidCopper, 400);
357
358 const office = harness.simulation.courier.nearestBountyOffice(10, 9, CAVE_ID);
359 assert.equal(office.dimensionId, CAVE_ID);
360 assert.equal(office.tileX, 9);
361 assert.equal(office.tileY, 9);
362
363 const open = harness.stateDb.listActiveCourierTransits();
364 assert.ok(open.length >= 1);
365 const row = open[open.length - 1];
366 assert.equal(row.originDimensionId, CAVE_ID);
367 assert.equal(Math.floor(row.originX), 9);
368 assert.equal(Math.floor(row.originY), 9);
369 }
370 finally
371 { harness.close(); }
372});