BlueGit

timfa questward

Files

questward / server / tests / quest-kill-spawner.test.js

quest-kill-spawner.test.js

quest-kill-spawner.test.js 2.9 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { MobId } from "../../shared/definitions/mobs.js";
4import { normalizeQuestDef } from "../../shared/quests/normalize.js";
5import { ConditionKind } from "../../shared/quests/conditions.js";
6import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
7import { mobComponents } from "../src/simulation/spawners.js";
8import { applyDamageAndKill } from "../src/combat/service.js";
9import { Component } from "../src/ecs/components.js";
10
11function saveQuest (harness, raw)
12{
13 const revisionId = harness.worldDb.getActiveRevisionId();
14 const quest = normalizeQuestDef(raw);
15 const pending = harness.worldDb.ensurePendingRevision(1);
16 harness.worldDb.saveQuestDef(pending, quest);
17 harness.worldDb.promotePendingRevision();
18 harness.simulation.questContent.reload();
19 return quest;
20}
21
22test("killMobFromSpawner counts only matching spawner kills", () =>
23{
24 const harness = makeSimulation({ workerCount: 1 });
25 try
26 {
27 saveQuest(harness, {
28 questId: "spawner-rats",
29 title: "Spawner rats",
30 steps: [{
31 stepId: "step-1",
32 title: "Hunt",
33 objectives: [{
34 objectiveId: "obj-1",
35 text: "Kill 2 from rats-a",
36 when: {
37 all: [{
38 condition: {
39 kind: ConditionKind.killMobFromSpawner,
40 mobTypeId: MobId.rat,
41 spawnerId: "rats-a",
42 count: 2
43 }
44 }]
45 }
46 }],
47 completionJournalText: "Done."
48 }]
49 });
50 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
51 const playerId = harness.simulation.spawnCharacter(1, character);
52 harness.simulation.quests.startQuest(character.character_id, "spawner-rats", null, []);
53
54 function killRat (spawnerId)
55 {
56 const ratId = harness.simulation.spawnEntity(mobComponents({
57 tileX: 9,
58 tileY: 8,
59 mobTypeId: MobId.rat,
60 spawnerId
61 }));
62 const worker = harness.simulation.workers[
63 harness.simulation.entityRecord(ratId).workerId
64 ];
65 worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1;
66 const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now());
67 const killed = events.find((event) => event.type === "mob-killed");
68 harness.simulation.quests.noteKill(character.character_id, {
69 mobTypeId: killed.mobTypeId,
70 spawnerId: killed.spawnerId
71 });
72 }
73
74 killRat("rats-b");
75 assert.equal(
76 harness.stateDb.getQuestState(character.character_id, "spawner-rats").progress?.counters?.["spawner:rats-a"] ?? 0,
77 0
78 );
79 killRat("rats-a");
80 assert.equal(
81 harness.stateDb.getQuestState(character.character_id, "spawner-rats").progress?.counters?.["spawner:rats-a"],
82 1
83 );
84 const snap = harness.simulation.quests.questSnapshot(character.character_id);
85 assert.equal(snap.quests[0].step.objectives[0].current, 1);
86 assert.equal(snap.quests[0].step.objectives[0].required, 2);
87 }
88 finally
89 { harness.close(); }
90});