BlueGit

timfa questward

Files

questward / server / tests / quests-conditions.test.js

quests-conditions.test.js

feat: branch quest steps and remember which steps were finished
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 4 weeks ago
25915c58 History
quests-conditions.test.js 10.4 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 evaluateConditionSet,
5 normalizeConditionSet,
6 ConditionKind,
7 ConditionSetMode
8} from "../../shared/quests/conditions.js";
9import {
10 countableObjectiveProgress,
11 formatObjectiveDisplayText
12} from "../../shared/quests/objectiveProgress.js";
13import { normalizeQuestDef, normalizeDialogueTree, questLookupFromRawDefs } from "../../shared/quests/normalize.js";
14import { deriveCompletedStepIds, resolveQuestStepId } from "../../shared/quests/questProgress.js";
15import { evaluateCondition } from "../../shared/quests/conditions.js";
16import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage, formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";
17
18test("ConditionSet AND + NOT", () =>
19{
20 const set = normalizeConditionSet({
21 all: [
22 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
23 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
24 ]
25 });
26 assert.equal(set.mode, ConditionSetMode.allOf);
27 assert.equal(evaluateConditionSet(set, {
28 countItem: (id) => (id === 1 ? 2 : 0),
29 questState: () => "not-started"
30 }), true);
31 assert.equal(evaluateConditionSet(set, {
32 countItem: (id) => (id === 1 ? 2 : 0),
33 questState: () => "completed"
34 }), false);
35});
36
37test("ConditionSet modes oneOrMoreOf onlyOneOf and noneOf", () =>
38{
39 const clauses = [
40 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
41 { condition: { kind: ConditionKind.hasItem, itemId: 2, quantity: 1 } }
42 ];
43 const ctxNone = { countItem: () => 0 };
44 const ctxOne = { countItem: (id) => (id === 1 ? 1 : 0) };
45 const ctxBoth = { countItem: () => 1 };
46
47 const oneOrMore = normalizeConditionSet({ mode: ConditionSetMode.oneOrMoreOf, all: clauses });
48 assert.equal(evaluateConditionSet(oneOrMore, ctxNone), false);
49 assert.equal(evaluateConditionSet(oneOrMore, ctxOne), true);
50 assert.equal(evaluateConditionSet(oneOrMore, ctxBoth), true);
51
52 const onlyOne = normalizeConditionSet({ mode: ConditionSetMode.onlyOneOf, all: clauses });
53 assert.equal(evaluateConditionSet(onlyOne, ctxNone), false);
54 assert.equal(evaluateConditionSet(onlyOne, ctxOne), true);
55 assert.equal(evaluateConditionSet(onlyOne, ctxBoth), false);
56
57 const none = normalizeConditionSet({ mode: ConditionSetMode.noneOf, all: clauses });
58 assert.equal(evaluateConditionSet(none, ctxNone), true);
59 assert.equal(evaluateConditionSet(none, ctxOne), false);
60 assert.equal(evaluateConditionSet(none, ctxBoth), false);
61
62 assert.equal(
63 normalizeConditionSet({ mode: "nope", all: clauses }).mode,
64 ConditionSetMode.allOf
65 );
66});
67
68test("normalize quest and dialogue", () =>
69{
70 const quest = normalizeQuestDef({
71 questId: "Fetch Ore!",
72 title: "Fetch Ore",
73 steps: [{
74 objectives: [{ text: "Have tin", when: { all: [{ kind: "hasItem", itemId: 10, quantity: 1 }] } }]
75 }]
76 });
77 assert.equal(quest.questId, "fetch-ore");
78 assert.equal(quest.steps[0].objectives[0].when.all[0].condition.kind, "hasItem");
79 assert.equal(quest.steps[0].exit.kind, "complete");
80 assert.equal(quest.steps[0].exit.implicit, true);
81
82 const tree = normalizeDialogueTree({
83 dialogueId: "Smith",
84 unique: true,
85 steps: [{
86 stepId: "hi",
87 lines: [{ speaker: "npc", text: "Hello" }],
88 itemRewards: [
89 { itemId: 10, quantity: 2 },
90 { itemId: 10, quantity: 9 },
91 { itemId: 0, quantity: 1 }
92 ],
93 exit: { kind: "end" }
94 }]
95 });
96 assert.equal(tree.dialogueId, "smith");
97 assert.equal(tree.unique, true);
98 assert.equal(tree.steps[0].exit.kind, "end");
99 assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);
100 assert.deepEqual(tree.steps[0].questRewards, []);
101});
102
103test("dialogue reward messages bag vs bank and lists", () =>
104{
105 assert.equal(
106 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 1 }], "bag"),
107 "Tin ore has been added to your bag."
108 );
109 assert.equal(
110 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 3 }], "bank"),
111 "3 Tin ore have been sent to your bank."
112 );
113 assert.equal(
114 formatDialogueRewardMessage([
115 { displayName: "Tin ore", quantity: 1 },
116 { displayName: "Copper ore", quantity: 1 }
117 ], "bag"),
118 "Tin ore and Copper ore have been added to your bag."
119 );
120 assert.equal(
121 formatDialogueRewardMessage([
122 { displayName: "A", quantity: 1 },
123 { displayName: "B", quantity: 1 },
124 { displayName: "C", quantity: 1 }
125 ], "bag"),
126 "A, B and C have been added to your bag."
127 );
128 assert.equal(
129 formatDialogueQuestRewardMessage(["Tin Delivery"]),
130 "The Tin Delivery quest has been added to your journal."
131 );
132 assert.equal(
133 formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
134 "The Alpha and Beta quests have been added to your journal."
135 );
136 assert.equal(
137 formatDialogueTakeMessage([{ displayName: "Copper ore", quantity: 2 }]),
138 "2 Copper ore have been removed from your bag."
139 );
140});
141
142test("countable objective progress for hasItem killCount and skillLevelAtLeast", () =>
143{
144 const hasItem = normalizeConditionSet({
145 all: [{ condition: { kind: ConditionKind.hasItem, itemId: 10, quantity: 30 } }]
146 });
147 assert.deepEqual(
148 countableObjectiveProgress(hasItem, { countItem: (id) => (id === 10 ? 5 : 0) }),
149 { current: 5, required: 30 }
150 );
151 assert.equal(
152 formatObjectiveDisplayText("Collect Pine Logs", { current: 5, required: 30 }),
153 "Collect Pine Logs (5 / 30)"
154 );
155
156 const quests = [{
157 questId: "q",
158 steps: [{ stepId: "alpha" }, { stepId: "beta" }]
159 }];
160 const converted = normalizeConditionSet({
161 all: [
162 { condition: { kind: "questStepAtLeast", questId: "q", step: 1 } },
163 { condition: { kind: "questStepAtLeast", questId: "q", step: 2 } },
164 { condition: { kind: "questStepAtLeast", questId: "q", step: 3 } },
165 { condition: { kind: "questStepAtLeast", questId: "q", step: 9 } },
166 { condition: { kind: "questState", questId: "q", state: 2 } }
167 ]
168 }, { quests });
169 assert.deepEqual(converted.all[0].condition, { kind: "questState", questId: "q", state: "started" });
170 assert.deepEqual(converted.all[1].condition, { kind: "hasCompletedStep", questId: "q", stepId: "alpha" });
171 assert.deepEqual(converted.all[2].condition, { kind: "hasCompletedStep", questId: "q", stepId: "beta" });
172 assert.deepEqual(converted.all[3].condition, { kind: "questState", questId: "q", state: "completed" });
173 assert.equal(converted.all[4].condition.state, "beta");
174
175 const pending = normalizeConditionSet({
176 all: [{ condition: { kind: "questStepAtLeast", questId: "missing", step: 2 } }]
177 });
178 assert.equal(pending.all[0].condition.kind, "questStepAtLeast");
179 assert.equal(
180 evaluateCondition(pending.all[0].condition, {
181 questState: () => "beta",
182 questSteps: () => ["alpha", "beta"],
183 hasCompletedStep: (_questId, stepId) => stepId === "alpha"
184 }),
185 true
186 );
187 assert.equal(
188 evaluateCondition(
189 { kind: ConditionKind.hasCompletedStep, questId: "q", stepId: "alpha" },
190 { hasCompletedStep: (_questId, stepId) => stepId === "alpha" }
191 ),
192 true
193 );
194 assert.equal(
195 evaluateCondition(
196 { kind: ConditionKind.questState, questId: "q", state: "beta" },
197 { questState: () => "beta" }
198 ),
199 true
200 );
201 assert.equal(
202 evaluateCondition(
203 { kind: ConditionKind.questState, questId: "q", state: "started" },
204 { questState: () => "not-started" }
205 ),
206 false
207 );
208
209 const lookup = questLookupFromRawDefs(quests);
210 const tree = normalizeDialogueTree({
211 dialogueId: "gate",
212 steps: [{
213 stepId: "hi",
214 lines: [{ speaker: "npc", text: "Hi" }],
215 exit: {
216 kind: "branches",
217 branches: [{
218 when: { all: [{ condition: { kind: "questStepAtLeast", questId: "q", step: 2 } }] },
219 nextStepId: null
220 }]
221 }
222 }]
223 }, lookup);
224 assert.equal(tree.steps[0].exit.branches[0].when.all[0].condition.kind, "hasCompletedStep");
225 assert.equal(tree.steps[0].exit.branches[0].when.all[0].condition.stepId, "alpha");
226
227 const linked = normalizeQuestDef({
228 questId: "chain",
229 steps: [{ stepId: "a" }, { stepId: "b" }]
230 });
231 assert.equal(linked.steps[0].exit.kind, "continue");
232 assert.equal(linked.steps[0].exit.nextStepId, "b");
233 assert.equal(linked.steps[0].exit.implicit, true);
234 const inserted = normalizeQuestDef({
235 ...linked,
236 steps: [linked.steps[0], { stepId: "mid", title: "Mid" }, linked.steps[1]]
237 });
238 assert.equal(inserted.steps[0].exit.nextStepId, "mid");
239 assert.equal(inserted.steps[1].exit.nextStepId, "b");
240 assert.equal(inserted.steps[2].exit.kind, "complete");
241
242 const branched = normalizeQuestDef({
243 questId: "hub-quest",
244 steps: [{
245 stepId: "hub",
246 exit: {
247 kind: "branches",
248 branches: [{
249 label: "Cave",
250 text: "Stand in the cave",
251 when: { all: [{ condition: { kind: "atTile", tileX: 3, tileY: 4, dimensionId: "cave" } }] },
252 nextStepId: "end"
253 }]
254 }
255 }, { stepId: "end" }]
256 });
257 assert.equal(branched.steps[0].exit.kind, "branches");
258 assert.equal(branched.steps[0].exit.implicit, undefined);
259 assert.equal(branched.steps[0].exit.branches[0].when.all[0].condition.dimensionId, "cave");
260 assert.equal(branched.steps[1].exit.kind, "complete");
261
262 const linearDef = {
263 steps: [{ stepId: "alpha" }, { stepId: "beta" }, { stepId: "gamma" }]
264 };
265 assert.deepEqual(deriveCompletedStepIds(linearDef, 2, {}), ["alpha"]);
266 assert.deepEqual(
267 deriveCompletedStepIds(linearDef, "completed", {}),
268 ["alpha", "beta", "gamma"]
269 );
270 assert.deepEqual(
271 deriveCompletedStepIds(linearDef, "beta", { completedStepIds: ["alpha"] }),
272 ["alpha"]
273 );
274 assert.equal(resolveQuestStepId(linearDef, 2), "beta");
275 assert.equal(resolveQuestStepId(linearDef, "gamma"), "gamma");
276
277 const kills = normalizeConditionSet({
278 all: [{ condition: { kind: ConditionKind.killCount, mobTypeId: "rat", count: 3 } }]
279 });
280 assert.deepEqual(
281 countableObjectiveProgress(kills, {
282 killCount: (c) => (c.mobTypeId === "rat" ? 2 : 0)
283 }),
284 { current: 2, required: 3 }
285 );
286
287 const fromSpawner = normalizeConditionSet({
288 all: [{ condition: { kind: ConditionKind.killMobFromSpawner, mobTypeId: "rat", spawnerId: "rats-1", count: 4 } }]
289 });
290 assert.deepEqual(
291 countableObjectiveProgress(fromSpawner, {
292 killCount: (c) => (c.kind === ConditionKind.killMobFromSpawner && c.spawnerId === "rats-1" ? 1 : 0)
293 }),
294 { current: 1, required: 4 }
295 );
296 assert.equal(
297 evaluateConditionSet(fromSpawner, {
298 killCount: (c) => (c.spawnerId === "rats-1" ? 4 : 0)
299 }),
300 true
301 );
302
303 const skill = normalizeConditionSet({
304 all: [{ condition: { kind: ConditionKind.skillLevelAtLeast, skillId: "woodcutting", level: 20 } }]
305 });
306 assert.deepEqual(
307 countableObjectiveProgress(skill, { skillLevel: (id) => (id === "woodcutting" ? 12 : 1) }),
308 { current: 12, required: 20 }
309 );
310});