boardgame-core/src/core/rule.ts

89 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-04-01 13:36:16 +08:00
import {Context} from "./context";
import {Command} from "../utils/command";
import {effect} from "@preact/signals-core";
export type RuleContext<T> = Context & {
actions: Command[];
handledActions: number;
invocations: RuleContext<unknown>[];
resolution?: T;
}
2026-04-01 17:34:21 +08:00
/**
*
* @param pushContext -
* @param type -
* @param rule -
* @returns
*/
export function invokeRuleContext<T>(
pushContext: (context: Context) => void,
type: string,
rule: Generator<string, T, Command>
): RuleContext<T> {
2026-04-01 13:36:16 +08:00
const ctx: RuleContext<T> = {
type,
actions: [],
handledActions: 0,
invocations: [],
resolution: undefined,
2026-04-01 17:34:21 +08:00
};
// 执行生成器直到完成或需要等待动作
const executeRule = () => {
try {
const result = rule.next();
if (result.done) {
// 规则执行完成,设置结果
ctx.resolution = result.value;
return;
}
// 如果生成器 yield 了一个动作类型,等待处理
// 这里可以扩展为实际的动作处理逻辑
const actionType = result.value;
// 继续执行直到有动作需要处理或规则完成
if (!result.done) {
executeRule();
}
} catch (error) {
// 规则执行出错,抛出错误
throw error;
}
};
// 使用 effect 来跟踪响应式依赖
2026-04-01 13:36:16 +08:00
const dispose = effect(() => {
2026-04-01 17:34:21 +08:00
if (ctx.resolution !== undefined) {
2026-04-01 13:36:16 +08:00
dispose();
return;
}
2026-04-01 17:34:21 +08:00
executeRule();
2026-04-01 13:36:16 +08:00
});
2026-04-01 17:34:21 +08:00
// 将规则上下文推入栈中
pushContext(ctx);
return ctx;
2026-04-01 13:36:16 +08:00
}
2026-04-01 17:34:21 +08:00
/**
*
* @param type -
* @param fn -
*/
export function createRule<T>(
type: string,
fn: (ctx: RuleContext<T>) => Generator<string, T, Command>
): Generator<string, T, Command> {
return fn({
type,
actions: [],
handledActions: 0,
invocations: [],
resolution: undefined,
});
}