feat: rule dispatch
This commit is contained in:
parent
de730c6479
commit
170217db30
|
|
@ -2,6 +2,7 @@ import {createModel, Signal, signal} from '@preact/signals-core';
|
||||||
import {createEntityCollection} from "../utils/entity";
|
import {createEntityCollection} from "../utils/entity";
|
||||||
import {Part} from "./part";
|
import {Part} from "./part";
|
||||||
import {Region} from "./region";
|
import {Region} from "./region";
|
||||||
|
import {RuleRegistry, RuleContext, dispatchCommand as dispatchRuleCommand} from "./rule";
|
||||||
|
|
||||||
export type Context = {
|
export type Context = {
|
||||||
type: string;
|
type: string;
|
||||||
|
|
@ -10,18 +11,23 @@ export type Context = {
|
||||||
export const GameContext = createModel((root: Context) => {
|
export const GameContext = createModel((root: Context) => {
|
||||||
const parts = createEntityCollection<Part>();
|
const parts = createEntityCollection<Part>();
|
||||||
const regions = createEntityCollection<Region>();
|
const regions = createEntityCollection<Region>();
|
||||||
|
const rules = signal<RuleRegistry>(new Map());
|
||||||
|
const ruleContexts = signal<RuleContext<unknown>[]>([]);
|
||||||
const contexts = signal<Signal<Context>[]>([]);
|
const contexts = signal<Signal<Context>[]>([]);
|
||||||
contexts.value = [signal(root)];
|
contexts.value = [signal(root)];
|
||||||
|
|
||||||
function pushContext(context: Context) {
|
function pushContext(context: Context) {
|
||||||
const ctxSignal = signal(context);
|
const ctxSignal = signal(context);
|
||||||
contexts.value = [...contexts.value, ctxSignal];
|
contexts.value = [...contexts.value, ctxSignal];
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
function popContext() {
|
function popContext() {
|
||||||
if (contexts.value.length > 1) {
|
if (contexts.value.length > 1) {
|
||||||
contexts.value = contexts.value.slice(0, -1);
|
contexts.value = contexts.value.slice(0, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function latestContext<T extends Context>(type: T['type']): Signal<T> | undefined {
|
function latestContext<T extends Context>(type: T['type']): Signal<T> | undefined {
|
||||||
for(let i = contexts.value.length - 1; i >= 0; i--){
|
for(let i = contexts.value.length - 1; i >= 0; i--){
|
||||||
if(contexts.value[i].value.type === type){
|
if(contexts.value[i].value.type === type){
|
||||||
|
|
@ -31,17 +37,28 @@ export const GameContext = createModel((root: Context) => {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dispatchCommand(input: string) {
|
||||||
|
return dispatchRuleCommand({
|
||||||
|
rules: rules.value,
|
||||||
|
ruleContexts: ruleContexts.value,
|
||||||
|
contexts,
|
||||||
|
}, input);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
parts,
|
parts,
|
||||||
regions,
|
regions,
|
||||||
|
rules,
|
||||||
|
ruleContexts,
|
||||||
contexts,
|
contexts,
|
||||||
pushContext,
|
pushContext,
|
||||||
popContext,
|
popContext,
|
||||||
latestContext,
|
latestContext,
|
||||||
|
dispatchCommand,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/** 创建游戏上下文实例 */
|
/** 创建游戏上下文实例 */
|
||||||
export function createGameContext(root: Context = { type: 'game' }) {
|
export function createGameContext(root: Context = { type: 'game' }) {
|
||||||
return new GameContext(root);
|
return new GameContext(root);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
177
src/core/rule.ts
177
src/core/rule.ts
|
|
@ -1,80 +1,129 @@
|
||||||
import {Context} from "./context";
|
import {Command, CommandSchema, parseCommand, parseCommandSchema, validateCommand} from "../utils/command";
|
||||||
import {Command} from "../utils/command";
|
|
||||||
import {effect} from "@preact/signals-core";
|
|
||||||
|
|
||||||
export type RuleContext<T> = Context & {
|
export type RuleState = 'running' | 'yielded' | 'waiting' | 'done';
|
||||||
actions: Command[];
|
|
||||||
handledActions: number;
|
export type RuleContext<T = unknown> = {
|
||||||
invocations: RuleContext<unknown>[];
|
type: string;
|
||||||
|
schema?: CommandSchema;
|
||||||
|
generator: Generator<string | CommandSchema, T, Command>;
|
||||||
|
parent?: RuleContext<unknown>;
|
||||||
|
children: RuleContext<unknown>[];
|
||||||
|
state: RuleState;
|
||||||
resolution?: T;
|
resolution?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export type RuleDef<T = unknown> = {
|
||||||
* 调用规则生成器并管理其上下文
|
schema: CommandSchema;
|
||||||
* @param pushContext - 用于推送上下文到上下文栈的函数
|
create: (cmd: Command) => Generator<string | CommandSchema, T, Command>;
|
||||||
* @param type - 规则类型
|
};
|
||||||
* @param rule - 规则生成器函数
|
|
||||||
* @returns 规则执行结果
|
export type RuleRegistry = Map<string, RuleDef<unknown>>;
|
||||||
*/
|
|
||||||
export function invokeRuleContext<T>(
|
export function createRule<T>(
|
||||||
pushContext: (context: Context) => void,
|
schemaStr: string,
|
||||||
type: string,
|
fn: (cmd: Command) => Generator<string | CommandSchema, T, Command>
|
||||||
rule: Generator<string, 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> {
|
): RuleContext<T> {
|
||||||
const ctx: RuleContext<T> = {
|
const ctx: RuleContext<T> = {
|
||||||
type,
|
type: ruleDef.schema.name,
|
||||||
actions: [],
|
schema: undefined,
|
||||||
handledActions: 0,
|
generator: ruleDef.create(command),
|
||||||
invocations: [],
|
parent,
|
||||||
|
children: [],
|
||||||
|
state: 'running',
|
||||||
resolution: undefined,
|
resolution: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let disposed = false;
|
if (parent) {
|
||||||
|
discardChildren(game, parent);
|
||||||
|
parent.children.push(ctx as RuleContext<unknown>);
|
||||||
|
parent.state = 'waiting';
|
||||||
|
}
|
||||||
|
|
||||||
const executeRule = () => {
|
pushContextToGame(game, ctx as RuleContext<unknown>);
|
||||||
if (disposed || ctx.resolution !== undefined) return;
|
|
||||||
|
|
||||||
try {
|
const result = ctx.generator.next();
|
||||||
const result = rule.next();
|
if (result.done) {
|
||||||
|
ctx.resolution = result.value;
|
||||||
if (result.done) {
|
ctx.state = 'done';
|
||||||
ctx.resolution = result.value;
|
} else {
|
||||||
return;
|
ctx.schema = parseYieldedSchema(result.value);
|
||||||
}
|
ctx.state = 'yielded';
|
||||||
|
}
|
||||||
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);
|
|
||||||
|
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export function dispatchCommand(game: GameContextLike, input: string): RuleContext<unknown> | undefined {
|
||||||
* 创建一个规则生成器辅助函数
|
const command = parseCommand(input);
|
||||||
* @param type - 规则类型
|
|
||||||
* @param fn - 规则逻辑函数
|
if (game.rules.has(command.name)) {
|
||||||
*/
|
const ruleDef = game.rules.get(command.name)!;
|
||||||
export function createRule<T>(
|
return invokeRule(game, command, ruleDef);
|
||||||
type: string,
|
}
|
||||||
fn: (ctx: RuleContext<T>) => Generator<string, T, Command>
|
|
||||||
): (ctx: RuleContext<T>) => Generator<string, T, Command> {
|
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
|
||||||
return fn;
|
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[] };
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@ export { flip, flipTo, roll } from './core/part';
|
||||||
export type { Region, RegionAxis } from './core/region';
|
export type { Region, RegionAxis } from './core/region';
|
||||||
export { applyAlign, shuffle } from './core/region';
|
export { applyAlign, shuffle } from './core/region';
|
||||||
|
|
||||||
export type { RuleContext } from './core/rule';
|
export type { RuleContext, RuleState, RuleDef, RuleRegistry } from './core/rule';
|
||||||
export { invokeRuleContext, createRule } from './core/rule';
|
export { createRule, dispatchCommand } from './core/rule';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';
|
export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue