Allow dialogue steps to grant quests like item rewards.
Normalize questRewards on steps, start them with item gifts after lines, and show a journal notice in the same reward UI.
@@ -1,4 +1,4 @@
1
import { normalizeDialogueTree } from "/shared/quests/normalize.js";
1
import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js";
2
2
import { emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";
3
3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
4
4
@@ -13,6 +13,8 @@ export function initDialoguesModal ({ socket, MessageType })
13
13
let open = false;
14
14
/** @type {any[]} */
15
15
let dialogues = [];
16
/** @type {{ questId: string, title: string }[]} */
17
let questCatalog = [];
16
18
/** @type {any|null} */
17
19
let draft = null;
18
20
let selectedId = null;
@@ -135,6 +137,7 @@ export function initDialoguesModal ({ socket, MessageType })
135
137
stepId: `step-${draft.steps.length + 1}`,
136
138
lines: [{ speaker: "npc", text: "…", effects: [] }],
137
139
itemRewards: [],
140
questRewards: [],
138
141
exit: { kind: "end" }
139
142
});
140
143
renderEditor();
@@ -197,6 +200,7 @@ export function initDialoguesModal ({ socket, MessageType })
197
200
stepId: "greeting",
198
201
lines: [{ speaker: "npc", text: "…", effects: [] }],
199
202
itemRewards: [],
203
questRewards: [],
200
204
exit: { kind: "end" }
201
205
});
202
206
}
@@ -300,6 +304,69 @@ export function initDialoguesModal ({ socket, MessageType })
300
304
});
301
305
box.append(searchWrap);
302
306
307
if (!Array.isArray(step.questRewards))
308
{ step.questRewards = []; }
309
const questRewardsHead = sectionHeader("Quest rewards", () =>
310
{
311
const available = questCatalog.find((q) => !step.questRewards.includes(q.questId));
312
if (!available)
313
{ return; }
314
step.questRewards.push(available.questId);
315
renderEditor();
316
});
317
box.append(questRewardsHead);
318
const questHint = document.createElement("p");
319
questHint.className = "content-editor-hint";
320
questHint.textContent = "Started after all lines (with item rewards), before triggers/branches.";
321
box.append(questHint);
322
if (!step.questRewards.length)
323
{
324
const empty = document.createElement("p");
325
empty.className = "content-editor-hint";
326
empty.textContent = "No quest rewards.";
327
box.append(empty);
328
}
329
step.questRewards.forEach((questId, rewardIndex) =>
330
{
331
const row = document.createElement("div");
332
row.className = "spawner-drops-row";
333
const select = document.createElement("select");
334
const taken = new Set(step.questRewards);
335
const catalog = questCatalog.length
336
? questCatalog
337
: [{ questId, title: questId }];
338
for (const quest of catalog)
339
{
340
if (taken.has(quest.questId) && quest.questId !== questId)
341
{ continue; }
342
const opt = document.createElement("option");
343
opt.value = quest.questId;
344
opt.textContent = quest.title
345
? `${quest.title} (${quest.questId})`
346
: quest.questId;
347
select.append(opt);
348
}
349
if (![...select.options].some((o) => o.value === questId))
350
{
351
const missing = document.createElement("option");
352
missing.value = questId;
353
missing.textContent = `${questId} (missing)`;
354
select.append(missing);
355
}
356
select.value = questId;
357
select.addEventListener("change", () =>
358
{
359
step.questRewards[rewardIndex] = select.value;
360
renderEditor();
361
});
362
row.append(select, removeButton(() =>
363
{
364
step.questRewards.splice(rewardIndex, 1);
365
renderEditor();
366
}));
367
box.append(row);
368
});
369
303
370
const exitKind = document.createElement("select");
304
371
for (const k of ["end", "triggers", "branches"])
305
372
{
@@ -433,6 +500,7 @@ export function initDialoguesModal ({ socket, MessageType })
433
500
document.getElementById("open-dialogues").addEventListener("click", () =>
434
501
{
435
502
socket.send({ type: MessageType.editorLoadDialogues });
503
socket.send({ type: MessageType.editorLoadQuests });
436
504
show(true);
437
505
render();
438
506
});
@@ -469,6 +537,17 @@ export function initDialoguesModal ({ socket, MessageType })
469
537
getDialogues: () => dialogues,
470
538
handleMessage (message)
471
539
{
540
if (message.type === MessageType.editorQuests)
541
{
542
questCatalog = (message.quests ?? []).map((q) =>
543
{
544
const def = normalizeQuestDef(q);
545
return { questId: def.questId, title: def.title };
546
});
547
if (open)
548
{ render(); }
549
return;
550
}
472
551
if (message.type !== MessageType.editorDialogues)
473
552
{ return; }
474
553
dialogues = (message.dialogues ?? []).map((d) =>
@@ -1,4 +1,4 @@1
import { normalizeDialogueTree } from "/shared/quests/normalize.js";1
import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js";2
import { emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";2
import { emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";3
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";4
4
@@ -13,6 +13,8 @@ export function initDialoguesModal ({ socket, MessageType })13
let open = false;13
let open = false;14
/** @type {any[]} */14
/** @type {any[]} */15
let dialogues = [];15
let dialogues = [];16
/** @type {{ questId: string, title: string }[]} */17
let questCatalog = [];16
/** @type {any|null} */18
/** @type {any|null} */17
let draft = null;19
let draft = null;18
let selectedId = null;20
let selectedId = null;@@ -135,6 +137,7 @@ export function initDialoguesModal ({ socket, MessageType })135
stepId: `step-${draft.steps.length + 1}`,137
stepId: `step-${draft.steps.length + 1}`,136
lines: [{ speaker: "npc", text: "…", effects: [] }],138
lines: [{ speaker: "npc", text: "…", effects: [] }],137
itemRewards: [],139
itemRewards: [],140
questRewards: [],138
exit: { kind: "end" }141
exit: { kind: "end" }139
});142
});140
renderEditor();143
renderEditor();@@ -197,6 +200,7 @@ export function initDialoguesModal ({ socket, MessageType })197
stepId: "greeting",200
stepId: "greeting",198
lines: [{ speaker: "npc", text: "…", effects: [] }],201
lines: [{ speaker: "npc", text: "…", effects: [] }],199
itemRewards: [],202
itemRewards: [],203
questRewards: [],200
exit: { kind: "end" }204
exit: { kind: "end" }201
});205
});202
}206
}@@ -300,6 +304,69 @@ export function initDialoguesModal ({ socket, MessageType })300
});304
});301
box.append(searchWrap);305
box.append(searchWrap);302
306
307
if (!Array.isArray(step.questRewards))308
{ step.questRewards = []; }309
const questRewardsHead = sectionHeader("Quest rewards", () =>310
{311
const available = questCatalog.find((q) => !step.questRewards.includes(q.questId));312
if (!available)313
{ return; }314
step.questRewards.push(available.questId);315
renderEditor();316
});317
box.append(questRewardsHead);318
const questHint = document.createElement("p");319
questHint.className = "content-editor-hint";320
questHint.textContent = "Started after all lines (with item rewards), before triggers/branches.";321
box.append(questHint);322
if (!step.questRewards.length)323
{324
const empty = document.createElement("p");325
empty.className = "content-editor-hint";326
empty.textContent = "No quest rewards.";327
box.append(empty);328
}329
step.questRewards.forEach((questId, rewardIndex) =>330
{331
const row = document.createElement("div");332
row.className = "spawner-drops-row";333
const select = document.createElement("select");334
const taken = new Set(step.questRewards);335
const catalog = questCatalog.length336
? questCatalog337
: [{ questId, title: questId }];338
for (const quest of catalog)339
{340
if (taken.has(quest.questId) && quest.questId !== questId)341
{ continue; }342
const opt = document.createElement("option");343
opt.value = quest.questId;344
opt.textContent = quest.title345
? `${quest.title} (${quest.questId})`346
: quest.questId;347
select.append(opt);348
}349
if (![...select.options].some((o) => o.value === questId))350
{351
const missing = document.createElement("option");352
missing.value = questId;353
missing.textContent = `${questId} (missing)`;354
select.append(missing);355
}356
select.value = questId;357
select.addEventListener("change", () =>358
{359
step.questRewards[rewardIndex] = select.value;360
renderEditor();361
});362
row.append(select, removeButton(() =>363
{364
step.questRewards.splice(rewardIndex, 1);365
renderEditor();366
}));367
box.append(row);368
});369
303
const exitKind = document.createElement("select");370
const exitKind = document.createElement("select");304
for (const k of ["end", "triggers", "branches"])371
for (const k of ["end", "triggers", "branches"])305
{372
{@@ -433,6 +500,7 @@ export function initDialoguesModal ({ socket, MessageType })433
document.getElementById("open-dialogues").addEventListener("click", () =>500
document.getElementById("open-dialogues").addEventListener("click", () =>434
{501
{435
socket.send({ type: MessageType.editorLoadDialogues });502
socket.send({ type: MessageType.editorLoadDialogues });503
socket.send({ type: MessageType.editorLoadQuests });436
show(true);504
show(true);437
render();505
render();438
});506
});@@ -469,6 +537,17 @@ export function initDialoguesModal ({ socket, MessageType })469
getDialogues: () => dialogues,537
getDialogues: () => dialogues,470
handleMessage (message)538
handleMessage (message)471
{539
{540
if (message.type === MessageType.editorQuests)541
{542
questCatalog = (message.quests ?? []).map((q) =>543
{544
const def = normalizeQuestDef(q);545
return { questId: def.questId, title: def.title };546
});547
if (open)548
{ render(); }549
return;550
}472
if (message.type !== MessageType.editorDialogues)551
if (message.type !== MessageType.editorDialogues)473
{ return; }552
{ return; }474
dialogues = (message.dialogues ?? []).map((d) =>553
dialogues = (message.dialogues ?? []).map((d) =>@@ -1375,6 +1375,7 @@ async function main ()
1375
1375
{
1376
1376
questsModal?.handleMessage(message);
1377
1377
settingsModal?.handleMessage(message);
1378
dialoguesModal?.handleMessage(message);
1378
1379
}
1379
1380
else if (message.type === MessageType.editorDialogues)
1380
1381
{
@@ -1375,6 +1375,7 @@ async function main ()1375
{1375
{1376
questsModal?.handleMessage(message);1376
questsModal?.handleMessage(message);1377
settingsModal?.handleMessage(message);1377
settingsModal?.handleMessage(message);1378
dialoguesModal?.handleMessage(message);1378
}1379
}1379
else if (message.type === MessageType.editorDialogues)1380
else if (message.type === MessageType.editorDialogues)1380
{1381
{@@ -5,7 +5,7 @@ import { attachOrigins, godsOrigin } from "../../../shared/items/origin.js";
5
5
import { itemDefinition } from "../../../shared/definitions/items.js";
6
6
import { applySkillXp } from "../../../shared/skills/state.js";
7
7
import { skillLevel } from "../../../shared/definitions/skills.js";
8
import { formatDialogueRewardMessage } from "../../../shared/quests/dialogueRewards.js";
8
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../../shared/quests/dialogueRewards.js";
9
9
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
10
10
import { floorDiv } from "../world/coordinates.js";
11
11
import { WorkerMessage } from "../simulation/protocol.js";
@@ -307,11 +307,10 @@ export class QuestRuntime
307
307
if (session.phase === "lines")
308
308
{
309
309
const step = tree.steps.find((s) => s.stepId === session.stepId);
310
const rewardMessages = this.#grantStepItemRewards(
311
characterId,
312
step,
313
events
314
);
310
const rewardMessages = [
311
...this.#grantStepItemRewards(characterId, step, events),
312
...this.#grantStepQuestRewards(characterId, step, playerView, events)
313
];
315
314
this.#reevaluateQuests(characterId, playerView, events);
316
315
if (rewardMessages.length > 0)
317
316
{
@@ -448,6 +447,28 @@ export class QuestRuntime
448
447
return messages;
449
448
}
450
449
450
/** Start step.questRewards; returns player-facing notices for newly started quests. */
451
#grantStepQuestRewards (characterId, step, playerView, events)
452
{
453
const rewards = step?.questRewards ?? [];
454
if (!rewards.length)
455
{ return []; }
456
const titles = [];
457
for (const questId of rewards)
458
{
459
const current = this.stateDb.getQuestState(characterId, questId);
460
if (current.state !== "not-started")
461
{ continue; }
462
const def = this.content.quest(questId);
463
if (!def)
464
{ continue; }
465
this.startQuest(characterId, questId, playerView, events);
466
titles.push(def.title || def.questId);
467
}
468
const message = formatDialogueQuestRewardMessage(titles);
469
return message ? [message] : [];
470
}
471
451
472
#loadBag (characterId)
452
473
{
453
474
const onlineId = this.simulation.characterEntities.get(characterId | 0);
@@ -5,7 +5,7 @@ import { attachOrigins, godsOrigin } from "../../../shared/items/origin.js";5
import { itemDefinition } from "../../../shared/definitions/items.js";
5
import { itemDefinition } from "../../../shared/definitions/items.js";
6
import { applySkillXp } from "../../../shared/skills/state.js";
6
import { applySkillXp } from "../../../shared/skills/state.js";
7
import { skillLevel } from "../../../shared/definitions/skills.js";
7
import { skillLevel } from "../../../shared/definitions/skills.js";
8
import { formatDialogueRewardMessage } from "../../../shared/quests/dialogueRewards.js";
8
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../../shared/quests/dialogueRewards.js";
9
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
9
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
10
import { floorDiv } from "../world/coordinates.js";
10
import { floorDiv } from "../world/coordinates.js";
11
import { WorkerMessage } from "../simulation/protocol.js";
11
import { WorkerMessage } from "../simulation/protocol.js";
@@ -307,11 +307,10 @@ export class QuestRuntime307
if (session.phase === "lines")
307
if (session.phase === "lines")
308
{
308
{
309
const step = tree.steps.find((s) => s.stepId === session.stepId);
309
const step = tree.steps.find((s) => s.stepId === session.stepId);
310
const rewardMessages = this.#grantStepItemRewards(
310
const rewardMessages = [
311
characterId,
311
...this.#grantStepItemRewards(characterId, step, events),
312
step,
312
...this.#grantStepQuestRewards(characterId, step, playerView, events)
313
events
313
];
314
);
315
this.#reevaluateQuests(characterId, playerView, events);
314
this.#reevaluateQuests(characterId, playerView, events);
316
if (rewardMessages.length > 0)
315
if (rewardMessages.length > 0)
317
{
316
{
@@ -448,6 +447,28 @@ export class QuestRuntime448
return messages;
447
return messages;
449
}
448
}
450
449
450
/** Start step.questRewards; returns player-facing notices for newly started quests. */
451
#grantStepQuestRewards (characterId, step, playerView, events)
452
{
453
const rewards = step?.questRewards ?? [];
454
if (!rewards.length)
455
{ return []; }
456
const titles = [];
457
for (const questId of rewards)
458
{
459
const current = this.stateDb.getQuestState(characterId, questId);
460
if (current.state !== "not-started")
461
{ continue; }
462
const def = this.content.quest(questId);
463
if (!def)
464
{ continue; }
465
this.startQuest(characterId, questId, playerView, events);
466
titles.push(def.title || def.questId);
467
}
468
const message = formatDialogueQuestRewardMessage(titles);
469
return message ? [message] : [];
470
}
471
451
#loadBag (characterId)
472
#loadBag (characterId)
452
{
473
{
453
const onlineId = this.simulation.characterEntities.get(characterId | 0);
474
const onlineId = this.simulation.characterEntities.get(characterId | 0);
@@ -6,7 +6,7 @@ import {
6
6
ConditionKind
7
7
} from "../../shared/quests/conditions.js";
8
8
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
9
import { formatDialogueRewardMessage } from "../../shared/quests/dialogueRewards.js";
9
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";
10
10
11
11
test("ConditionSet AND + NOT", () =>
12
12
{
@@ -56,6 +56,7 @@ test("normalize quest and dialogue", () =>
56
56
assert.equal(tree.unique, true);
57
57
assert.equal(tree.steps[0].exit.kind, "end");
58
58
assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);
59
assert.deepEqual(tree.steps[0].questRewards, []);
59
60
});
60
61
61
62
test("dialogue reward messages bag vs bank and lists", () =>
@@ -83,4 +84,12 @@ test("dialogue reward messages bag vs bank and lists", () =>
83
84
], "bag"),
84
85
"A, B and C have been added to your bag."
85
86
);
87
assert.equal(
88
formatDialogueQuestRewardMessage(["Tin Delivery"]),
89
"The Tin Delivery quest has been added to your journal."
90
);
91
assert.equal(
92
formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
93
"The Alpha and Beta quests have been added to your journal."
94
);
86
95
});
@@ -6,7 +6,7 @@ import {6
ConditionKind6
ConditionKind7
} from "../../shared/quests/conditions.js";7
} from "../../shared/quests/conditions.js";8
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";8
import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";9
import { formatDialogueRewardMessage } from "../../shared/quests/dialogueRewards.js";9
import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";10
10
11
test("ConditionSet AND + NOT", () =>11
test("ConditionSet AND + NOT", () =>12
{12
{@@ -56,6 +56,7 @@ test("normalize quest and dialogue", () =>56
assert.equal(tree.unique, true);56
assert.equal(tree.unique, true);57
assert.equal(tree.steps[0].exit.kind, "end");57
assert.equal(tree.steps[0].exit.kind, "end");58
assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);58
assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);59
assert.deepEqual(tree.steps[0].questRewards, []);59
});60
});60
61
61
test("dialogue reward messages bag vs bank and lists", () =>62
test("dialogue reward messages bag vs bank and lists", () =>@@ -83,4 +84,12 @@ test("dialogue reward messages bag vs bank and lists", () =>83
], "bag"),84
], "bag"),84
"A, B and C have been added to your bag."85
"A, B and C have been added to your bag."85
);86
);87
assert.equal(88
formatDialogueQuestRewardMessage(["Tin Delivery"]),89
"The Tin Delivery quest has been added to your journal."90
);91
assert.equal(92
formatDialogueQuestRewardMessage(["Alpha", "Beta"]),93
"The Alpha and Beta quests have been added to your journal."94
);86
});95
});