@arcadia-eternity/content-sdk 4.10.0 → 4.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,4 @@
1
- import { readEnvironmentRuleSets, resolveEnhancement, splitRuleDeclaration, RuleCompositionError, } from '@arcadia-eternity/const';
2
- import { isRecord } from './types.js';
3
- function issue(code, detail) {
4
- throw new RuleCompositionError(code, detail);
5
- }
6
- // Arrays that describe eligibility or a selector are sets. Domain records with several
7
- // independently editable entries (matchups, bindings, learnsets) get one address per entry.
8
- function canonical(value) {
9
- if (Array.isArray(value))
10
- return '[' + value.map(canonical).sort().join(',') + ']';
11
- if (!isRecord(value))
12
- return JSON.stringify(value);
13
- return ('{' +
14
- Object.keys(value)
15
- .sort()
16
- .map(key => JSON.stringify(key) + ':' + canonical(value[key]))
17
- .join(',') +
18
- '}');
19
- }
1
+ import { EnvironmentRulesSchema, parseRuleData, resolveEnhancement, RuleCompositionError, } from '@arcadia-eternity/const';
20
2
  function entries(rules) {
21
3
  const out = [];
22
4
  const add = (path, value) => out.push({ path, value });
@@ -43,182 +25,17 @@ function entries(rules) {
43
25
  add(['skillParameters', 'overrides', row.skill, row.useId, row.parameter], row);
44
26
  return out;
45
27
  }
46
- function merge(fields, path, choice, sources, replace = false) {
47
- const key = JSON.stringify(path);
48
- let field = fields.get(key);
49
- if (!field) {
50
- field = { path, choices: new Map(), sources: new Set() };
51
- fields.set(key, field);
52
- }
53
- if (replace)
54
- field.choices.clear();
55
- for (const source of sources)
56
- field.sources.add(source);
57
- const identity = canonical(choice.value);
58
- const previous = field.choices.get(identity);
59
- if (previous) {
60
- for (const source of choice.sources)
61
- previous.sources.add(source);
62
- }
63
- else
64
- field.choices.set(identity, { value: choice.value, sources: new Set(choice.sources) });
65
- }
66
- function effective(field) {
67
- const [section, key] = field.path;
68
- const values = Array.from(field.choices.values(), choice => choice.value);
69
- if (section === 'allow' || section === 'deny') {
70
- const sets = values.map(list => new Set(list));
71
- const result = section === 'allow'
72
- ? [...sets[0]].filter(id => sets.every(set => set.has(id)))
73
- : Array.from(new Set(sets.flatMap(set => [...set])));
74
- return result.sort();
75
- }
76
- if (section === 'team') {
77
- if (key === 'uniqueSpecies')
78
- return values.includes(true);
79
- return key === 'minSize' ? Math.max(...values) : Math.min(...values);
80
- }
81
- if (values.length === 1)
82
- return values[0];
83
- if (section === 'mechanics' &&
84
- key === 'enhancement' &&
85
- values.every(value => isRecord(value) && value.mode === 'suppressed'))
86
- return { mode: 'suppressed' };
87
- const contenders = [...field.choices.values()].map(choice => [...choice.sources].sort().join(', ') + ' = ' + canonical(choice.value));
88
- issue('ENVIRONMENT_RULE_CONFLICT', field.path.join(' / ') + ' (' + contenders.sort().join(' | ') + ')');
89
- }
90
- function writeField(result, path, value) {
91
- const [section, key] = path;
92
- if (path.length === 1) {
93
- result[section] = value;
94
- return;
95
- }
96
- if (section === 'constraints' || section === 'learnsetAdjustments') {
97
- const rows = (result[section] ??= []);
98
- rows.push(value);
99
- return;
100
- }
101
- const group = (result[section] ??= Object.create(null));
102
- if (path.length === 2)
103
- group[key] = value;
104
- else if (key === 'groups') {
105
- const groups = (group.groups ??= Object.create(null));
106
- groups[path[2]] = value;
107
- }
108
- else {
109
- const rows = (group[key] ??= []);
110
- rows.push(value);
111
- }
112
- }
113
- function applyConstraintEdits(fields, source, edits, local = []) {
114
- const put = (id, value) => merge(fields, ['constraints', id], { value, sources: new Set([source]) }, [source], true);
115
- const lookup = (id) => fields.get(JSON.stringify(['constraints', id]));
116
- const requireField = (id) => {
117
- const field = lookup(id);
118
- if (!field || [...field.choices.values()].every(choice => choice.value === null))
119
- issue('ENVIRONMENT_RULE_TARGET_MISSING', source + ' / ' + id);
120
- return field;
121
- };
122
- const clears = new Set(edits.clearExceptions);
123
- const changed = new Set();
124
- for (const { id, value } of [
125
- ...(edits.remove ?? []).map(id => ({ id, value: null })),
126
- ...(edits.replace ?? []).map(value => ({ id: value.id, value })),
127
- ]) {
128
- if (changed.has(id))
129
- issue('ENVIRONMENT_RULE_EDIT_CONFLICT', source + ' / ' + id);
130
- changed.add(id);
131
- const field = requireField(id);
132
- const hasExceptions = [...field.choices.values()].some(choice => {
133
- const rule = choice.value;
134
- return rule?.kind === 'ban' && (rule.exceptWhen?.length || rule.exceptAny?.length);
135
- });
136
- if (hasExceptions && !clears.has(id))
137
- issue('ENVIRONMENT_RULE_EXCEPTION_DANGLING', source + ' / ' + id);
138
- put(id, value);
139
- }
140
- for (const id of clears) {
141
- if (changed.has(id))
142
- continue;
143
- const row = effective(requireField(id));
144
- if (row.kind !== 'ban' || (!row.exceptWhen && !row.exceptAny))
145
- issue('ENVIRONMENT_RULE_TARGET_MISSING', source + ' / ' + id);
146
- const { exceptWhen: _old, exceptAny: _others, ...clean } = row;
147
- put(id, clean);
148
- }
149
- for (const row of local) {
150
- const prior = lookup(row.id);
151
- if (prior && [...prior.choices.values()].some(choice => choice.value !== null))
152
- issue('ENVIRONMENT_RULE_ALREADY_EXISTS', source + ' / ' + row.id);
153
- put(row.id, row);
154
- }
155
- for (const exception of edits.exceptions ?? []) {
156
- const field = requireField(exception.target);
157
- const row = effective(field);
158
- if (row.kind !== 'ban')
159
- issue('ENVIRONMENT_RULE_EXCEPTION_INVALID', source + ' / ' + row.id);
160
- const clauses = row.exceptAny ?? [];
161
- if (!clauses.some(clause => canonical(clause) === canonical(exception.allOf)))
162
- put(row.id, { ...row, exceptAny: [...clauses, exception.allOf] });
163
- }
164
- }
165
- export function composeRuleSets(input, definitions, validate) {
166
- const root = splitRuleDeclaration(input);
167
- const ruleSets = readEnvironmentRuleSets(definitions);
168
- const catalog = new Map(ruleSets.map(row => [row.id, row.rules]));
169
- const done = new Map();
170
- const stack = new Set();
171
- function visit(id) {
172
- if (stack.has(id))
173
- issue('ENVIRONMENT_RULE_CYCLE', [...stack, id].join(' → '));
174
- const cached = done.get(id);
175
- if (cached)
176
- return cached;
177
- if (!catalog.has(id))
178
- issue('ENVIRONMENT_RULE_SET_MISSING', [...stack, id].join(' → '));
179
- if (stack.size >= 128)
180
- issue('ENVIRONMENT_RULES_INVALID', '规则集引用超过 128 层');
181
- stack.add(id);
182
- const fields = combine(id, splitRuleDeclaration(catalog.get(id)));
183
- stack.delete(id);
184
- done.set(id, fields);
185
- return fields;
186
- }
187
- function combine(source, declaration) {
188
- const fields = new Map();
189
- for (const id of [...declaration.include].sort())
190
- for (const field of visit(id).values())
191
- for (const choice of field.choices.values())
192
- merge(fields, field.path, choice, field.sources);
193
- for (const collection of declaration.collections)
194
- merge(fields, ['collections', collection.id], { value: collection, sources: new Set([source]) }, [source], true);
195
- applyConstraintEdits(fields, source, declaration.edits, declaration.rules.constraints);
196
- const own = new Set();
197
- for (const { path, value } of entries(declaration.rules)) {
198
- if (path[0] === 'constraints')
199
- continue;
200
- const key = JSON.stringify(path);
201
- if (own.has(key))
202
- issue('ENVIRONMENT_RULES_INVALID', source + ' / ' + path.join(' / '));
203
- own.add(key);
204
- merge(fields, path, { value, sources: new Set([source]) }, [source], true);
205
- }
206
- return fields;
207
- }
208
- const fields = combine('$environment', root);
209
- const collections = new Map();
210
- for (const field of fields.values())
211
- if (field.path[0] === 'collections')
212
- collections.set(field.path[1], effective(field));
28
+ export function resolveDeclaredRules({ collections: lists = [], constraints, ...local }) {
29
+ const collections = new Map(lists.map(row => [row.id, row]));
213
30
  function expandConstraint(row) {
214
31
  const expand = (selector) => {
215
32
  const ids = new Set(selector.ids);
216
33
  for (const id of selector.fromCollections ?? []) {
217
34
  const collection = collections.get(id);
218
35
  if (!collection)
219
- issue('ENVIRONMENT_COLLECTION_MISSING', row.id + ' / ' + id);
36
+ throw new RuleCompositionError('ENVIRONMENT_COLLECTION_MISSING', row.id + ' / ' + id);
220
37
  if (collection.kind !== selector.kind)
221
- issue('ENVIRONMENT_COLLECTION_KIND', row.id + ' / ' + id);
38
+ throw new RuleCompositionError('ENVIRONMENT_COLLECTION_KIND', row.id + ' / ' + id);
222
39
  for (const member of collection.ids)
223
40
  ids.add(member);
224
41
  }
@@ -233,35 +50,10 @@ export function composeRuleSets(input, definitions, validate) {
233
50
  }),
234
51
  };
235
52
  }
236
- const constraintDeclarations = [];
237
- const fieldsWithSources = [];
238
- const result = { schemaVersion: root.rules.schemaVersion };
239
- for (const key of [...fields.keys()].sort()) {
240
- const field = fields.get(key);
241
- const raw = effective(field);
242
- let value = raw;
243
- if (raw && field.path[0] === 'constraints') {
244
- const declaration = raw;
245
- constraintDeclarations.push(declaration);
246
- value = expandConstraint(declaration);
247
- }
248
- if (field.path[0] !== 'collections' && !(field.path[0] === 'constraints' && value === null))
249
- writeField(result, field.path, value);
250
- const effectiveSources = new Set([...field.choices.values()].flatMap(choice => [...choice.sources]));
251
- fieldsWithSources.push({
252
- path: [...field.path],
253
- value,
254
- sources: [...field.sources].sort(),
255
- effectiveSources: [...effectiveSources].sort(),
256
- });
257
- }
258
- // Existing documents retain their original representation and revision.
259
- return {
260
- rules: root.include.length || root.collections.length || root.rules.constraints?.length || Object.keys(root.edits).length
261
- ? validate(result)
262
- : root.rules,
263
- declarations: { constraints: constraintDeclarations, collections: [...collections.values()] },
264
- fields: fieldsWithSources,
265
- ruleSets,
266
- };
53
+ const rules = parseRuleData(EnvironmentRulesSchema, {
54
+ ...local,
55
+ ...(constraints && { constraints: constraints.map(expandConstraint) }),
56
+ });
57
+ const fields = [...entries(rules), ...lists.map(value => ({ path: ['collections', value.id], value }))].map(field => ({ ...field, sources: ['$environment'], effectiveSources: ['$environment'] }));
58
+ return { rules, fields };
267
59
  }
@@ -21,7 +21,7 @@
21
21
  "devDependencies": {
22
22
  "@types/node": "26.6.6",
23
23
  "typescript": "6.0.3",
24
- "@arcadia-eternity/content-sdk": "4.10.0",
24
+ "@arcadia-eternity/content-sdk": "4.11.1",
25
25
  "fflate": "0.8.3"
26
26
  },
27
27
  "arcadia": {
@@ -38,6 +38,6 @@
38
38
  "license": "MIT",
39
39
  "engines": {
40
40
  "node": ">=24 <25",
41
- "arcadia": "^4.10.0"
41
+ "arcadia": "^4.11.1"
42
42
  }
43
43
  }
package/dist/testing.js CHANGED
@@ -7,6 +7,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
7
7
  return path;
8
8
  };
9
9
  import assert from 'node:assert/strict';
10
+ import { resolveEnvironmentBattleOptions } from '@arcadia-eternity/const';
10
11
  import { randomBytes } from 'node:crypto';
11
12
  import { join, resolve } from 'node:path';
12
13
  import { pathToFileURL } from 'node:url';
@@ -33,6 +34,7 @@ export async function prepareContentPack(root = process.cwd(), options = {}) {
33
34
  const { environment } = prepared;
34
35
  const environmentRules = validateEnvironmentRules(prepared.effectiveRules);
35
36
  const { allowFaintSwitch, enhancement, individualValues } = resolveEnvironmentMechanics(environmentRules);
37
+ const { timerConfig, teamSelection } = resolveEnvironmentBattleOptions(environmentRules);
36
38
  const enhancementActive = enhancement.mode === 'active';
37
39
  // Four automation sessions, one human preview, and room for their validated replacements.
38
40
  const workers = createNodeBattleWorkerManager({ maxBattles: 7 });
@@ -98,7 +100,7 @@ export async function prepareContentPack(root = process.cwd(), options = {}) {
98
100
  matchups: environmentRules.matchups,
99
101
  enhancementActive,
100
102
  rngSeed: seed,
101
- timerConfig: battleOptions.timerConfig ?? { enabled: false },
103
+ timerConfig: timerConfig ?? battleOptions.timerConfig ?? { enabled: false },
102
104
  }), effects);
103
105
  }
104
106
  catch (error) {
@@ -138,9 +140,9 @@ export async function prepareContentPack(root = process.cwd(), options = {}) {
138
140
  skillParameters: environmentRules.skillParameters,
139
141
  matchups: environmentRules.matchups,
140
142
  rngSeed: seed,
141
- timerConfig: battleOptions.timerConfig ?? { enabled: false },
143
+ timerConfig: timerConfig ?? battleOptions.timerConfig ?? { enabled: false },
142
144
  },
143
- hostOptions: { teamSelection: battleOptions.teamSelection },
145
+ hostOptions: { teamSelection: teamSelection ?? battleOptions.teamSelection },
144
146
  });
145
147
  return system;
146
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcadia-eternity/content-sdk",
3
- "version": "4.10.0",
3
+ "version": "4.11.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -63,10 +63,10 @@
63
63
  "zod": "4.6.7",
64
64
  "fflate": "0.8.3",
65
65
  "simple-git": "4.1.0",
66
- "@arcadia-eternity/battle": "4.10.0",
67
- "@arcadia-eternity/const": "4.10.0",
68
- "@arcadia-eternity/pack-loader": "4.10.0",
69
- "@arcadia-eternity/schema": "4.10.0"
66
+ "@arcadia-eternity/battle": "4.11.1",
67
+ "@arcadia-eternity/const": "4.11.1",
68
+ "@arcadia-eternity/pack-loader": "4.11.1",
69
+ "@arcadia-eternity/schema": "4.11.1"
70
70
  },
71
71
  "repository": {
72
72
  "type": "git",