Merge branch 'feature/dialogue-heard-after-step-complete'
@@ -285,7 +285,6 @@ export class QuestRuntime
285
285
priorEvents.push({ type: "dialogueClose", reason: "missing-step" });
286
286
return { ok: false, error: "Broken dialogue.", events: priorEvents };
287
287
}
288
this.stateDb.markDialogueHeard(characterId, tree.dialogueId, step.stepId);
289
288
const events = [...priorEvents];
290
289
const linePayloads = [];
291
290
for (const line of step.lines)
@@ -294,6 +293,7 @@ export class QuestRuntime
294
293
linePayloads.push({ speaker: line.speaker, text: line.text });
295
294
}
296
295
// Stay open while the client pages lines; resolve exit only on dialogueAdvance.
296
// dialogueHeard is marked only when the step fully completes (after rewards).
297
297
this.sessions.set(characterId | 0, {
298
298
entityId: entityId | 0,
299
299
dialogueId: tree.dialogueId,
@@ -356,6 +356,10 @@ export class QuestRuntime
356
356
357
357
#resolveDialogueExit (characterId, session, tree, playerView, events)
358
358
{
359
// Step is fully finished: lines read + takes/rewards/quest notices acknowledged.
360
this.stateDb.markDialogueHeard(characterId, tree.dialogueId, session.stepId);
361
this.#reevaluateQuests(characterId, playerView, events);
362
359
363
const exit = session.pendingExit ?? { kind: "end" };
360
364
361
365
if (exit.kind === "branches")
@@ -285,7 +285,6 @@ export class QuestRuntime285
priorEvents.push({ type: "dialogueClose", reason: "missing-step" });
285
priorEvents.push({ type: "dialogueClose", reason: "missing-step" });
286
return { ok: false, error: "Broken dialogue.", events: priorEvents };
286
return { ok: false, error: "Broken dialogue.", events: priorEvents };
287
}
287
}
288
this.stateDb.markDialogueHeard(characterId, tree.dialogueId, step.stepId);
289
const events = [...priorEvents];
288
const events = [...priorEvents];
290
const linePayloads = [];
289
const linePayloads = [];
291
for (const line of step.lines)
290
for (const line of step.lines)
@@ -294,6 +293,7 @@ export class QuestRuntime294
linePayloads.push({ speaker: line.speaker, text: line.text });
293
linePayloads.push({ speaker: line.speaker, text: line.text });
295
}
294
}
296
// Stay open while the client pages lines; resolve exit only on dialogueAdvance.
295
// Stay open while the client pages lines; resolve exit only on dialogueAdvance.
296
// dialogueHeard is marked only when the step fully completes (after rewards).
297
this.sessions.set(characterId | 0, {
297
this.sessions.set(characterId | 0, {
298
entityId: entityId | 0,
298
entityId: entityId | 0,
299
dialogueId: tree.dialogueId,
299
dialogueId: tree.dialogueId,
@@ -356,6 +356,10 @@ export class QuestRuntime356
356
357
#resolveDialogueExit (characterId, session, tree, playerView, events)
357
#resolveDialogueExit (characterId, session, tree, playerView, events)
358
{
358
{
359
// Step is fully finished: lines read + takes/rewards/quest notices acknowledged.
360
this.stateDb.markDialogueHeard(characterId, tree.dialogueId, session.stepId);
361
this.#reevaluateQuests(characterId, playerView, events);
362
359
const exit = session.pendingExit ?? { kind: "end" };
363
const exit = session.pendingExit ?? { kind: "end" };
360
364
361
if (exit.kind === "branches")
365
if (exit.kind === "branches")
@@ -166,3 +166,58 @@ test("dialogue advance soft-takes itemTakes then reports a notice", () =>
166
166
finally
167
167
{ harness.close(); }
168
168
});
169
170
test("dialogueHeard is set only after the step fully completes", () =>
171
{
172
const harness = makeSimulation({ workerCount: 1 });
173
try
174
{
175
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
176
const playerId = harness.simulation.spawnCharacter(1, character);
177
harness.simulation.step();
178
179
const tree = normalizeDialogueTree({
180
dialogueId: "heard-timing",
181
steps: [{
182
stepId: "greeting",
183
lines: [{ speaker: "npc", text: "Hello." }],
184
itemRewards: [{ itemId: ItemId.bones, quantity: 1 }]
185
}]
186
});
187
const revisionId = harness.worldDb.getActiveRevisionId();
188
harness.worldDb.saveDialogueTree(revisionId, tree);
189
harness.simulation.questContent.reload();
190
191
const opened = harness.simulation.quests.tryOpenDialogue(
192
character.character_id,
193
9002,
194
"heard-timing"
195
);
196
assert.equal(opened.ok, true);
197
assert.equal(
198
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),
199
false,
200
"opening / entering lines must not mark heard"
201
);
202
203
const afterLines = harness.simulation.quests.advanceDialogue(character.character_id);
204
assert.equal(afterLines.ok, true);
205
assert.ok((afterLines.events ?? []).some((e) => e.type === "dialogueItemRewards"));
206
assert.equal(
207
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),
208
false,
209
"reward notices must be acknowledged before heard"
210
);
211
212
const afterRewards = harness.simulation.quests.advanceDialogue(character.character_id);
213
assert.equal(afterRewards.ok, true);
214
assert.equal(
215
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),
216
true,
217
"heard only after lines + rewards are finished"
218
);
219
assert.equal(countItemInSlots(harness.simulation.bagOf(playerId).inventory, ItemId.bones), 1);
220
}
221
finally
222
{ harness.close(); }
223
});
@@ -166,3 +166,58 @@ test("dialogue advance soft-takes itemTakes then reports a notice", () =>166
finally166
finally167
{ harness.close(); }167
{ harness.close(); }168
});168
});169
170
test("dialogueHeard is set only after the step fully completes", () =>171
{172
const harness = makeSimulation({ workerCount: 1 });173
try174
{175
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });176
const playerId = harness.simulation.spawnCharacter(1, character);177
harness.simulation.step();178
179
const tree = normalizeDialogueTree({180
dialogueId: "heard-timing",181
steps: [{182
stepId: "greeting",183
lines: [{ speaker: "npc", text: "Hello." }],184
itemRewards: [{ itemId: ItemId.bones, quantity: 1 }]185
}]186
});187
const revisionId = harness.worldDb.getActiveRevisionId();188
harness.worldDb.saveDialogueTree(revisionId, tree);189
harness.simulation.questContent.reload();190
191
const opened = harness.simulation.quests.tryOpenDialogue(192
character.character_id,193
9002,194
"heard-timing"195
);196
assert.equal(opened.ok, true);197
assert.equal(198
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),199
false,200
"opening / entering lines must not mark heard"201
);202
203
const afterLines = harness.simulation.quests.advanceDialogue(character.character_id);204
assert.equal(afterLines.ok, true);205
assert.ok((afterLines.events ?? []).some((e) => e.type === "dialogueItemRewards"));206
assert.equal(207
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),208
false,209
"reward notices must be acknowledged before heard"210
);211
212
const afterRewards = harness.simulation.quests.advanceDialogue(character.character_id);213
assert.equal(afterRewards.ok, true);214
assert.equal(215
harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"),216
true,217
"heard only after lines + rewards are finished"218
);219
assert.equal(countItemInSlots(harness.simulation.bagOf(playerId).inventory, ItemId.bones), 1);220
}221
finally222
{ harness.close(); }223
});