BlueGit

timfa questward

Re-evaluate quest objectives when opening the quest log.

hasItem and similar conditions were only checked on dialogue/kills, so the log showed stale done state until something else refreshed progress.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
f27f48ef8852af17483887dd3828c72e082074ad
Parent
9ca737d7
3 files changed +83 −5
server/src/quests/runtime.js +11 −0
@@ -601,6 +601,17 @@ export class QuestRuntime
601 601 return events;
602 602 }
603 603
604 /**
605 * Re-check active quest objectives against live bag/position/etc.
606 * Used when opening the quest log (and wherever progress may have changed offline of dialogue/kills).
607 */
608 reevaluateQuests (characterId, playerView = null, events = [])
609 {
610 const view = playerView ?? this.playerViewFor(characterId);
611 this.#reevaluateQuests(characterId, view, events);
612 return events;
613 }
614
604 615 /** Apply initial quests to every character in state (settings save / apply). */
605 616 grantInitialQuestsToAllCharacters ()
606 617 {
@@ -601,6 +601,17 @@ export class QuestRuntime
601 return events;
601 return events;
602 }
602 }
603
603
604 /**
605 * Re-check active quest objectives against live bag/position/etc.
606 * Used when opening the quest log (and wherever progress may have changed offline of dialogue/kills).
607 */
608 reevaluateQuests (characterId, playerView = null, events = [])
609 {
610 const view = playerView ?? this.playerViewFor(characterId);
611 this.#reevaluateQuests(characterId, view, events);
612 return events;
613 }
614
604 /** Apply initial quests to every character in state (settings save / apply). */
615 /** Apply initial quests to every character in state (settings save / apply). */
605 grantInitialQuestsToAllCharacters ()
616 grantInitialQuestsToAllCharacters ()
606 {
617 {
server/src/simulation/simulation.js +4 −5
@@ -1904,11 +1904,10 @@ export class Simulation
1904 1904 if (item.command.type === "request-quests")
1905 1905 {
1906 1906 const events = [];
1907 this.quests.ensureInitialQuests(
1908 characterId,
1909 this.quests.playerViewFor(characterId),
1910 events
1911 );
1907 const playerView = this.quests.playerViewFor(characterId);
1908 this.quests.ensureInitialQuests(characterId, playerView, events);
1909 // Objectives like hasItem only update when re-checked; refresh on open.
1910 this.quests.reevaluateQuests(characterId, playerView, events);
1912 1911 const out = this.#mapQuestPrivateEvents(events, sessionId, characterId);
1913 1912 if (!out.some((event) => event.type === "quests-snapshot"))
1914 1913 {
@@ -1904,11 +1904,10 @@ export class Simulation
1904 if (item.command.type === "request-quests")
1904 if (item.command.type === "request-quests")
1905 {
1905 {
1906 const events = [];
1906 const events = [];
1907 this.quests.ensureInitialQuests(
1907 const playerView = this.quests.playerViewFor(characterId);
1908 characterId,
1908 this.quests.ensureInitialQuests(characterId, playerView, events);
1909 this.quests.playerViewFor(characterId),
1909 // Objectives like hasItem only update when re-checked; refresh on open.
1910 events
1910 this.quests.reevaluateQuests(characterId, playerView, events);
1911 );
1912 const out = this.#mapQuestPrivateEvents(events, sessionId, characterId);
1911 const out = this.#mapQuestPrivateEvents(events, sessionId, characterId);
1913 if (!out.some((event) => event.type === "quests-snapshot"))
1912 if (!out.some((event) => event.type === "quests-snapshot"))
1914 {
1913 {
server/tests/initial-quests.test.js +68 −0
@@ -1,6 +1,7 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { normalizeQuestDef } from "../../shared/quests/normalize.js";
4 import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js";
4 5 import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5 6
6 7 function saveLiveQuest (harness, raw)
@@ -108,3 +109,70 @@ test("request-quests ensures missing initial quests before snapshot", () =>
108 109 finally
109 110 { harness.close(); }
110 111 });
112
113 test("request-quests re-checks hasItem objectives against the live bag", () =>
114 {
115 const harness = makeSimulation({ workerCount: 1 });
116 try
117 {
118 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
119 const playerId = harness.simulation.spawnCharacter(1, character);
120 harness.simulation.step();
121
122 const quest = saveLiveQuest(harness, {
123 questId: "fetch-rock",
124 title: "Fetch Rock",
125 steps: [{
126 stepId: "step-1",
127 title: "Gather",
128 objectives: [
129 {
130 objectiveId: "obj-rock",
131 text: "Obtain a rock",
132 when: { all: [{ condition: { kind: "hasItem", itemId: 4242, quantity: 1 } }] }
133 },
134 {
135 objectiveId: "obj-gem",
136 text: "Obtain a gem",
137 when: { all: [{ condition: { kind: "hasItem", itemId: 4243, quantity: 1 } }] }
138 }
139 ],
140 completionJournalText: "Got both."
141 }]
142 });
143 harness.simulation.quests.startQuest(
144 character.character_id,
145 quest.questId,
146 harness.simulation.quests.playerViewFor(character.character_id),
147 []
148 );
149
150 const before = harness.simulation.quests.questSnapshot(character.character_id);
151 assert.equal(before.quests[0].step.objectives[0].done, false);
152 assert.equal(before.quests[0].step.objectives[1].done, false);
153
154 const inventory = emptySlots(INVENTORY_SLOTS);
155 inventory[0] = { itemId: 4242, quantity: 1 };
156 harness.simulation.applyBag(playerId, {
157 inventory,
158 equipment: emptyEquipment(),
159 vault: [],
160 vaultReserved: []
161 });
162
163 harness.simulation.enqueue(1, {
164 type: "request-quests",
165 characterId: character.character_id
166 });
167 const result = harness.simulation.step();
168 const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
169 assert.ok(snap);
170 const questSnap = snap.quests.find((q) => q.questId === "fetch-rock");
171 assert.ok(questSnap);
172 assert.equal(questSnap.state, 1);
173 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-rock")?.done, true);
174 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-gem")?.done, false);
175 }
176 finally
177 { harness.close(); }
178 });
@@ -1,6 +1,7 @@
1import test from "node:test";
1import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { normalizeQuestDef } from "../../shared/quests/normalize.js";
3import { normalizeQuestDef } from "../../shared/quests/normalize.js";
4import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js";
4import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5
6
6function saveLiveQuest (harness, raw)
7function saveLiveQuest (harness, raw)
@@ -108,3 +109,70 @@ test("request-quests ensures missing initial quests before snapshot", () =>
108 finally
109 finally
109 { harness.close(); }
110 { harness.close(); }
110});
111});
112
113test("request-quests re-checks hasItem objectives against the live bag", () =>
114{
115 const harness = makeSimulation({ workerCount: 1 });
116 try
117 {
118 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
119 const playerId = harness.simulation.spawnCharacter(1, character);
120 harness.simulation.step();
121
122 const quest = saveLiveQuest(harness, {
123 questId: "fetch-rock",
124 title: "Fetch Rock",
125 steps: [{
126 stepId: "step-1",
127 title: "Gather",
128 objectives: [
129 {
130 objectiveId: "obj-rock",
131 text: "Obtain a rock",
132 when: { all: [{ condition: { kind: "hasItem", itemId: 4242, quantity: 1 } }] }
133 },
134 {
135 objectiveId: "obj-gem",
136 text: "Obtain a gem",
137 when: { all: [{ condition: { kind: "hasItem", itemId: 4243, quantity: 1 } }] }
138 }
139 ],
140 completionJournalText: "Got both."
141 }]
142 });
143 harness.simulation.quests.startQuest(
144 character.character_id,
145 quest.questId,
146 harness.simulation.quests.playerViewFor(character.character_id),
147 []
148 );
149
150 const before = harness.simulation.quests.questSnapshot(character.character_id);
151 assert.equal(before.quests[0].step.objectives[0].done, false);
152 assert.equal(before.quests[0].step.objectives[1].done, false);
153
154 const inventory = emptySlots(INVENTORY_SLOTS);
155 inventory[0] = { itemId: 4242, quantity: 1 };
156 harness.simulation.applyBag(playerId, {
157 inventory,
158 equipment: emptyEquipment(),
159 vault: [],
160 vaultReserved: []
161 });
162
163 harness.simulation.enqueue(1, {
164 type: "request-quests",
165 characterId: character.character_id
166 });
167 const result = harness.simulation.step();
168 const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
169 assert.ok(snap);
170 const questSnap = snap.quests.find((q) => q.questId === "fetch-rock");
171 assert.ok(questSnap);
172 assert.equal(questSnap.state, 1);
173 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-rock")?.done, true);
174 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-gem")?.done, false);
175 }
176 finally
177 { harness.close(); }
178});