feat: rule dispatch

This commit is contained in:
hypercross 2026-04-01 22:20:38 +08:00
parent de730c6479
commit 170217db30
3 changed files with 133 additions and 67 deletions

View File

@ -2,6 +2,7 @@ import {createModel, Signal, signal} from '@preact/signals-core';
import {createEntityCollection} from "../utils/entity";
import {Part} from "./part";
import {Region} from "./region";
import {RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule";
export type Context = {
type: string;
@ -10,18 +11,23 @@ export type Context = {
export const GameContext = createModel((root: Context) => {
const parts = createEntityCollection<Part>();
const regions = createEntityCollection<Region>();
const rules = signal<RuleRegistry>(new Map());
const ruleContexts = signal<RuleContext<unknown>[]>([]);
const contexts = signal<Signal<Context>[]>([]);
contexts.value = [signal(root)];
function pushContext(context: Context) {
const ctxSignal = signal(context);
contexts.value = [...contexts.value, ctxSignal];
return context;
}
function popContext() {
if (contexts.value.length > 1) {
contexts.value = contexts.value.slice(0, -1);
}
}
function latestContext<T extends Context>(type: T['type']): Signal<T> | undefined {
for(let i = contexts.value.length - 1; i >= 0; i--){
if(contexts.value[i].value.type === type){
@ -31,17 +37,28 @@ export const GameContext = createModel((root: Context) => {
return undefined;
}
function dispatchCommand(input: string) {
return dispatchRuleCommand({
rules: rules.value,
ruleContexts: ruleContexts.value,
contexts,
}, input);
}
return {
parts,
regions,
rules,
ruleContexts,
contexts,
pushContext,
popContext,
latestContext,
dispatchCommand,
}
})
/** 创建游戏上下文实例 */
export function createGameContext(root: Context = { type: 'game' }) {
return new GameContext(root);
}
}

View File

@ -1,80 +1,129 @@
import {Context} from "./context";
import {Command} from "../utils/command";
import {effect} from "@preact/signals-core";
import {Command, CommandSchema, parseCommand, parseCommandSchema, validateCommand} from "../utils/command";
export type RuleContext<T> = Context & {
actions: Command[];
handledActions: number;
invocations: RuleContext<unknown>[];
export type RuleState = 'running' | 'yielded' | 'waiting' | 'done';
export type RuleContext<T = unknown> = {
type: string;
schema?: CommandSchema;
generator: Generator<string | CommandSchema, T, Command>;
parent?: RuleContext<unknown>;
children: RuleContext<unknown>[];
state: RuleState;
resolution?: T;
}
/**
*
* @param pushContext -
* @param type -
* @param rule -
* @returns
*/
export function invokeRuleContext<T>(
pushContext: (context: Context) => void,
type: string,
rule: Generator<string, T, Command>
export type RuleDef<T = unknown> = {
schema: CommandSchema;
create: (cmd: Command) => Generator<string | CommandSchema, T, Command>;
};
export type RuleRegistry = Map<string, RuleDef<unknown>>;
export function createRule<T>(
schemaStr: string,
fn: (cmd: Command) => Generator<string | CommandSchema, T, Command>
): RuleDef<T> {
return {
schema: parseCommandSchema(schemaStr),
create: fn as RuleDef<T>['create'],
};
}
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
if (typeof value === 'string') {
return parseCommandSchema(value);
}
return value;
}
function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) {
game.contexts.value = [...game.contexts.value, { value: ctx } as any];
game.ruleContexts.push(ctx);
}
function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) {
for (const child of parent.children) {
const idx = game.ruleContexts.indexOf(child);
if (idx !== -1) game.ruleContexts.splice(idx, 1);
const ctxIdx = game.contexts.value.findIndex((c: any) => c.value === child);
if (ctxIdx !== -1) {
const arr = [...game.contexts.value];
arr.splice(ctxIdx, 1);
game.contexts.value = arr;
}
}
parent.children = [];
parent.state = 'yielded';
}
function invokeRule<T>(
game: GameContextLike,
command: Command,
ruleDef: RuleDef<T>,
parent?: RuleContext<unknown>
): RuleContext<T> {
const ctx: RuleContext<T> = {
type,
actions: [],
handledActions: 0,
invocations: [],
type: ruleDef.schema.name,
schema: undefined,
generator: ruleDef.create(command),
parent,
children: [],
state: 'running',
resolution: undefined,
};
let disposed = false;
if (parent) {
discardChildren(game, parent);
parent.children.push(ctx as RuleContext<unknown>);
parent.state = 'waiting';
}
const executeRule = () => {
if (disposed || ctx.resolution !== undefined) return;
pushContextToGame(game, ctx as RuleContext<unknown>);
try {
const result = rule.next();
if (result.done) {
ctx.resolution = result.value;
return;
}
const actionType = result.value;
if (actionType) {
// 暂停于 yield 点,等待外部处理动作
// 当外部更新 actions 后effect 会重新触发
}
} catch (error) {
throw error;
}
};
const dispose = effect(() => {
if (ctx.resolution !== undefined) {
dispose();
disposed = true;
return;
}
executeRule();
});
pushContext(ctx);
const result = ctx.generator.next();
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
} else {
ctx.schema = parseYieldedSchema(result.value);
ctx.state = 'yielded';
}
return ctx;
}
/**
*
* @param type -
* @param fn -
*/
export function createRule<T>(
type: string,
fn: (ctx: RuleContext<T>) => Generator<string, T, Command>
): (ctx: RuleContext<T>) => Generator<string, T, Command> {
return fn;
export function dispatchCommand(game: GameContextLike, input: string): RuleContext<unknown> | undefined {
const command = parseCommand(input);
if (game.rules.has(command.name)) {
const ruleDef = game.rules.get(command.name)!;
return invokeRule(game, command, ruleDef);
}
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
const ctx = game.ruleContexts[i];
if (ctx.state === 'yielded' && ctx.schema) {
const validation = validateCommand(command, ctx.schema);
if (validation.valid) {
const result = ctx.generator.next(command);
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
} else {
ctx.schema = parseYieldedSchema(result.value);
ctx.state = 'yielded';
}
return ctx;
}
}
}
return undefined;
}
type GameContextLike = {
rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[];
contexts: { value: any[] };
};

View File

@ -13,8 +13,8 @@ export { flip, flipTo, roll } from './core/part';
export type { Region, RegionAxis } from './core/region';
export { applyAlign, shuffle } from './core/region';
export type { RuleContext } from './core/rule';
export { invokeRuleContext, createRule } from './core/rule';
export type { RuleContext, RuleState, RuleDef, RuleRegistry } from './core/rule';
export { createRule, dispatchCommand } from './core/rule';
// Utils
export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';