Grant initial quests when defs go live and when opening the quest log.
Existing characters missed initial quests if settings were saved before Apply; reload and request-quests now catch up.
@@ -156,7 +156,28 @@ export class Simulation
156
156
this.settings = { ...this.settings, ...settings };
157
157
this.#configureWorkers();
158
158
if (grantInitialQuests)
159
{ this.quests?.grantInitialQuestsToAllCharacters?.(); }
159
{ this.#grantInitialQuestsAndNotify(); }
160
}
161
162
/**
163
* Start any configured initial quests that characters have not begun yet.
164
* Online characters get quest snapshots (and reward side-effects) immediately.
165
*/
166
#grantInitialQuestsAndNotify ()
167
{
168
if (!this.quests)
169
{ return; }
170
for (const characterId of this.stateDb.listAllCharacterIds())
171
{
172
const events = [];
173
const view = this.isCharacterOnline(characterId)
174
? this.quests.playerViewFor(characterId)
175
: null;
176
this.quests.ensureInitialQuests(characterId, view, events);
177
if (!events.length || !this.isCharacterOnline(characterId))
178
{ continue; }
179
this.pendingPrivate.push(...this.#mapQuestPrivateEvents(events, null, characterId));
180
}
160
181
}
161
182
162
183
/** Sparse last-sale map for editor base-price readouts. */
@@ -204,6 +225,8 @@ export class Simulation
204
225
this.questContent.reload();
205
226
this.quests?.closeAllSessions();
206
227
this.#refreshAltarIndex();
228
// Quest defs become live on Apply — grant any newly available initial quests.
229
this.#grantInitialQuestsAndNotify();
207
230
}
208
231
209
232
enqueue (sessionId, command)
@@ -544,8 +567,18 @@ export class Simulation
544
567
this.#recalculateActivation(this.now);
545
568
this.#syncGhosts();
546
569
this.courier?.onCharacterEnteredWorld(character.character_id, tileX, tileY);
547
this.quests?.ensureInitialQuests?.(character.character_id ?? row.character_id);
548
this.syncCharacterConditionAccess(character.character_id ?? row.character_id);
570
const characterId = character.character_id ?? row.character_id;
571
this.quests?.ensureInitialQuests?.(characterId);
572
this.syncCharacterConditionAccess(characterId);
573
const snap = this.quests?.questSnapshot?.(characterId);
574
if (snap)
575
{
576
this.pendingPrivate.push({
577
type: "quests-snapshot",
578
characterId,
579
...snap
580
});
581
}
549
582
return entityId;
550
583
}
551
584
@@ -1870,8 +1903,19 @@ export class Simulation
1870
1903
{ return []; }
1871
1904
if (item.command.type === "request-quests")
1872
1905
{
1873
const snap = this.quests.questSnapshot(characterId);
1874
return [{ type: "quests-snapshot", sessionId, characterId, ...snap }];
1906
const events = [];
1907
this.quests.ensureInitialQuests(
1908
characterId,
1909
this.quests.playerViewFor(characterId),
1910
events
1911
);
1912
const out = this.#mapQuestPrivateEvents(events, sessionId, characterId);
1913
if (!out.some((event) => event.type === "quests-snapshot"))
1914
{
1915
const snap = this.quests.questSnapshot(characterId);
1916
out.push({ type: "quests-snapshot", sessionId, characterId, ...snap });
1917
}
1918
return out;
1875
1919
}
1876
1920
if (item.command.type === "close-dialogue")
1877
1921
{
@@ -156,7 +156,28 @@ export class Simulation156
this.settings = { ...this.settings, ...settings };156
this.settings = { ...this.settings, ...settings };157
this.#configureWorkers();157
this.#configureWorkers();158
if (grantInitialQuests)158
if (grantInitialQuests)159
{ this.quests?.grantInitialQuestsToAllCharacters?.(); }159
{ this.#grantInitialQuestsAndNotify(); }160
}161
162
/**163
* Start any configured initial quests that characters have not begun yet.164
* Online characters get quest snapshots (and reward side-effects) immediately.165
*/166
#grantInitialQuestsAndNotify ()167
{168
if (!this.quests)169
{ return; }170
for (const characterId of this.stateDb.listAllCharacterIds())171
{172
const events = [];173
const view = this.isCharacterOnline(characterId)174
? this.quests.playerViewFor(characterId)175
: null;176
this.quests.ensureInitialQuests(characterId, view, events);177
if (!events.length || !this.isCharacterOnline(characterId))178
{ continue; }179
this.pendingPrivate.push(...this.#mapQuestPrivateEvents(events, null, characterId));180
}160
}181
}161
182
162
/** Sparse last-sale map for editor base-price readouts. */183
/** Sparse last-sale map for editor base-price readouts. */@@ -204,6 +225,8 @@ export class Simulation204
this.questContent.reload();225
this.questContent.reload();205
this.quests?.closeAllSessions();226
this.quests?.closeAllSessions();206
this.#refreshAltarIndex();227
this.#refreshAltarIndex();228
// Quest defs become live on Apply — grant any newly available initial quests.229
this.#grantInitialQuestsAndNotify();207
}230
}208
231
209
enqueue (sessionId, command)232
enqueue (sessionId, command)@@ -544,8 +567,18 @@ export class Simulation544
this.#recalculateActivation(this.now);567
this.#recalculateActivation(this.now);545
this.#syncGhosts();568
this.#syncGhosts();546
this.courier?.onCharacterEnteredWorld(character.character_id, tileX, tileY);569
this.courier?.onCharacterEnteredWorld(character.character_id, tileX, tileY);547
this.quests?.ensureInitialQuests?.(character.character_id ?? row.character_id);570
const characterId = character.character_id ?? row.character_id;548
this.syncCharacterConditionAccess(character.character_id ?? row.character_id);571
this.quests?.ensureInitialQuests?.(characterId);572
this.syncCharacterConditionAccess(characterId);573
const snap = this.quests?.questSnapshot?.(characterId);574
if (snap)575
{576
this.pendingPrivate.push({577
type: "quests-snapshot",578
characterId,579
...snap580
});581
}549
return entityId;582
return entityId;550
}583
}551
584
@@ -1870,8 +1903,19 @@ export class Simulation1870
{ return []; }1903
{ return []; }1871
if (item.command.type === "request-quests")1904
if (item.command.type === "request-quests")1872
{1905
{1873
const snap = this.quests.questSnapshot(characterId);1906
const events = [];1874
return [{ type: "quests-snapshot", sessionId, characterId, ...snap }];1907
this.quests.ensureInitialQuests(1908
characterId,1909
this.quests.playerViewFor(characterId),1910
events1911
);1912
const out = this.#mapQuestPrivateEvents(events, sessionId, characterId);1913
if (!out.some((event) => event.type === "quests-snapshot"))1914
{1915
const snap = this.quests.questSnapshot(characterId);1916
out.push({ type: "quests-snapshot", sessionId, characterId, ...snap });1917
}1918
return out;1875
}1919
}1876
if (item.command.type === "close-dialogue")1920
if (item.command.type === "close-dialogue")1877
{1921
{@@ -0,0 +1,110 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { normalizeQuestDef } from "../../shared/quests/normalize.js";
4
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5
6
function saveLiveQuest (harness, raw)
7
{
8
const revisionId = harness.worldDb.getActiveRevisionId();
9
const quest = normalizeQuestDef(raw);
10
harness.worldDb.saveQuestDef(revisionId, quest);
11
harness.simulation.questContent.reload();
12
return quest;
13
}
14
15
test("initial quests grant on apply after quest def becomes live", () =>
16
{
17
const harness = makeSimulation({ workerCount: 1 });
18
try
19
{
20
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
21
harness.simulation.spawnCharacter(1, character);
22
harness.simulation.step();
23
24
const quest = normalizeQuestDef({
25
questId: "starter-hello",
26
title: "Hello Adventurer",
27
steps: [{
28
stepId: "step-1",
29
title: "Begin",
30
objectives: [{
31
objectiveId: "obj-1",
32
text: "Talk to someone",
33
when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
34
}],
35
completionJournalText: "Done."
36
}]
37
});
38
39
// Persist settings like the editor (common order: initial quest before Apply).
40
const live = {
41
...harness.worldDb.getLiveSettings(),
42
initialQuests: ["starter-hello"]
43
};
44
harness.worldDb.saveLiveSettings(live, 1);
45
harness.simulation.applySettings(live, { grantInitialQuests: true });
46
assert.equal(
47
harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
48
"not-started"
49
);
50
51
const revisionId = harness.worldDb.ensurePendingRevision(1);
52
harness.worldDb.saveQuestDef(revisionId, quest);
53
harness.worldDb.promotePendingRevision();
54
harness.simulation.reloadWorld();
55
56
assert.equal(
57
harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
58
1
59
);
60
const snap = harness.simulation.quests.questSnapshot(character.character_id);
61
assert.equal(snap.quests.length, 1);
62
assert.equal(snap.quests[0].questId, "starter-hello");
63
assert.equal(snap.quests[0].title, "Hello Adventurer");
64
}
65
finally
66
{ harness.close(); }
67
});
68
69
test("request-quests ensures missing initial quests before snapshot", () =>
70
{
71
const harness = makeSimulation({ workerCount: 1 });
72
try
73
{
74
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
75
harness.simulation.spawnCharacter(1, character);
76
harness.simulation.step();
77
78
saveLiveQuest(harness, {
79
questId: "late-grant",
80
title: "Late Grant",
81
steps: [{
82
objectives: [{
83
text: "Wait",
84
when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
85
}]
86
}]
87
});
88
harness.simulation.applySettings({ initialQuests: ["late-grant"] }, { grantInitialQuests: false });
89
assert.equal(
90
harness.stateDb.getQuestState(character.character_id, "late-grant").state,
91
"not-started"
92
);
93
94
harness.simulation.enqueue(1, {
95
type: "request-quests",
96
characterId: character.character_id
97
});
98
const result = harness.simulation.step();
99
const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
100
assert.ok(snap);
101
assert.equal(snap.quests.length, 1);
102
assert.equal(snap.quests[0].questId, "late-grant");
103
assert.equal(
104
harness.stateDb.getQuestState(character.character_id, "late-grant").state,
105
1
106
);
107
}
108
finally
109
{ harness.close(); }
110
});
@@ -0,0 +1,110 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { normalizeQuestDef } from "../../shared/quests/normalize.js";4
import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";5
6
function saveLiveQuest (harness, raw)7
{8
const revisionId = harness.worldDb.getActiveRevisionId();9
const quest = normalizeQuestDef(raw);10
harness.worldDb.saveQuestDef(revisionId, quest);11
harness.simulation.questContent.reload();12
return quest;13
}14
15
test("initial quests grant on apply after quest def becomes live", () =>16
{17
const harness = makeSimulation({ workerCount: 1 });18
try19
{20
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });21
harness.simulation.spawnCharacter(1, character);22
harness.simulation.step();23
24
const quest = normalizeQuestDef({25
questId: "starter-hello",26
title: "Hello Adventurer",27
steps: [{28
stepId: "step-1",29
title: "Begin",30
objectives: [{31
objectiveId: "obj-1",32
text: "Talk to someone",33
when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }34
}],35
completionJournalText: "Done."36
}]37
});38
39
// Persist settings like the editor (common order: initial quest before Apply).40
const live = {41
...harness.worldDb.getLiveSettings(),42
initialQuests: ["starter-hello"]43
};44
harness.worldDb.saveLiveSettings(live, 1);45
harness.simulation.applySettings(live, { grantInitialQuests: true });46
assert.equal(47
harness.stateDb.getQuestState(character.character_id, "starter-hello").state,48
"not-started"49
);50
51
const revisionId = harness.worldDb.ensurePendingRevision(1);52
harness.worldDb.saveQuestDef(revisionId, quest);53
harness.worldDb.promotePendingRevision();54
harness.simulation.reloadWorld();55
56
assert.equal(57
harness.stateDb.getQuestState(character.character_id, "starter-hello").state,58
159
);60
const snap = harness.simulation.quests.questSnapshot(character.character_id);61
assert.equal(snap.quests.length, 1);62
assert.equal(snap.quests[0].questId, "starter-hello");63
assert.equal(snap.quests[0].title, "Hello Adventurer");64
}65
finally66
{ harness.close(); }67
});68
69
test("request-quests ensures missing initial quests before snapshot", () =>70
{71
const harness = makeSimulation({ workerCount: 1 });72
try73
{74
const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });75
harness.simulation.spawnCharacter(1, character);76
harness.simulation.step();77
78
saveLiveQuest(harness, {79
questId: "late-grant",80
title: "Late Grant",81
steps: [{82
objectives: [{83
text: "Wait",84
when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }85
}]86
}]87
});88
harness.simulation.applySettings({ initialQuests: ["late-grant"] }, { grantInitialQuests: false });89
assert.equal(90
harness.stateDb.getQuestState(character.character_id, "late-grant").state,91
"not-started"92
);93
94
harness.simulation.enqueue(1, {95
type: "request-quests",96
characterId: character.character_id97
});98
const result = harness.simulation.step();99
const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");100
assert.ok(snap);101
assert.equal(snap.quests.length, 1);102
assert.equal(snap.quests[0].questId, "late-grant");103
assert.equal(104
harness.stateDb.getQuestState(character.character_id, "late-grant").state,105
1106
);107
}108
finally109
{ harness.close(); }110
});