refactor: improve rule handling
This commit is contained in:
parent
ff9d9bd9a1
commit
e06dc8ecba
|
|
@ -2,7 +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 {RuleDef, RuleRegistry, RuleContext, GameContextLike, dispatchCommand as dispatchRuleCommand} from "./rule";
|
import {RuleDef, RuleRegistry, RuleContext, RuleEngineHost, dispatchCommand as dispatchRuleCommand} from "./rule";
|
||||||
|
|
||||||
export type Context = {
|
export type Context = {
|
||||||
type: string;
|
type: string;
|
||||||
|
|
@ -37,7 +37,7 @@ export const GameContext = createModel((root: Context) => {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerRule(name: string, rule: RuleDef<unknown>) {
|
function registerRule(name: string, rule: RuleDef<unknown, RuleEngineHost>) {
|
||||||
const newRules = new Map(rules.value);
|
const newRules = new Map(rules.value);
|
||||||
newRules.set(name, rule);
|
newRules.set(name, rule);
|
||||||
rules.value = newRules;
|
rules.value = newRules;
|
||||||
|
|
@ -57,15 +57,18 @@ export const GameContext = createModel((root: Context) => {
|
||||||
ruleContexts.value = ruleContexts.value.filter(c => c !== ctx);
|
ruleContexts.value = ruleContexts.value.filter(c => c !== ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
function dispatchCommand(this: GameContextLike, input: string) {
|
function dispatchCommand(this: GameContextInstance, input: string) {
|
||||||
return dispatchRuleCommand({
|
return dispatchRuleCommand({
|
||||||
...this,
|
|
||||||
rules: rules.value,
|
rules: rules.value,
|
||||||
ruleContexts: ruleContexts.value,
|
ruleContexts: ruleContexts.value,
|
||||||
contexts,
|
|
||||||
addRuleContext,
|
addRuleContext,
|
||||||
removeRuleContext,
|
removeRuleContext,
|
||||||
}, input);
|
pushContext,
|
||||||
|
popContext,
|
||||||
|
latestContext,
|
||||||
|
parts,
|
||||||
|
regions,
|
||||||
|
} as any, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -83,7 +86,7 @@ export const GameContext = createModel((root: Context) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/** 创建游æˆ<EFBFBD>上下文实ä¾?*/
|
/** 创建游戏上下文实<EFBFBD>?*/
|
||||||
export function createGameContext(root: Context = { type: 'game' }) {
|
export function createGameContext(root: Context = { type: 'game' }) {
|
||||||
return new GameContext(root);
|
return new GameContext(root);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
295
src/core/rule.ts
295
src/core/rule.ts
|
|
@ -2,13 +2,9 @@ import {Command, CommandSchema, parseCommand, parseCommandSchema, applyCommandSc
|
||||||
|
|
||||||
export type RuleState = 'running' | 'yielded' | 'waiting' | 'invoking' | 'done';
|
export type RuleState = 'running' | 'yielded' | 'waiting' | 'invoking' | 'done';
|
||||||
|
|
||||||
export type InvokeYield = {
|
export type SchemaYield = { type: 'schema'; value: string | CommandSchema };
|
||||||
type: 'invoke';
|
export type InvokeYield = { type: 'invoke'; rule: string; command: Command };
|
||||||
rule: string;
|
export type RuleYield = SchemaYield | InvokeYield;
|
||||||
command: Command;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type RuleYield = string | CommandSchema | InvokeYield;
|
|
||||||
|
|
||||||
export type RuleContext<T = unknown> = {
|
export type RuleContext<T = unknown> = {
|
||||||
type: string;
|
type: string;
|
||||||
|
|
@ -20,27 +16,30 @@ export type RuleContext<T = unknown> = {
|
||||||
resolution?: T;
|
resolution?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RuleDef<T = unknown> = {
|
export type RuleDef<T = unknown, H extends RuleEngineHost = RuleEngineHost> = {
|
||||||
schema: CommandSchema;
|
schema: CommandSchema;
|
||||||
create: (this: GameContextLike, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>;
|
create: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RuleRegistry = Map<string, RuleDef<unknown>>;
|
export type RuleRegistry = Map<string, RuleDef<unknown, RuleEngineHost>>;
|
||||||
|
|
||||||
export function createRule<T>(
|
export type RuleEngineHost = {
|
||||||
|
rules: RuleRegistry;
|
||||||
|
ruleContexts: RuleContext<unknown>[];
|
||||||
|
addRuleContext: (ctx: RuleContext<unknown>) => void;
|
||||||
|
removeRuleContext: (ctx: RuleContext<unknown>) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createRule<T, H extends RuleEngineHost = RuleEngineHost>(
|
||||||
schemaStr: string,
|
schemaStr: string,
|
||||||
fn: (this: GameContextLike, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>
|
fn: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>
|
||||||
): RuleDef<T> {
|
): RuleDef<T, H> {
|
||||||
return {
|
return {
|
||||||
schema: parseCommandSchema(schemaStr, ''),
|
schema: parseCommandSchema(schemaStr, ''),
|
||||||
create: fn as RuleDef<T>['create'],
|
create: fn as RuleDef<T, H>['create'],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInvokeYield(value: RuleYield): value is InvokeYield {
|
|
||||||
return typeof value === 'object' && value !== null && 'type' in value && (value as InvokeYield).type === 'invoke';
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
|
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return parseCommandSchema(value, '');
|
return parseCommandSchema(value, '');
|
||||||
|
|
@ -48,31 +47,19 @@ function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseCommandWithSchema(command: Command, schema: CommandSchema): Command {
|
function addContextToHost(host: RuleEngineHost, ctx: RuleContext<unknown>) {
|
||||||
return applyCommandSchema(command, schema).command;
|
host.addRuleContext(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) {
|
function discardChildren(host: RuleEngineHost, parent: RuleContext<unknown>) {
|
||||||
game.contexts.value = [...game.contexts.value, { value: ctx } as any];
|
|
||||||
game.addRuleContext(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) {
|
|
||||||
for (const child of parent.children) {
|
for (const child of parent.children) {
|
||||||
game.removeRuleContext(child);
|
host.removeRuleContext(child);
|
||||||
|
|
||||||
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.children = [];
|
||||||
parent.state = 'yielded';
|
parent.state = 'yielded';
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateYieldedSchema(command: Command, schema: CommandSchema): boolean {
|
function commandMatchesSchema(command: Command, schema: CommandSchema): boolean {
|
||||||
const requiredParams = schema.params.filter(p => p.required);
|
const requiredParams = schema.params.filter(p => p.required);
|
||||||
const variadicParam = schema.params.find(p => p.variadic);
|
const variadicParam = schema.params.find(p => p.variadic);
|
||||||
|
|
||||||
|
|
@ -95,31 +82,93 @@ function validateYieldedSchema(command: Command, schema: CommandSchema): boolean
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function invokeChildRule(
|
function applySchemaToCommand(command: Command, schema: CommandSchema): Command {
|
||||||
game: GameContextLike,
|
return applyCommandSchema(command, schema).command;
|
||||||
ruleName: string,
|
}
|
||||||
|
|
||||||
|
function findYieldedContext(contexts: RuleContext<unknown>[]): RuleContext<unknown> | undefined {
|
||||||
|
for (let i = contexts.length - 1; i >= 0; i--) {
|
||||||
|
const ctx = contexts[i];
|
||||||
|
if (ctx.state === 'yielded') {
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContext<T>(
|
||||||
command: Command,
|
command: Command,
|
||||||
parent: RuleContext<unknown>
|
ruleDef: RuleDef<T>,
|
||||||
): RuleContext<unknown> {
|
host: RuleEngineHost,
|
||||||
const ruleDef = game.rules.get(ruleName)!;
|
parent?: RuleContext<unknown>
|
||||||
const ctx: RuleContext<unknown> = {
|
): RuleContext<T> {
|
||||||
|
return {
|
||||||
type: ruleDef.schema.name,
|
type: ruleDef.schema.name,
|
||||||
schema: undefined,
|
schema: undefined,
|
||||||
generator: ruleDef.create.call(game, command),
|
generator: ruleDef.create.call(host, command),
|
||||||
parent,
|
parent,
|
||||||
children: [],
|
children: [],
|
||||||
state: 'running',
|
state: 'running',
|
||||||
resolution: undefined,
|
resolution: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
parent.children.push(ctx);
|
|
||||||
pushContextToGame(game, ctx);
|
|
||||||
|
|
||||||
return stepGenerator(game, ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resumeInvokingParent(
|
function handleGeneratorResult<T>(
|
||||||
game: GameContextLike,
|
host: RuleEngineHost,
|
||||||
|
ctx: RuleContext<T>,
|
||||||
|
result: IteratorResult<RuleYield, T>
|
||||||
|
): RuleContext<unknown> | undefined {
|
||||||
|
if (result.done) {
|
||||||
|
ctx.resolution = result.value;
|
||||||
|
ctx.state = 'done';
|
||||||
|
return resumeParentAfterChildComplete(host, ctx as RuleContext<unknown>);
|
||||||
|
}
|
||||||
|
|
||||||
|
const yielded = result.value;
|
||||||
|
if (yielded.type === 'invoke') {
|
||||||
|
const childRuleDef = host.rules.get(yielded.rule);
|
||||||
|
if (childRuleDef) {
|
||||||
|
ctx.state = 'invoking';
|
||||||
|
return invokeChildRule(host, yielded.rule, yielded.command, ctx as RuleContext<unknown>);
|
||||||
|
} else {
|
||||||
|
ctx.schema = parseYieldedSchema({ name: '', params: [], options: [], flags: [] });
|
||||||
|
ctx.state = 'yielded';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.schema = parseYieldedSchema(yielded.value);
|
||||||
|
ctx.state = 'yielded';
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stepGenerator<T>(
|
||||||
|
host: RuleEngineHost,
|
||||||
|
ctx: RuleContext<T>
|
||||||
|
): RuleContext<T> {
|
||||||
|
const result = ctx.generator.next();
|
||||||
|
const resumed = handleGeneratorResult(host, ctx, result);
|
||||||
|
if (resumed) return resumed as RuleContext<T>;
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
function invokeChildRule<T>(
|
||||||
|
host: RuleEngineHost,
|
||||||
|
ruleName: string,
|
||||||
|
command: Command,
|
||||||
|
parent: RuleContext<unknown>
|
||||||
|
): RuleContext<T> {
|
||||||
|
const ruleDef = host.rules.get(ruleName)!;
|
||||||
|
const ctx = createContext(command, ruleDef, host, parent);
|
||||||
|
|
||||||
|
parent.children.push(ctx as RuleContext<unknown>);
|
||||||
|
addContextToHost(host, ctx as RuleContext<unknown>);
|
||||||
|
|
||||||
|
return stepGenerator(host, ctx) as RuleContext<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resumeParentAfterChildComplete(
|
||||||
|
host: RuleEngineHost,
|
||||||
childCtx: RuleContext<unknown>
|
childCtx: RuleContext<unknown>
|
||||||
): RuleContext<unknown> | undefined {
|
): RuleContext<unknown> | undefined {
|
||||||
const parent = childCtx.parent;
|
const parent = childCtx.parent;
|
||||||
|
|
@ -128,147 +177,57 @@ function resumeInvokingParent(
|
||||||
parent.children = parent.children.filter(c => c !== childCtx);
|
parent.children = parent.children.filter(c => c !== childCtx);
|
||||||
|
|
||||||
const result = parent.generator.next(childCtx);
|
const result = parent.generator.next(childCtx);
|
||||||
if (result.done) {
|
const resumed = handleGeneratorResult(host, parent, result);
|
||||||
(parent as RuleContext<unknown>).resolution = result.value;
|
if (resumed) return resumed;
|
||||||
(parent as RuleContext<unknown>).state = 'done';
|
|
||||||
const resumed = resumeInvokingParent(game, parent);
|
|
||||||
return resumed ?? parent;
|
|
||||||
} else if (isInvokeYield(result.value)) {
|
|
||||||
(parent as RuleContext<unknown>).state = 'invoking';
|
|
||||||
const childCtx2 = invokeChildRule(game, result.value.rule, result.value.command, parent);
|
|
||||||
return childCtx2;
|
|
||||||
} else {
|
|
||||||
(parent as RuleContext<unknown>).schema = parseYieldedSchema(result.value);
|
|
||||||
(parent as RuleContext<unknown>).state = 'yielded';
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
function stepGenerator<T>(
|
|
||||||
game: GameContextLike,
|
|
||||||
ctx: RuleContext<T>
|
|
||||||
): RuleContext<T> {
|
|
||||||
const result = ctx.generator.next();
|
|
||||||
|
|
||||||
if (result.done) {
|
|
||||||
ctx.resolution = result.value;
|
|
||||||
ctx.state = 'done';
|
|
||||||
const resumed = resumeInvokingParent(game, ctx as RuleContext<unknown>);
|
|
||||||
if (resumed) return resumed as RuleContext<T>;
|
|
||||||
} else if (isInvokeYield(result.value)) {
|
|
||||||
const childRuleDef = game.rules.get(result.value.rule);
|
|
||||||
if (childRuleDef) {
|
|
||||||
ctx.state = 'invoking';
|
|
||||||
const childCtx = invokeChildRule(game, result.value.rule, result.value.command, ctx as RuleContext<unknown>);
|
|
||||||
return childCtx as RuleContext<T>;
|
|
||||||
} else {
|
|
||||||
ctx.schema = parseYieldedSchema('');
|
|
||||||
ctx.state = 'yielded';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.schema = parseYieldedSchema(result.value);
|
|
||||||
ctx.state = 'yielded';
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
function invokeRule<T>(
|
function invokeRule<T>(
|
||||||
game: GameContextLike,
|
host: RuleEngineHost,
|
||||||
command: Command,
|
command: Command,
|
||||||
ruleDef: RuleDef<T>,
|
ruleDef: RuleDef<T>,
|
||||||
parent?: RuleContext<unknown>
|
parent?: RuleContext<unknown>
|
||||||
): RuleContext<T> {
|
): RuleContext<T> {
|
||||||
const ctx: RuleContext<T> = {
|
const ctx = createContext(command, ruleDef, host, parent);
|
||||||
type: ruleDef.schema.name,
|
|
||||||
schema: undefined,
|
|
||||||
generator: ruleDef.create.call(game, command),
|
|
||||||
parent,
|
|
||||||
children: [],
|
|
||||||
state: 'running',
|
|
||||||
resolution: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (parent) {
|
if (parent) {
|
||||||
discardChildren(game, parent);
|
discardChildren(host, parent);
|
||||||
parent.children.push(ctx as RuleContext<unknown>);
|
parent.children.push(ctx as RuleContext<unknown>);
|
||||||
parent.state = 'waiting';
|
parent.state = 'waiting';
|
||||||
}
|
}
|
||||||
|
|
||||||
pushContextToGame(game, ctx as RuleContext<unknown>);
|
addContextToHost(host, ctx as RuleContext<unknown>);
|
||||||
|
|
||||||
return stepGenerator(game, ctx);
|
return stepGenerator(host, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function dispatchCommand(game: GameContextLike, input: string): RuleContext<unknown> | undefined {
|
function feedYieldedContext(
|
||||||
|
host: RuleEngineHost,
|
||||||
|
ctx: RuleContext<unknown>,
|
||||||
|
command: Command
|
||||||
|
): RuleContext<unknown> {
|
||||||
|
const typedCommand = applySchemaToCommand(command, ctx.schema!);
|
||||||
|
const result = ctx.generator.next(typedCommand);
|
||||||
|
const resumed = handleGeneratorResult(host, ctx, result);
|
||||||
|
return resumed ?? ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dispatchCommand(host: RuleEngineHost, input: string): RuleContext<unknown> | undefined {
|
||||||
const command = parseCommand(input);
|
const command = parseCommand(input);
|
||||||
|
|
||||||
if (game.rules.has(command.name)) {
|
const matchedRule = host.rules.get(command.name);
|
||||||
const ruleDef = game.rules.get(command.name)!;
|
if (matchedRule) {
|
||||||
const typedCommand = parseCommandWithSchema(command, ruleDef.schema);
|
const typedCommand = applySchemaToCommand(command, matchedRule.schema);
|
||||||
|
const parent = findYieldedContext(host.ruleContexts);
|
||||||
const parent = findYieldedParent(game);
|
return invokeRule(host, typedCommand, matchedRule, parent);
|
||||||
|
|
||||||
return invokeRule(game, typedCommand, ruleDef, parent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
|
for (let i = host.ruleContexts.length - 1; i >= 0; i--) {
|
||||||
const ctx = game.ruleContexts[i];
|
const ctx = host.ruleContexts[i];
|
||||||
if (ctx.state === 'yielded' && ctx.schema) {
|
if (ctx.state === 'yielded' && ctx.schema && commandMatchesSchema(command, ctx.schema)) {
|
||||||
if (validateYieldedSchema(command, ctx.schema)) {
|
return feedYieldedContext(host, ctx, command);
|
||||||
const typedCommand = parseCommandWithSchema(command, ctx.schema);
|
|
||||||
const result = ctx.generator.next(typedCommand);
|
|
||||||
if (result.done) {
|
|
||||||
ctx.resolution = result.value;
|
|
||||||
ctx.state = 'done';
|
|
||||||
const resumed = resumeInvokingParent(game, ctx);
|
|
||||||
return resumed ?? ctx;
|
|
||||||
} else if (isInvokeYield(result.value)) {
|
|
||||||
ctx.state = 'invoking';
|
|
||||||
const childCtx = invokeChildRule(game, result.value.rule, result.value.command, ctx);
|
|
||||||
return childCtx;
|
|
||||||
} else {
|
|
||||||
ctx.schema = parseYieldedSchema(result.value);
|
|
||||||
ctx.state = 'yielded';
|
|
||||||
}
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findYieldedParent(game: GameContextLike): RuleContext<unknown> | undefined {
|
|
||||||
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
|
|
||||||
const ctx = game.ruleContexts[i];
|
|
||||||
if (ctx.state === 'yielded') {
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type GameContextLike = {
|
|
||||||
rules: RuleRegistry;
|
|
||||||
ruleContexts: RuleContext<unknown>[];
|
|
||||||
contexts: { value: any[] };
|
|
||||||
addRuleContext: (ctx: RuleContext<unknown>) => void;
|
|
||||||
removeRuleContext: (ctx: RuleContext<unknown>) => void;
|
|
||||||
parts: {
|
|
||||||
collection: { value: Record<string, any> };
|
|
||||||
add: (...entities: any[]) => void;
|
|
||||||
remove: (...ids: string[]) => void;
|
|
||||||
get: (id: string) => any;
|
|
||||||
};
|
|
||||||
regions: {
|
|
||||||
collection: { value: Record<string, any> };
|
|
||||||
add: (...entities: any[]) => void;
|
|
||||||
remove: (...ids: string[]) => void;
|
|
||||||
get: (id: string) => any;
|
|
||||||
};
|
|
||||||
pushContext: (context: any) => any;
|
|
||||||
popContext: () => void;
|
|
||||||
latestContext: <T>(type: string) => any | undefined;
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { GameContextInstance } from '../core/context';
|
import { GameContextInstance } from '../core/context';
|
||||||
import type { GameContextLike, RuleContext } from '../core/rule';
|
import type { RuleEngineHost, RuleContext } from '../core/rule';
|
||||||
import { createRule, type InvokeYield, type RuleYield } from '../core/rule';
|
import { createRule, type InvokeYield, type SchemaYield } from '../core/rule';
|
||||||
import type { Command } from '../utils/command';
|
import type { Command } from '../utils/command';
|
||||||
import type { Part } from '../core/part';
|
import type { Part } from '../core/part';
|
||||||
import type { Region } from '../core/region';
|
import type { Region } from '../core/region';
|
||||||
|
|
@ -17,19 +17,26 @@ type TurnResult = {
|
||||||
winner: 'X' | 'O' | 'draw' | null;
|
winner: 'X' | 'O' | 'draw' | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getBoardRegion(game: GameContextLike) {
|
type TicTacToeHost = RuleEngineHost & {
|
||||||
return game.regions.get('board');
|
pushContext: (context: Context) => any;
|
||||||
|
latestContext: <T>(type: string) => { value: T } | undefined;
|
||||||
|
regions: { add: (...entities: any[]) => void; get: (id: string) => { value: { children: any[] } } };
|
||||||
|
parts: { add: (...entities: any[]) => void; get: (id: string) => any; collection: { value: Record<string, { value: Part }> } };
|
||||||
|
};
|
||||||
|
|
||||||
|
function getBoardRegion(host: TicTacToeHost) {
|
||||||
|
return host.regions.get('board');
|
||||||
}
|
}
|
||||||
|
|
||||||
function isCellOccupied(game: GameContextLike, row: number, col: number): boolean {
|
function isCellOccupied(host: TicTacToeHost, row: number, col: number): boolean {
|
||||||
const board = getBoardRegion(game);
|
const board = getBoardRegion(host);
|
||||||
return board.value.children.some(
|
return board.value.children.some(
|
||||||
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
|
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkWinner(game: GameContextLike): 'X' | 'O' | 'draw' | null {
|
function checkWinner(host: TicTacToeHost): 'X' | 'O' | 'draw' | null {
|
||||||
const parts = Object.values(game.parts.collection.value).map((s: { value: Part }) => s.value);
|
const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value);
|
||||||
|
|
||||||
const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position);
|
const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position);
|
||||||
const oPositions = parts.filter((_: Part, i: number) => i % 2 === 1).map((p: Part) => p.position);
|
const oPositions = parts.filter((_: Part, i: number) => i % 2 === 1).map((p: Part) => p.position);
|
||||||
|
|
@ -59,8 +66,8 @@ function hasWinningLine(positions: number[][]): boolean {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function placePiece(game: GameContextLike, row: number, col: number, moveCount: number) {
|
function placePiece(host: TicTacToeHost, row: number, col: number, moveCount: number) {
|
||||||
const board = getBoardRegion(game);
|
const board = getBoardRegion(host);
|
||||||
const piece: Part = {
|
const piece: Part = {
|
||||||
id: `piece-${moveCount}`,
|
id: `piece-${moveCount}`,
|
||||||
sides: 1,
|
sides: 1,
|
||||||
|
|
@ -68,14 +75,14 @@ function placePiece(game: GameContextLike, row: number, col: number, moveCount:
|
||||||
region: board,
|
region: board,
|
||||||
position: [row, col],
|
position: [row, col],
|
||||||
};
|
};
|
||||||
game.parts.add(piece);
|
host.parts.add(piece);
|
||||||
board.value.children.push(game.parts.get(piece.id));
|
board.value.children.push(host.parts.get(piece.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
const playSchema = 'play <player> <row:number> <col:number>';
|
const playSchema: SchemaYield = { type: 'schema', value: 'play <player> <row:number> <col:number>' };
|
||||||
|
|
||||||
export function createSetupRule() {
|
export function createSetupRule() {
|
||||||
return createRule('start', function*() {
|
return createRule('start', function*(this: TicTacToeHost) {
|
||||||
this.pushContext({
|
this.pushContext({
|
||||||
type: 'tic-tac-toe',
|
type: 'tic-tac-toe',
|
||||||
currentPlayer: 'X',
|
currentPlayer: 'X',
|
||||||
|
|
@ -101,7 +108,7 @@ export function createSetupRule() {
|
||||||
rule: 'turn',
|
rule: 'turn',
|
||||||
command: { name: 'turn', params: [currentPlayer], flags: {}, options: {} } as Command,
|
command: { name: 'turn', params: [currentPlayer], flags: {}, options: {} } as Command,
|
||||||
};
|
};
|
||||||
const ctx = yield yieldValue as RuleYield;
|
const ctx = yield yieldValue;
|
||||||
turnResult = (ctx as RuleContext<TurnResult>).resolution;
|
turnResult = (ctx as RuleContext<TurnResult>).resolution;
|
||||||
if (turnResult?.winner) break;
|
if (turnResult?.winner) break;
|
||||||
|
|
||||||
|
|
@ -117,7 +124,7 @@ export function createSetupRule() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createTurnRule() {
|
export function createTurnRule() {
|
||||||
return createRule('turn <player>', function*(cmd) {
|
return createRule('turn <player>', function*(this: TicTacToeHost, cmd) {
|
||||||
while (true) {
|
while (true) {
|
||||||
const received = yield playSchema;
|
const received = yield playSchema;
|
||||||
if ('resolution' in received) continue;
|
if ('resolution' in received) continue;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { createRule, type RuleContext, type GameContextLike } from '../../src/core/rule';
|
import { createRule, type RuleContext, type RuleEngineHost } from '../../src/core/rule';
|
||||||
import { createGameContext } from '../../src/core/context';
|
import { createGameContext } from '../../src/core/context';
|
||||||
import type { Command } from '../../src/utils/command';
|
import type { Command } from '../../src/utils/command';
|
||||||
|
|
||||||
|
|
@ -7,6 +7,10 @@ function isCommand(value: Command | RuleContext<unknown>): value is Command {
|
||||||
return 'name' in value;
|
return 'name' in value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function schema(value: string | { name: string; params: any[]; options: any[]; flags: any[] }) {
|
||||||
|
return { type: 'schema' as const, value };
|
||||||
|
}
|
||||||
|
|
||||||
describe('Rule System', () => {
|
describe('Rule System', () => {
|
||||||
function createTestGame() {
|
function createTestGame() {
|
||||||
const game = createGameContext();
|
const game = createGameContext();
|
||||||
|
|
@ -34,7 +38,7 @@ describe('Rule System', () => {
|
||||||
return cmd.params[0];
|
return cmd.params[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
const gen = rule.create.call(game as unknown as GameContextLike, { name: 'test', params: ['card1'], flags: {}, options: {} });
|
const gen = rule.create.call(game as unknown as RuleEngineHost, { name: 'test', params: ['card1'], flags: {}, options: {} });
|
||||||
const result = gen.next();
|
const result = gen.next();
|
||||||
expect(result.done).toBe(true);
|
expect(result.done).toBe(true);
|
||||||
expect(result.value).toBe('card1');
|
expect(result.value).toBe('card1');
|
||||||
|
|
@ -46,7 +50,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
return { moved: cmd.params[0] };
|
return { moved: cmd.params[0] };
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -62,7 +66,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||||
const confirm = yield { name: '', params: [], options: [], flags: [] };
|
const confirm = yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
const confirmCmd = isCommand(confirm) ? confirm : undefined;
|
const confirmCmd = isCommand(confirm) ? confirm : undefined;
|
||||||
return { moved: cmd.params[0], confirmed: confirmCmd?.name === 'confirm' };
|
return { moved: cmd.params[0], confirmed: confirmCmd?.name === 'confirm' };
|
||||||
}));
|
}));
|
||||||
|
|
@ -115,7 +119,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
return { moved: cmd.params[0] };
|
return { moved: cmd.params[0] };
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -138,7 +142,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||||
const response = yield { name: '', params: [], options: [], flags: [] };
|
const response = yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
const rcmd = isCommand(response) ? response : undefined;
|
const rcmd = isCommand(response) ? response : undefined;
|
||||||
return { moved: cmd.params[0], response: rcmd?.name };
|
return { moved: cmd.params[0], response: rcmd?.name };
|
||||||
}));
|
}));
|
||||||
|
|
@ -154,7 +158,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||||
const response = yield '<item>';
|
const response = yield schema('<item>');
|
||||||
const rcmd = isCommand(response) ? response : undefined;
|
const rcmd = isCommand(response) ? response : undefined;
|
||||||
return { response: rcmd?.params[0] };
|
return { response: rcmd?.params[0] };
|
||||||
}));
|
}));
|
||||||
|
|
@ -170,7 +174,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
|
game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
|
||||||
const response = yield '<item> [amount: number]';
|
const response = yield schema('<item> [amount: number]');
|
||||||
const rcmd = isCommand(response) ? response : undefined;
|
const rcmd = isCommand(response) ? response : undefined;
|
||||||
return { traded: rcmd?.params[0] };
|
return { traded: rcmd?.params[0] };
|
||||||
}));
|
}));
|
||||||
|
|
@ -188,12 +192,12 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('parent', createRule('<action>', function*() {
|
game.registerRule('parent', createRule('<action>', function*() {
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
return 'parent done';
|
return 'parent done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
game.registerRule('child', createRule('<target>', function*() {
|
game.registerRule('child', createRule('<target>', function*() {
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
return 'child done';
|
return 'child done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -212,7 +216,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('parent', createRule('<action>', function*() {
|
game.registerRule('parent', createRule('<action>', function*() {
|
||||||
yield 'child_cmd';
|
yield schema('child_cmd');
|
||||||
return 'parent done';
|
return 'parent done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -236,7 +240,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('parent', createRule('<action>', function*() {
|
game.registerRule('parent', createRule('<action>', function*() {
|
||||||
yield 'child_a | child_b';
|
yield schema('child_a | child_b');
|
||||||
return 'parent done';
|
return 'parent done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -270,7 +274,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('test', createRule('<arg>', function*() {
|
game.registerRule('test', createRule('<arg>', function*() {
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
return 'done';
|
return 'done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -281,21 +285,6 @@ describe('Rule System', () => {
|
||||||
expect(game.ruleContexts.value.length).toBe(1);
|
expect(game.ruleContexts.value.length).toBe(1);
|
||||||
expect(game.ruleContexts.value[0].state).toBe('yielded');
|
expect(game.ruleContexts.value[0].state).toBe('yielded');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add context to the context stack', () => {
|
|
||||||
const game = createTestGame();
|
|
||||||
|
|
||||||
game.registerRule('test', createRule('<arg>', function*() {
|
|
||||||
yield { name: '', params: [], options: [], flags: [] };
|
|
||||||
return 'done';
|
|
||||||
}));
|
|
||||||
|
|
||||||
const initialStackLength = game.contexts.value.length;
|
|
||||||
|
|
||||||
game.dispatchCommand('test arg1');
|
|
||||||
|
|
||||||
expect(game.contexts.value.length).toBe(initialStackLength + 1);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('error handling', () => {
|
describe('error handling', () => {
|
||||||
|
|
@ -315,7 +304,7 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('parent', createRule('<action>', function*() {
|
game.registerRule('parent', createRule('<action>', function*() {
|
||||||
yield 'child';
|
yield schema('child');
|
||||||
return 'parent done';
|
return 'parent done';
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
@ -342,7 +331,7 @@ describe('Rule System', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
game.registerRule('test', createRule('<arg>', function*() {
|
game.registerRule('test', createRule('<arg>', function*() {
|
||||||
const cmd = yield customSchema;
|
const cmd = yield schema(customSchema);
|
||||||
const rcmd = isCommand(cmd) ? cmd : undefined;
|
const rcmd = isCommand(cmd) ? cmd : undefined;
|
||||||
return { received: rcmd?.params[0] };
|
return { received: rcmd?.params[0] };
|
||||||
}));
|
}));
|
||||||
|
|
@ -358,8 +347,8 @@ describe('Rule System', () => {
|
||||||
const game = createTestGame();
|
const game = createTestGame();
|
||||||
|
|
||||||
game.registerRule('multi', createRule('<start>', function*() {
|
game.registerRule('multi', createRule('<start>', function*() {
|
||||||
const a = yield '<value>';
|
const a = yield schema('<value>');
|
||||||
const b = yield '<value>';
|
const b = yield schema('<value>');
|
||||||
const acmd = isCommand(a) ? a : undefined;
|
const acmd = isCommand(a) ? a : undefined;
|
||||||
const bcmd = isCommand(b) ? b : undefined;
|
const bcmd = isCommand(b) ? b : undefined;
|
||||||
return { a: acmd?.params[0], b: bcmd?.params[0] };
|
return { a: acmd?.params[0], b: bcmd?.params[0] };
|
||||||
|
|
@ -380,13 +369,13 @@ describe('Rule System', () => {
|
||||||
|
|
||||||
game.registerRule('start', createRule('<player>', function*(cmd) {
|
game.registerRule('start', createRule('<player>', function*(cmd) {
|
||||||
const player = cmd.params[0];
|
const player = cmd.params[0];
|
||||||
const action = yield { name: '', params: [], options: [], flags: [] };
|
const action = yield schema({ name: '', params: [], options: [], flags: [] });
|
||||||
|
|
||||||
if (isCommand(action)) {
|
if (isCommand(action)) {
|
||||||
if (action.name === 'move') {
|
if (action.name === 'move') {
|
||||||
yield '<target>';
|
yield schema('<target>');
|
||||||
} else if (action.name === 'attack') {
|
} else if (action.name === 'attack') {
|
||||||
yield '<target> [--power: number]';
|
yield schema('<target> [--power: number]');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue