Compare commits
No commits in common. "f5df1c26dc3b647ca2ea7d70acbdaa861867d33b" and "59b99d2042a162964b8d8ac6e01bf0a81cd4b4cf" have entirely different histories.
f5df1c26dc
...
59b99d2042
|
|
@ -1,40 +1,100 @@
|
||||||
import { ReadonlySignal, Signal } from "@preact/signals-core";
|
import { ReadonlySignal, Signal } from "@preact/signals-core";
|
||||||
import { createPromptContext } from "@/utils/command";
|
import { CommandSchema, CommandRegistry, PromptEvent } from "@/utils/command";
|
||||||
import { createGameContext, IGameContext } from "./game";
|
import {
|
||||||
import { PromptContext } from "@/utils/command/command-prompt";
|
createGameCommandRegistry,
|
||||||
|
createGameContext,
|
||||||
|
IGameContext,
|
||||||
|
PromptDef,
|
||||||
|
} from "./game";
|
||||||
|
|
||||||
export type GameHostStatus = "created" | "running" | "disposed";
|
export type GameHostStatus = "created" | "running" | "disposed";
|
||||||
|
|
||||||
export class GameHost<TModule extends GameModule> {
|
export class GameHost<
|
||||||
readonly state: ReadonlySignal<GameState<TModule>>;
|
TState extends Record<string, unknown>,
|
||||||
|
TResult = unknown,
|
||||||
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
|
> {
|
||||||
|
readonly state: ReadonlySignal<TState>;
|
||||||
readonly status: ReadonlySignal<GameHostStatus>;
|
readonly status: ReadonlySignal<GameHostStatus>;
|
||||||
readonly prompts: PromptContext;
|
readonly activePromptSchema: ReadonlySignal<CommandSchema | null>;
|
||||||
|
readonly activePromptPlayer: ReadonlySignal<string | null>;
|
||||||
|
readonly activePromptHint: ReadonlySignal<string | null>;
|
||||||
|
|
||||||
private _context: IGameContext<GameState<TModule>>;
|
private _context: IGameContext<TState>;
|
||||||
private _start: (
|
private _start: (ctx: IGameContext<TState>) => Promise<TResult>;
|
||||||
ctx: IGameContext<GameState<TModule>>,
|
|
||||||
) => Promise<GameResult<TModule>>;
|
|
||||||
private _status: Signal<GameHostStatus>;
|
private _status: Signal<GameHostStatus>;
|
||||||
private _createInitialState: () => GameState<TModule>;
|
private _activePromptSchema: Signal<CommandSchema | null>;
|
||||||
|
private _activePromptPlayer: Signal<string | null>;
|
||||||
|
private _activePromptHint: Signal<string | null>;
|
||||||
|
private _createInitialState: () => TState;
|
||||||
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
private _eventListeners: Map<"start" | "dispose", Set<() => void>>;
|
||||||
private _isDisposed = false;
|
private _isDisposed = false;
|
||||||
|
|
||||||
constructor(public readonly gameModule: TModule) {
|
constructor(public readonly gameModule: TModule) {
|
||||||
const { createInitialState, start } = gameModule as unknown as GameModule<
|
const { createInitialState, registry, start } = gameModule;
|
||||||
GameState<TModule>,
|
|
||||||
GameResult<TModule>
|
|
||||||
>;
|
|
||||||
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(this.prompts, initialState);
|
this._context = createGameContext(
|
||||||
|
registry ?? createGameCommandRegistry(),
|
||||||
|
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: {},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -53,12 +113,12 @@ export class GameHost<TModule extends GameModule> {
|
||||||
this._context._state.clearInterruptions();
|
this._context._state.clearInterruptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
start(seed?: number): Promise<GameResult<TModule>> {
|
start(seed?: number): Promise<TResult> {
|
||||||
if (this._isDisposed) {
|
if (this._isDisposed) {
|
||||||
throw new Error("GameHost is disposed");
|
throw new Error("GameHost is disposed");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.prompts.reset();
|
this._context._commands._cancel();
|
||||||
|
|
||||||
const initialState = this._createInitialState();
|
const initialState = this._createInitialState();
|
||||||
this._context._state.value = initialState as any;
|
this._context._state.value = initialState as any;
|
||||||
|
|
@ -79,7 +139,7 @@ export class GameHost<TModule extends GameModule> {
|
||||||
}
|
}
|
||||||
|
|
||||||
this._isDisposed = true;
|
this._isDisposed = true;
|
||||||
this.prompts.reset();
|
this._context._commands._cancel();
|
||||||
this._status.value = "disposed";
|
this._status.value = "disposed";
|
||||||
|
|
||||||
// Emit dispose event BEFORE clearing listeners
|
// Emit dispose event BEFORE clearing listeners
|
||||||
|
|
@ -109,21 +169,18 @@ export class GameHost<TModule extends GameModule> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GameModule<
|
export type GameModule<
|
||||||
TState extends Record<string, unknown> = Record<string, unknown>,
|
TState extends 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 type GameState<TModule extends GameModule> = ReturnType<
|
|
||||||
TModule["createInitialState"]
|
|
||||||
>;
|
|
||||||
export type GameResult<TModule extends GameModule> = Awaited<
|
|
||||||
ReturnType<TModule["start"]>
|
|
||||||
>;
|
|
||||||
|
|
||||||
export function createGameHost<TModule extends GameModule>(
|
export function createGameHost<
|
||||||
gameModule: TModule,
|
TState extends Record<string, unknown>,
|
||||||
): GameHost<TModule> {
|
TResult = unknown,
|
||||||
|
TModule extends GameModule<TState, TResult> = GameModule<TState, TResult>,
|
||||||
|
>(gameModule: TModule): GameHost<TState, TResult> {
|
||||||
return new GameHost(gameModule);
|
return new GameHost(gameModule);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,40 @@
|
||||||
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
||||||
import { CommandSchema, parseCommandSchema, PromptDef } from "@/utils/command";
|
import {
|
||||||
|
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>,
|
||||||
player?: string,
|
currentPlayer?: string | null,
|
||||||
) => 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> = {}>(
|
||||||
promptContext: PromptContext,
|
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
||||||
initialState?: TState | (() => TState),
|
initialState?: TState | (() => TState),
|
||||||
): IGameContext<TState> {
|
): IGameContext<TState> {
|
||||||
const stateValue =
|
const stateValue =
|
||||||
|
|
@ -31,7 +42,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);
|
||||||
const { prompt } = promptContext;
|
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
||||||
|
|
||||||
const context: IGameContext<TState> = {
|
const context: IGameContext<TState> = {
|
||||||
get value(): TState {
|
get value(): TState {
|
||||||
|
|
@ -46,15 +57,38 @@ 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);
|
||||||
},
|
},
|
||||||
prompt,
|
run<T>(input: string) {
|
||||||
|
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,
|
||||||
|
|
@ -62,3 +96,9 @@ 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,7 +5,7 @@
|
||||||
|
|
||||||
// Core types
|
// Core types
|
||||||
export type { IGameContext } from "./core/game";
|
export type { IGameContext } from "./core/game";
|
||||||
export { createPromptDef } from "./core/game";
|
export { createGameCommandRegistry, 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";
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,75 @@
|
||||||
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 { boop } from "./boop";
|
import {createGameCommandRegistry} from "@/core/game";
|
||||||
import { checkFullBoard } from "./full-board";
|
|
||||||
import { checkGraduates } from "./graduate";
|
export const registry = createGameCommandRegistry<BoopGame['value']>();
|
||||||
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动游戏主循环
|
* 启动游戏主循环
|
||||||
|
|
@ -15,11 +79,10 @@ 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 = state.currentPlayer === 'white' ? 'black' : 'white';
|
||||||
state.currentPlayer === "white" ? "black" : "white";
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (game.value.winner) break;
|
if (game.value.winner) break;
|
||||||
|
|
@ -28,4 +91,11 @@ export async function start(game: BoopGame) {
|
||||||
return game.value;
|
return game.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { place, boop, checkWin, checkGraduates, checkFullBoard, turn };
|
export {
|
||||||
|
placeCmd as place,
|
||||||
|
boopCmd as boop,
|
||||||
|
checkWinCmd as checkWin,
|
||||||
|
checkGraduatesCmd as checkGraduates,
|
||||||
|
checkFullBoardCmd as checkFullBoard,
|
||||||
|
turnCmd as turn
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,30 +5,38 @@ 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 { BOARD_SIZE, MAX_PIECES_PER_PLAYER, WIN_LENGTH } from "./constants";
|
export {
|
||||||
|
BOARD_SIZE,
|
||||||
|
MAX_PIECES_PER_PLAYER,
|
||||||
|
WIN_LENGTH
|
||||||
|
} from './constants';
|
||||||
|
|
||||||
// State
|
// State
|
||||||
export { createInitialState, type BoopState } from "./state";
|
export {
|
||||||
|
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 {
|
||||||
|
|
@ -37,5 +45,5 @@ export {
|
||||||
isCellOccupied,
|
isCellOccupied,
|
||||||
getNeighborPositions,
|
getNeighborPositions,
|
||||||
findPartInRegion,
|
findPartInRegion,
|
||||||
findPartAtPosition,
|
findPartAtPosition
|
||||||
} from "./utils";
|
} from './utils';
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,13 @@ 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>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查位置是否在棋盘范围内
|
* 检查位置是否在棋盘范围内
|
||||||
*/
|
*/
|
||||||
|
|
@ -26,12 +29,8 @@ function getPawnAtPosition(state: OnitamaState, x: number, y: number) {
|
||||||
/**
|
/**
|
||||||
* 检查玩家是否拥有某张卡牌
|
* 检查玩家是否拥有某张卡牌
|
||||||
*/
|
*/
|
||||||
function playerHasCard(
|
function playerHasCard(state: OnitamaState, player: PlayerType, cardName: string): boolean {
|
||||||
state: OnitamaState,
|
const cardList = player === 'red' ? state.redCards : state.blackCards;
|
||||||
player: PlayerType,
|
|
||||||
cardName: string,
|
|
||||||
): boolean {
|
|
||||||
const cardList = player === "red" ? state.redCards : state.blackCards;
|
|
||||||
return cardList.includes(cardName);
|
return cardList.includes(cardName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,17 +38,13 @@ function playerHasCard(
|
||||||
* 获取卡牌的移动候选项,根据玩家视角进行旋转
|
* 获取卡牌的移动候选项,根据玩家视角进行旋转
|
||||||
* 黑方需要将卡牌旋转180度
|
* 黑方需要将卡牌旋转180度
|
||||||
*/
|
*/
|
||||||
export function getCardMoveCandidates(
|
export function getCardMoveCandidates(state: OnitamaState, cardName: string, player: PlayerType) {
|
||||||
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;
|
||||||
|
|
@ -58,15 +53,7 @@ export function getCardMoveCandidates(
|
||||||
/**
|
/**
|
||||||
* 检查移动是否合法
|
* 检查移动是否合法
|
||||||
*/
|
*/
|
||||||
export function isValidMove(
|
export function isValidMove(state: OnitamaState, cardName: string, fromX: number, fromY: number, toX: number, toY: number, player: PlayerType): string | null {
|
||||||
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}`;
|
||||||
|
|
@ -93,7 +80,7 @@ export function isValidMove(
|
||||||
|
|
||||||
// 检查移动是否在卡牌的移动候选项中(黑方需要旋转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})`;
|
||||||
}
|
}
|
||||||
|
|
@ -115,15 +102,7 @@ export function isValidMove(
|
||||||
/**
|
/**
|
||||||
* 执行移动
|
* 执行移动
|
||||||
*/
|
*/
|
||||||
async function move(
|
async function handleMove(game: OnitamaGame, player: PlayerType, cardName: string, fromX: number, fromY: number, toX: number, toY: number) {
|
||||||
game: OnitamaGame,
|
|
||||||
player: PlayerType,
|
|
||||||
cardName: string,
|
|
||||||
fromX: number,
|
|
||||||
fromY: number,
|
|
||||||
toX: number,
|
|
||||||
toY: number,
|
|
||||||
) {
|
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
// 验证移动
|
// 验证移动
|
||||||
|
|
@ -134,7 +113,7 @@ async function move(
|
||||||
|
|
||||||
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];
|
||||||
|
|
||||||
// 如果目标位置有敌方棋子,将其移除(吃掉)
|
// 如果目标位置有敌方棋子,将其移除(吃掉)
|
||||||
|
|
@ -148,40 +127,41 @@ async function move(
|
||||||
});
|
});
|
||||||
|
|
||||||
// 交换卡牌
|
// 交换卡牌
|
||||||
await swapCard(game, player, cardName);
|
await handleSwapCard(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 swapCard(
|
async function handleSwapCard(game: OnitamaGame, player: PlayerType, usedCard: string) {
|
||||||
game: OnitamaGame,
|
await game.produceAsync(state => {
|
||||||
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;
|
||||||
|
|
||||||
// 更新备用卡牌
|
// 更新备用卡牌
|
||||||
|
|
@ -189,70 +169,75 @@ async function swapCard(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const swapCard = registry.register({
|
||||||
|
schema: 'swap-card <player> <card:string>',
|
||||||
|
run: handleSwapCard
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查占领胜利条件:玩家的师父棋子到达对手的初始位置
|
* 检查占领胜利条件:玩家的师父棋子到达对手的初始位置
|
||||||
* 红色师父需要到达 (2, 4) - 黑色师父的初始位置
|
* 红色师父需要到达 (2, 4) - 黑色师父的初始位置
|
||||||
* 黑色师父需要到达 (2, 0) - 红色师父的初始位置
|
* 黑色师父需要到达 (2, 0) - 红色师父的初始位置
|
||||||
*/
|
*/
|
||||||
async function checkConquestWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function handleCheckConquestWin(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 (
|
if (redMaster && redMaster.regionId === 'board' && redMaster.position[0] === 2 && redMaster.position[1] === 4) {
|
||||||
redMaster &&
|
return 'red';
|
||||||
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 (
|
if (blackMaster && blackMaster.regionId === 'board' && blackMaster.position[0] === 2 && blackMaster.position[1] === 0) {
|
||||||
blackMaster &&
|
return 'black';
|
||||||
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 checkCaptureWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function handleCheckCaptureWin(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 checkWin(game: OnitamaGame): Promise<PlayerType | null> {
|
async function handleCheckWin(game: OnitamaGame): Promise<PlayerType | null> {
|
||||||
const conquestWinner = await checkConquestWin(game);
|
const conquestWinner = await handleCheckConquestWin(game);
|
||||||
if (conquestWinner) {
|
if (conquestWinner) {
|
||||||
return conquestWinner;
|
return conquestWinner;
|
||||||
}
|
}
|
||||||
|
|
||||||
const captureWinner = await checkCaptureWin(game);
|
const captureWinner = await handleCheckCaptureWin(game);
|
||||||
if (captureWinner) {
|
if (captureWinner) {
|
||||||
return captureWinner;
|
return captureWinner;
|
||||||
}
|
}
|
||||||
|
|
@ -260,34 +245,22 @@ async function checkWin(game: OnitamaGame): Promise<PlayerType | null> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const checkWin = registry.register({
|
||||||
|
schema: 'check-win',
|
||||||
|
run: handleCheckWin
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取玩家可用的移动
|
* 获取玩家可用的移动
|
||||||
*/
|
*/
|
||||||
export function getAvailableMoves(
|
export function getAvailableMoves(state: OnitamaState, player: PlayerType): Array<{card: string, fromX: number, fromY: number, toX: number, toY: number}> {
|
||||||
state: OnitamaState,
|
const moves: Array<{card: string, fromX: number, fromY: number, toX: number, toY: number}> = [];
|
||||||
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(
|
const playerPawns = Object.values(state.pawns).filter(p => p.owner === player && p.regionId === 'board');
|
||||||
(p) => p.owner === player && p.regionId === "board",
|
|
||||||
);
|
|
||||||
|
|
||||||
// 对于每张卡牌
|
// 对于每张卡牌
|
||||||
for (const cardName of cardNames) {
|
for (const cardName of cardNames) {
|
||||||
|
|
@ -321,7 +294,7 @@ export function getAvailableMoves(
|
||||||
/**
|
/**
|
||||||
* 处理回合
|
* 处理回合
|
||||||
*/
|
*/
|
||||||
async function turn(game: OnitamaGame, turnPlayer: PlayerType) {
|
async function handleTurn(game: OnitamaGame, turnPlayer: PlayerType) {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
const availableMoves = getAvailableMoves(state, turnPlayer);
|
const availableMoves = getAvailableMoves(state, turnPlayer);
|
||||||
|
|
||||||
|
|
@ -340,7 +313,7 @@ async function turn(game: OnitamaGame, turnPlayer: PlayerType) {
|
||||||
}
|
}
|
||||||
return card;
|
return card;
|
||||||
},
|
},
|
||||||
turnPlayer,
|
turnPlayer
|
||||||
);
|
);
|
||||||
|
|
||||||
await swapCard(game, turnPlayer, cardToSwap);
|
await swapCard(game, turnPlayer, cardToSwap);
|
||||||
|
|
@ -361,27 +334,19 @@ async function turn(game: OnitamaGame, turnPlayer: PlayerType) {
|
||||||
|
|
||||||
return { player, card, fromX, fromY, toX, toY };
|
return { player, card, fromX, fromY, toX, toY };
|
||||||
},
|
},
|
||||||
turnPlayer,
|
turnPlayer
|
||||||
);
|
);
|
||||||
|
|
||||||
await move(
|
await move(game, moveOutput.player, moveOutput.card, moveOutput.fromX, moveOutput.fromY, moveOutput.toX, moveOutput.toY);
|
||||||
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++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -389,6 +354,11 @@ async function turn(game: OnitamaGame, turnPlayer: PlayerType) {
|
||||||
return { winner, move: moveOutput };
|
return { winner, move: moveOutput };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const turn = registry.register({
|
||||||
|
schema: 'turn <player>',
|
||||||
|
run: handleTurn
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始游戏主循环
|
* 开始游戏主循环
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,20 @@
|
||||||
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>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出一张牌(对当前敌人造成伤害)
|
* 打出一张牌(对当前敌人造成伤害)
|
||||||
*/
|
*/
|
||||||
async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
const playCmd = registry.register({
|
||||||
|
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];
|
||||||
|
|
||||||
|
|
@ -21,15 +26,12 @@ async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
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 {
|
return {success: false, error: `卡牌 ${cardId} 不在玩家 ${player} 的手牌中`};
|
||||||
success: false,
|
|
||||||
error: `卡牌 ${cardId} 不在玩家 ${player} 的手牌中`,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有当前敌人
|
// 检查是否有当前敌人
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return { success: false, error: "没有活跃的敌人" };
|
return {success: false, error: '没有活跃的敌人'};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算伤害(基础伤害为卡牌面值)
|
// 计算伤害(基础伤害为卡牌面值)
|
||||||
|
|
@ -37,27 +39,24 @@ async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
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.produceAsync((state) => {
|
await game.produce(state => {
|
||||||
// 对敌人造成伤害
|
// 对敌人造成伤害
|
||||||
state.currentEnemy!.hp -= damage;
|
state.currentEnemy!.hp -= damage;
|
||||||
|
|
||||||
// 记录黑桃的攻击力降低
|
// 记录黑桃的攻击力降低
|
||||||
if (attackReduction > 0) {
|
if (attackReduction > 0) {
|
||||||
state.currentEnemy!.value = Math.max(
|
state.currentEnemy!.value = Math.max(0, state.currentEnemy!.value - attackReduction);
|
||||||
0,
|
|
||||||
state.currentEnemy!.value - attackReduction,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从手牌移除卡牌
|
// 从手牌移除卡牌
|
||||||
|
|
@ -68,17 +67,15 @@ async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将卡牌移到弃牌堆
|
// 将卡牌移到弃牌堆
|
||||||
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(
|
const discardIds = state.regions.discardPile.childIds.filter(id => id !== state.currentEnemy!.id);
|
||||||
(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];
|
||||||
|
|
@ -86,7 +83,7 @@ async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 方块能力:从酒馆牌堆抓牌
|
// 方块能力:从酒馆牌堆抓牌
|
||||||
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()!;
|
||||||
|
|
@ -107,30 +104,28 @@ async function playCmd(game: RegicideGame, player: string, cardId: string) {
|
||||||
enemyHpBefore,
|
enemyHpBefore,
|
||||||
enemyHpAfter: game.value.currentEnemy!.hp,
|
enemyHpAfter: game.value.currentEnemy!.hp,
|
||||||
enemyDefeated,
|
enemyDefeated,
|
||||||
suitAbility: card.suit,
|
suitAbility: card.suit
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出A配合另一张牌
|
* 打出A配合另一张牌
|
||||||
*/
|
*/
|
||||||
async function playWithACmd(
|
const playWithACmd = registry.register({
|
||||||
game: RegicideGame,
|
schema: 'play-with-a <player:string> <aceCardId:string> <otherCardId:string>',
|
||||||
player: string,
|
run: async (game: RegicideGame, player: string, aceCardId: string, otherCardId: 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`};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,39 +134,36 @@ async function playWithACmd(
|
||||||
|
|
||||||
// 检查两张牌都在手牌中
|
// 检查两张牌都在手牌中
|
||||||
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.produceAsync((state) => {
|
await game.produce(state => {
|
||||||
// 对敌人造成伤害
|
// 对敌人造成伤害
|
||||||
state.currentEnemy!.hp -= totalDamage;
|
state.currentEnemy!.hp -= totalDamage;
|
||||||
|
|
||||||
// 记录黑桃的攻击力降低
|
// 记录黑桃的攻击力降低
|
||||||
if (attackReduction > 0) {
|
if (attackReduction > 0) {
|
||||||
state.currentEnemy!.value = Math.max(
|
state.currentEnemy!.value = Math.max(0, state.currentEnemy!.value - attackReduction);
|
||||||
0,
|
|
||||||
state.currentEnemy!.value - attackReduction,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从手牌移除两张牌
|
// 从手牌移除两张牌
|
||||||
|
|
@ -182,8 +174,8 @@ async function playWithACmd(
|
||||||
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);
|
||||||
|
|
@ -194,31 +186,33 @@ async function playWithACmd(
|
||||||
damage: totalDamage,
|
damage: totalDamage,
|
||||||
attackReduction,
|
attackReduction,
|
||||||
enemyHp: state.currentEnemy!.hp,
|
enemyHp: state.currentEnemy!.hp,
|
||||||
enemyDefeated,
|
enemyDefeated
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 让过(不出牌)
|
* 让过(不出牌)
|
||||||
*/
|
*/
|
||||||
async function passCmd(game: RegicideGame, player: string) {
|
const passCmd = registry.register({
|
||||||
|
schema: 'pass <player:string>',
|
||||||
|
run: async (game: RegicideGame, player: string) => {
|
||||||
// 即使让过,也会受到敌人反击(在回合结束时处理)
|
// 即使让过,也会受到敌人反击(在回合结束时处理)
|
||||||
return {success: true, result: {message: `${player} 让过`}};
|
return {success: true, result: {message: `${player} 让过`}};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 敌人反击 - 玩家必须弃掉点数和 >= 敌人攻击力的牌
|
* 敌人反击 - 玩家必须弃掉点数和 >= 敌人攻击力的牌
|
||||||
*/
|
*/
|
||||||
async function enemyCounterattackCmd(
|
const enemyCounterattackCmd = registry.register({
|
||||||
game: RegicideGame,
|
schema: 'counterattack <player:string> <discardCards:string[]>',
|
||||||
player: string,
|
run: async (game: RegicideGame, player: string, discardCards: 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;
|
||||||
|
|
@ -246,19 +240,19 @@ async function enemyCounterattackCmd(
|
||||||
if (totalValue < enemyAttack) {
|
if (totalValue < enemyAttack) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `弃牌点数和 (${totalValue}) 小于敌人攻击力 (${enemyAttack}),游戏失败`,
|
error: `弃牌点数和 (${totalValue}) 小于敌人攻击力 (${enemyAttack}),游戏失败`
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行弃牌
|
// 执行弃牌
|
||||||
await game.produceAsync((state) => {
|
await game.produce(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';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -267,19 +261,22 @@ async function enemyCounterattackCmd(
|
||||||
result: {
|
result: {
|
||||||
discardedCards: discardCards,
|
discardedCards: discardCards,
|
||||||
totalValue,
|
totalValue,
|
||||||
enemyAttack,
|
enemyAttack
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查敌人是否被击败,如果击败则翻开下一个敌人
|
* 检查敌人是否被击败,如果击败则翻开下一个敌人
|
||||||
*/
|
*/
|
||||||
async function checkEnemyDefeatedCmd(game: RegicideGame) {
|
const checkEnemyDefeatedCmd = registry.register({
|
||||||
|
schema: 'check-enemy',
|
||||||
|
run: async (game: RegicideGame) => {
|
||||||
const state = game.value;
|
const state = game.value;
|
||||||
|
|
||||||
if (!state.currentEnemy) {
|
if (!state.currentEnemy) {
|
||||||
return { success: false as const, error: "没有活跃的敌人" };
|
return {success: false, error: '没有活跃的敌人'};
|
||||||
}
|
}
|
||||||
|
|
||||||
const defeated = state.currentEnemy.hp <= 0;
|
const defeated = state.currentEnemy.hp <= 0;
|
||||||
|
|
@ -287,7 +284,7 @@ async function checkEnemyDefeatedCmd(game: RegicideGame) {
|
||||||
if (defeated) {
|
if (defeated) {
|
||||||
const defeatedEnemy = {...state.currentEnemy};
|
const defeatedEnemy = {...state.currentEnemy};
|
||||||
|
|
||||||
await game.produceAsync((state) => {
|
await game.produce(state => {
|
||||||
// 将当前敌人移到弃牌堆
|
// 将当前敌人移到弃牌堆
|
||||||
state.regions.discardPile.childIds.push(state.currentEnemy!.id);
|
state.regions.discardPile.childIds.push(state.currentEnemy!.id);
|
||||||
|
|
||||||
|
|
@ -303,20 +300,20 @@ async function checkEnemyDefeatedCmd(game: RegicideGame) {
|
||||||
|
|
||||||
// 检查是否胜利(没有更多敌人)
|
// 检查是否胜利(没有更多敌人)
|
||||||
if (!game.value.currentEnemy) {
|
if (!game.value.currentEnemy) {
|
||||||
await game.produceAsync((state) => {
|
await game.produce(state => {
|
||||||
state.phase = "victory";
|
state.phase = 'victory';
|
||||||
state.winner = true;
|
state.winner = true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true as const,
|
success: true,
|
||||||
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
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -325,23 +322,26 @@ async function checkEnemyDefeatedCmd(game: RegicideGame) {
|
||||||
result: {
|
result: {
|
||||||
defeated: false,
|
defeated: false,
|
||||||
currentEnemy: {...state.currentEnemy},
|
currentEnemy: {...state.currentEnemy},
|
||||||
enemiesRemaining: state.enemyDeck.length,
|
enemiesRemaining: state.enemyDeck.length
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查玩家是否有可出的牌
|
* 检查玩家是否有可出的牌
|
||||||
*/
|
*/
|
||||||
async function checkCanPlayCmd(game: RegicideGame, player: string) {
|
const checkCanPlayCmd = registry.register({
|
||||||
|
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,26 +349,27 @@ async function checkCanPlayCmd(game: RegicideGame, player: string) {
|
||||||
result: {
|
result: {
|
||||||
canPlay,
|
canPlay,
|
||||||
canPlayWithA,
|
canPlayWithA,
|
||||||
handSize: playerHand.length,
|
handSize: playerHand.length
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查酒馆牌堆是否为空
|
* 检查酒馆牌堆是否为空
|
||||||
*/
|
*/
|
||||||
async function checkTavernDeckCmd(game: RegicideGame) {
|
const checkTavernDeckCmd = registry.register({
|
||||||
|
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(
|
const allHandsEmpty = Object.values(state.playerHands).every(hand => hand.length === 0);
|
||||||
(hand) => hand.length === 0,
|
|
||||||
);
|
|
||||||
if (allHandsEmpty) {
|
if (allHandsEmpty) {
|
||||||
await game.produceAsync((state) => {
|
await game.produce(state => {
|
||||||
state.phase = "defeat";
|
state.phase = 'defeat';
|
||||||
state.winner = false;
|
state.winner = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -378,32 +379,35 @@ async function checkTavernDeckCmd(game: RegicideGame) {
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
isEmpty,
|
isEmpty,
|
||||||
cardsRemaining: state.regions.tavernDeck.childIds.length,
|
cardsRemaining: state.regions.tavernDeck.childIds.length
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下一个玩家回合
|
* 下一个玩家回合
|
||||||
*/
|
*/
|
||||||
async function nextTurnCmd(game: RegicideGame) {
|
const nextTurnCmd = registry.register({
|
||||||
|
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 = (state.currentPlayerIndex + 1) % state.playerCount;
|
||||||
(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,15 +1,8 @@
|
||||||
import {IGameContext} from "@/core/game";
|
import {IGameContext} from "@/core/game";
|
||||||
import {RegicideState} from "@/samples/regicide/state";
|
import {RegicideState} from "@/samples/regicide/state";
|
||||||
import {
|
import {buildEnemyDeck, buildTavernDeck, createAllCards, getPlayerHandRegionId} from "@/samples/regicide/utils";
|
||||||
buildEnemyDeck,
|
|
||||||
buildTavernDeck,
|
|
||||||
createAllCards,
|
|
||||||
getPlayerHandRegionId,
|
|
||||||
} from "@/samples/regicide/utils";
|
|
||||||
import {INITIAL_HAND_SIZE} from "@/samples/regicide/constants";
|
import {INITIAL_HAND_SIZE} from "@/samples/regicide/constants";
|
||||||
import {Enemy, PlayerType, RegicideCard} from "@/samples/regicide/types";
|
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>;
|
||||||
|
|
||||||
|
|
@ -19,13 +12,9 @@ export type RegicideGame = IGameContext<RegicideState>;
|
||||||
* @param playerCount 玩家数量(1-4)
|
* @param playerCount 玩家数量(1-4)
|
||||||
* @param seed 随机种子(可选)
|
* @param seed 随机种子(可选)
|
||||||
*/
|
*/
|
||||||
export async function setupGame(
|
export async function setupGame(game: RegicideGame, playerCount: number, seed?: number) {
|
||||||
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) {
|
||||||
|
|
@ -42,7 +31,7 @@ export async function setupGame(
|
||||||
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;
|
||||||
|
|
@ -50,15 +39,15 @@ export async function setupGame(
|
||||||
|
|
||||||
// 设置酒馆牌堆区域
|
// 设置酒馆牌堆区域
|
||||||
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);
|
||||||
|
|
@ -89,13 +78,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];
|
||||||
|
|
||||||
|
|
@ -103,9 +92,8 @@ 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 = (state.currentPlayerIndex + 1) % state.playerCount;
|
||||||
(state.currentPlayerIndex + 1) % state.playerCount;
|
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -125,37 +113,31 @@ export async function start(game: RegicideGame) {
|
||||||
/**
|
/**
|
||||||
* 处理完整的玩家回合
|
* 处理完整的玩家回合
|
||||||
*/
|
*/
|
||||||
export async function playTurn(
|
export async function playTurn(game: RegicideGame, player: PlayerType, action: 'play' | 'pass', cardId?: string, secondCardId?: string) {
|
||||||
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 playWithA(game, player, cardId, secondCardId);
|
playResult = await game.run(`play-with-a ${player} ${cardId} ${secondCardId}`);
|
||||||
} else {
|
} else {
|
||||||
playResult = await playCmd(game, player, cardId);
|
playResult = await game.run(`play ${player} ${cardId}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 让过
|
// 让过
|
||||||
playResult = await pass(game, player);
|
playResult = await game.run(`pass ${player}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!playResult.success) {
|
if (!playResult.success) {
|
||||||
|
|
@ -163,7 +145,7 @@ export async function playTurn(
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查敌人是否被击败
|
// 检查敌人是否被击败
|
||||||
const checkResult = await checkEnemy(game);
|
const checkResult = await game.run<{defeated: boolean; currentEnemy?: any; nextEnemy?: any; defeatedEnemy?: any; enemiesRemaining?: number}>('check-enemy');
|
||||||
if (!checkResult.success) {
|
if (!checkResult.success) {
|
||||||
return checkResult;
|
return checkResult;
|
||||||
}
|
}
|
||||||
|
|
@ -178,19 +160,15 @@ export async function playTurn(
|
||||||
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 (
|
if (state.enemyDeck.length === 0 && state.currentEnemy && state.currentEnemy.hp <= 0) {
|
||||||
state.enemyDeck.length === 0 &&
|
await game.produceAsync(state => {
|
||||||
state.currentEnemy &&
|
state.phase = 'victory';
|
||||||
state.currentEnemy.hp <= 0
|
|
||||||
) {
|
|
||||||
await game.produceAsync((state) => {
|
|
||||||
state.phase = "victory";
|
|
||||||
state.winner = true;
|
state.winner = true;
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
|
@ -198,44 +176,41 @@ export async function playTurn(
|
||||||
result: {
|
result: {
|
||||||
playResult: playResult.result,
|
playResult: playResult.result,
|
||||||
enemyDefeated: true,
|
enemyDefeated: true,
|
||||||
gameWon: true,
|
gameWon: true
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换到下一个玩家
|
// 切换到下一个玩家
|
||||||
await nextTurn(game);
|
await game.run('next-turn');
|
||||||
|
|
||||||
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(
|
export async function handleCounterattack(game: RegicideGame, player: PlayerType, discardCardIds: string[]) {
|
||||||
game: RegicideGame,
|
const result = await game.run(`counterattack ${player} ${JSON.stringify(discardCardIds)}`);
|
||||||
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 nextTurn(game);
|
await game.run('next-turn');
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -248,26 +223,17 @@ export function getGameStatus(game: RegicideGame) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
phase: state.phase,
|
phase: state.phase,
|
||||||
currentPlayer: ["player1", "player2", "player3", "player4"][
|
currentPlayer: ['player1', 'player2', 'player3', 'player4'][state.currentPlayerIndex],
|
||||||
state.currentPlayerIndex
|
currentEnemy: state.currentEnemy ? {
|
||||||
],
|
|
||||||
currentEnemy: state.currentEnemy
|
|
||||||
? {
|
|
||||||
...state.currentEnemy,
|
...state.currentEnemy,
|
||||||
hpPercent: Math.round(
|
hpPercent: Math.round((state.currentEnemy.hp / state.currentEnemy.maxHp) * 100)
|
||||||
(state.currentEnemy.hp / state.currentEnemy.maxHp) * 100,
|
} : null,
|
||||||
),
|
|
||||||
}
|
|
||||||
: 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]) => [
|
Object.entries(state.playerHands).map(([player, hand]) => [player, hand.length])
|
||||||
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,17 +18,21 @@ export {
|
||||||
ALL_SUITS,
|
ALL_SUITS,
|
||||||
ALL_RANKS,
|
ALL_RANKS,
|
||||||
FACE_CARDS,
|
FACE_CARDS,
|
||||||
NUMBER_CARDS,
|
NUMBER_CARDS
|
||||||
} from "./constants";
|
} from './constants';
|
||||||
|
|
||||||
// State
|
// State
|
||||||
export { createInitialState, type RegicideState } from "./state";
|
export {
|
||||||
|
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,
|
||||||
|
|
@ -37,7 +41,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 {
|
||||||
|
|
@ -46,8 +50,8 @@ export {
|
||||||
playTurn,
|
playTurn,
|
||||||
handleCounterattack,
|
handleCounterattack,
|
||||||
getGameStatus,
|
getGameStatus,
|
||||||
type RegicideGame,
|
type RegicideGame
|
||||||
} from "./game";
|
} from './game';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
export {
|
export {
|
||||||
|
|
@ -59,5 +63,5 @@ export {
|
||||||
buildTavernDeck,
|
buildTavernDeck,
|
||||||
drawFromDeck,
|
drawFromDeck,
|
||||||
isEnemyDefeated,
|
isEnemyDefeated,
|
||||||
getPlayerHandRegionId,
|
getPlayerHandRegionId
|
||||||
} from "./utils";
|
} from './utils';
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
import { Part } from "@/core/part";
|
import { Part } from "@/core/part";
|
||||||
import { createRegion } from "@/core/region";
|
import { createRegion } from "@/core/region";
|
||||||
import { createPromptDef, IGameContext } from "@/core/game";
|
import {
|
||||||
|
createGameCommandRegistry,
|
||||||
|
createPromptDef,
|
||||||
|
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;
|
||||||
|
|
@ -65,6 +69,7 @@ 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>",
|
||||||
|
|
@ -90,11 +95,9 @@ export async function start(game: TicTacToeGame) {
|
||||||
return game.value;
|
return game.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function turn(
|
const turn = registry.register({
|
||||||
game: TicTacToeGame,
|
schema: "turn <player> <turnNumber:number>",
|
||||||
turnPlayer: PlayerType,
|
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||||
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) => {
|
||||||
|
|
@ -118,7 +121,8 @@ async function turn(
|
||||||
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 (
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { CommandSchema } from "./types";
|
||||||
|
|
||||||
export interface PromptDef<TArgs extends any[]> {
|
export interface PromptDef<TArgs extends any[]> {
|
||||||
schema: CommandSchema;
|
schema: CommandSchema;
|
||||||
hintText?: string;
|
hint?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PromptCall<TArgs extends any[] = unknown[], TRes = unknown> {
|
export interface PromptCall<TArgs extends any[] = unknown[], TRes = unknown> {
|
||||||
|
|
@ -26,7 +26,6 @@ export type PromptTryResult =
|
||||||
ok: true;
|
ok: true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PromptContext = ReturnType<typeof createPromptContext>;
|
|
||||||
export function createPromptContext() {
|
export function createPromptContext() {
|
||||||
const map = new Map<string, PromptCall>();
|
const map = new Map<string, PromptCall>();
|
||||||
const handleCall = createMiddlewareChain(async (call: PromptCall) => {
|
const handleCall = createMiddlewareChain(async (call: PromptCall) => {
|
||||||
|
|
@ -45,7 +44,6 @@ export function createPromptContext() {
|
||||||
});
|
});
|
||||||
|
|
||||||
function tryCommit<TArgs extends any[]>(
|
function tryCommit<TArgs extends any[]>(
|
||||||
def: PromptDef<TArgs>,
|
|
||||||
player: string,
|
player: string,
|
||||||
...args: TArgs
|
...args: TArgs
|
||||||
): PromptTryResult {
|
): PromptTryResult {
|
||||||
|
|
@ -85,14 +83,7 @@ export function createPromptContext() {
|
||||||
reject: reject!,
|
reject: reject!,
|
||||||
promise,
|
promise,
|
||||||
} as PromptCall<unknown[], unknown>;
|
} as PromptCall<unknown[], unknown>;
|
||||||
return (await handleCall.execute(call)) as TRes;
|
return await handleCall.execute(call);
|
||||||
}
|
|
||||||
|
|
||||||
function reset() {
|
|
||||||
for (const call of map.values()) {
|
|
||||||
call.reject("Prompt Reset");
|
|
||||||
}
|
|
||||||
map.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -100,6 +91,5 @@ export function createPromptContext() {
|
||||||
tryCommit,
|
tryCommit,
|
||||||
cancel,
|
cancel,
|
||||||
handleCall,
|
handleCall,
|
||||||
reset,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,7 @@
|
||||||
import { Signal, SignalOptions } from "@preact/signals-core";
|
import { Signal, SignalOptions } from "@preact/signals-core";
|
||||||
import { create } from "mutative";
|
import { create } from "mutative";
|
||||||
|
|
||||||
export interface MutableSignal<T> extends Signal<T> {
|
export class 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>) {
|
||||||
|
|
@ -48,5 +41,5 @@ export function mutableSignal<T>(
|
||||||
initial?: T,
|
initial?: T,
|
||||||
options?: SignalOptions<T>,
|
options?: SignalOptions<T>,
|
||||||
): MutableSignal<T> {
|
): MutableSignal<T> {
|
||||||
return new MutableSignalImpl<T>(initial, options);
|
return new MutableSignal<T>(initial, options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue