Compare commits
20
Commits
0d7a84edee
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ed23295ae | ||
|
|
ca86bfc427 | ||
|
|
09c3fc443b | ||
|
|
a0b2003c65 | ||
|
|
03db1e8a06 | ||
|
|
20993d3b72 | ||
|
|
59391c74d9 | ||
|
|
fb3c98b1ef | ||
|
|
5c1ef232fd | ||
|
|
f5df1c26dc | ||
|
|
2014162819 | ||
|
|
d932c2f5e2 | ||
|
|
1bd49c32e0 | ||
|
|
b009189b9a | ||
|
|
59b99d2042 | ||
|
|
32408c3cee | ||
|
|
23ac09ff21 | ||
|
|
e8c995f74f | ||
|
|
bbab3cc43b | ||
|
|
2e2ddebec4 |
+1
-1
@@ -18,7 +18,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsup",
|
"build": "tsup",
|
||||||
"build:samples": "tsup --config tsup.samples.config.ts",
|
"build:samples": "tsup --config tsup.samples.config.ts",
|
||||||
"prepare": "npm run build",
|
"prepare": "npm run build && npm run build:samples",
|
||||||
"test": "vitest",
|
"test": "vitest",
|
||||||
"test:run": "vitest run",
|
"test:run": "vitest run",
|
||||||
"typecheck": "tsc --noEmit"
|
"typecheck": "tsc --noEmit"
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
import { createPromptContext, PromptDef } from "@/utils/command";
|
||||||
|
import { computed, effect, signal } from "@preact/signals-core";
|
||||||
|
import { createGameContext, IGameContext } from "./game";
|
||||||
|
|
||||||
|
export enum ClientStatus {
|
||||||
|
Idle = "idle",
|
||||||
|
Running = "running",
|
||||||
|
Disposed = "disposed",
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PromptInfo =
|
||||||
|
| {
|
||||||
|
hasPrompt: false;
|
||||||
|
player: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
hasPrompt: true;
|
||||||
|
player: string;
|
||||||
|
def: PromptDef<unknown[]>;
|
||||||
|
tryAnswer(...args: unknown[]): string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1. create a client instance
|
||||||
|
* 2. setStatus("running")
|
||||||
|
* 3. select("selector", t => {}) for c# to capture game state
|
||||||
|
* 4. useClient("status" | "prompt") for c# to handle prompt / game status
|
||||||
|
*/
|
||||||
|
export abstract class BaseGameClient<
|
||||||
|
T extends Record<string, unknown> = Record<string, unknown>,
|
||||||
|
R = unknown,
|
||||||
|
> {
|
||||||
|
protected _initialState: T;
|
||||||
|
protected _context: IGameContext<T>;
|
||||||
|
protected _prompts = createPromptContext();
|
||||||
|
protected _status = signal(ClientStatus.Idle);
|
||||||
|
protected _result = signal(null as null | R);
|
||||||
|
public get state() {
|
||||||
|
return this._context.value;
|
||||||
|
}
|
||||||
|
constructor(is: T) {
|
||||||
|
this._initialState = is;
|
||||||
|
this._context = createGameContext(this._prompts, is);
|
||||||
|
}
|
||||||
|
|
||||||
|
async setStatus(status: ClientStatus) {
|
||||||
|
this._status.value = status;
|
||||||
|
if (status === ClientStatus.Disposed) {
|
||||||
|
this._prompts.reset();
|
||||||
|
} else if (status === ClientStatus.Running) {
|
||||||
|
this._prompts.reset();
|
||||||
|
this._context._state.value = this._initialState;
|
||||||
|
this._result.value = null;
|
||||||
|
this._result.value = await this.start(this._context);
|
||||||
|
this.setStatus(ClientStatus.Idle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract start(ctx: IGameContext<T>): Promise<R>;
|
||||||
|
|
||||||
|
assignSelector<S>(selector: (t: T) => S, callback: (s: S) => void) {
|
||||||
|
const selected = computed(() => selector(this._context.value));
|
||||||
|
return effect(() => {
|
||||||
|
callback(selected.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addInterruption() {
|
||||||
|
let resolve!: () => void;
|
||||||
|
const promise = new Promise<void>((resolver) => {
|
||||||
|
resolve = resolver;
|
||||||
|
});
|
||||||
|
this._context._state.addInterruption(promise);
|
||||||
|
return resolve;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectPrompt(callback: (prompt: PromptInfo) => void) {
|
||||||
|
return this._prompts.handleCall.use(async (ctx, next) => {
|
||||||
|
function tryAnswer(...args: any[]) {
|
||||||
|
try {
|
||||||
|
const res = ctx.validator(...args);
|
||||||
|
ctx.resolve(res);
|
||||||
|
return null;
|
||||||
|
} catch (reason) {
|
||||||
|
if (typeof reason === "string") {
|
||||||
|
return reason;
|
||||||
|
}
|
||||||
|
throw reason;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
callback({
|
||||||
|
hasPrompt: true,
|
||||||
|
player: ctx.player || "global",
|
||||||
|
def: ctx.def,
|
||||||
|
tryAnswer,
|
||||||
|
});
|
||||||
|
return await next();
|
||||||
|
} finally {
|
||||||
|
callback({
|
||||||
|
hasPrompt: false,
|
||||||
|
player: ctx.player || "global",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
selectStatus(callback: (status: ClientStatus) => void) {
|
||||||
|
return effect(() => {
|
||||||
|
callback(this._status.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
selectResult(callback: (status: R | null) => void) {
|
||||||
|
return effect(() => {
|
||||||
|
callback(this._result.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-80
@@ -1,99 +1,39 @@
|
|||||||
|
import { createPromptContext } from "@/utils/command";
|
||||||
|
import { PromptContext } from "@/utils/command/command-prompt";
|
||||||
import { ReadonlySignal, Signal } from "@preact/signals-core";
|
import { ReadonlySignal, Signal } from "@preact/signals-core";
|
||||||
import { CommandSchema, CommandRegistry, PromptEvent } from "@/utils/command";
|
import { createGameContext, IGameContext } from "./game";
|
||||||
import {
|
|
||||||
createGameCommandRegistry,
|
|
||||||
createGameContext,
|
|
||||||
IGameContext,
|
|
||||||
PromptDef,
|
|
||||||
} from "./game";
|
|
||||||
|
|
||||||
export type GameHostStatus = "created" | "running" | "disposed";
|
export type GameHostStatus = "created" | "running" | "disposed";
|
||||||
|
|
||||||
export class GameHost<
|
export class GameHost<
|
||||||
TState extends Record<string, unknown>,
|
TState extends Record<string, unknown>,
|
||||||
TResult = unknown,
|
TResult = unknown,
|
||||||
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
> {
|
> {
|
||||||
readonly state: ReadonlySignal<TState>;
|
readonly state: ReadonlySignal<TState>;
|
||||||
readonly status: ReadonlySignal<GameHostStatus>;
|
readonly status: ReadonlySignal<GameHostStatus>;
|
||||||
readonly activePromptSchema: ReadonlySignal<CommandSchema | null>;
|
readonly prompts: PromptContext;
|
||||||
readonly activePromptPlayer: ReadonlySignal<string | null>;
|
|
||||||
readonly activePromptHint: ReadonlySignal<string | null>;
|
|
||||||
|
|
||||||
private _context: IGameContext<TState>;
|
private _context: IGameContext<TState>;
|
||||||
private _start: (ctx: IGameContext<TState>) => Promise<TResult>;
|
private _start: (ctx: IGameContext<TState>) => Promise<TResult>;
|
||||||
private _status: Signal<GameHostStatus>;
|
private _status: Signal<GameHostStatus>;
|
||||||
private _activePromptSchema: Signal<CommandSchema | null>;
|
|
||||||
private _activePromptPlayer: Signal<string | null>;
|
|
||||||
private _activePromptHint: Signal<string | null>;
|
|
||||||
private _createInitialState: () => TState;
|
private _createInitialState: () => TState;
|
||||||
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
||||||
private _isDisposed = false;
|
private _isDisposed = false;
|
||||||
|
|
||||||
constructor(
|
constructor(public readonly gameModule: TModule) {
|
||||||
registry: CommandRegistry<IGameContext<TState>>,
|
const { createInitialState, start } = gameModule;
|
||||||
createInitialState: () => TState,
|
|
||||||
start: (ctx: IGameContext<TState>) => Promise<TResult>,
|
|
||||||
) {
|
|
||||||
this._createInitialState = createInitialState;
|
this._createInitialState = createInitialState;
|
||||||
this._eventListeners = new Map();
|
this._eventListeners = new Map();
|
||||||
|
|
||||||
|
this.prompts = createPromptContext();
|
||||||
const initialState = createInitialState();
|
const initialState = createInitialState();
|
||||||
this._context = createGameContext(registry, initialState);
|
this._context = createGameContext(this.prompts, initialState);
|
||||||
this._start = start;
|
this._start = start;
|
||||||
this.state = this._context._state;
|
this.state = this._context._state;
|
||||||
|
|
||||||
this._status = new Signal<GameHostStatus>("created");
|
this._status = new Signal<GameHostStatus>("created");
|
||||||
this.status = this._status;
|
this.status = this._status;
|
||||||
|
|
||||||
this._activePromptSchema = new Signal<CommandSchema | null>(null);
|
|
||||||
this.activePromptSchema = this._activePromptSchema;
|
|
||||||
|
|
||||||
this._activePromptPlayer = new Signal<string | null>(null);
|
|
||||||
this.activePromptPlayer = this._activePromptPlayer;
|
|
||||||
|
|
||||||
this._activePromptHint = new Signal<string | null>(null);
|
|
||||||
this.activePromptHint = this._activePromptHint;
|
|
||||||
|
|
||||||
this._setupPromptTracking();
|
|
||||||
}
|
|
||||||
|
|
||||||
private _setupPromptTracking() {
|
|
||||||
let currentPromptEvent: PromptEvent | null = null;
|
|
||||||
|
|
||||||
this._context._commands.on("prompt", (e) => {
|
|
||||||
currentPromptEvent = e as PromptEvent;
|
|
||||||
this._activePromptSchema.value = currentPromptEvent.schema;
|
|
||||||
this._activePromptPlayer.value = currentPromptEvent.currentPlayer;
|
|
||||||
this._activePromptHint.value = currentPromptEvent.hintText || null;
|
|
||||||
});
|
|
||||||
|
|
||||||
this._context._commands.on("promptEnd", () => {
|
|
||||||
currentPromptEvent = null;
|
|
||||||
this._activePromptSchema.value = null;
|
|
||||||
this._activePromptPlayer.value = null;
|
|
||||||
this._activePromptHint.value = null;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Initial state
|
|
||||||
this._activePromptSchema.value = null;
|
|
||||||
this._activePromptPlayer.value = null;
|
|
||||||
this._activePromptHint.value = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
tryInput(input: string): string | null {
|
|
||||||
if (this._isDisposed) {
|
|
||||||
return "GameHost is disposed";
|
|
||||||
}
|
|
||||||
return this._context._commands._tryCommit(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
tryAnswerPrompt<TArgs extends any[]>(def: PromptDef<TArgs>, ...args: TArgs) {
|
|
||||||
return this._context._commands._tryCommit({
|
|
||||||
name: def.schema.name,
|
|
||||||
params: args,
|
|
||||||
options: {},
|
|
||||||
flags: {},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,7 +57,7 @@ export class GameHost<
|
|||||||
throw new Error("GameHost is disposed");
|
throw new Error("GameHost is disposed");
|
||||||
}
|
}
|
||||||
|
|
||||||
this._context._commands._cancel();
|
this.prompts.reset();
|
||||||
|
|
||||||
const initialState = this._createInitialState();
|
const initialState = this._createInitialState();
|
||||||
this._context._state.value = initialState as any;
|
this._context._state.value = initialState as any;
|
||||||
@@ -138,7 +78,7 @@ export class GameHost<
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._isDisposed = true;
|
this._isDisposed = true;
|
||||||
this._context._commands._cancel();
|
this.prompts.reset();
|
||||||
this._status.value = "disposed";
|
this._status.value = "disposed";
|
||||||
|
|
||||||
// Emit dispose event BEFORE clearing listeners
|
// Emit dispose event BEFORE clearing listeners
|
||||||
@@ -168,21 +108,17 @@ export class GameHost<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type GameModule<
|
export type GameModule<
|
||||||
TState extends Record<string, unknown>,
|
TState extends Record<string, unknown> = Record<string, unknown>,
|
||||||
TResult = unknown,
|
TResult = unknown,
|
||||||
> = {
|
> = {
|
||||||
registry?: CommandRegistry<IGameContext<TState>>;
|
|
||||||
createInitialState: () => TState;
|
createInitialState: () => TState;
|
||||||
start: (ctx: IGameContext<TState>) => Promise<TResult>;
|
start: (ctx: IGameContext<TState>) => Promise<TResult>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createGameHost<
|
export function createGameHost<
|
||||||
TState extends Record<string, unknown>,
|
TState extends Record<string, unknown> = Record<string, unknown>,
|
||||||
TResult = unknown,
|
TResult = unknown,
|
||||||
>(gameModule: GameModule<TState, TResult>): GameHost<TState, TResult> {
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
return new GameHost(
|
>(gameModule: TModule): GameHost<TState, TResult, TModule> {
|
||||||
gameModule.registry || createGameCommandRegistry(),
|
return new GameHost(gameModule);
|
||||||
gameModule.createInitialState,
|
|
||||||
gameModule.start,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-46
@@ -1,40 +1,29 @@
|
|||||||
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
||||||
import {
|
import { CommandSchema, parseCommandSchema, PromptDef } from "@/utils/command";
|
||||||
Command,
|
|
||||||
CommandRegistry,
|
|
||||||
CommandResult,
|
|
||||||
CommandRunnerContextExport,
|
|
||||||
CommandSchema,
|
|
||||||
createCommandRegistry,
|
|
||||||
createCommandRunnerContext,
|
|
||||||
parseCommandSchema,
|
|
||||||
} from "@/utils/command";
|
|
||||||
import { PromptValidator } from "@/utils/command/command-runner";
|
import { PromptValidator } from "@/utils/command/command-runner";
|
||||||
import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
|
import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
|
||||||
|
import { PromptContext } from "@/utils/command/command-prompt";
|
||||||
|
|
||||||
export interface IGameContext<TState extends Record<string, unknown> = {}> {
|
export interface IGameContext<TState extends Record<string, unknown> = {}> {
|
||||||
get value(): TState;
|
get value(): TState;
|
||||||
get rng(): ReadonlyRNG;
|
get rng(): ReadonlyRNG;
|
||||||
produce(fn: (draft: TState) => undefined): void;
|
produce(fn: (draft: TState) => undefined): void;
|
||||||
produceAsync(fn: (draft: TState) => undefined): Promise<void>;
|
produceAsync(fn: (draft: TState) => undefined): Promise<void>;
|
||||||
run<T>(input: string): Promise<CommandResult<T>>;
|
|
||||||
runParsed<T>(command: Command): Promise<CommandResult<T>>;
|
|
||||||
prompt: <TResult, TArgs extends any[] = any[]>(
|
prompt: <TResult, TArgs extends any[] = any[]>(
|
||||||
def: PromptDef<TArgs>,
|
def: PromptDef<TArgs>,
|
||||||
validator: PromptValidator<TResult, TArgs>,
|
validator: PromptValidator<TResult, TArgs>,
|
||||||
currentPlayer?: string | null,
|
player?: string,
|
||||||
) => Promise<TResult>;
|
) => Promise<TResult>;
|
||||||
|
|
||||||
// test only
|
// test only
|
||||||
_state: MutableSignal<TState>;
|
_state: MutableSignal<TState>;
|
||||||
_commands: CommandRunnerContextExport<IGameContext<TState>>;
|
|
||||||
_rng: RNG;
|
_rng: RNG;
|
||||||
}
|
}
|
||||||
export type IGameContextExport<TState extends Record<string, unknown> = {}> =
|
export type IGameContextExport<TState extends Record<string, unknown> = {}> =
|
||||||
Omit<IGameContext<TState>, "_state" | "_commands" | "_rng">;
|
Omit<IGameContext<TState>, "_state" | "_commands" | "_rng">;
|
||||||
|
|
||||||
export function createGameContext<TState extends Record<string, unknown> = {}>(
|
export function createGameContext<TState extends Record<string, unknown> = {}>(
|
||||||
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
promptContext: PromptContext,
|
||||||
initialState?: TState | (() => TState),
|
initialState?: TState | (() => TState),
|
||||||
): IGameContext<TState> {
|
): IGameContext<TState> {
|
||||||
const stateValue =
|
const stateValue =
|
||||||
@@ -42,7 +31,7 @@ export function createGameContext<TState extends Record<string, unknown> = {}>(
|
|||||||
? initialState()
|
? initialState()
|
||||||
: (initialState ?? ({} as TState));
|
: (initialState ?? ({} as TState));
|
||||||
const state = mutableSignal(stateValue);
|
const state = mutableSignal(stateValue);
|
||||||
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
const { prompt } = promptContext;
|
||||||
|
|
||||||
const context: IGameContext<TState> = {
|
const context: IGameContext<TState> = {
|
||||||
get value(): TState {
|
get value(): TState {
|
||||||
@@ -57,38 +46,15 @@ export function createGameContext<TState extends Record<string, unknown> = {}>(
|
|||||||
produceAsync(fn: (draft: TState) => undefined) {
|
produceAsync(fn: (draft: TState) => undefined) {
|
||||||
return state.produceAsync(fn);
|
return state.produceAsync(fn);
|
||||||
},
|
},
|
||||||
run<T>(input: string) {
|
prompt,
|
||||||
return commands.run<T>(input);
|
|
||||||
},
|
|
||||||
runParsed<T>(command: Command) {
|
|
||||||
return commands.runParsed<T>(command);
|
|
||||||
},
|
|
||||||
prompt(def, validator, currentPlayer) {
|
|
||||||
return commands.prompt(
|
|
||||||
def.schema,
|
|
||||||
validator,
|
|
||||||
def.hintText,
|
|
||||||
currentPlayer,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
_state: state,
|
_state: state,
|
||||||
_commands: commands,
|
|
||||||
_rng: new Mulberry32RNG(),
|
_rng: new Mulberry32RNG(),
|
||||||
};
|
};
|
||||||
|
|
||||||
context._commands = commands = createCommandRunnerContext(
|
|
||||||
commandRegistry,
|
|
||||||
context,
|
|
||||||
);
|
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PromptDef<TArgs extends any[] = any[]> = {
|
|
||||||
schema: CommandSchema;
|
|
||||||
hintText?: string;
|
|
||||||
};
|
|
||||||
export function createPromptDef<TArgs extends any[] = any[]>(
|
export function createPromptDef<TArgs extends any[] = any[]>(
|
||||||
schema: CommandSchema | string,
|
schema: CommandSchema | string,
|
||||||
hintText?: string,
|
hintText?: string,
|
||||||
@@ -96,9 +62,3 @@ export function createPromptDef<TArgs extends any[] = any[]>(
|
|||||||
schema = typeof schema === "string" ? parseCommandSchema(schema) : schema;
|
schema = typeof schema === "string" ? parseCommandSchema(schema) : schema;
|
||||||
return { schema, hintText };
|
return { schema, hintText };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGameCommandRegistry<
|
|
||||||
TState extends Record<string, unknown> = {},
|
|
||||||
>() {
|
|
||||||
return createCommandRegistry<IGameContext<TState>>();
|
|
||||||
}
|
|
||||||
|
|||||||
+5
-1
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
// Core types
|
// Core types
|
||||||
export type { IGameContext } from "./core/game";
|
export type { IGameContext } from "./core/game";
|
||||||
export { createGameCommandRegistry, createPromptDef } from "./core/game";
|
export { createPromptDef } from "./core/game";
|
||||||
|
|
||||||
export type { GameHost, GameHostStatus, GameModule } from "./core/game-host";
|
export type { GameHost, GameHostStatus, GameModule } from "./core/game-host";
|
||||||
export { createGameHost } from "./core/game-host";
|
export { createGameHost } from "./core/game-host";
|
||||||
@@ -24,6 +24,8 @@ export {
|
|||||||
moveToRegion,
|
moveToRegion,
|
||||||
} from "./core/region";
|
} from "./core/region";
|
||||||
|
|
||||||
|
export * from "./core/game-client";
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
export type {
|
export type {
|
||||||
Command,
|
Command,
|
||||||
@@ -71,4 +73,6 @@ export { createRNG, Mulberry32RNG } from "./utils/rng";
|
|||||||
export type { MiddlewareChain } from "./utils/middleware";
|
export type { MiddlewareChain } from "./utils/middleware";
|
||||||
export { createMiddlewareChain } from "./utils/middleware";
|
export { createMiddlewareChain } from "./utils/middleware";
|
||||||
|
|
||||||
|
export * from "./utils/spawner";
|
||||||
|
|
||||||
export * from "@preact/signals-core";
|
export * from "@preact/signals-core";
|
||||||
|
|||||||
@@ -1,75 +1,11 @@
|
|||||||
import {BoopGame} from "@/samples/boop/types-extensions";
|
import { BoopGame } from "@/samples/boop/types-extensions";
|
||||||
import {PlayerType} from "@/samples/boop/types";
|
import { PlayerType } from "@/samples/boop/types";
|
||||||
import {turn} from "@/samples/boop/commands/turn";
|
import { turn } from "@/samples/boop/commands/turn";
|
||||||
import {createGameCommandRegistry} from "@/core/game";
|
import { boop } from "./boop";
|
||||||
|
import { checkFullBoard } from "./full-board";
|
||||||
export const registry = createGameCommandRegistry<BoopGame['value']>();
|
import { checkGraduates } from "./graduate";
|
||||||
|
import { place } from "./place";
|
||||||
/**
|
import { checkWin } from "./win";
|
||||||
* 放置棋子到棋盘
|
|
||||||
*/
|
|
||||||
const placeCmd = registry.register({
|
|
||||||
schema: 'place <row:number> <col:number> <player> <type>',
|
|
||||||
run: async (game, row, col, player, type) => {
|
|
||||||
const {place} = await import('./place');
|
|
||||||
return place(game, row, col, player, type);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行boop - 推动周围棋子
|
|
||||||
*/
|
|
||||||
const boopCmd = registry.register({
|
|
||||||
schema: 'boop <row:number> <col:number> <type>',
|
|
||||||
run: async (game, row, col, type) => {
|
|
||||||
const {boop} = await import('./boop');
|
|
||||||
return boop(game, row, col, type);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查是否有玩家获胜(三个猫连线)
|
|
||||||
*/
|
|
||||||
const checkWinCmd = registry.register({
|
|
||||||
schema: 'check-win',
|
|
||||||
run: async (game) => {
|
|
||||||
const {checkWin} = await import('./win');
|
|
||||||
return checkWin(game);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查并执行小猫升级(三个小猫连线变成猫)
|
|
||||||
*/
|
|
||||||
const checkGraduatesCmd = registry.register({
|
|
||||||
schema: 'check-graduates',
|
|
||||||
run: async (game) => {
|
|
||||||
const {checkGraduates} = await import('./graduate');
|
|
||||||
return checkGraduates(game);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查并执行 8-piece 规则
|
|
||||||
*/
|
|
||||||
const checkFullBoardCmd = registry.register({
|
|
||||||
schema: 'check-full-board <player:string>',
|
|
||||||
run: async (game, player) => {
|
|
||||||
const {checkFullBoard} = await import('./full-board');
|
|
||||||
return checkFullBoard(game, player);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理一个回合
|
|
||||||
*/
|
|
||||||
const turnCmd = registry.register({
|
|
||||||
schema: 'turn <player:string>',
|
|
||||||
run: async (game, player) => {
|
|
||||||
const {turn} = await import('./turn');
|
|
||||||
return turn(game, player);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动游戏主循环
|
* 启动游戏主循环
|
||||||
@@ -79,10 +15,11 @@ export async function start(game: BoopGame) {
|
|||||||
const currentPlayer = game.value.currentPlayer;
|
const currentPlayer = game.value.currentPlayer;
|
||||||
const turnOutput = await turn(game, currentPlayer);
|
const turnOutput = await turn(game, currentPlayer);
|
||||||
|
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
state.winner = turnOutput.winner;
|
state.winner = turnOutput.winner;
|
||||||
if (!state.winner) {
|
if (!state.winner) {
|
||||||
state.currentPlayer = state.currentPlayer === 'white' ? 'black' : 'white';
|
state.currentPlayer =
|
||||||
|
state.currentPlayer === "white" ? "black" : "white";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (game.value.winner) break;
|
if (game.value.winner) break;
|
||||||
@@ -91,11 +28,4 @@ export async function start(game: BoopGame) {
|
|||||||
return game.value;
|
return game.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export { place, boop, checkWin, checkGraduates, checkFullBoard, turn };
|
||||||
placeCmd as place,
|
|
||||||
boopCmd as boop,
|
|
||||||
checkWinCmd as checkWin,
|
|
||||||
checkGraduatesCmd as checkGraduates,
|
|
||||||
checkFullBoardCmd as checkFullBoard,
|
|
||||||
turnCmd as turn
|
|
||||||
};
|
|
||||||
|
|||||||
+10
-18
@@ -5,38 +5,30 @@ export type {
|
|||||||
WinnerType,
|
WinnerType,
|
||||||
RegionType,
|
RegionType,
|
||||||
BoopPartMeta,
|
BoopPartMeta,
|
||||||
BoopPart
|
BoopPart,
|
||||||
} from './types';
|
} from "./types";
|
||||||
|
|
||||||
export type {BoopGame} from './types-extensions';
|
export type { BoopGame } from "./types-extensions";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
export {
|
export { BOARD_SIZE, MAX_PIECES_PER_PLAYER, WIN_LENGTH } from "./constants";
|
||||||
BOARD_SIZE,
|
|
||||||
MAX_PIECES_PER_PLAYER,
|
|
||||||
WIN_LENGTH
|
|
||||||
} from './constants';
|
|
||||||
|
|
||||||
// State
|
// State
|
||||||
export {
|
export { createInitialState, type BoopState } from "./state";
|
||||||
createInitialState,
|
|
||||||
type BoopState
|
|
||||||
} from './state';
|
|
||||||
|
|
||||||
// Prompts
|
// Prompts
|
||||||
export {prompts} from './prompts';
|
export { prompts } from "./prompts";
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
export {
|
export {
|
||||||
registry,
|
|
||||||
start,
|
start,
|
||||||
place as placeCmd,
|
place as placeCmd,
|
||||||
boop as boopCmd,
|
boop as boopCmd,
|
||||||
checkWin as checkWinCmd,
|
checkWin as checkWinCmd,
|
||||||
checkGraduates as checkGraduatesCmd,
|
checkGraduates as checkGraduatesCmd,
|
||||||
checkFullBoard as checkFullBoardCmd,
|
checkFullBoard as checkFullBoardCmd,
|
||||||
turn as turnCmd
|
turn as turnCmd,
|
||||||
} from './commands';
|
} from "./commands";
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
export {
|
export {
|
||||||
@@ -45,5 +37,5 @@ export {
|
|||||||
isCellOccupied,
|
isCellOccupied,
|
||||||
getNeighborPositions,
|
getNeighborPositions,
|
||||||
findPartInRegion,
|
findPartInRegion,
|
||||||
findPartAtPosition
|
findPartAtPosition,
|
||||||
} from './utils';
|
} from "./utils";
|
||||||
|
|||||||
+109
-79
@@ -3,12 +3,9 @@ import {
|
|||||||
OnitamaState,
|
OnitamaState,
|
||||||
PlayerType,
|
PlayerType,
|
||||||
prompts,
|
prompts,
|
||||||
initializeCards
|
initializeCards,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import {createGameCommandRegistry} from "@/core/game";
|
import { moveToRegion } from "@/core/region";
|
||||||
import {moveToRegion} from "@/core/region";
|
|
||||||
|
|
||||||
export const registry = createGameCommandRegistry<OnitamaState>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查位置是否在棋盘范围内
|
* 检查位置是否在棋盘范围内
|
||||||
@@ -29,8 +26,12 @@ function getPawnAtPosition(state: OnitamaState, x: number, y: number) {
|
|||||||
/**
|
/**
|
||||||
* 检查玩家是否拥有某张卡牌
|
* 检查玩家是否拥有某张卡牌
|
||||||
*/
|
*/
|
||||||
function playerHasCard(state: OnitamaState, player: PlayerType, cardName: string): boolean {
|
function playerHasCard(
|
||||||
const cardList = player === 'red' ? state.redCards : state.blackCards;
|
state: OnitamaState,
|
||||||
|
player: PlayerType,
|
||||||
|
cardName: string,
|
||||||
|
): boolean {
|
||||||
|
const cardList = player === "red" ? state.redCards : state.blackCards;
|
||||||
return cardList.includes(cardName);
|
return cardList.includes(cardName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,13 +39,17 @@ function playerHasCard(state: OnitamaState, player: PlayerType, cardName: string
|
|||||||
* 获取卡牌的移动候选项,根据玩家视角进行旋转
|
* 获取卡牌的移动候选项,根据玩家视角进行旋转
|
||||||
* 黑方需要将卡牌旋转180度
|
* 黑方需要将卡牌旋转180度
|
||||||
*/
|
*/
|
||||||
export function getCardMoveCandidates(state: OnitamaState, cardName: string, player: PlayerType) {
|
export function getCardMoveCandidates(
|
||||||
|
state: OnitamaState,
|
||||||
|
cardName: string,
|
||||||
|
player: PlayerType,
|
||||||
|
) {
|
||||||
const card = state.cards[cardName];
|
const card = state.cards[cardName];
|
||||||
const candidates = card.moveCandidates;
|
const candidates = card.moveCandidates;
|
||||||
|
|
||||||
// 黑方需要将卡牌旋转180度
|
// 黑方需要将卡牌旋转180度
|
||||||
if (player === 'black') {
|
if (player === "black") {
|
||||||
return candidates.map(m => ({ dx: -m.dx, dy: -m.dy }));
|
return candidates.map((m) => ({ dx: -m.dx, dy: -m.dy }));
|
||||||
}
|
}
|
||||||
|
|
||||||
return candidates;
|
return candidates;
|
||||||
@@ -53,7 +58,15 @@ export function getCardMoveCandidates(state: OnitamaState, cardName: string, pla
|
|||||||
/**
|
/**
|
||||||
* 检查移动是否合法
|
* 检查移动是否合法
|
||||||
*/
|
*/
|
||||||
export function isValidMove(state: OnitamaState, cardName: string, fromX: number, fromY: number, toX: number, toY: number, player: PlayerType): string | null {
|
export function isValidMove(
|
||||||
|
state: OnitamaState,
|
||||||
|
cardName: string,
|
||||||
|
fromX: number,
|
||||||
|
fromY: number,
|
||||||
|
toX: number,
|
||||||
|
toY: number,
|
||||||
|
player: PlayerType,
|
||||||
|
): string | null {
|
||||||
// 检查玩家是否拥有该卡牌
|
// 检查玩家是否拥有该卡牌
|
||||||
if (!playerHasCard(state, player, cardName)) {
|
if (!playerHasCard(state, player, cardName)) {
|
||||||
return `玩家 ${player} 不拥有卡牌 ${cardName}`;
|
return `玩家 ${player} 不拥有卡牌 ${cardName}`;
|
||||||
@@ -80,7 +93,7 @@ export function isValidMove(state: OnitamaState, cardName: string, fromX: number
|
|||||||
|
|
||||||
// 检查移动是否在卡牌的移动候选项中(黑方需要旋转180度)
|
// 检查移动是否在卡牌的移动候选项中(黑方需要旋转180度)
|
||||||
const candidates = getCardMoveCandidates(state, cardName, player);
|
const candidates = getCardMoveCandidates(state, cardName, player);
|
||||||
const isValid = candidates.some(m => m.dx === dx && m.dy === dy);
|
const isValid = candidates.some((m) => m.dx === dx && m.dy === dy);
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
return `卡牌 ${cardName} 不支持移动 (${dx}, ${dy})`;
|
return `卡牌 ${cardName} 不支持移动 (${dx}, ${dy})`;
|
||||||
}
|
}
|
||||||
@@ -102,7 +115,15 @@ export function isValidMove(state: OnitamaState, cardName: string, fromX: number
|
|||||||
/**
|
/**
|
||||||
* 执行移动
|
* 执行移动
|
||||||
*/
|
*/
|
||||||
async function handleMove(game: OnitamaGame, player: PlayerType, cardName: string, fromX: number, fromY: number, toX: number, toY: number) {
|
async function move(
|
||||||
|
game: OnitamaGame,
|
||||||
|
player: PlayerType,
|
||||||
|
cardName: string,
|
||||||
|
fromX: number,
|
||||||
|
fromY: number,
|
||||||
|
toX: number,
|
||||||
|
toY: number,
|
||||||
|
) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
// 验证移动
|
// 验证移动
|
||||||
@@ -113,7 +134,7 @@ async function handleMove(game: OnitamaGame, player: PlayerType, cardName: strin
|
|||||||
|
|
||||||
const capturedPawnId = getPawnAtPosition(state, toX, toY)?.id || null;
|
const capturedPawnId = getPawnAtPosition(state, toX, toY)?.id || null;
|
||||||
|
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
const pawn = state.pawns[getPawnAtPosition(state, fromX, fromY)!.id];
|
const pawn = state.pawns[getPawnAtPosition(state, fromX, fromY)!.id];
|
||||||
|
|
||||||
// 如果目标位置有敌方棋子,将其移除(吃掉)
|
// 如果目标位置有敌方棋子,将其移除(吃掉)
|
||||||
@@ -127,41 +148,40 @@ async function handleMove(game: OnitamaGame, player: PlayerType, cardName: strin
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 交换卡牌
|
// 交换卡牌
|
||||||
await handleSwapCard(game, player, cardName);
|
await swapCard(game, player, cardName);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: { x: fromX, y: fromY },
|
from: { x: fromX, y: fromY },
|
||||||
to: { x: toX, y: toY },
|
to: { x: toX, y: toY },
|
||||||
card: cardName,
|
card: cardName,
|
||||||
captured: capturedPawnId
|
captured: capturedPawnId,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const move = registry.register({
|
|
||||||
schema: 'move <player> <card:string> <fromX:number> <fromY:number> <toX:number> <toY:number>',
|
|
||||||
run: handleMove
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 交换卡牌:将使用的卡牌与备用卡牌交换
|
* 交换卡牌:将使用的卡牌与备用卡牌交换
|
||||||
*/
|
*/
|
||||||
async function handleSwapCard(game: OnitamaGame, player: PlayerType, usedCard: string) {
|
async function swapCard(
|
||||||
await game.produceAsync(state => {
|
game: OnitamaGame,
|
||||||
|
player: PlayerType,
|
||||||
|
usedCard: string,
|
||||||
|
) {
|
||||||
|
await game.produceAsync((state) => {
|
||||||
const spareCard = state.spareCard;
|
const spareCard = state.spareCard;
|
||||||
const usedCardData = state.cards[usedCard];
|
const usedCardData = state.cards[usedCard];
|
||||||
const spareCardData = state.cards[spareCard];
|
const spareCardData = state.cards[spareCard];
|
||||||
|
|
||||||
// 从玩家手牌中移除使用的卡牌
|
// 从玩家手牌中移除使用的卡牌
|
||||||
if (player === 'red') {
|
if (player === "red") {
|
||||||
state.redCards = state.redCards.filter(c => c !== usedCard);
|
state.redCards = state.redCards.filter((c) => c !== usedCard);
|
||||||
state.redCards.push(spareCard);
|
state.redCards.push(spareCard);
|
||||||
} else {
|
} else {
|
||||||
state.blackCards = state.blackCards.filter(c => c !== usedCard);
|
state.blackCards = state.blackCards.filter((c) => c !== usedCard);
|
||||||
state.blackCards.push(spareCard);
|
state.blackCards.push(spareCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新卡牌区域
|
// 更新卡牌区域
|
||||||
usedCardData.regionId = 'spare';
|
usedCardData.regionId = "spare";
|
||||||
spareCardData.regionId = player;
|
spareCardData.regionId = player;
|
||||||
|
|
||||||
// 更新备用卡牌
|
// 更新备用卡牌
|
||||||
@@ -169,75 +189,70 @@ async function handleSwapCard(game: OnitamaGame, player: PlayerType, usedCard: s
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const swapCard = registry.register({
|
|
||||||
schema: 'swap-card <player> <card:string>',
|
|
||||||
run: handleSwapCard
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查占领胜利条件:玩家的师父棋子到达对手的初始位置
|
* 检查占领胜利条件:玩家的师父棋子到达对手的初始位置
|
||||||
* 红色师父需要到达 (2, 4) - 黑色师父的初始位置
|
* 红色师父需要到达 (2, 4) - 黑色师父的初始位置
|
||||||
* 黑色师父需要到达 (2, 0) - 红色师父的初始位置
|
* 黑色师父需要到达 (2, 0) - 红色师父的初始位置
|
||||||
*/
|
*/
|
||||||
async function handleCheckConquestWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function checkConquestWin(game: OnitamaGame): Promise<PlayerType | null> {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
// 红色师父到达 (2, 4)(黑色师父的初始位置)
|
// 红色师父到达 (2, 4)(黑色师父的初始位置)
|
||||||
const redMaster = state.pawns['red-master'];
|
const redMaster = state.pawns["red-master"];
|
||||||
if (redMaster && redMaster.regionId === 'board' && redMaster.position[0] === 2 && redMaster.position[1] === 4) {
|
if (
|
||||||
return 'red';
|
redMaster &&
|
||||||
|
redMaster.regionId === "board" &&
|
||||||
|
redMaster.position[0] === 2 &&
|
||||||
|
redMaster.position[1] === 4
|
||||||
|
) {
|
||||||
|
return "red";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 黑色师父到达 (2, 0)(红色师父的初始位置)
|
// 黑色师父到达 (2, 0)(红色师父的初始位置)
|
||||||
const blackMaster = state.pawns['black-master'];
|
const blackMaster = state.pawns["black-master"];
|
||||||
if (blackMaster && blackMaster.regionId === 'board' && blackMaster.position[0] === 2 && blackMaster.position[1] === 0) {
|
if (
|
||||||
return 'black';
|
blackMaster &&
|
||||||
|
blackMaster.regionId === "board" &&
|
||||||
|
blackMaster.position[0] === 2 &&
|
||||||
|
blackMaster.position[1] === 0
|
||||||
|
) {
|
||||||
|
return "black";
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkConquestWin = registry.register({
|
|
||||||
schema: 'check-conquest-win',
|
|
||||||
run: handleCheckConquestWin
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查吃掉胜利条件:对手的师父棋子被吃掉(不在棋盘上)
|
* 检查吃掉胜利条件:对手的师父棋子被吃掉(不在棋盘上)
|
||||||
*/
|
*/
|
||||||
async function handleCheckCaptureWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function checkCaptureWin(game: OnitamaGame): Promise<PlayerType | null> {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
// 红色师父不在棋盘上,黑色获胜
|
// 红色师父不在棋盘上,黑色获胜
|
||||||
const redMaster = state.pawns['red-master'];
|
const redMaster = state.pawns["red-master"];
|
||||||
if (!redMaster || redMaster.regionId !== 'board') {
|
if (!redMaster || redMaster.regionId !== "board") {
|
||||||
return 'black';
|
return "black";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 黑色师父不在棋盘上,红色获胜
|
// 黑色师父不在棋盘上,红色获胜
|
||||||
const blackMaster = state.pawns['black-master'];
|
const blackMaster = state.pawns["black-master"];
|
||||||
if (!blackMaster || blackMaster.regionId !== 'board') {
|
if (!blackMaster || blackMaster.regionId !== "board") {
|
||||||
return 'red';
|
return "red";
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkCaptureWin = registry.register({
|
|
||||||
schema: 'check-capture-win',
|
|
||||||
run: handleCheckCaptureWin
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 综合胜利检测
|
* 综合胜利检测
|
||||||
*/
|
*/
|
||||||
async function handleCheckWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function checkWin(game: OnitamaGame): Promise<PlayerType | null> {
|
||||||
const conquestWinner = await handleCheckConquestWin(game);
|
const conquestWinner = await checkConquestWin(game);
|
||||||
if (conquestWinner) {
|
if (conquestWinner) {
|
||||||
return conquestWinner;
|
return conquestWinner;
|
||||||
}
|
}
|
||||||
|
|
||||||
const captureWinner = await handleCheckCaptureWin(game);
|
const captureWinner = await checkCaptureWin(game);
|
||||||
if (captureWinner) {
|
if (captureWinner) {
|
||||||
return captureWinner;
|
return captureWinner;
|
||||||
}
|
}
|
||||||
@@ -245,22 +260,34 @@ async function handleCheckWin(game: OnitamaGame): Promise<PlayerType | null> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkWin = registry.register({
|
|
||||||
schema: 'check-win',
|
|
||||||
run: handleCheckWin
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取玩家可用的移动
|
* 获取玩家可用的移动
|
||||||
*/
|
*/
|
||||||
export function getAvailableMoves(state: OnitamaState, player: PlayerType): Array<{card: string, fromX: number, fromY: number, toX: number, toY: number}> {
|
export function getAvailableMoves(
|
||||||
const moves: Array<{card: string, fromX: number, fromY: number, toX: number, toY: number}> = [];
|
state: OnitamaState,
|
||||||
|
player: PlayerType,
|
||||||
|
): Array<{
|
||||||
|
card: string;
|
||||||
|
fromX: number;
|
||||||
|
fromY: number;
|
||||||
|
toX: number;
|
||||||
|
toY: number;
|
||||||
|
}> {
|
||||||
|
const moves: Array<{
|
||||||
|
card: string;
|
||||||
|
fromX: number;
|
||||||
|
fromY: number;
|
||||||
|
toX: number;
|
||||||
|
toY: number;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
// 获取玩家的所有卡牌
|
// 获取玩家的所有卡牌
|
||||||
const cardNames = player === 'red' ? state.redCards : state.blackCards;
|
const cardNames = player === "red" ? state.redCards : state.blackCards;
|
||||||
|
|
||||||
// 获取玩家的所有棋子
|
// 获取玩家的所有棋子
|
||||||
const playerPawns = Object.values(state.pawns).filter(p => p.owner === player && p.regionId === 'board');
|
const playerPawns = Object.values(state.pawns).filter(
|
||||||
|
(p) => p.owner === player && p.regionId === "board",
|
||||||
|
);
|
||||||
|
|
||||||
// 对于每张卡牌
|
// 对于每张卡牌
|
||||||
for (const cardName of cardNames) {
|
for (const cardName of cardNames) {
|
||||||
@@ -294,7 +321,7 @@ export function getAvailableMoves(state: OnitamaState, player: PlayerType): Arra
|
|||||||
/**
|
/**
|
||||||
* 处理回合
|
* 处理回合
|
||||||
*/
|
*/
|
||||||
async function handleTurn(game: OnitamaGame, turnPlayer: PlayerType) {
|
async function turn(game: OnitamaGame, turnPlayer: PlayerType) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const availableMoves = getAvailableMoves(state, turnPlayer);
|
const availableMoves = getAvailableMoves(state, turnPlayer);
|
||||||
|
|
||||||
@@ -313,7 +340,7 @@ async function handleTurn(game: OnitamaGame, turnPlayer: PlayerType) {
|
|||||||
}
|
}
|
||||||
return card;
|
return card;
|
||||||
},
|
},
|
||||||
turnPlayer
|
turnPlayer,
|
||||||
);
|
);
|
||||||
|
|
||||||
await swapCard(game, turnPlayer, cardToSwap);
|
await swapCard(game, turnPlayer, cardToSwap);
|
||||||
@@ -334,19 +361,27 @@ async function handleTurn(game: OnitamaGame, turnPlayer: PlayerType) {
|
|||||||
|
|
||||||
return { player, card, fromX, fromY, toX, toY };
|
return { player, card, fromX, fromY, toX, toY };
|
||||||
},
|
},
|
||||||
turnPlayer
|
turnPlayer,
|
||||||
);
|
);
|
||||||
|
|
||||||
await move(game, moveOutput.player, moveOutput.card, moveOutput.fromX, moveOutput.fromY, moveOutput.toX, moveOutput.toY);
|
await move(
|
||||||
|
game,
|
||||||
|
moveOutput.player,
|
||||||
|
moveOutput.card,
|
||||||
|
moveOutput.fromX,
|
||||||
|
moveOutput.fromY,
|
||||||
|
moveOutput.toX,
|
||||||
|
moveOutput.toY,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查胜利
|
// 检查胜利
|
||||||
const winner = await checkWin(game);
|
const winner = await checkWin(game);
|
||||||
|
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
state.winner = winner;
|
state.winner = winner;
|
||||||
if (!winner) {
|
if (!winner) {
|
||||||
state.currentPlayer = state.currentPlayer === 'red' ? 'black' : 'red';
|
state.currentPlayer = state.currentPlayer === "red" ? "black" : "red";
|
||||||
state.turn++;
|
state.turn++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -354,11 +389,6 @@ async function handleTurn(game: OnitamaGame, turnPlayer: PlayerType) {
|
|||||||
return { winner, move: moveOutput };
|
return { winner, move: moveOutput };
|
||||||
}
|
}
|
||||||
|
|
||||||
const turn = registry.register({
|
|
||||||
schema: 'turn <player>',
|
|
||||||
run: handleTurn
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始游戏主循环
|
* 开始游戏主循环
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,37 +1,35 @@
|
|||||||
import {IGameContext} from "@/core/game";
|
import { IGameContext } from "@/core/game";
|
||||||
import {RegicideState} from "@/samples/regicide/state";
|
import { RegicideState } from "@/samples/regicide/state";
|
||||||
import {createGameCommandRegistry} from "@/core/game";
|
import { PlayerType, RegicideCard } from "@/samples/regicide/types";
|
||||||
import {PlayerType, RegicideCard} from "@/samples/regicide/types";
|
import { CARD_VALUES, FACE_CARDS } from "@/samples/regicide/constants";
|
||||||
import {CARD_VALUES, FACE_CARDS} from "@/samples/regicide/constants";
|
import { isEnemyDefeated } from "@/samples/regicide/utils";
|
||||||
import {isEnemyDefeated} from "@/samples/regicide/utils";
|
|
||||||
|
|
||||||
export type RegicideGame = IGameContext<RegicideState>;
|
export type RegicideGame = IGameContext<RegicideState>;
|
||||||
|
|
||||||
export const registry = createGameCommandRegistry<RegicideState>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出一张牌(对当前敌人造成伤害)
|
* 打出一张牌(对当前敌人造成伤害)
|
||||||
*/
|
*/
|
||||||
const playCmd = registry.register({
|
async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
schema: 'play <player:string> <cardId:string>',
|
|
||||||
run: async (game: RegicideGame, player: string, cardId: string) => {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const card = state.cards[cardId];
|
const card = state.cards[cardId];
|
||||||
|
|
||||||
if (!card) {
|
if (!card) {
|
||||||
return {success: false, error: `卡牌 ${cardId} 不存在`};
|
return { success: false, error: `卡牌 ${cardId} 不存在` };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查卡牌是否在玩家手牌中
|
// 检查卡牌是否在玩家手牌中
|
||||||
const playerKey = player as PlayerType;
|
const playerKey = player as PlayerType;
|
||||||
const playerHand = state.playerHands[playerKey];
|
const playerHand = state.playerHands[playerKey];
|
||||||
if (!playerHand || !playerHand.includes(cardId)) {
|
if (!playerHand || !playerHand.includes(cardId)) {
|
||||||
return {success: false, error: `卡牌 ${cardId} 不在玩家 ${player} 的手牌中`};
|
return {
|
||||||
|
success: false,
|
||||||
|
error: `卡牌 ${cardId} 不在玩家 ${player} 的手牌中`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有当前敌人
|
// 检查是否有当前敌人
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return {success: false, error: '没有活跃的敌人'};
|
return { success: false, error: "没有活跃的敌人" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算伤害(基础伤害为卡牌面值)
|
// 计算伤害(基础伤害为卡牌面值)
|
||||||
@@ -39,24 +37,27 @@ const playCmd = registry.register({
|
|||||||
let attackReduction = 0;
|
let attackReduction = 0;
|
||||||
|
|
||||||
// 梅花双倍伤害
|
// 梅花双倍伤害
|
||||||
if (card.suit === 'clubs') {
|
if (card.suit === "clubs") {
|
||||||
damage *= 2;
|
damage *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 黑桃降低敌人攻击力
|
// 黑桃降低敌人攻击力
|
||||||
if (card.suit === 'spades') {
|
if (card.suit === "spades") {
|
||||||
attackReduction = card.value;
|
attackReduction = card.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const enemyHpBefore = state.currentEnemy.hp;
|
const enemyHpBefore = state.currentEnemy.hp;
|
||||||
|
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
// 对敌人造成伤害
|
// 对敌人造成伤害
|
||||||
state.currentEnemy!.hp -= damage;
|
state.currentEnemy!.hp -= damage;
|
||||||
|
|
||||||
// 记录黑桃的攻击力降低
|
// 记录黑桃的攻击力降低
|
||||||
if (attackReduction > 0) {
|
if (attackReduction > 0) {
|
||||||
state.currentEnemy!.value = Math.max(0, state.currentEnemy!.value - attackReduction);
|
state.currentEnemy!.value = Math.max(
|
||||||
|
0,
|
||||||
|
state.currentEnemy!.value - attackReduction,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从手牌移除卡牌
|
// 从手牌移除卡牌
|
||||||
@@ -67,15 +68,17 @@ const playCmd = registry.register({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 将卡牌移到弃牌堆
|
// 将卡牌移到弃牌堆
|
||||||
state.cards[cardId].regionId = 'discardPile';
|
state.cards[cardId].regionId = "discardPile";
|
||||||
|
|
||||||
// 红心能力:将弃牌堆洗回酒馆牌堆
|
// 红心能力:将弃牌堆洗回酒馆牌堆
|
||||||
if (card.suit === 'hearts') {
|
if (card.suit === "hearts") {
|
||||||
const discardIds = state.regions.discardPile.childIds.filter(id => id !== state.currentEnemy!.id);
|
const discardIds = state.regions.discardPile.childIds.filter(
|
||||||
|
(id) => id !== state.currentEnemy!.id,
|
||||||
|
);
|
||||||
if (discardIds.length > 0) {
|
if (discardIds.length > 0) {
|
||||||
// 将弃牌堆(除当前敌人外)移回酒馆牌堆
|
// 将弃牌堆(除当前敌人外)移回酒馆牌堆
|
||||||
for (const discardId of discardIds) {
|
for (const discardId of discardIds) {
|
||||||
state.cards[discardId].regionId = 'tavernDeck';
|
state.cards[discardId].regionId = "tavernDeck";
|
||||||
}
|
}
|
||||||
state.regions.tavernDeck.childIds.push(...discardIds);
|
state.regions.tavernDeck.childIds.push(...discardIds);
|
||||||
state.regions.discardPile.childIds = [state.currentEnemy!.id];
|
state.regions.discardPile.childIds = [state.currentEnemy!.id];
|
||||||
@@ -83,7 +86,7 @@ const playCmd = registry.register({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 方块能力:从酒馆牌堆抓牌
|
// 方块能力:从酒馆牌堆抓牌
|
||||||
if (card.suit === 'diamonds') {
|
if (card.suit === "diamonds") {
|
||||||
const tavernDeckCount = state.regions.tavernDeck.childIds.length;
|
const tavernDeckCount = state.regions.tavernDeck.childIds.length;
|
||||||
if (tavernDeckCount > 0) {
|
if (tavernDeckCount > 0) {
|
||||||
const drawCardId = state.regions.tavernDeck.childIds.shift()!;
|
const drawCardId = state.regions.tavernDeck.childIds.shift()!;
|
||||||
@@ -104,29 +107,31 @@ const playCmd = registry.register({
|
|||||||
enemyHpBefore,
|
enemyHpBefore,
|
||||||
enemyHpAfter: game.value.currentEnemy!.hp,
|
enemyHpAfter: game.value.currentEnemy!.hp,
|
||||||
enemyDefeated,
|
enemyDefeated,
|
||||||
suitAbility: card.suit
|
suitAbility: card.suit,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出A配合另一张牌
|
* 打出A配合另一张牌
|
||||||
*/
|
*/
|
||||||
const playWithACmd = registry.register({
|
async function playWithACmd(
|
||||||
schema: 'play-with-a <player:string> <aceCardId:string> <otherCardId:string>',
|
game: RegicideGame,
|
||||||
run: async (game: RegicideGame, player: string, aceCardId: string, otherCardId: string) => {
|
player: string,
|
||||||
|
aceCardId: string,
|
||||||
|
otherCardId: string,
|
||||||
|
) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const aceCard = state.cards[aceCardId];
|
const aceCard = state.cards[aceCardId];
|
||||||
const otherCard = state.cards[otherCardId];
|
const otherCard = state.cards[otherCardId];
|
||||||
|
|
||||||
if (!aceCard || !otherCard) {
|
if (!aceCard || !otherCard) {
|
||||||
return {success: false, error: '卡牌不存在'};
|
return { success: false, error: "卡牌不存在" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否是A牌
|
// 检查是否是A牌
|
||||||
if (aceCard.rank !== 'A') {
|
if (aceCard.rank !== "A") {
|
||||||
return {success: false, error: `第一张牌必须是A`};
|
return { success: false, error: `第一张牌必须是A` };
|
||||||
}
|
}
|
||||||
|
|
||||||
const playerKey = player as PlayerType;
|
const playerKey = player as PlayerType;
|
||||||
@@ -134,36 +139,39 @@ const playWithACmd = registry.register({
|
|||||||
|
|
||||||
// 检查两张牌都在手牌中
|
// 检查两张牌都在手牌中
|
||||||
if (!playerHand.includes(aceCardId) || !playerHand.includes(otherCardId)) {
|
if (!playerHand.includes(aceCardId) || !playerHand.includes(otherCardId)) {
|
||||||
return {success: false, error: '卡牌不在手牌中'};
|
return { success: false, error: "卡牌不在手牌中" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return {success: false, error: '没有活跃的敌人'};
|
return { success: false, error: "没有活跃的敌人" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算两张牌的总伤害
|
// 计算两张牌的总伤害
|
||||||
let totalDamage = aceCard.value + otherCard.value;
|
let totalDamage = aceCard.value + otherCard.value;
|
||||||
|
|
||||||
// 如果另一张牌是梅花,双倍伤害
|
// 如果另一张牌是梅花,双倍伤害
|
||||||
if (otherCard.suit === 'clubs') {
|
if (otherCard.suit === "clubs") {
|
||||||
totalDamage *= 2;
|
totalDamage *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
let attackReduction = 0;
|
let attackReduction = 0;
|
||||||
if (aceCard.suit === 'spades') {
|
if (aceCard.suit === "spades") {
|
||||||
attackReduction += aceCard.value;
|
attackReduction += aceCard.value;
|
||||||
}
|
}
|
||||||
if (otherCard.suit === 'spades') {
|
if (otherCard.suit === "spades") {
|
||||||
attackReduction += otherCard.value;
|
attackReduction += otherCard.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
// 对敌人造成伤害
|
// 对敌人造成伤害
|
||||||
state.currentEnemy!.hp -= totalDamage;
|
state.currentEnemy!.hp -= totalDamage;
|
||||||
|
|
||||||
// 记录黑桃的攻击力降低
|
// 记录黑桃的攻击力降低
|
||||||
if (attackReduction > 0) {
|
if (attackReduction > 0) {
|
||||||
state.currentEnemy!.value = Math.max(0, state.currentEnemy!.value - attackReduction);
|
state.currentEnemy!.value = Math.max(
|
||||||
|
0,
|
||||||
|
state.currentEnemy!.value - attackReduction,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从手牌移除两张牌
|
// 从手牌移除两张牌
|
||||||
@@ -174,8 +182,8 @@ const playWithACmd = registry.register({
|
|||||||
if (otherIndex !== -1) hand.splice(otherIndex, 1);
|
if (otherIndex !== -1) hand.splice(otherIndex, 1);
|
||||||
|
|
||||||
// 将卡牌移到弃牌堆
|
// 将卡牌移到弃牌堆
|
||||||
state.cards[aceCardId].regionId = 'discardPile';
|
state.cards[aceCardId].regionId = "discardPile";
|
||||||
state.cards[otherCardId].regionId = 'discardPile';
|
state.cards[otherCardId].regionId = "discardPile";
|
||||||
});
|
});
|
||||||
|
|
||||||
const enemyDefeated = isEnemyDefeated(state.currentEnemy);
|
const enemyDefeated = isEnemyDefeated(state.currentEnemy);
|
||||||
@@ -186,33 +194,31 @@ const playWithACmd = registry.register({
|
|||||||
damage: totalDamage,
|
damage: totalDamage,
|
||||||
attackReduction,
|
attackReduction,
|
||||||
enemyHp: state.currentEnemy!.hp,
|
enemyHp: state.currentEnemy!.hp,
|
||||||
enemyDefeated
|
enemyDefeated,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 让过(不出牌)
|
* 让过(不出牌)
|
||||||
*/
|
*/
|
||||||
const passCmd = registry.register({
|
async function passCmd(game: RegicideGame, player: string) {
|
||||||
schema: 'pass <player:string>',
|
|
||||||
run: async (game: RegicideGame, player: string) => {
|
|
||||||
// 即使让过,也会受到敌人反击(在回合结束时处理)
|
// 即使让过,也会受到敌人反击(在回合结束时处理)
|
||||||
return {success: true, result: {message: `${player} 让过`}};
|
return { success: true, result: { message: `${player} 让过` } };
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 敌人反击 - 玩家必须弃掉点数和 >= 敌人攻击力的牌
|
* 敌人反击 - 玩家必须弃掉点数和 >= 敌人攻击力的牌
|
||||||
*/
|
*/
|
||||||
const enemyCounterattackCmd = registry.register({
|
async function enemyCounterattackCmd(
|
||||||
schema: 'counterattack <player:string> <discardCards:string[]>',
|
game: RegicideGame,
|
||||||
run: async (game: RegicideGame, player: string, discardCards: string[]) => {
|
player: string,
|
||||||
|
discardCards: string[],
|
||||||
|
) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return {success: false, error: '没有活跃的敌人'};
|
return { success: false, error: "没有活跃的敌人" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const playerKey = player as PlayerType;
|
const playerKey = player as PlayerType;
|
||||||
@@ -221,7 +227,7 @@ const enemyCounterattackCmd = registry.register({
|
|||||||
// 检查要弃的牌都在手牌中
|
// 检查要弃的牌都在手牌中
|
||||||
for (const cardId of discardCards) {
|
for (const cardId of discardCards) {
|
||||||
if (!playerHand.includes(cardId)) {
|
if (!playerHand.includes(cardId)) {
|
||||||
return {success: false, error: `卡牌 ${cardId} 不在手牌中`};
|
return { success: false, error: `卡牌 ${cardId} 不在手牌中` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,19 +246,19 @@ const enemyCounterattackCmd = registry.register({
|
|||||||
if (totalValue < enemyAttack) {
|
if (totalValue < enemyAttack) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `弃牌点数和 (${totalValue}) 小于敌人攻击力 (${enemyAttack}),游戏失败`
|
error: `弃牌点数和 (${totalValue}) 小于敌人攻击力 (${enemyAttack}),游戏失败`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行弃牌
|
// 执行弃牌
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
const hand = state.playerHands[playerKey];
|
const hand = state.playerHands[playerKey];
|
||||||
for (const cardId of discardCards) {
|
for (const cardId of discardCards) {
|
||||||
const index = hand.indexOf(cardId);
|
const index = hand.indexOf(cardId);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
hand.splice(index, 1);
|
hand.splice(index, 1);
|
||||||
}
|
}
|
||||||
state.cards[cardId].regionId = 'discardPile';
|
state.cards[cardId].regionId = "discardPile";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -261,30 +267,27 @@ const enemyCounterattackCmd = registry.register({
|
|||||||
result: {
|
result: {
|
||||||
discardedCards: discardCards,
|
discardedCards: discardCards,
|
||||||
totalValue,
|
totalValue,
|
||||||
enemyAttack
|
enemyAttack,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查敌人是否被击败,如果击败则翻开下一个敌人
|
* 检查敌人是否被击败,如果击败则翻开下一个敌人
|
||||||
*/
|
*/
|
||||||
const checkEnemyDefeatedCmd = registry.register({
|
async function checkEnemyDefeatedCmd(game: RegicideGame) {
|
||||||
schema: 'check-enemy',
|
|
||||||
run: async (game: RegicideGame) => {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return {success: false, error: '没有活跃的敌人'};
|
return { success: false as const, error: "没有活跃的敌人" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const defeated = state.currentEnemy.hp <= 0;
|
const defeated = state.currentEnemy.hp <= 0;
|
||||||
|
|
||||||
if (defeated) {
|
if (defeated) {
|
||||||
const defeatedEnemy = {...state.currentEnemy};
|
const defeatedEnemy = { ...state.currentEnemy };
|
||||||
|
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
// 将当前敌人移到弃牌堆
|
// 将当前敌人移到弃牌堆
|
||||||
state.regions.discardPile.childIds.push(state.currentEnemy!.id);
|
state.regions.discardPile.childIds.push(state.currentEnemy!.id);
|
||||||
|
|
||||||
@@ -300,20 +303,20 @@ const checkEnemyDefeatedCmd = registry.register({
|
|||||||
|
|
||||||
// 检查是否胜利(没有更多敌人)
|
// 检查是否胜利(没有更多敌人)
|
||||||
if (!game.value.currentEnemy) {
|
if (!game.value.currentEnemy) {
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
state.phase = 'victory';
|
state.phase = "victory";
|
||||||
state.winner = true;
|
state.winner = true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true as const,
|
||||||
result: {
|
result: {
|
||||||
defeated: true,
|
defeated: true,
|
||||||
defeatedEnemy,
|
defeatedEnemy,
|
||||||
nextEnemy: game.value.currentEnemy,
|
nextEnemy: game.value.currentEnemy,
|
||||||
enemiesRemaining: game.value.enemyDeck.length
|
enemiesRemaining: game.value.enemyDeck.length,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,27 +324,24 @@ const checkEnemyDefeatedCmd = registry.register({
|
|||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
defeated: false,
|
defeated: false,
|
||||||
currentEnemy: {...state.currentEnemy},
|
currentEnemy: { ...state.currentEnemy },
|
||||||
enemiesRemaining: state.enemyDeck.length
|
enemiesRemaining: state.enemyDeck.length,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查玩家是否有可出的牌
|
* 检查玩家是否有可出的牌
|
||||||
*/
|
*/
|
||||||
const checkCanPlayCmd = registry.register({
|
async function checkCanPlayCmd(game: RegicideGame, player: string) {
|
||||||
schema: 'check-can-play <player:string>',
|
|
||||||
run: async (game: RegicideGame, player: string) => {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const playerKey = player as PlayerType;
|
const playerKey = player as PlayerType;
|
||||||
const playerHand = state.playerHands[playerKey];
|
const playerHand = state.playerHands[playerKey];
|
||||||
|
|
||||||
const canPlay = playerHand.length > 0;
|
const canPlay = playerHand.length > 0;
|
||||||
const canPlayWithA = playerHand.some(cardId => {
|
const canPlayWithA = playerHand.some((cardId) => {
|
||||||
const card = state.cards[cardId];
|
const card = state.cards[cardId];
|
||||||
return card && card.rank === 'A' && playerHand.length > 1;
|
return card && card.rank === "A" && playerHand.length > 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -349,27 +349,26 @@ const checkCanPlayCmd = registry.register({
|
|||||||
result: {
|
result: {
|
||||||
canPlay,
|
canPlay,
|
||||||
canPlayWithA,
|
canPlayWithA,
|
||||||
handSize: playerHand.length
|
handSize: playerHand.length,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查酒馆牌堆是否为空
|
* 检查酒馆牌堆是否为空
|
||||||
*/
|
*/
|
||||||
const checkTavernDeckCmd = registry.register({
|
async function checkTavernDeckCmd(game: RegicideGame) {
|
||||||
schema: 'check-tavern-deck',
|
|
||||||
run: async (game: RegicideGame) => {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const isEmpty = state.regions.tavernDeck.childIds.length === 0;
|
const isEmpty = state.regions.tavernDeck.childIds.length === 0;
|
||||||
|
|
||||||
// 如果酒馆牌堆为空且所有玩家手牌也为空,则游戏失败
|
// 如果酒馆牌堆为空且所有玩家手牌也为空,则游戏失败
|
||||||
if (isEmpty) {
|
if (isEmpty) {
|
||||||
const allHandsEmpty = Object.values(state.playerHands).every(hand => hand.length === 0);
|
const allHandsEmpty = Object.values(state.playerHands).every(
|
||||||
|
(hand) => hand.length === 0,
|
||||||
|
);
|
||||||
if (allHandsEmpty) {
|
if (allHandsEmpty) {
|
||||||
await game.produce(state => {
|
await game.produceAsync((state) => {
|
||||||
state.phase = 'defeat';
|
state.phase = "defeat";
|
||||||
state.winner = false;
|
state.winner = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -379,35 +378,32 @@ const checkTavernDeckCmd = registry.register({
|
|||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
isEmpty,
|
isEmpty,
|
||||||
cardsRemaining: state.regions.tavernDeck.childIds.length
|
cardsRemaining: state.regions.tavernDeck.childIds.length,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下一个玩家回合
|
* 下一个玩家回合
|
||||||
*/
|
*/
|
||||||
const nextTurnCmd = registry.register({
|
async function nextTurnCmd(game: RegicideGame) {
|
||||||
schema: 'next-turn',
|
|
||||||
run: async (game: RegicideGame) => {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
await game.produce(state => {
|
await game.produce((state) => {
|
||||||
state.currentPlayerIndex = (state.currentPlayerIndex + 1) % state.playerCount;
|
state.currentPlayerIndex =
|
||||||
|
(state.currentPlayerIndex + 1) % state.playerCount;
|
||||||
});
|
});
|
||||||
|
|
||||||
const players: PlayerType[] = ['player1', 'player2', 'player3', 'player4'];
|
const players: PlayerType[] = ["player1", "player2", "player3", "player4"];
|
||||||
const currentPlayer = players[game.value.currentPlayerIndex];
|
const currentPlayer = players[game.value.currentPlayerIndex];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
currentPlayer,
|
currentPlayer,
|
||||||
currentPlayerIndex: game.value.currentPlayerIndex
|
currentPlayerIndex: game.value.currentPlayerIndex,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
playCmd as play,
|
playCmd as play,
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import {IGameContext} from "@/core/game";
|
import { IGameContext } from "@/core/game";
|
||||||
import {RegicideState} from "@/samples/regicide/state";
|
import { RegicideState } from "@/samples/regicide/state";
|
||||||
import {buildEnemyDeck, buildTavernDeck, createAllCards, getPlayerHandRegionId} from "@/samples/regicide/utils";
|
import {
|
||||||
import {INITIAL_HAND_SIZE} from "@/samples/regicide/constants";
|
buildEnemyDeck,
|
||||||
import {Enemy, PlayerType, RegicideCard} from "@/samples/regicide/types";
|
buildTavernDeck,
|
||||||
|
createAllCards,
|
||||||
|
getPlayerHandRegionId,
|
||||||
|
} from "@/samples/regicide/utils";
|
||||||
|
import { INITIAL_HAND_SIZE } from "@/samples/regicide/constants";
|
||||||
|
import { Enemy, PlayerType, RegicideCard } from "@/samples/regicide/types";
|
||||||
|
import { checkEnemy, nextTurn, pass, playWithA } from "./commands";
|
||||||
|
import { enemyCounterattackCmd, playCmd } from ".";
|
||||||
|
|
||||||
export type RegicideGame = IGameContext<RegicideState>;
|
export type RegicideGame = IGameContext<RegicideState>;
|
||||||
|
|
||||||
@@ -12,9 +19,13 @@ export type RegicideGame = IGameContext<RegicideState>;
|
|||||||
* @param playerCount 玩家数量(1-4)
|
* @param playerCount 玩家数量(1-4)
|
||||||
* @param seed 随机种子(可选)
|
* @param seed 随机种子(可选)
|
||||||
*/
|
*/
|
||||||
export async function setupGame(game: RegicideGame, playerCount: number, seed?: number) {
|
export async function setupGame(
|
||||||
|
game: RegicideGame,
|
||||||
|
playerCount: number,
|
||||||
|
seed?: number,
|
||||||
|
) {
|
||||||
if (playerCount < 1 || playerCount > 4) {
|
if (playerCount < 1 || playerCount > 4) {
|
||||||
throw new Error('玩家数量必须为 1-4 人');
|
throw new Error("玩家数量必须为 1-4 人");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (seed) {
|
if (seed) {
|
||||||
@@ -31,7 +42,7 @@ export async function setupGame(game: RegicideGame, playerCount: number, seed?:
|
|||||||
const tavernDeck = buildTavernDeck(game._rng);
|
const tavernDeck = buildTavernDeck(game._rng);
|
||||||
|
|
||||||
// 初始化游戏状态
|
// 初始化游戏状态
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
state.cards = allCards;
|
state.cards = allCards;
|
||||||
state.playerCount = playerCount;
|
state.playerCount = playerCount;
|
||||||
state.currentPlayerIndex = 0;
|
state.currentPlayerIndex = 0;
|
||||||
@@ -39,15 +50,15 @@ export async function setupGame(game: RegicideGame, playerCount: number, seed?:
|
|||||||
|
|
||||||
// 设置酒馆牌堆区域
|
// 设置酒馆牌堆区域
|
||||||
for (const card of tavernDeck) {
|
for (const card of tavernDeck) {
|
||||||
card.regionId = 'tavernDeck';
|
card.regionId = "tavernDeck";
|
||||||
state.regions.tavernDeck.childIds.push(card.id);
|
state.regions.tavernDeck.childIds.push(card.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置敌人牌堆区域(只存储ID,敌人是独立对象)
|
// 设置敌人牌堆区域(只存储ID,敌人是独立对象)
|
||||||
state.regions.enemyDeck.childIds = enemyDeck.map(e => e.id);
|
state.regions.enemyDeck.childIds = enemyDeck.map((e) => e.id);
|
||||||
|
|
||||||
// 给每个玩家发牌
|
// 给每个玩家发牌
|
||||||
const players: PlayerType[] = ['player1', 'player2', 'player3', 'player4'];
|
const players: PlayerType[] = ["player1", "player2", "player3", "player4"];
|
||||||
for (let i = 0; i < playerCount; i++) {
|
for (let i = 0; i < playerCount; i++) {
|
||||||
const player = players[i];
|
const player = players[i];
|
||||||
const regionId = getPlayerHandRegionId(player);
|
const regionId = getPlayerHandRegionId(player);
|
||||||
@@ -78,13 +89,13 @@ export async function start(game: RegicideGame) {
|
|||||||
|
|
||||||
// 检查游戏是否已设置
|
// 检查游戏是否已设置
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
throw new Error('请先调用 setupGame 初始化游戏');
|
throw new Error("请先调用 setupGame 初始化游戏");
|
||||||
}
|
}
|
||||||
|
|
||||||
const players: PlayerType[] = ['player1', 'player2', 'player3', 'player4'];
|
const players: PlayerType[] = ["player1", "player2", "player3", "player4"];
|
||||||
|
|
||||||
// 主游戏循环
|
// 主游戏循环
|
||||||
while (state.phase === 'playing') {
|
while (state.phase === "playing") {
|
||||||
const currentPlayerIndex = state.currentPlayerIndex;
|
const currentPlayerIndex = state.currentPlayerIndex;
|
||||||
const currentPlayer = players[currentPlayerIndex];
|
const currentPlayer = players[currentPlayerIndex];
|
||||||
|
|
||||||
@@ -92,8 +103,9 @@ export async function start(game: RegicideGame) {
|
|||||||
const currentHand = state.playerHands[currentPlayer];
|
const currentHand = state.playerHands[currentPlayer];
|
||||||
if (currentHand.length === 0) {
|
if (currentHand.length === 0) {
|
||||||
// 玩家没有手牌,跳过回合
|
// 玩家没有手牌,跳过回合
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
state.currentPlayerIndex = (state.currentPlayerIndex + 1) % state.playerCount;
|
state.currentPlayerIndex =
|
||||||
|
(state.currentPlayerIndex + 1) % state.playerCount;
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -113,31 +125,37 @@ export async function start(game: RegicideGame) {
|
|||||||
/**
|
/**
|
||||||
* 处理完整的玩家回合
|
* 处理完整的玩家回合
|
||||||
*/
|
*/
|
||||||
export async function playTurn(game: RegicideGame, player: PlayerType, action: 'play' | 'pass', cardId?: string, secondCardId?: string) {
|
export async function playTurn(
|
||||||
|
game: RegicideGame,
|
||||||
|
player: PlayerType,
|
||||||
|
action: "play" | "pass",
|
||||||
|
cardId?: string,
|
||||||
|
secondCardId?: string,
|
||||||
|
) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
if (state.phase !== 'playing') {
|
if (state.phase !== "playing") {
|
||||||
return {success: false, error: '游戏已结束'};
|
return { success: false, error: "游戏已结束" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return {success: false, error: '没有活跃的敌人'};
|
return { success: false, error: "没有活跃的敌人" };
|
||||||
}
|
}
|
||||||
|
|
||||||
let playResult: any;
|
let playResult: any;
|
||||||
|
|
||||||
// 执行玩家动作
|
// 执行玩家动作
|
||||||
if (action === 'play' && cardId) {
|
if (action === "play" && cardId) {
|
||||||
// 检查是否是A配合另一张牌
|
// 检查是否是A配合另一张牌
|
||||||
const card = state.cards[cardId];
|
const card = state.cards[cardId];
|
||||||
if (card.rank === 'A' && secondCardId) {
|
if (card.rank === "A" && secondCardId) {
|
||||||
playResult = await game.run(`play-with-a ${player} ${cardId} ${secondCardId}`);
|
playResult = await playWithA(game, player, cardId, secondCardId);
|
||||||
} else {
|
} else {
|
||||||
playResult = await game.run(`play ${player} ${cardId}`);
|
playResult = await playCmd(game, player, cardId);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 让过
|
// 让过
|
||||||
playResult = await game.run(`pass ${player}`);
|
playResult = await pass(game, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!playResult.success) {
|
if (!playResult.success) {
|
||||||
@@ -145,7 +163,7 @@ export async function playTurn(game: RegicideGame, player: PlayerType, action: '
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查敌人是否被击败
|
// 检查敌人是否被击败
|
||||||
const checkResult = await game.run<{defeated: boolean; currentEnemy?: any; nextEnemy?: any; defeatedEnemy?: any; enemiesRemaining?: number}>('check-enemy');
|
const checkResult = await checkEnemy(game);
|
||||||
if (!checkResult.success) {
|
if (!checkResult.success) {
|
||||||
return checkResult;
|
return checkResult;
|
||||||
}
|
}
|
||||||
@@ -160,15 +178,19 @@ export async function playTurn(game: RegicideGame, player: PlayerType, action: '
|
|||||||
enemyDefeated: false,
|
enemyDefeated: false,
|
||||||
needsDiscard: true,
|
needsDiscard: true,
|
||||||
enemyAttack: state.currentEnemy.value,
|
enemyAttack: state.currentEnemy.value,
|
||||||
playerHand: state.playerHands[player]
|
playerHand: state.playerHands[player],
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 敌人被击败,检查是否还有更多敌人
|
// 敌人被击败,检查是否还有更多敌人
|
||||||
if (state.enemyDeck.length === 0 && state.currentEnemy && state.currentEnemy.hp <= 0) {
|
if (
|
||||||
await game.produceAsync(state => {
|
state.enemyDeck.length === 0 &&
|
||||||
state.phase = 'victory';
|
state.currentEnemy &&
|
||||||
|
state.currentEnemy.hp <= 0
|
||||||
|
) {
|
||||||
|
await game.produceAsync((state) => {
|
||||||
|
state.phase = "victory";
|
||||||
state.winner = true;
|
state.winner = true;
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
@@ -176,41 +198,44 @@ export async function playTurn(game: RegicideGame, player: PlayerType, action: '
|
|||||||
result: {
|
result: {
|
||||||
playResult: playResult.result,
|
playResult: playResult.result,
|
||||||
enemyDefeated: true,
|
enemyDefeated: true,
|
||||||
gameWon: true
|
gameWon: true,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换到下一个玩家
|
// 切换到下一个玩家
|
||||||
await game.run('next-turn');
|
await nextTurn(game);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
playResult: playResult.result,
|
playResult: playResult.result,
|
||||||
enemyDefeated: true,
|
enemyDefeated: true,
|
||||||
nextEnemy: state.currentEnemy
|
nextEnemy: state.currentEnemy,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理反击阶段的弃牌
|
* 处理反击阶段的弃牌
|
||||||
*/
|
*/
|
||||||
export async function handleCounterattack(game: RegicideGame, player: PlayerType, discardCardIds: string[]) {
|
export async function handleCounterattack(
|
||||||
const result = await game.run(`counterattack ${player} ${JSON.stringify(discardCardIds)}`);
|
game: RegicideGame,
|
||||||
|
player: PlayerType,
|
||||||
|
discardCardIds: string[],
|
||||||
|
) {
|
||||||
|
const result = await enemyCounterattackCmd(game, player, discardCardIds);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
// 弃牌失败(点数和不足),游戏失败
|
// 弃牌失败(点数和不足),游戏失败
|
||||||
await game.produceAsync(state => {
|
await game.produceAsync((state) => {
|
||||||
state.phase = 'defeat';
|
state.phase = "defeat";
|
||||||
state.winner = false;
|
state.winner = false;
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 弃牌成功,切换到下一个玩家
|
// 弃牌成功,切换到下一个玩家
|
||||||
await game.run('next-turn');
|
await nextTurn(game);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -223,17 +248,26 @@ export function getGameStatus(game: RegicideGame) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
phase: state.phase,
|
phase: state.phase,
|
||||||
currentPlayer: ['player1', 'player2', 'player3', 'player4'][state.currentPlayerIndex],
|
currentPlayer: ["player1", "player2", "player3", "player4"][
|
||||||
currentEnemy: state.currentEnemy ? {
|
state.currentPlayerIndex
|
||||||
|
],
|
||||||
|
currentEnemy: state.currentEnemy
|
||||||
|
? {
|
||||||
...state.currentEnemy,
|
...state.currentEnemy,
|
||||||
hpPercent: Math.round((state.currentEnemy.hp / state.currentEnemy.maxHp) * 100)
|
hpPercent: Math.round(
|
||||||
} : null,
|
(state.currentEnemy.hp / state.currentEnemy.maxHp) * 100,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: null,
|
||||||
enemiesRemaining: state.enemyDeck.length,
|
enemiesRemaining: state.enemyDeck.length,
|
||||||
tavernDeckCount: state.regions.tavernDeck.childIds.length,
|
tavernDeckCount: state.regions.tavernDeck.childIds.length,
|
||||||
discardPileCount: state.regions.discardPile.childIds.length,
|
discardPileCount: state.regions.discardPile.childIds.length,
|
||||||
playerHands: Object.fromEntries(
|
playerHands: Object.fromEntries(
|
||||||
Object.entries(state.playerHands).map(([player, hand]) => [player, hand.length])
|
Object.entries(state.playerHands).map(([player, hand]) => [
|
||||||
|
player,
|
||||||
|
hand.length,
|
||||||
|
]),
|
||||||
),
|
),
|
||||||
winner: state.winner
|
winner: state.winner,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ export type {
|
|||||||
RegicideCardMeta,
|
RegicideCardMeta,
|
||||||
RegicideCard,
|
RegicideCard,
|
||||||
Enemy,
|
Enemy,
|
||||||
GamePhase
|
GamePhase,
|
||||||
} from './types';
|
} from "./types";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
export {
|
export {
|
||||||
@@ -18,21 +18,17 @@ export {
|
|||||||
ALL_SUITS,
|
ALL_SUITS,
|
||||||
ALL_RANKS,
|
ALL_RANKS,
|
||||||
FACE_CARDS,
|
FACE_CARDS,
|
||||||
NUMBER_CARDS
|
NUMBER_CARDS,
|
||||||
} from './constants';
|
} from "./constants";
|
||||||
|
|
||||||
// State
|
// State
|
||||||
export {
|
export { createInitialState, type RegicideState } from "./state";
|
||||||
createInitialState,
|
|
||||||
type RegicideState
|
|
||||||
} from './state';
|
|
||||||
|
|
||||||
// Prompts
|
// Prompts
|
||||||
export {prompts} from './prompts';
|
export { prompts } from "./prompts";
|
||||||
|
|
||||||
// Commands
|
// Commands
|
||||||
export {
|
export {
|
||||||
registry,
|
|
||||||
play as playCmd,
|
play as playCmd,
|
||||||
playWithA as playWithACmd,
|
playWithA as playWithACmd,
|
||||||
pass as passCmd,
|
pass as passCmd,
|
||||||
@@ -41,7 +37,7 @@ export {
|
|||||||
checkCanPlay as checkCanPlayCmd,
|
checkCanPlay as checkCanPlayCmd,
|
||||||
checkTavernDeck as checkTavernDeckCmd,
|
checkTavernDeck as checkTavernDeckCmd,
|
||||||
nextTurn as nextTurnCmd,
|
nextTurn as nextTurnCmd,
|
||||||
} from './commands';
|
} from "./commands";
|
||||||
|
|
||||||
// Game
|
// Game
|
||||||
export {
|
export {
|
||||||
@@ -50,8 +46,8 @@ export {
|
|||||||
playTurn,
|
playTurn,
|
||||||
handleCounterattack,
|
handleCounterattack,
|
||||||
getGameStatus,
|
getGameStatus,
|
||||||
type RegicideGame
|
type RegicideGame,
|
||||||
} from './game';
|
} from "./game";
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
export {
|
export {
|
||||||
@@ -63,5 +59,5 @@ export {
|
|||||||
buildTavernDeck,
|
buildTavernDeck,
|
||||||
drawFromDeck,
|
drawFromDeck,
|
||||||
isEnemyDefeated,
|
isEnemyDefeated,
|
||||||
getPlayerHandRegionId
|
getPlayerHandRegionId,
|
||||||
} from './utils';
|
} from "./utils";
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
import { IRunContext } from "@/samples/slay-the-spire-like/system/combat/types";
|
|
||||||
import { Triggers } from "@/samples/slay-the-spire-like/system/combat/triggers";
|
|
||||||
import { addInstantEffectTriggers } from "./instant";
|
|
||||||
import { addDamageTriggers } from "./damage";
|
|
||||||
import { addTurnStartTriggers } from "./turn-start";
|
|
||||||
import { addCardEventTriggers } from "./card-events";
|
|
||||||
|
|
||||||
export function addDesertTriggers(triggers: Triggers, run: IRunContext) {
|
|
||||||
addInstantEffectTriggers(triggers);
|
|
||||||
addDamageTriggers(triggers);
|
|
||||||
addTurnStartTriggers(triggers);
|
|
||||||
addCardEventTriggers(triggers, run);
|
|
||||||
}
|
|
||||||
@@ -1 +1,15 @@
|
|||||||
export { addDesertTriggers as addTriggers } from './effect';
|
import { addInstantEffectTriggers } from "./instant";
|
||||||
|
import { addDamageTriggers } from "./damage";
|
||||||
|
import { addTurnStartTriggers } from "./turn-start";
|
||||||
|
import { addCardEventTriggers } from "./card-events";
|
||||||
|
import {
|
||||||
|
IRunContext,
|
||||||
|
Triggers,
|
||||||
|
} from "@/samples/slay-the-spire-like/system/combat";
|
||||||
|
|
||||||
|
export function addTriggers(triggers: Triggers, run: IRunContext) {
|
||||||
|
addInstantEffectTriggers(triggers);
|
||||||
|
addDamageTriggers(triggers);
|
||||||
|
addTurnStartTriggers(triggers);
|
||||||
|
addCardEventTriggers(triggers, run);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,16 +2,17 @@ import { LoadResult as YarnDialogues } from "yarn-spinner-loader";
|
|||||||
import { Triggers } from "../system/combat/triggers";
|
import { Triggers } from "../system/combat/triggers";
|
||||||
|
|
||||||
import type * as desert from "./desert";
|
import type * as desert from "./desert";
|
||||||
|
import { IRunContext } from "../system/combat";
|
||||||
|
|
||||||
export type ContentModule = {
|
export type ContentModule = {
|
||||||
getCards: () => desert.Card[];
|
readonly getCards: () => readonly desert.Card[];
|
||||||
getEffects: () => desert.Effect[];
|
readonly getEffects: () => readonly desert.Effect[];
|
||||||
getEncounters: () => desert.Encounter[];
|
readonly getEncounters: () => readonly desert.Encounter[];
|
||||||
getEnemies: () => desert.Enemy[];
|
readonly getEnemies: () => readonly desert.Enemy[];
|
||||||
getIntents: () => desert.Intent[];
|
readonly getIntents: () => readonly desert.Intent[];
|
||||||
getItems: () => desert.Item[];
|
readonly getItems: () => readonly desert.Item[];
|
||||||
getStartingItems: () => desert.Item[];
|
readonly getStartingItems: () => readonly desert.Item[];
|
||||||
|
|
||||||
dialogues: YarnDialogues;
|
readonly dialogues: YarnDialogues;
|
||||||
addTriggers: (triggers: Triggers) => void;
|
readonly addTriggers: (triggers: Triggers, run: IRunContext) => void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -287,12 +287,7 @@ export function createTriggers(run: IRunContext) {
|
|||||||
return triggers;
|
return triggers;
|
||||||
}
|
}
|
||||||
export type Triggers = ReturnType<typeof createTriggers>;
|
export type Triggers = ReturnType<typeof createTriggers>;
|
||||||
export function createStartWith(
|
export function createStart(triggers: Triggers, run: IRunContext) {
|
||||||
build: (triggers: Triggers, run: IRunContext) => void,
|
|
||||||
run: IRunContext,
|
|
||||||
) {
|
|
||||||
const triggers = createTriggers(run);
|
|
||||||
build(triggers, run);
|
|
||||||
return async function (game: CombatGameContext) {
|
return async function (game: CombatGameContext) {
|
||||||
await triggers.onCombatStart.execute(game, {});
|
await triggers.onCombatStart.execute(game, {});
|
||||||
|
|
||||||
|
|||||||
@@ -21,3 +21,5 @@ export {
|
|||||||
CardEffectTarget,
|
CardEffectTarget,
|
||||||
IntentEffectTarget,
|
IntentEffectTarget,
|
||||||
} from "../data/desert";
|
} from "../data/desert";
|
||||||
|
|
||||||
|
export { ContentModule } from "../data";
|
||||||
|
|||||||
+57
-16
@@ -1,10 +1,8 @@
|
|||||||
import { Part } from "@/core/part";
|
import { Part } from "@/core/part";
|
||||||
import { createRegion } from "@/core/region";
|
import { createRegion, Region } from "@/core/region";
|
||||||
import {
|
import { createPromptDef, IGameContext } from "@/core/game";
|
||||||
createGameCommandRegistry,
|
import { BaseGameClient } from "@/core/game-client";
|
||||||
createPromptDef,
|
import { createMiddlewareChain } from "@/utils/middleware";
|
||||||
IGameContext,
|
|
||||||
} from "@/core/game";
|
|
||||||
|
|
||||||
const BOARD_SIZE = 3;
|
const BOARD_SIZE = 3;
|
||||||
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
||||||
@@ -69,7 +67,6 @@ export function createInitialState() {
|
|||||||
}
|
}
|
||||||
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
||||||
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
||||||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
|
||||||
export const prompts = {
|
export const prompts = {
|
||||||
play: createPromptDef<[PlayerType, number, number]>(
|
play: createPromptDef<[PlayerType, number, number]>(
|
||||||
"play <player> <row:number> <col:number>",
|
"play <player> <row:number> <col:number>",
|
||||||
@@ -82,7 +79,7 @@ export async function start(game: TicTacToeGame) {
|
|||||||
const turnNumber = game.value.turn + 1;
|
const turnNumber = game.value.turn + 1;
|
||||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||||
|
|
||||||
game.produce((state) => {
|
await game.produceAsync((state) => {
|
||||||
state.winner = turnOutput.winner;
|
state.winner = turnOutput.winner;
|
||||||
if (!state.winner) {
|
if (!state.winner) {
|
||||||
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||||
@@ -95,9 +92,11 @@ export async function start(game: TicTacToeGame) {
|
|||||||
return game.value;
|
return game.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const turn = registry.register({
|
async function turn(
|
||||||
schema: "turn <player> <turnNumber:number>",
|
game: TicTacToeGame,
|
||||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
turnPlayer: PlayerType,
|
||||||
|
turnNumber: number,
|
||||||
|
) {
|
||||||
const { player, row, col } = await game.prompt(
|
const { player, row, col } = await game.prompt(
|
||||||
prompts.play,
|
prompts.play,
|
||||||
(player, row, col) => {
|
(player, row, col) => {
|
||||||
@@ -114,15 +113,14 @@ const turn = registry.register({
|
|||||||
game.value.currentPlayer,
|
game.value.currentPlayer,
|
||||||
);
|
);
|
||||||
|
|
||||||
placePiece(game, row, col, turnPlayer);
|
await placePiece(game, row, col, turnPlayer);
|
||||||
|
|
||||||
const winner = checkWinner(game);
|
const winner = checkWinner(game);
|
||||||
if (winner) return { winner };
|
if (winner) return { winner };
|
||||||
if (turnNumber >= MAX_TURNS) return { winner: "draw" as WinnerType };
|
if (turnNumber >= MAX_TURNS) return { winner: "draw" as WinnerType };
|
||||||
|
|
||||||
return { winner: null };
|
return { winner: null };
|
||||||
},
|
}
|
||||||
});
|
|
||||||
|
|
||||||
function isValidMove(row: number, col: number): boolean {
|
function isValidMove(row: number, col: number): boolean {
|
||||||
return (
|
return (
|
||||||
@@ -167,7 +165,7 @@ export function checkWinner(host: TicTacToeGame): WinnerType {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function placePiece(
|
export async function placePiece(
|
||||||
host: TicTacToeGame,
|
host: TicTacToeGame,
|
||||||
row: number,
|
row: number,
|
||||||
col: number,
|
col: number,
|
||||||
@@ -181,9 +179,52 @@ export function placePiece(
|
|||||||
player,
|
player,
|
||||||
id: `piece-${player}-${moveNumber}`,
|
id: `piece-${player}-${moveNumber}`,
|
||||||
};
|
};
|
||||||
host.produce((state) => {
|
await host.produceAsync((state) => {
|
||||||
state.parts[piece.id] = piece;
|
state.parts[piece.id] = piece;
|
||||||
board.childIds.push(piece.id);
|
board.childIds.push(piece.id);
|
||||||
board.partMap[`${row},${col}`] = piece.id;
|
board.partMap[`${row},${col}`] = piece.id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Client extends BaseGameClient<TicTacToeState> {
|
||||||
|
private onTurn = createMiddlewareChain(
|
||||||
|
async (ctx: { player: PlayerType; turn: number }) => {
|
||||||
|
return await turn(this._context, ctx.player, ctx.turn);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super(createInitialState());
|
||||||
|
}
|
||||||
|
|
||||||
|
async start(game: IGameContext<TicTacToeState>): Promise<TicTacToeState> {
|
||||||
|
while (true) {
|
||||||
|
const currentPlayer = game.value.currentPlayer;
|
||||||
|
const turnNumber = game.value.turn + 1;
|
||||||
|
const turnOutput = await this.onTurn.execute({
|
||||||
|
player: currentPlayer,
|
||||||
|
turn: turnNumber,
|
||||||
|
});
|
||||||
|
|
||||||
|
await game.produceAsync((state) => {
|
||||||
|
state.winner = turnOutput.winner;
|
||||||
|
if (!state.winner) {
|
||||||
|
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||||
|
state.turn = turnNumber;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (game.value.winner) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return game.value;
|
||||||
|
}
|
||||||
|
selectPart(id: string, callback: (t: TicTacToePart) => void): () => void {
|
||||||
|
return this.assignSelector((state) => state.parts[id], callback);
|
||||||
|
}
|
||||||
|
selectTurn(callback: (turns: number) => void): () => void {
|
||||||
|
return this.assignSelector((s) => s.turn, callback);
|
||||||
|
}
|
||||||
|
selectCurrentPlayer(callback: (player: PlayerType) => void) {
|
||||||
|
return this.assignSelector((s) => s.currentPlayer, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import { createMiddlewareChain } from "../middleware";
|
||||||
|
import { PromptValidator } from "./command-runner";
|
||||||
|
import { CommandSchema } from "./types";
|
||||||
|
|
||||||
|
export interface PromptDef<TArgs extends any[]> {
|
||||||
|
schema: CommandSchema;
|
||||||
|
hintText?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PromptCall<TArgs extends any[] = unknown[], TRes = unknown> {
|
||||||
|
def: PromptDef<TArgs>;
|
||||||
|
player?: string;
|
||||||
|
validator: PromptValidator<TRes, TArgs>;
|
||||||
|
|
||||||
|
resolve(res: TRes): void;
|
||||||
|
reject(reason: string): void;
|
||||||
|
promise: Promise<TRes>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PromptTryResult =
|
||||||
|
| {
|
||||||
|
ok: false;
|
||||||
|
reason: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
ok: true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PromptContext = ReturnType<typeof createPromptContext>;
|
||||||
|
export function createPromptContext() {
|
||||||
|
const map = new Map<string, PromptCall>();
|
||||||
|
const handleCall = createMiddlewareChain(async (call: PromptCall) => {
|
||||||
|
const key = call.player ?? "global";
|
||||||
|
const existing = map.get(key);
|
||||||
|
if (existing) {
|
||||||
|
existing.reject("Prompt overriden");
|
||||||
|
}
|
||||||
|
|
||||||
|
map.set(key, call);
|
||||||
|
try {
|
||||||
|
return await call.promise;
|
||||||
|
} finally {
|
||||||
|
if (map.get(key) === call) map.delete(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function tryCommit<TArgs extends any[]>(
|
||||||
|
def: PromptDef<TArgs>,
|
||||||
|
player: string,
|
||||||
|
...args: TArgs
|
||||||
|
): PromptTryResult {
|
||||||
|
const call = map.get(player);
|
||||||
|
if (!call) return { ok: false, reason: "No Prompt" };
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = call.validator(...args);
|
||||||
|
call.resolve(res);
|
||||||
|
return { ok: true };
|
||||||
|
} catch (reason) {
|
||||||
|
if (typeof reason === "string") return { ok: false, reason };
|
||||||
|
throw reason;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancel(player: string, reason = "Prompt Cancelled") {
|
||||||
|
map.get(player)?.reject(reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function prompt<TArgs extends any[], TRes>(
|
||||||
|
def: PromptDef<TArgs>,
|
||||||
|
validator: PromptValidator<TRes, TArgs>,
|
||||||
|
player?: string,
|
||||||
|
) {
|
||||||
|
let resolve: (res: TRes) => void;
|
||||||
|
let reject: (reason: string) => void;
|
||||||
|
const promise = new Promise<TRes>((res, rej) => {
|
||||||
|
resolve = res;
|
||||||
|
reject = rej;
|
||||||
|
});
|
||||||
|
const call = {
|
||||||
|
def,
|
||||||
|
player,
|
||||||
|
validator,
|
||||||
|
resolve: resolve!,
|
||||||
|
reject: reject!,
|
||||||
|
promise,
|
||||||
|
} as PromptCall<unknown[], unknown>;
|
||||||
|
return (await handleCall.execute(call)) as TRes;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
for (const call of map.values()) {
|
||||||
|
call.reject("Prompt Reset");
|
||||||
|
}
|
||||||
|
map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
prompt,
|
||||||
|
tryCommit,
|
||||||
|
cancel,
|
||||||
|
handleCall,
|
||||||
|
reset,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
export { parseCommand } from './command-parse';
|
export { parseCommand } from "./command-parse";
|
||||||
export { parseCommandSchema } from './schema-parse';
|
export { parseCommandSchema } from "./schema-parse";
|
||||||
export { validateCommand, parseCommandWithSchema, applyCommandSchema } from './command-validate';
|
export {
|
||||||
|
validateCommand,
|
||||||
|
parseCommandWithSchema,
|
||||||
|
applyCommandSchema,
|
||||||
|
} from "./command-validate";
|
||||||
export {
|
export {
|
||||||
createCommandRegistry,
|
createCommandRegistry,
|
||||||
registerCommand,
|
registerCommand,
|
||||||
@@ -10,13 +14,28 @@ export {
|
|||||||
runCommand,
|
runCommand,
|
||||||
runCommandParsed,
|
runCommandParsed,
|
||||||
createCommandRunnerContext,
|
createCommandRunnerContext,
|
||||||
} from './command-registry';
|
} from "./command-registry";
|
||||||
export type {
|
export type {
|
||||||
Command,
|
Command,
|
||||||
CommandParamSchema,
|
CommandParamSchema,
|
||||||
CommandOptionSchema,
|
CommandOptionSchema,
|
||||||
CommandFlagSchema,
|
CommandFlagSchema,
|
||||||
CommandSchema,
|
CommandSchema,
|
||||||
} from './types';
|
} from "./types";
|
||||||
export type { CommandRunner, CommandDef, CommandResult, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './command-runner';
|
export type {
|
||||||
export type { CommandRegistry, CommandRunnerContextExport } from './command-registry';
|
CommandRunner,
|
||||||
|
CommandDef,
|
||||||
|
CommandResult,
|
||||||
|
CommandRunnerHandler,
|
||||||
|
CommandRunnerContext,
|
||||||
|
PromptEvent,
|
||||||
|
CommandRunnerEvents,
|
||||||
|
} from "./command-runner";
|
||||||
|
export type {
|
||||||
|
CommandRegistry,
|
||||||
|
CommandRunnerContextExport,
|
||||||
|
} from "./command-registry";
|
||||||
|
|
||||||
|
export type { PromptDef, PromptTryResult, PromptCall } from "./command-prompt";
|
||||||
|
|
||||||
|
export { createPromptContext } from "./command-prompt";
|
||||||
|
|||||||
+15
-5
@@ -1,28 +1,38 @@
|
|||||||
type Middleware<TContext, TReturn> = (
|
type Middleware<TContext, TReturn> = (
|
||||||
context: TContext,
|
context: TContext,
|
||||||
next: () => Promise<TReturn>
|
next: () => Promise<TReturn>,
|
||||||
) => Promise<TReturn>;
|
) => Promise<TReturn>;
|
||||||
|
|
||||||
export type MiddlewareChain<TContext, TReturn> = {
|
export type MiddlewareChain<TContext, TReturn> = {
|
||||||
use: (middleware: Middleware<TContext, TReturn>) => void;
|
use: (middleware: Middleware<TContext, TReturn>) => () => void;
|
||||||
execute: (context: TContext) => Promise<TReturn>;
|
execute: (context: TContext) => Promise<TReturn>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createMiddlewareChain<TContext extends object, TReturn=TContext>(
|
export function createMiddlewareChain<
|
||||||
fallback?: (context: TContext) => Promise<TReturn>
|
TContext extends object,
|
||||||
|
TReturn = TContext,
|
||||||
|
>(
|
||||||
|
fallback?: (context: TContext) => Promise<TReturn>,
|
||||||
): MiddlewareChain<TContext, TReturn> {
|
): MiddlewareChain<TContext, TReturn> {
|
||||||
const middlewares: Middleware<TContext, TReturn>[] = [];
|
const middlewares: Middleware<TContext, TReturn>[] = [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
use(middleware: Middleware<TContext, TReturn>) {
|
use(middleware: Middleware<TContext, TReturn>) {
|
||||||
middlewares.push(middleware);
|
middlewares.push(middleware);
|
||||||
|
|
||||||
|
return function () {
|
||||||
|
const index = middlewares.indexOf(middleware);
|
||||||
|
if (index !== -1) {
|
||||||
|
middlewares.splice(index, 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
},
|
},
|
||||||
async execute(context: TContext) {
|
async execute(context: TContext) {
|
||||||
let index = 0;
|
let index = 0;
|
||||||
|
|
||||||
async function dispatch(ctx: TContext): Promise<TReturn> {
|
async function dispatch(ctx: TContext): Promise<TReturn> {
|
||||||
if (index >= middlewares.length) {
|
if (index >= middlewares.length) {
|
||||||
return fallback ? fallback(ctx) : ctx as unknown as TReturn;
|
return fallback ? fallback(ctx) : (ctx as unknown as TReturn);
|
||||||
}
|
}
|
||||||
const current = middlewares[index++];
|
const current = middlewares[index++];
|
||||||
return current(ctx, () => dispatch(ctx));
|
return current(ctx, () => dispatch(ctx));
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
import { Signal, SignalOptions } from "@preact/signals-core";
|
import { Signal, SignalOptions } from "@preact/signals-core";
|
||||||
import { create } from "mutative";
|
import { create } from "mutative";
|
||||||
|
|
||||||
export class MutableSignal<T> extends Signal<T> {
|
export interface MutableSignal<T> extends Signal<T> {
|
||||||
|
produce(fn: (draft: T) => undefined): void;
|
||||||
|
addInterruption(promise: Promise<void>): void;
|
||||||
|
clearInterruptions(): void;
|
||||||
|
produceAsync(fn: (draft: T) => undefined): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MutableSignalImpl<T> extends Signal<T> {
|
||||||
private _interruptions: Promise<void>[] = [];
|
private _interruptions: Promise<void>[] = [];
|
||||||
|
|
||||||
public constructor(t?: T, options?: SignalOptions<T>) {
|
public constructor(t?: T, options?: SignalOptions<T>) {
|
||||||
@@ -41,5 +48,5 @@ export function mutableSignal<T>(
|
|||||||
initial?: T,
|
initial?: T,
|
||||||
options?: SignalOptions<T>,
|
options?: SignalOptions<T>,
|
||||||
): MutableSignal<T> {
|
): MutableSignal<T> {
|
||||||
return new MutableSignal<T>(initial, options);
|
return new MutableSignalImpl<T>(initial, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { effect } from "@preact/signals-core";
|
||||||
|
|
||||||
|
export type SpawnerFunction = (id: string) => () => void;
|
||||||
|
|
||||||
|
export function createSpawner(spawner: SpawnerFunction) {
|
||||||
|
const entries = new Map<string, () => void>();
|
||||||
|
|
||||||
|
function update(set: Set<string>) {
|
||||||
|
for (const key of set) {
|
||||||
|
if (!entries.has(key)) {
|
||||||
|
entries.set(key, spawner(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [key, entry] of entries) {
|
||||||
|
if (!set.has(key)) {
|
||||||
|
entry();
|
||||||
|
entries.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return update;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function spawnerEffect(
|
||||||
|
iterable: () => Iterable<string>,
|
||||||
|
spawner: SpawnerFunction,
|
||||||
|
) {
|
||||||
|
const update = createSpawner(spawner);
|
||||||
|
|
||||||
|
return effect(() => {
|
||||||
|
const set = new Set<string>();
|
||||||
|
for (const each of iterable()) {
|
||||||
|
set.add(each);
|
||||||
|
}
|
||||||
|
update(set);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { createPromptContext } from "@/utils/command/command-prompt";
|
||||||
|
import type { CommandSchema } from "@/utils/command/types";
|
||||||
|
|
||||||
|
const mockSchema: CommandSchema = {
|
||||||
|
name: "test",
|
||||||
|
params: [{ name: "value", required: true, variadic: false }],
|
||||||
|
options: {},
|
||||||
|
flags: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("createPromptContext", () => {
|
||||||
|
describe("prompt", () => {
|
||||||
|
it("should store prompt and return promise", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator);
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
const result = ctx.tryCommit("global", "hello");
|
||||||
|
expect(result).toEqual({ ok: true });
|
||||||
|
await expect(promise).resolves.toBe("hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should associate prompt with specific player", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
const result = ctx.tryCommit("player1", "hello");
|
||||||
|
expect(result).toEqual({ ok: true });
|
||||||
|
await expect(promise).resolves.toBe("hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should cancel existing prompt when new prompt starts for same player", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise1 = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
const promise2 = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
|
||||||
|
// Resolve promise2 first, then both will settle
|
||||||
|
ctx.tryCommit("player1", "test");
|
||||||
|
const [result1, result2] = await Promise.allSettled([promise1, promise2]);
|
||||||
|
|
||||||
|
expect(result1.status).toBe("rejected");
|
||||||
|
expect((result1 as PromiseRejectedResult).reason).toBe(
|
||||||
|
"Prompt overriden",
|
||||||
|
);
|
||||||
|
expect(result2.status).toBe("fulfilled");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should allow different players to have independent prompts", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise1 = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
const promise2 = ctx.prompt({ schema: mockSchema }, validator, "player2");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.tryCommit("player1", "hello");
|
||||||
|
ctx.tryCommit("player2", "world");
|
||||||
|
|
||||||
|
await expect(promise1).resolves.toBe("hello");
|
||||||
|
await expect(promise2).resolves.toBe("world");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("tryCommit", () => {
|
||||||
|
it("should return ok:true when prompt exists and validation passes", () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value.toUpperCase();
|
||||||
|
|
||||||
|
ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
const result = ctx.tryCommit("player1", "hello");
|
||||||
|
|
||||||
|
expect(result).toEqual({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return ok:false with reason when no prompt exists", () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const result = ctx.tryCommit("nonexistent", "hello");
|
||||||
|
|
||||||
|
expect(result).toEqual({ ok: false, reason: "No Prompt" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return ok:false when validator throws string", () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => {
|
||||||
|
if (value.length < 3) throw "Value too short";
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
const result = ctx.tryCommit("player1", "ab");
|
||||||
|
|
||||||
|
expect(result).toEqual({ ok: false, reason: "Value too short" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw when validator throws non-string error", () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = () => {
|
||||||
|
throw new Error("unexpected");
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
expect(() => ctx.tryCommit("player1", "test")).toThrow("unexpected");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should pass validator return value to promise resolver", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (a: number, b: number) => a + b;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.tryCommit("player1", 2, 3);
|
||||||
|
await expect(promise).resolves.toBe(5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("cancel", () => {
|
||||||
|
it("should reject promise with default message", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.cancel("player1");
|
||||||
|
|
||||||
|
const result = await Promise.allSettled([promise]);
|
||||||
|
expect(result[0].status).toBe("rejected");
|
||||||
|
expect((result[0] as PromiseRejectedResult).reason).toBe(
|
||||||
|
"Prompt Cancelled",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject promise with custom reason", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.cancel("player1", "Custom cancel reason");
|
||||||
|
|
||||||
|
const result = await Promise.allSettled([promise]);
|
||||||
|
expect(result[0].status).toBe("rejected");
|
||||||
|
expect((result[0] as PromiseRejectedResult).reason).toBe(
|
||||||
|
"Custom cancel reason",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should do nothing when no prompt exists for player", () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
ctx.cancel("nonexistent");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should only cancel prompt for specific player", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise1 = ctx.prompt({ schema: mockSchema }, validator, "player1");
|
||||||
|
const promise2 = ctx.prompt({ schema: mockSchema }, validator, "player2");
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.cancel("player1");
|
||||||
|
ctx.tryCommit("player2", "test");
|
||||||
|
|
||||||
|
const [result1, result2] = await Promise.allSettled([promise1, promise2]);
|
||||||
|
expect(result1.status).toBe("rejected");
|
||||||
|
expect((result1 as PromiseRejectedResult).reason).toBe(
|
||||||
|
"Prompt Cancelled",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result2.status).toBe("fulfilled");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("handleCall", () => {
|
||||||
|
it("should be a middleware chain that can be extended", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
let middlewareCalled = false;
|
||||||
|
ctx.handleCall.use(async (call, next) => {
|
||||||
|
middlewareCalled = true;
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator);
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
expect(middlewareCalled).toBe(true);
|
||||||
|
ctx.tryCommit("global", "test");
|
||||||
|
await expect(promise).resolves.toBe("test");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should remove prompt from map after resolution", async () => {
|
||||||
|
const ctx = createPromptContext();
|
||||||
|
const validator = (value: string) => value;
|
||||||
|
|
||||||
|
const promise = ctx.prompt({ schema: mockSchema }, validator);
|
||||||
|
await new Promise((r) => setTimeout(r, 0));
|
||||||
|
|
||||||
|
ctx.tryCommit("global", "test");
|
||||||
|
await promise;
|
||||||
|
|
||||||
|
const result = ctx.tryCommit("global", "another");
|
||||||
|
expect(result).toEqual({ ok: false, reason: "No Prompt" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user