@bct-app/game-engine 0.1.1 → 0.1.4
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.
- package/dist/index.d.mts +24 -92
- package/dist/index.d.ts +24 -92
- package/dist/index.js +495 -165
- package/dist/index.mjs +495 -165
- package/package.json +20 -8
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
// src/effects/handler-types.ts
|
|
2
|
+
var createEffectHandler = (type, handler) => {
|
|
3
|
+
return (context) => {
|
|
4
|
+
if (context.effect.type !== type) {
|
|
5
|
+
console.warn("Invalid effect type for handler:", context.effect.type, "expected:", type);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
handler({
|
|
9
|
+
...context,
|
|
10
|
+
effect: context.effect
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/effects/handlers/ability-change.ts
|
|
16
|
+
var applyAbilityChange = createEffectHandler("ABILITY_CHANGE", () => {
|
|
17
|
+
console.warn("ABILITY_CHANGE effect handling not implemented yet.");
|
|
18
|
+
});
|
|
19
|
+
|
|
1
20
|
// src/guards.ts
|
|
2
21
|
var isNumberOrNumberArray = (value) => {
|
|
3
22
|
if (typeof value === "number") {
|
|
@@ -38,6 +57,7 @@ var isPlayerReminderArray = (value) => {
|
|
|
38
57
|
};
|
|
39
58
|
|
|
40
59
|
// src/player-utils.ts
|
|
60
|
+
var DEFAULT_ARRAY = [];
|
|
41
61
|
var transformEmptyArray = (arr) => {
|
|
42
62
|
return arr.length === 0 ? [] : arr;
|
|
43
63
|
};
|
|
@@ -57,7 +77,7 @@ var adjustValueAsNumberArray = (value) => {
|
|
|
57
77
|
if (isNumberOrNumberArray(value)) {
|
|
58
78
|
return typeof value === "number" ? [value] : value;
|
|
59
79
|
}
|
|
60
|
-
return
|
|
80
|
+
return DEFAULT_ARRAY;
|
|
61
81
|
};
|
|
62
82
|
var adjustValueAsNumber = (value) => {
|
|
63
83
|
if (isNumberOrNumberArray(value)) {
|
|
@@ -83,33 +103,39 @@ var getValueFromPayloads = (payloads, idx) => {
|
|
|
83
103
|
return payloads[idx];
|
|
84
104
|
};
|
|
85
105
|
var getSourceValue = (source, payloads) => {
|
|
86
|
-
if (source
|
|
106
|
+
if (source?.from === "PAYLOAD") {
|
|
87
107
|
if (!isNumberOrNumberArray(source.value)) {
|
|
88
108
|
return void 0;
|
|
89
109
|
}
|
|
90
110
|
return getValueFromPayloads(payloads, source.value);
|
|
91
111
|
}
|
|
92
|
-
if (source
|
|
112
|
+
if (source?.from === "CONSTANT") {
|
|
93
113
|
return source.value;
|
|
94
114
|
}
|
|
95
115
|
return null;
|
|
96
116
|
};
|
|
97
|
-
var resolveSourceValue = (source,
|
|
98
|
-
|
|
117
|
+
var resolveSourceValue = (source, writableInputs, payloads, effector, snapshotSeatMap, resolveFromPlayer, isResolvedValue) => {
|
|
118
|
+
const castOrValidate = (value) => {
|
|
119
|
+
if (!isResolvedValue) {
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
return isResolvedValue(value) ? value : void 0;
|
|
123
|
+
};
|
|
124
|
+
if (source?.from === "PAYLOAD") {
|
|
99
125
|
const payloadValue = getSourceValue(source, payloads);
|
|
100
126
|
const idx = adjustValueAsNumberArray(source.value);
|
|
101
|
-
const
|
|
102
|
-
if (
|
|
127
|
+
const input = writableInputs[idx[0]];
|
|
128
|
+
if (input?.type === "PLAYER") {
|
|
103
129
|
const seat = adjustValueAsNumber(payloadValue);
|
|
104
130
|
const player = typeof seat === "number" ? snapshotSeatMap.get(seat) : void 0;
|
|
105
131
|
return player ? resolveFromPlayer(player) : void 0;
|
|
106
132
|
}
|
|
107
|
-
return payloadValue;
|
|
133
|
+
return castOrValidate(payloadValue);
|
|
108
134
|
}
|
|
109
|
-
if (source
|
|
110
|
-
return source.value;
|
|
135
|
+
if (source?.from === "CONSTANT") {
|
|
136
|
+
return castOrValidate(source.value);
|
|
111
137
|
}
|
|
112
|
-
if (source
|
|
138
|
+
if (source?.from === "EFFECTOR") {
|
|
113
139
|
if (!effector) {
|
|
114
140
|
return void 0;
|
|
115
141
|
}
|
|
@@ -119,7 +145,423 @@ var resolveSourceValue = (source, writableParts, payloads, effector, snapshotSea
|
|
|
119
145
|
return void 0;
|
|
120
146
|
};
|
|
121
147
|
|
|
148
|
+
// src/effects/handlers/alignment-change.ts
|
|
149
|
+
var applyAlignmentChange = createEffectHandler("ALIGNMENT_CHANGE", ({
|
|
150
|
+
effect,
|
|
151
|
+
payloads,
|
|
152
|
+
effector,
|
|
153
|
+
writableInputs,
|
|
154
|
+
getSnapshotSeatMap,
|
|
155
|
+
makePlayersEffect
|
|
156
|
+
}) => {
|
|
157
|
+
const alignment = resolveSourceValue(
|
|
158
|
+
effect.source,
|
|
159
|
+
writableInputs,
|
|
160
|
+
payloads,
|
|
161
|
+
effector,
|
|
162
|
+
getSnapshotSeatMap(),
|
|
163
|
+
(player) => player.alignment,
|
|
164
|
+
(isResolvedValue) => typeof isResolvedValue === "string" && (isResolvedValue === "GOOD" || isResolvedValue === "EVIL")
|
|
165
|
+
);
|
|
166
|
+
if (!alignment) {
|
|
167
|
+
console.warn("Alignment not found for ALIGNMENT_CHANGE effect:", effect.source);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
makePlayersEffect.forEach((player) => {
|
|
171
|
+
player.alignment = alignment;
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// src/effects/handlers/character-change.ts
|
|
176
|
+
var resolveCharacterId = (value) => {
|
|
177
|
+
if (typeof value === "string") {
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
if (Array.isArray(value) && typeof value[0] === "string") {
|
|
181
|
+
return value[0];
|
|
182
|
+
}
|
|
183
|
+
return void 0;
|
|
184
|
+
};
|
|
185
|
+
var applyCharacterChange = createEffectHandler("CHARACTER_CHANGE", ({
|
|
186
|
+
effect,
|
|
187
|
+
payloads,
|
|
188
|
+
effector,
|
|
189
|
+
writableInputs,
|
|
190
|
+
getSnapshotSeatMap,
|
|
191
|
+
characterMap,
|
|
192
|
+
makePlayersEffect
|
|
193
|
+
}) => {
|
|
194
|
+
const resolvedCharacterId = resolveSourceValue(
|
|
195
|
+
effect.source,
|
|
196
|
+
writableInputs,
|
|
197
|
+
payloads,
|
|
198
|
+
effector,
|
|
199
|
+
getSnapshotSeatMap(),
|
|
200
|
+
(player) => player.characterId
|
|
201
|
+
);
|
|
202
|
+
const characterId = resolveCharacterId(resolvedCharacterId);
|
|
203
|
+
if (!characterId) {
|
|
204
|
+
console.warn("Character ID not found for CHARACTER_CHANGE effect:", effect.source);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const newCharacter = characterMap.get(characterId);
|
|
208
|
+
if (!newCharacter) {
|
|
209
|
+
console.warn("Character not found for CHARACTER_CHANGE effect:", characterId);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const abilityIds = newCharacter.abilities.map((ability) => ability.id);
|
|
213
|
+
makePlayersEffect.forEach((player) => {
|
|
214
|
+
player.characterId = newCharacter.id;
|
|
215
|
+
player.abilities = abilityIds;
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// src/effects/handlers/character-count-change.ts
|
|
220
|
+
var applyCharacterCountChange = createEffectHandler("CHARACTER_COUNT_CHANGE", () => {
|
|
221
|
+
console.warn("CHARACTER_COUNT_CHANGE effect handling not implemented yet.");
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// src/effects/handlers/game-change.ts
|
|
225
|
+
var applyGameChange = createEffectHandler("GAME_CHANGE", () => {
|
|
226
|
+
console.warn("GAME_CHANGE effect handling not implemented yet.");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// src/effects/handlers/perceived-character-change.ts
|
|
230
|
+
var resolveCharacterId2 = (value) => {
|
|
231
|
+
if (typeof value === "string") {
|
|
232
|
+
return value;
|
|
233
|
+
}
|
|
234
|
+
if (Array.isArray(value) && typeof value[0] === "string") {
|
|
235
|
+
return value[0];
|
|
236
|
+
}
|
|
237
|
+
return void 0;
|
|
238
|
+
};
|
|
239
|
+
var applyPerceivedCharacterChange = createEffectHandler("PERCEIVED_CHARACTER_CHANGE", ({
|
|
240
|
+
effect,
|
|
241
|
+
payloads,
|
|
242
|
+
effector,
|
|
243
|
+
writableInputs,
|
|
244
|
+
getSnapshotSeatMap,
|
|
245
|
+
characterMap,
|
|
246
|
+
makePlayersEffect
|
|
247
|
+
}) => {
|
|
248
|
+
const resolvedCharacterId = resolveSourceValue(
|
|
249
|
+
effect.source,
|
|
250
|
+
writableInputs,
|
|
251
|
+
payloads,
|
|
252
|
+
effector,
|
|
253
|
+
getSnapshotSeatMap(),
|
|
254
|
+
(player) => player.characterId
|
|
255
|
+
);
|
|
256
|
+
const characterId = resolveCharacterId2(resolvedCharacterId);
|
|
257
|
+
if (!characterId) {
|
|
258
|
+
console.warn("Character ID not found for PERCEIVED_CHARACTER_CHANGE effect:", effect.source);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const newCharacter = characterMap.get(characterId);
|
|
262
|
+
if (!newCharacter) {
|
|
263
|
+
console.warn("Character not found for PERCEIVED_CHARACTER_CHANGE effect:", characterId);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
makePlayersEffect.forEach((player) => {
|
|
267
|
+
player.perceivedCharacter = {
|
|
268
|
+
characterId: newCharacter.id,
|
|
269
|
+
abilities: newCharacter.abilities.map((ability) => ability.id),
|
|
270
|
+
asCharacter: effect.followPriority || false
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// src/effects/handlers/reminder-add.ts
|
|
276
|
+
var applyReminderAdd = createEffectHandler("REMINDER_ADD", ({
|
|
277
|
+
effect,
|
|
278
|
+
payloads,
|
|
279
|
+
nowTimelineIndex,
|
|
280
|
+
operationInTimelineIdx,
|
|
281
|
+
makePlayersEffect
|
|
282
|
+
}) => {
|
|
283
|
+
const maybeReminder = getSourceValue(effect.source, payloads);
|
|
284
|
+
if (!isReminder(maybeReminder)) {
|
|
285
|
+
console.warn("Invalid reminder data for REMINDER_ADD effect:", maybeReminder);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const timelineDistance = nowTimelineIndex - operationInTimelineIdx;
|
|
289
|
+
if (typeof maybeReminder.duration === "number" && maybeReminder.duration > 0 && timelineDistance >= maybeReminder.duration) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
makePlayersEffect.forEach((player) => {
|
|
293
|
+
if (!player.reminders) {
|
|
294
|
+
player.reminders = [];
|
|
295
|
+
}
|
|
296
|
+
player.reminders.push(maybeReminder);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// src/effects/handlers/reminder-remove.ts
|
|
301
|
+
var applyReminderRemove = createEffectHandler("REMINDER_REMOVE", ({ effect, payloads, playerSeatMap }) => {
|
|
302
|
+
const maybeReminder = getSourceValue(effect.source, payloads);
|
|
303
|
+
if (!isPlayerReminderArray(maybeReminder)) {
|
|
304
|
+
console.warn("Invalid reminder data for REMINDER_REMOVE effect:", maybeReminder);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
maybeReminder.sort((left, right) => right.index - left.index).forEach((reminder) => {
|
|
308
|
+
const player = playerSeatMap.get(reminder.playerSeat);
|
|
309
|
+
if (!player) {
|
|
310
|
+
console.warn("Player not found for REMINDER_REMOVE effect:", reminder.playerSeat);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (!player.reminders) {
|
|
314
|
+
player.reminders = [];
|
|
315
|
+
}
|
|
316
|
+
player.reminders = player.reminders.filter((_value, index) => index !== reminder.index);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// src/effects/handlers/seat-change.ts
|
|
321
|
+
var applySeatChange = createEffectHandler("SEAT_CHANGE", () => {
|
|
322
|
+
console.warn("SEAT_CHANGE effect handling not implemented yet.");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// src/effects/handlers/status-change.ts
|
|
326
|
+
var applyStatusChange = createEffectHandler("STATUS_CHANGE", ({ effect, payloads, makePlayersEffect }) => {
|
|
327
|
+
const maybeStatus = getSourceValue(effect.source, payloads);
|
|
328
|
+
if (maybeStatus === "DEAD") {
|
|
329
|
+
makePlayersEffect.forEach((player) => {
|
|
330
|
+
player.isDead = true;
|
|
331
|
+
});
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (maybeStatus === "ALIVE") {
|
|
335
|
+
makePlayersEffect.forEach((player) => {
|
|
336
|
+
player.isDead = false;
|
|
337
|
+
});
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
console.warn("Invalid status value for STATUS_CHANGE effect:", maybeStatus);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// src/effects/registry.ts
|
|
344
|
+
var effectHandlers = {
|
|
345
|
+
ABILITY_CHANGE: applyAbilityChange,
|
|
346
|
+
ALIGNMENT_CHANGE: applyAlignmentChange,
|
|
347
|
+
CHARACTER_CHANGE: applyCharacterChange,
|
|
348
|
+
CHARACTER_COUNT_CHANGE: applyCharacterCountChange,
|
|
349
|
+
GAME_CHANGE: applyGameChange,
|
|
350
|
+
PERCEIVED_CHARACTER_CHANGE: applyPerceivedCharacterChange,
|
|
351
|
+
REMINDER_ADD: applyReminderAdd,
|
|
352
|
+
REMINDER_REMOVE: applyReminderRemove,
|
|
353
|
+
SEAT_CHANGE: applySeatChange,
|
|
354
|
+
STATUS_CHANGE: applyStatusChange
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// src/effects/resolve-targets.ts
|
|
358
|
+
var isPayloadRef = (value) => {
|
|
359
|
+
if (!value || typeof value !== "object") {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
const candidate = value;
|
|
363
|
+
if (candidate.from !== "PAYLOAD") {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
return typeof candidate.value === "number" || Array.isArray(candidate.value);
|
|
367
|
+
};
|
|
368
|
+
var resolveDynamicValue = (value, payloads, isExpected) => {
|
|
369
|
+
if (typeof value === "undefined") {
|
|
370
|
+
return void 0;
|
|
371
|
+
}
|
|
372
|
+
if (!isPayloadRef(value)) {
|
|
373
|
+
return isExpected(value) ? value : void 0;
|
|
374
|
+
}
|
|
375
|
+
const resolved = getValueFromPayloads(payloads, value.value);
|
|
376
|
+
if (!isExpected(resolved)) {
|
|
377
|
+
return void 0;
|
|
378
|
+
}
|
|
379
|
+
return resolved;
|
|
380
|
+
};
|
|
381
|
+
var getSortedPlayers = (playerSeatMap) => {
|
|
382
|
+
return [...playerSeatMap.values()].sort((left, right) => left.seat - right.seat);
|
|
383
|
+
};
|
|
384
|
+
var getCircularDistance = (anchorIdx, targetIdx, total) => {
|
|
385
|
+
const clockwise = (targetIdx - anchorIdx + total) % total;
|
|
386
|
+
const anticlockwise = (anchorIdx - targetIdx + total) % total;
|
|
387
|
+
return Math.min(clockwise, anticlockwise);
|
|
388
|
+
};
|
|
389
|
+
var getAnchorSeat = (dynamicTarget, payloads, effector) => {
|
|
390
|
+
const anchor = dynamicTarget.anchor;
|
|
391
|
+
if (!anchor || anchor.from === "EFFECTOR") {
|
|
392
|
+
return effector?.seat;
|
|
393
|
+
}
|
|
394
|
+
if (anchor.from === "STATIC") {
|
|
395
|
+
return anchor.value;
|
|
396
|
+
}
|
|
397
|
+
const payloadSeat = getValueFromPayloads(payloads, anchor.value);
|
|
398
|
+
return typeof payloadSeat === "number" ? payloadSeat : void 0;
|
|
399
|
+
};
|
|
400
|
+
var resolveSelectorScope = (dynamicTarget, payloads) => {
|
|
401
|
+
return resolveDynamicValue(
|
|
402
|
+
dynamicTarget.selector?.scope,
|
|
403
|
+
payloads,
|
|
404
|
+
(value) => value === "BOTH_SIDES" || value === "LEFT_SIDE" || value === "RIGHT_SIDE"
|
|
405
|
+
) ?? "BOTH_SIDES";
|
|
406
|
+
};
|
|
407
|
+
var getCandidatesBySelector = (dynamicTarget, sortedPlayers, anchorSeat, payloads) => {
|
|
408
|
+
const selector = dynamicTarget.selector;
|
|
409
|
+
if (!selector) {
|
|
410
|
+
return sortedPlayers;
|
|
411
|
+
}
|
|
412
|
+
const scope = resolveSelectorScope(dynamicTarget, payloads);
|
|
413
|
+
if (!anchorSeat) {
|
|
414
|
+
return [];
|
|
415
|
+
}
|
|
416
|
+
const anchorIdx = sortedPlayers.findIndex((player) => player.seat === anchorSeat);
|
|
417
|
+
if (anchorIdx < 0) {
|
|
418
|
+
return [];
|
|
419
|
+
}
|
|
420
|
+
const total = sortedPlayers.length;
|
|
421
|
+
if (scope === "LEFT_SIDE") {
|
|
422
|
+
const left2 = [];
|
|
423
|
+
for (let step = 1; step < total; step += 1) {
|
|
424
|
+
left2.push(sortedPlayers[(anchorIdx - step + total) % total]);
|
|
425
|
+
}
|
|
426
|
+
return left2;
|
|
427
|
+
}
|
|
428
|
+
if (scope === "RIGHT_SIDE") {
|
|
429
|
+
const right2 = [];
|
|
430
|
+
for (let step = 1; step < total; step += 1) {
|
|
431
|
+
right2.push(sortedPlayers[(anchorIdx + step) % total]);
|
|
432
|
+
}
|
|
433
|
+
return right2;
|
|
434
|
+
}
|
|
435
|
+
const left = [];
|
|
436
|
+
const right = [];
|
|
437
|
+
for (let step = 1; step < total; step += 1) {
|
|
438
|
+
left.push(sortedPlayers[(anchorIdx - step + total) % total]);
|
|
439
|
+
right.push(sortedPlayers[(anchorIdx + step) % total]);
|
|
440
|
+
}
|
|
441
|
+
return [...left, ...right];
|
|
442
|
+
};
|
|
443
|
+
var matchesCondition = (player, condition, payloads, characterMap) => {
|
|
444
|
+
if (condition.field === "IS_DEAD") {
|
|
445
|
+
const expected = resolveDynamicValue(condition.value, payloads, (value) => typeof value === "boolean");
|
|
446
|
+
if (typeof expected !== "boolean") {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
return Boolean(player.isDead) === expected;
|
|
450
|
+
}
|
|
451
|
+
if (condition.field === "SEAT") {
|
|
452
|
+
if (condition.operator === "IN") {
|
|
453
|
+
const expectedSeats = resolveDynamicValue(condition.value, payloads, (value) => Array.isArray(value) && value.every((item) => typeof item === "number"));
|
|
454
|
+
return Array.isArray(expectedSeats) && expectedSeats.includes(player.seat);
|
|
455
|
+
}
|
|
456
|
+
const expectedSeat = resolveDynamicValue(condition.value, payloads, (value) => typeof value === "number");
|
|
457
|
+
return typeof expectedSeat === "number" && player.seat === expectedSeat;
|
|
458
|
+
}
|
|
459
|
+
const character = characterMap.get(player.characterId);
|
|
460
|
+
const kind = character?.kind;
|
|
461
|
+
if (!kind) {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
if (condition.operator === "IN") {
|
|
465
|
+
const expectedKinds = resolveDynamicValue(condition.value, payloads, (value) => Array.isArray(value) && value.every((item) => typeof item === "string"));
|
|
466
|
+
return Array.isArray(expectedKinds) && expectedKinds.includes(kind);
|
|
467
|
+
}
|
|
468
|
+
const expectedKind = resolveDynamicValue(condition.value, payloads, (value) => typeof value === "string");
|
|
469
|
+
return typeof expectedKind === "string" && kind === expectedKind;
|
|
470
|
+
};
|
|
471
|
+
var filterByWhere = (candidates, dynamicTarget, payloads, characterMap) => {
|
|
472
|
+
const where = dynamicTarget.where;
|
|
473
|
+
const conditions = where?.conditions ?? [];
|
|
474
|
+
if (conditions.length === 0) {
|
|
475
|
+
return candidates;
|
|
476
|
+
}
|
|
477
|
+
const isAllMode = where?.mode !== "ANY";
|
|
478
|
+
return candidates.filter((player) => {
|
|
479
|
+
const results = conditions.map((condition) => matchesCondition(player, condition, payloads, characterMap));
|
|
480
|
+
return isAllMode ? results.every(Boolean) : results.some(Boolean);
|
|
481
|
+
});
|
|
482
|
+
};
|
|
483
|
+
var applySort = (candidates, dynamicTarget, payloads, sortedPlayers, anchorSeat) => {
|
|
484
|
+
const scope = resolveSelectorScope(dynamicTarget, payloads);
|
|
485
|
+
if (scope !== "BOTH_SIDES") {
|
|
486
|
+
return [...candidates];
|
|
487
|
+
}
|
|
488
|
+
if (!anchorSeat || sortedPlayers.length <= 1) {
|
|
489
|
+
return [...candidates].sort((left, right) => left.seat - right.seat);
|
|
490
|
+
}
|
|
491
|
+
const seatIndexMap = new Map(sortedPlayers.map((player, idx) => [player.seat, idx]));
|
|
492
|
+
const anchorIdx = seatIndexMap.get(anchorSeat);
|
|
493
|
+
if (typeof anchorIdx !== "number") {
|
|
494
|
+
return [...candidates].sort((left, right) => left.seat - right.seat);
|
|
495
|
+
}
|
|
496
|
+
return [...candidates].sort((left, right) => {
|
|
497
|
+
const leftIdx = seatIndexMap.get(left.seat);
|
|
498
|
+
const rightIdx = seatIndexMap.get(right.seat);
|
|
499
|
+
if (typeof leftIdx !== "number" || typeof rightIdx !== "number") {
|
|
500
|
+
return left.seat - right.seat;
|
|
501
|
+
}
|
|
502
|
+
const leftDistance = getCircularDistance(anchorIdx, leftIdx, sortedPlayers.length);
|
|
503
|
+
const rightDistance = getCircularDistance(anchorIdx, rightIdx, sortedPlayers.length);
|
|
504
|
+
if (leftDistance !== rightDistance) {
|
|
505
|
+
return leftDistance - rightDistance;
|
|
506
|
+
}
|
|
507
|
+
return left.seat - right.seat;
|
|
508
|
+
});
|
|
509
|
+
};
|
|
510
|
+
var pickTargets = (sortedCandidates, dynamicTarget, payloads) => {
|
|
511
|
+
const dedupedCandidates = [...new Map(sortedCandidates.map((player) => [player.seat, player])).values()];
|
|
512
|
+
const configuredLimit = resolveDynamicValue(dynamicTarget.limit, payloads, (value) => typeof value === "number");
|
|
513
|
+
if (typeof configuredLimit === "undefined") {
|
|
514
|
+
return dedupedCandidates;
|
|
515
|
+
}
|
|
516
|
+
if (configuredLimit <= 0) {
|
|
517
|
+
return [];
|
|
518
|
+
}
|
|
519
|
+
return dedupedCandidates.slice(0, configuredLimit);
|
|
520
|
+
};
|
|
521
|
+
var resolveEffectTargets = ({
|
|
522
|
+
effect,
|
|
523
|
+
operation,
|
|
524
|
+
payloads,
|
|
525
|
+
effector,
|
|
526
|
+
playerSeatMap,
|
|
527
|
+
characterMap
|
|
528
|
+
}) => {
|
|
529
|
+
if (!("target" in effect)) {
|
|
530
|
+
return [];
|
|
531
|
+
}
|
|
532
|
+
if (effect.target.from === "EFFECTOR") {
|
|
533
|
+
if (!effector) {
|
|
534
|
+
console.warn("Effector player not found for operation:", operation, "effector index:", operation.effector);
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
return [effector];
|
|
538
|
+
}
|
|
539
|
+
if (effect.target.from === "PAYLOAD") {
|
|
540
|
+
const seatValue = getValueFromPayloads(payloads, effect.target.value);
|
|
541
|
+
if (typeof seatValue !== "number" && !Array.isArray(seatValue)) {
|
|
542
|
+
console.warn("Expected seat number or array of seat numbers from payloads, got:", seatValue);
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
const seats = Array.isArray(seatValue) ? seatValue : [seatValue];
|
|
546
|
+
return seats.map((seat) => playerSeatMap.get(seat)).filter((player) => Boolean(player));
|
|
547
|
+
}
|
|
548
|
+
if (effect.target.from === "DYNAMIC") {
|
|
549
|
+
const dynamicTarget = effect.target;
|
|
550
|
+
const sortedPlayers = getSortedPlayers(playerSeatMap);
|
|
551
|
+
const anchorSeat = getAnchorSeat(dynamicTarget, payloads, effector);
|
|
552
|
+
const candidates = getCandidatesBySelector(dynamicTarget, sortedPlayers, anchorSeat, payloads);
|
|
553
|
+
const filtered = filterByWhere(candidates, dynamicTarget, payloads, characterMap);
|
|
554
|
+
const ordered = applySort(filtered, dynamicTarget, payloads, sortedPlayers, anchorSeat);
|
|
555
|
+
return pickTargets(ordered, dynamicTarget, payloads);
|
|
556
|
+
}
|
|
557
|
+
return [];
|
|
558
|
+
};
|
|
559
|
+
|
|
122
560
|
// src/apply-operation.ts
|
|
561
|
+
var isDeferredOperation = (operation, abilityMap) => {
|
|
562
|
+
const ability = abilityMap.get(operation.abilityId);
|
|
563
|
+
return ability?.executionTiming === "DEFER_TO_END";
|
|
564
|
+
};
|
|
123
565
|
var applyOperationToPlayers = ({
|
|
124
566
|
players,
|
|
125
567
|
operations,
|
|
@@ -145,7 +587,7 @@ var applyOperationToPlayers = ({
|
|
|
145
587
|
}
|
|
146
588
|
operationsByTimeline.get(timelineIndex)?.push(operation);
|
|
147
589
|
});
|
|
148
|
-
const
|
|
590
|
+
const applyOpsSequence = (ops) => {
|
|
149
591
|
ops.forEach((operation) => {
|
|
150
592
|
const operationInTimelineIdx = operationTimelineMap.get(operation) ?? -1;
|
|
151
593
|
const ability = abilityMap.get(operation.abilityId);
|
|
@@ -156,7 +598,7 @@ var applyOperationToPlayers = ({
|
|
|
156
598
|
const playerSeatMap = buildPlayerSeatMap(playersWithStatus);
|
|
157
599
|
const effector = playerSeatMap.get(operation.effector);
|
|
158
600
|
const payloads = operation.payloads || [];
|
|
159
|
-
const
|
|
601
|
+
const writableInputs = ability.inputs.filter((part) => !part.readonly);
|
|
160
602
|
let snapshotSeatMap = null;
|
|
161
603
|
const getSnapshotSeatMap = () => {
|
|
162
604
|
if (!snapshotSeatMap) {
|
|
@@ -165,164 +607,52 @@ var applyOperationToPlayers = ({
|
|
|
165
607
|
return snapshotSeatMap;
|
|
166
608
|
};
|
|
167
609
|
ability.effects.forEach((effect) => {
|
|
168
|
-
const makePlayersEffect =
|
|
169
|
-
|
|
170
|
-
|
|
610
|
+
const makePlayersEffect = resolveEffectTargets({
|
|
611
|
+
effect,
|
|
612
|
+
operation,
|
|
613
|
+
payloads,
|
|
614
|
+
effector,
|
|
615
|
+
playerSeatMap,
|
|
616
|
+
characterMap
|
|
617
|
+
});
|
|
618
|
+
if (!makePlayersEffect) {
|
|
171
619
|
return;
|
|
172
620
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
makePlayersEffect.push(effector);
|
|
179
|
-
} else if (effect.target.from === "PAYLOAD") {
|
|
180
|
-
const seatValue = getValueFromPayloads(payloads, effect.target.value);
|
|
181
|
-
if (typeof seatValue !== "number" && !Array.isArray(seatValue)) {
|
|
182
|
-
console.warn("Expected seat number or array of seat numbers from payloads, got:", seatValue);
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
const seats = Array.isArray(seatValue) ? seatValue : [seatValue];
|
|
186
|
-
seats.forEach((seat) => {
|
|
187
|
-
const player = playerSeatMap.get(seat);
|
|
188
|
-
if (player) {
|
|
189
|
-
makePlayersEffect.push(player);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
} else if (effect.target.from === "CUSTOM") {
|
|
193
|
-
console.warn("Custom target handling not implemented yet.");
|
|
194
|
-
}
|
|
195
|
-
switch (effect.type) {
|
|
196
|
-
case "ABILITY_CHANGE": {
|
|
197
|
-
console.warn("ABILITY_CHANGE effect handling not implemented yet.");
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
case "ALIGNMENT_CHANGE": {
|
|
201
|
-
const resolved = resolveSourceValue(
|
|
202
|
-
effect.source,
|
|
203
|
-
writableParts,
|
|
204
|
-
payloads,
|
|
205
|
-
effector,
|
|
206
|
-
getSnapshotSeatMap(),
|
|
207
|
-
(player) => player.alignment
|
|
208
|
-
);
|
|
209
|
-
let alignment;
|
|
210
|
-
if (resolved === "GOOD" || resolved === "EVIL") {
|
|
211
|
-
alignment = resolved;
|
|
212
|
-
}
|
|
213
|
-
if (!alignment) {
|
|
214
|
-
console.warn("Alignment not found for ALIGNMENT_CHANGE effect:", effect.source);
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
makePlayersEffect.forEach((player) => {
|
|
218
|
-
player.alignment = alignment;
|
|
219
|
-
});
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
case "CHARACTER_CHANGE":
|
|
223
|
-
case "PERCEIVED_CHARACTER_CHANGE": {
|
|
224
|
-
const resolvedCharacterId = resolveSourceValue(
|
|
225
|
-
effect.source,
|
|
226
|
-
writableParts,
|
|
227
|
-
payloads,
|
|
228
|
-
effector,
|
|
229
|
-
getSnapshotSeatMap(),
|
|
230
|
-
(player) => player.characterId
|
|
231
|
-
);
|
|
232
|
-
let characterId;
|
|
233
|
-
if (typeof resolvedCharacterId === "string") {
|
|
234
|
-
characterId = resolvedCharacterId;
|
|
235
|
-
} else if (Array.isArray(resolvedCharacterId) && typeof resolvedCharacterId[0] === "string") {
|
|
236
|
-
characterId = resolvedCharacterId[0];
|
|
237
|
-
}
|
|
238
|
-
if (!characterId) {
|
|
239
|
-
console.warn("Character ID not found for " + effect.type + " effect:", effect.source);
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
const newCharacter = characterMap.get(characterId);
|
|
243
|
-
if (!newCharacter) {
|
|
244
|
-
console.warn("Character not found for " + effect.type + " effect:", characterId);
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
if (effect.type === "CHARACTER_CHANGE") {
|
|
248
|
-
const abilityIds = newCharacter.abilities.map((ability2) => ability2.id);
|
|
249
|
-
makePlayersEffect.forEach((player) => {
|
|
250
|
-
player.characterId = newCharacter.id;
|
|
251
|
-
player.abilities = abilityIds;
|
|
252
|
-
});
|
|
253
|
-
} else {
|
|
254
|
-
makePlayersEffect.forEach((player) => {
|
|
255
|
-
player.perceivedCharacter = {
|
|
256
|
-
characterId: newCharacter.id,
|
|
257
|
-
abilities: newCharacter.abilities.map((ability2) => ability2.id),
|
|
258
|
-
asCharacter: effect.followPriority || false
|
|
259
|
-
};
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
break;
|
|
263
|
-
}
|
|
264
|
-
case "REMINDER_ADD": {
|
|
265
|
-
const maybeReminder = getSourceValue(effect.source, payloads);
|
|
266
|
-
if (!isReminder(maybeReminder)) {
|
|
267
|
-
console.warn("Invalid reminder data for REMINDER_ADD effect:", maybeReminder);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
const timelineDistance = nowTimelineIndex - operationInTimelineIdx;
|
|
271
|
-
if (typeof maybeReminder.duration === "number" && maybeReminder.duration > 0 && timelineDistance >= maybeReminder.duration) {
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
makePlayersEffect.forEach((player) => {
|
|
275
|
-
if (!player.reminders) {
|
|
276
|
-
player.reminders = [];
|
|
277
|
-
}
|
|
278
|
-
player.reminders.push(maybeReminder);
|
|
279
|
-
});
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
case "REMINDER_REMOVE": {
|
|
283
|
-
const maybeReminder = getSourceValue(effect.source, payloads);
|
|
284
|
-
if (!isPlayerReminderArray(maybeReminder)) {
|
|
285
|
-
console.warn("Invalid reminder data for REMINDER_REMOVE effect:", maybeReminder);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
maybeReminder.sort((left, right) => right.index - left.index).forEach((reminder) => {
|
|
289
|
-
const player = playerSeatMap.get(reminder.playerSeat);
|
|
290
|
-
if (!player) {
|
|
291
|
-
console.warn("Player not found for REMINDER_REMOVE effect:", reminder.playerSeat);
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
if (!player.reminders) {
|
|
295
|
-
player.reminders = [];
|
|
296
|
-
}
|
|
297
|
-
player.reminders = player.reminders.filter((_value, index) => index !== reminder.index);
|
|
298
|
-
});
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
case "SEAT_CHANGE": {
|
|
302
|
-
console.warn("SEAT_CHANGE effect handling not implemented yet.");
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
305
|
-
case "STATUS_CHANGE": {
|
|
306
|
-
const maybeStatus = getSourceValue(effect.source, payloads);
|
|
307
|
-
if (maybeStatus === "DEAD") {
|
|
308
|
-
makePlayersEffect.forEach((player) => {
|
|
309
|
-
player.isDead = true;
|
|
310
|
-
});
|
|
311
|
-
} else if (maybeStatus === "ALIVE") {
|
|
312
|
-
makePlayersEffect.forEach((player) => {
|
|
313
|
-
player.isDead = false;
|
|
314
|
-
});
|
|
315
|
-
} else {
|
|
316
|
-
console.warn("Invalid status value for STATUS_CHANGE effect:", maybeStatus);
|
|
317
|
-
}
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
default: {
|
|
321
|
-
}
|
|
621
|
+
const handler = effectHandlers[effect.type];
|
|
622
|
+
if (!handler) {
|
|
623
|
+
console.warn("Unknown effect type:", effect.type);
|
|
624
|
+
return;
|
|
322
625
|
}
|
|
626
|
+
const context = {
|
|
627
|
+
effect,
|
|
628
|
+
operation,
|
|
629
|
+
payloads,
|
|
630
|
+
effector,
|
|
631
|
+
writableInputs,
|
|
632
|
+
playerSeatMap,
|
|
633
|
+
getSnapshotSeatMap,
|
|
634
|
+
characterMap,
|
|
635
|
+
nowTimelineIndex,
|
|
636
|
+
operationInTimelineIdx,
|
|
637
|
+
makePlayersEffect
|
|
638
|
+
};
|
|
639
|
+
handler(context);
|
|
323
640
|
});
|
|
324
641
|
});
|
|
325
642
|
};
|
|
643
|
+
const applyOps = (ops) => {
|
|
644
|
+
const normalOps = [];
|
|
645
|
+
const deferredOps = [];
|
|
646
|
+
ops.forEach((operation) => {
|
|
647
|
+
if (isDeferredOperation(operation, abilityMap)) {
|
|
648
|
+
deferredOps.push(operation);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
normalOps.push(operation);
|
|
652
|
+
});
|
|
653
|
+
applyOpsSequence(normalOps);
|
|
654
|
+
applyOpsSequence(deferredOps);
|
|
655
|
+
};
|
|
326
656
|
const settingsOps = operationsByTimeline.get(-1);
|
|
327
657
|
if (settingsOps) {
|
|
328
658
|
applyOps(settingsOps);
|