Add ConditionSet combine modes for requirement sets.
Editor dropdown chooses All of / One or more of / Only one of / None of; legacy sets without mode keep All-of behaviour.
@@ -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 @@1
import { ConditionKind, emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";1
import {2
ConditionKind,3
ConditionSetMode,4
CONDITION_SET_MODE_LABELS,5
emptyConditionSet,6
normalizeConditionSet7
} from "/shared/quests/conditions.js";2
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";8
import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js";3
import { listSpawnableMobTypes } from "/shared/definitions/mobs.js";9
import { listSpawnableMobTypes } from "/shared/definitions/mobs.js";4
import {10
import {@@ -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. */13
export const CONDITION_EDITOR_KINDS = Object.freeze(Object.values(ConditionKind));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_LABELS27
};16
28
17
export function renderConditionSet (when, onChange, ctx = {})29
export 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
{@@ -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;@@ -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";3
import {3
import {4
evaluateConditionSet,4
evaluateConditionSet,5
normalizeConditionSet,5
normalizeConditionSet,6
ConditionKind6
ConditionKind,7
ConditionSetMode7
} from "../../shared/quests/conditions.js";8
} from "../../shared/quests/conditions.js";8
import {9
import {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
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.allOf63
);64
});65
33
test("normalize quest and dialogue", () =>66
test("normalize quest and dialogue", () =>34
{67
{35
const quest = normalizeQuestDef({68
const quest = normalizeQuestDef({