BlueGit

timfa questward

Merge branch 'feature/condition-set-mode'

timfa Tim Falken · pushed by timfa

Authored 2026-09-08 · 2 months ago

Hash
ac2f632aa2b565189d2b1605487b81d3ff1f4aee
4 files changed +132 −10
client/src/editor/conditionSetEditor.js +36 −4
@@ -1,4 +1,10 @@
1 import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";
1 import {
2 ConditionKind,
3 ConditionSetMode,
4 CONDITION_SET_MODE_LABELS,
5 emptyConditionSet,
6 normalizeConditionSet
7 } from "/shared/quests/conditions.js";
2 8 import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
3 9 import { listSpawnableMobTypes } from "/shared/definitions/mobs.js";
4 10 import {
@@ -12,7 +18,13 @@ import { OVERWORLD_ID } from "/shared/world/dimensions.js";
12 18 /** All condition kinds available in the shared editor UI. */
13 19 export const CONDITION_EDITOR_KINDS = Object.freeze(Object.values(ConditionKind));
14 20
15 export { emptyConditionSet, normalizeConditionSet, ConditionKind };
21 export {
22 emptyConditionSet,
23 normalizeConditionSet,
24 ConditionKind,
25 ConditionSetMode,
26 CONDITION_SET_MODE_LABELS
27 };
16 28
17 29 export function renderConditionSet (when, onChange, ctx = {})
18 30 {
@@ -21,9 +33,29 @@ export function renderConditionSet (when, onChange, ctx = {})
21 33 const bump = (remount = true) => onChange(set, { remount });
22 34 const questList = Array.isArray(ctx.quests) ? ctx.quests : [];
23 35 const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : [];
36 const header = document.createElement("div");
37 header.className = "content-editor-condition-set-header";
24 38 const title = document.createElement("strong");
25 title.textContent = "When (all of)";
26 wrap.append(title);
39 title.textContent = "When";
40 const mode = document.createElement("select");
41 mode.className = "content-editor-condition-set-mode";
42 mode.title = "How clauses in this set combine";
43 for (const value of Object.values(ConditionSetMode))
44 {
45 const opt = document.createElement("option");
46 opt.value = value;
47 opt.textContent = CONDITION_SET_MODE_LABELS[value] ?? value;
48 if (set.mode === value)
49 { opt.selected = true; }
50 mode.append(opt);
51 }
52 mode.addEventListener("change", () =>
53 {
54 set.mode = mode.value;
55 bump(false);
56 });
57 header.append(title, mode);
58 wrap.append(header);
27 59
28 60 set.all.forEach((clause, index) =>
29 61 {
@@ -1,4 +1,10 @@
1import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";
1import {
2 ConditionKind,
3 ConditionSetMode,
4 CONDITION_SET_MODE_LABELS,
5 emptyConditionSet,
6 normalizeConditionSet
7} from "/shared/quests/conditions.js";
2import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
8import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";
3import { listSpawnableMobTypes } from "/shared/definitions/mobs.js";
9import { listSpawnableMobTypes } from "/shared/definitions/mobs.js";
4import {
10import {
@@ -12,7 +18,13 @@ import { OVERWORLD_ID } from "/shared/world/dimensions.js";
12/** All condition kinds available in the shared editor UI. */
18/** All condition kinds available in the shared editor UI. */
13export const CONDITION_EDITOR_KINDS = Object.freeze(Object.values(ConditionKind));
19export const CONDITION_EDITOR_KINDS = Object.freeze(Object.values(ConditionKind));
14
20
15export { emptyConditionSet, normalizeConditionSet, ConditionKind };
21export {
22 emptyConditionSet,
23 normalizeConditionSet,
24 ConditionKind,
25 ConditionSetMode,
26 CONDITION_SET_MODE_LABELS
27};
16
28
17export function renderConditionSet (when, onChange, ctx = {})
29export function renderConditionSet (when, onChange, ctx = {})
18{
30{
@@ -21,9 +33,29 @@ export function renderConditionSet (when, onChange, ctx = {})
21 const bump = (remount = true) => onChange(set, { remount });
33 const bump = (remount = true) => onChange(set, { remount });
22 const questList = Array.isArray(ctx.quests) ? ctx.quests : [];
34 const questList = Array.isArray(ctx.quests) ? ctx.quests : [];
23 const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : [];
35 const dialogueList = Array.isArray(ctx.dialogues) ? ctx.dialogues : [];
36 const header = document.createElement("div");
37 header.className = "content-editor-condition-set-header";
24 const title = document.createElement("strong");
38 const title = document.createElement("strong");
25 title.textContent = "When (all of)";
39 title.textContent = "When";
26 wrap.append(title);
40 const mode = document.createElement("select");
41 mode.className = "content-editor-condition-set-mode";
42 mode.title = "How clauses in this set combine";
43 for (const value of Object.values(ConditionSetMode))
44 {
45 const opt = document.createElement("option");
46 opt.value = value;
47 opt.textContent = CONDITION_SET_MODE_LABELS[value] ?? value;
48 if (set.mode === value)
49 { opt.selected = true; }
50 mode.append(opt);
51 }
52 mode.addEventListener("change", () =>
53 {
54 set.mode = mode.value;
55 bump(false);
56 });
57 header.append(title, mode);
58 wrap.append(header);
27
59
28 set.all.forEach((clause, index) =>
60 set.all.forEach((clause, index) =>
29 {
61 {
client/ui/editor.css +12 −0
@@ -1835,6 +1835,18 @@ label {
1835 1835 margin: 0.35rem 0;
1836 1836 }
1837 1837
1838 .content-editor-condition-set-header {
1839 display: flex;
1840 align-items: center;
1841 gap: 0.5rem;
1842 flex-wrap: wrap;
1843 margin-bottom: 0.25rem;
1844 }
1845
1846 .content-editor-condition-set-mode {
1847 min-width: 10rem;
1848 }
1849
1838 1850 .content-editor-hint {
1839 1851 color: #8a909c;
1840 1852 font-size: 0.85rem;
@@ -1835,6 +1835,18 @@ label {
1835 margin: 0.35rem 0;
1835 margin: 0.35rem 0;
1836}
1836}
1837
1837
1838.content-editor-condition-set-header {
1839 display: flex;
1840 align-items: center;
1841 gap: 0.5rem;
1842 flex-wrap: wrap;
1843 margin-bottom: 0.25rem;
1844}
1845
1846.content-editor-condition-set-mode {
1847 min-width: 10rem;
1848}
1849
1838.content-editor-hint {
1850.content-editor-hint {
1839 color: #8a909c;
1851 color: #8a909c;
1840 font-size: 0.85rem;
1852 font-size: 0.85rem;
server/tests/quests-conditions.test.js +34 −1
@@ -3,7 +3,8 @@ import assert from "node:assert/strict";
3 3 import {
4 4 evaluateConditionSet,
5 5 normalizeConditionSet,
6 ConditionKind
6 ConditionKind,
7 ConditionSetMode
7 8 } from "../../shared/quests/conditions.js";
8 9 import {
9 10 countableObjectiveProgress,
@@ -20,6 +21,7 @@ test("ConditionSet AND + NOT", () =>
20 21 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
21 22 ]
22 23 });
24 assert.equal(set.mode, ConditionSetMode.allOf);
23 25 assert.equal(evaluateConditionSet(set, {
24 26 countItem: (id) => (id === 1 ? 2 : 0),
25 27 questState: () => "not-started"
@@ -30,6 +32,37 @@ test("ConditionSet AND + NOT", () =>
30 32 }), false);
31 33 });
32 34
35 test("ConditionSet modes oneOrMoreOf onlyOneOf and noneOf", () =>
36 {
37 const clauses = [
38 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
39 { condition: { kind: ConditionKind.hasItem, itemId: 2, quantity: 1 } }
40 ];
41 const ctxNone = { countItem: () => 0 };
42 const ctxOne = { countItem: (id) => (id === 1 ? 1 : 0) };
43 const ctxBoth = { countItem: () => 1 };
44
45 const oneOrMore = normalizeConditionSet({ mode: ConditionSetMode.oneOrMoreOf, all: clauses });
46 assert.equal(evaluateConditionSet(oneOrMore, ctxNone), false);
47 assert.equal(evaluateConditionSet(oneOrMore, ctxOne), true);
48 assert.equal(evaluateConditionSet(oneOrMore, ctxBoth), true);
49
50 const onlyOne = normalizeConditionSet({ mode: ConditionSetMode.onlyOneOf, all: clauses });
51 assert.equal(evaluateConditionSet(onlyOne, ctxNone), false);
52 assert.equal(evaluateConditionSet(onlyOne, ctxOne), true);
53 assert.equal(evaluateConditionSet(onlyOne, ctxBoth), false);
54
55 const none = normalizeConditionSet({ mode: ConditionSetMode.noneOf, all: clauses });
56 assert.equal(evaluateConditionSet(none, ctxNone), true);
57 assert.equal(evaluateConditionSet(none, ctxOne), false);
58 assert.equal(evaluateConditionSet(none, ctxBoth), false);
59
60 assert.equal(
61 normalizeConditionSet({ mode: "nope", all: clauses }).mode,
62 ConditionSetMode.allOf
63 );
64 });
65
33 66 test("normalize quest and dialogue", () =>
34 67 {
35 68 const quest = normalizeQuestDef({
@@ -3,7 +3,8 @@ import assert from "node:assert/strict";
3import {
3import {
4 evaluateConditionSet,
4 evaluateConditionSet,
5 normalizeConditionSet,
5 normalizeConditionSet,
6 ConditionKind
6 ConditionKind,
7 ConditionSetMode
7} from "../../shared/quests/conditions.js";
8} from "../../shared/quests/conditions.js";
8import {
9import {
9 countableObjectiveProgress,
10 countableObjectiveProgress,
@@ -20,6 +21,7 @@ test("ConditionSet AND + NOT", () =>
20 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
21 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
21 ]
22 ]
22 });
23 });
24 assert.equal(set.mode, ConditionSetMode.allOf);
23 assert.equal(evaluateConditionSet(set, {
25 assert.equal(evaluateConditionSet(set, {
24 countItem: (id) => (id === 1 ? 2 : 0),
26 countItem: (id) => (id === 1 ? 2 : 0),
25 questState: () => "not-started"
27 questState: () => "not-started"
@@ -30,6 +32,37 @@ test("ConditionSet AND + NOT", () =>
30 }), false);
32 }), false);
31});
33});
32
34
35test("ConditionSet modes oneOrMoreOf onlyOneOf and noneOf", () =>
36{
37 const clauses = [
38 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
39 { condition: { kind: ConditionKind.hasItem, itemId: 2, quantity: 1 } }
40 ];
41 const ctxNone = { countItem: () => 0 };
42 const ctxOne = { countItem: (id) => (id === 1 ? 1 : 0) };
43 const ctxBoth = { countItem: () => 1 };
44
45 const oneOrMore = normalizeConditionSet({ mode: ConditionSetMode.oneOrMoreOf, all: clauses });
46 assert.equal(evaluateConditionSet(oneOrMore, ctxNone), false);
47 assert.equal(evaluateConditionSet(oneOrMore, ctxOne), true);
48 assert.equal(evaluateConditionSet(oneOrMore, ctxBoth), true);
49
50 const onlyOne = normalizeConditionSet({ mode: ConditionSetMode.onlyOneOf, all: clauses });
51 assert.equal(evaluateConditionSet(onlyOne, ctxNone), false);
52 assert.equal(evaluateConditionSet(onlyOne, ctxOne), true);
53 assert.equal(evaluateConditionSet(onlyOne, ctxBoth), false);
54
55 const none = normalizeConditionSet({ mode: ConditionSetMode.noneOf, all: clauses });
56 assert.equal(evaluateConditionSet(none, ctxNone), true);
57 assert.equal(evaluateConditionSet(none, ctxOne), false);
58 assert.equal(evaluateConditionSet(none, ctxBoth), false);
59
60 assert.equal(
61 normalizeConditionSet({ mode: "nope", all: clauses }).mode,
62 ConditionSetMode.allOf
63 );
64});
65
33test("normalize quest and dialogue", () =>
66test("normalize quest and dialogue", () =>
34{
67{
35 const quest = normalizeQuestDef({
68 const quest = normalizeQuestDef({
shared/quests/conditions.js +50 −5
@@ -33,9 +33,38 @@ export function skillRequirementThreshold (condition)
33 33 return Math.min(99, level);
34 34 }
35 35
36 /**
37 * How clauses in a ConditionSet combine.
38 * - allOf: every clause must pass (legacy default)
39 * - oneOrMoreOf: at least one clause must pass
40 * - onlyOneOf: exactly one clause must pass
41 * - noneOf: fail if any clause passes (inverted one-or-more)
42 */
43 export const ConditionSetMode = Object.freeze({
44 allOf: "allOf",
45 oneOrMoreOf: "oneOrMoreOf",
46 onlyOneOf: "onlyOneOf",
47 noneOf: "noneOf"
48 });
49
50 export const CONDITION_SET_MODE_LABELS = Object.freeze({
51 [ConditionSetMode.allOf]: "All of",
52 [ConditionSetMode.oneOrMoreOf]: "One or more of",
53 [ConditionSetMode.onlyOneOf]: "Only one of",
54 [ConditionSetMode.noneOf]: "None of"
55 });
56
57 export function normalizeConditionSetMode (raw)
58 {
59 const mode = String(raw ?? "").trim();
60 if (Object.values(ConditionSetMode).includes(mode))
61 { return mode; }
62 return ConditionSetMode.allOf;
63 }
64
36 65 export function emptyConditionSet ()
37 66 {
38 return { all: [] };
67 return { mode: ConditionSetMode.allOf, all: [] };
39 68 }
40 69
41 70 export function normalizeConditionSet (raw)
@@ -48,7 +77,10 @@ export function normalizeConditionSet (raw)
48 77 if (clause)
49 78 { all.push(clause); }
50 79 }
51 return { all };
80 return {
81 mode: normalizeConditionSetMode(raw?.mode),
82 all
83 };
52 84 }
53 85
54 86 function normalizeConditionClause (entry)
@@ -98,14 +130,27 @@ export function evaluateConditionSet (set, ctx)
98 130 const clauses = set?.all ?? [];
99 131 if (clauses.length === 0)
100 132 { return true; }
133 let passCount = 0;
101 134 for (const clause of clauses)
102 135 {
103 136 const raw = evaluateCondition(clause.condition, ctx);
104 137 const ok = clause.not ? !raw : raw;
105 if (!ok)
106 { return false; }
138 if (ok)
139 { passCount += 1; }
140 }
141 const mode = normalizeConditionSetMode(set?.mode);
142 switch (mode)
143 {
144 case ConditionSetMode.oneOrMoreOf:
145 return passCount >= 1;
146 case ConditionSetMode.onlyOneOf:
147 return passCount === 1;
148 case ConditionSetMode.noneOf:
149 return passCount === 0;
150 case ConditionSetMode.allOf:
151 default:
152 return passCount === clauses.length;
107 153 }
108 return true;
109 154 }
110 155
111 156 function sameDimension (condition, ctx)
@@ -33,9 +33,38 @@ export function skillRequirementThreshold (condition)
33 return Math.min(99, level);
33 return Math.min(99, level);
34}
34}
35
35
36/**
37 * How clauses in a ConditionSet combine.
38 * - allOf: every clause must pass (legacy default)
39 * - oneOrMoreOf: at least one clause must pass
40 * - onlyOneOf: exactly one clause must pass
41 * - noneOf: fail if any clause passes (inverted one-or-more)
42 */
43export const ConditionSetMode = Object.freeze({
44 allOf: "allOf",
45 oneOrMoreOf: "oneOrMoreOf",
46 onlyOneOf: "onlyOneOf",
47 noneOf: "noneOf"
48});
49
50export const CONDITION_SET_MODE_LABELS = Object.freeze({
51 [ConditionSetMode.allOf]: "All of",
52 [ConditionSetMode.oneOrMoreOf]: "One or more of",
53 [ConditionSetMode.onlyOneOf]: "Only one of",
54 [ConditionSetMode.noneOf]: "None of"
55});
56
57export function normalizeConditionSetMode (raw)
58{
59 const mode = String(raw ?? "").trim();
60 if (Object.values(ConditionSetMode).includes(mode))
61 { return mode; }
62 return ConditionSetMode.allOf;
63}
64
36export function emptyConditionSet ()
65export function emptyConditionSet ()
37{
66{
38 return { all: [] };
67 return { mode: ConditionSetMode.allOf, all: [] };
39}
68}
40
69
41export function normalizeConditionSet (raw)
70export function normalizeConditionSet (raw)
@@ -48,7 +77,10 @@ export function normalizeConditionSet (raw)
48 if (clause)
77 if (clause)
49 { all.push(clause); }
78 { all.push(clause); }
50 }
79 }
51 return { all };
80 return {
81 mode: normalizeConditionSetMode(raw?.mode),
82 all
83 };
52}
84}
53
85
54function normalizeConditionClause (entry)
86function normalizeConditionClause (entry)
@@ -98,14 +130,27 @@ export function evaluateConditionSet (set, ctx)
98 const clauses = set?.all ?? [];
130 const clauses = set?.all ?? [];
99 if (clauses.length === 0)
131 if (clauses.length === 0)
100 { return true; }
132 { return true; }
133 let passCount = 0;
101 for (const clause of clauses)
134 for (const clause of clauses)
102 {
135 {
103 const raw = evaluateCondition(clause.condition, ctx);
136 const raw = evaluateCondition(clause.condition, ctx);
104 const ok = clause.not ? !raw : raw;
137 const ok = clause.not ? !raw : raw;
105 if (!ok)
138 if (ok)
106 { return false; }
139 { passCount += 1; }
140 }
141 const mode = normalizeConditionSetMode(set?.mode);
142 switch (mode)
143 {
144 case ConditionSetMode.oneOrMoreOf:
145 return passCount >= 1;
146 case ConditionSetMode.onlyOneOf:
147 return passCount === 1;
148 case ConditionSetMode.noneOf:
149 return passCount === 0;
150 case ConditionSetMode.allOf:
151 default:
152 return passCount === clauses.length;
107 }
153 }
108 return true;
109}
154}
110
155
111function sameDimension (condition, ctx)
156function sameDimension (condition, ctx)