refactor: renaming api
This commit is contained in:
@@ -23,7 +23,7 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||
private _activePromptSchema: Signal<CommandSchema | null>;
|
||||
private _activePromptPlayer: Signal<string | null>;
|
||||
private _createInitialState: () => TState;
|
||||
private _eventListeners: Map<'setup' | 'dispose', Set<() => void>>;
|
||||
private _eventListeners: Map<'start' | 'dispose', Set<() => void>>;
|
||||
private _isDisposed = false;
|
||||
|
||||
constructor(
|
||||
@@ -94,7 +94,7 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||
this._state.clearInterruptions();
|
||||
}
|
||||
|
||||
setup(setupCommand: string): Promise<CommandResult<unknown>> {
|
||||
start(startCommand: string): Promise<CommandResult<unknown>> {
|
||||
if (this._isDisposed) {
|
||||
throw new Error('GameHost is disposed');
|
||||
}
|
||||
@@ -104,10 +104,10 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||
const initialState = this._createInitialState();
|
||||
this._state.value = initialState as any;
|
||||
|
||||
const promise = this._commands.run(setupCommand);
|
||||
const promise = this._commands.run(startCommand);
|
||||
|
||||
this._status.value = 'running';
|
||||
this._emitEvent('setup');
|
||||
this._emitEvent('start');
|
||||
|
||||
return promise;
|
||||
}
|
||||
@@ -126,7 +126,7 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||
this._eventListeners.clear();
|
||||
}
|
||||
|
||||
on(event: 'setup' | 'dispose', listener: () => void): () => void {
|
||||
on(event: 'start' | 'dispose', listener: () => void): () => void {
|
||||
if (!this._eventListeners.has(event)) {
|
||||
this._eventListeners.set(event, new Set());
|
||||
}
|
||||
@@ -137,7 +137,7 @@ export class GameHost<TState extends Record<string, unknown>> {
|
||||
};
|
||||
}
|
||||
|
||||
private _emitEvent(event: 'setup' | 'dispose') {
|
||||
private _emitEvent(event: 'start' | 'dispose') {
|
||||
const listeners = this._eventListeners.get(event);
|
||||
if (listeners) {
|
||||
for (const listener of listeners) {
|
||||
|
||||
@@ -42,7 +42,7 @@ async function place(game: BoopGame, row: number, col: number, player: PlayerTyp
|
||||
|
||||
return { row, col, player, type, partId };
|
||||
}
|
||||
const placeCommand = registry.register( 'place <row:number> <col:number> <player> <type>', place);
|
||||
const placeCommand = registry.asCommand( 'place <row:number> <col:number> <player> <type>', place);
|
||||
|
||||
/**
|
||||
* 执行boop - 推动周围棋子
|
||||
@@ -85,7 +85,7 @@ async function boop(game: BoopGame, row: number, col: number, type: PieceType) {
|
||||
|
||||
return { booped };
|
||||
}
|
||||
const boopCommand = registry.register('boop <row:number> <col:number> <type>', boop);
|
||||
const boopCommand = registry.asCommand('boop <row:number> <col:number> <type>', boop);
|
||||
|
||||
/**
|
||||
* 检查是否有玩家获胜(三个猫连线)
|
||||
@@ -109,7 +109,7 @@ async function checkWin(game: BoopGame): Promise<WinnerType | null> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const checkWinCommand = registry.register('check-win', checkWin);
|
||||
const checkWinCommand = registry.asCommand('check-win', checkWin);
|
||||
|
||||
/**
|
||||
* 检查并执行小猫升级(三个小猫连线变成猫)
|
||||
@@ -145,7 +145,7 @@ async function checkGraduates(game: BoopGame){
|
||||
}
|
||||
});
|
||||
}
|
||||
const checkGraduatesCommand = registry.register('check-graduates', checkGraduates);
|
||||
const checkGraduatesCommand = registry.asCommand('check-graduates', checkGraduates);
|
||||
|
||||
async function setup(game: BoopGame) {
|
||||
while (true) {
|
||||
@@ -163,7 +163,7 @@ async function setup(game: BoopGame) {
|
||||
|
||||
return game.value;
|
||||
}
|
||||
registry.register('setup', setup);
|
||||
registry.asCommand('setup', setup);
|
||||
|
||||
async function checkFullBoard(game: BoopGame, turnPlayer: PlayerType){
|
||||
// 检查8-piece规则: 如果玩家所有8个棋子都在棋盘上且没有获胜,强制升级一个小猫
|
||||
@@ -238,4 +238,4 @@ async function turn(game: BoopGame, turnPlayer: PlayerType) {
|
||||
await checkFullBoard(game, turnPlayer);
|
||||
return { winner: null };
|
||||
}
|
||||
const turnCommand = registry.register('turn <player>', turn);
|
||||
const turnCommand = registry.asCommand('turn <player>', turn);
|
||||
@@ -36,7 +36,7 @@ export type TicTacToeState = ReturnType<typeof createInitialState>;
|
||||
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
||||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
||||
|
||||
async function setup(game: TicTacToeGame) {
|
||||
async function start(game: TicTacToeGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
@@ -54,7 +54,7 @@ async function setup(game: TicTacToeGame) {
|
||||
|
||||
return game.value;
|
||||
}
|
||||
registry.register('setup', setup);
|
||||
registry.asCommand('start', start);
|
||||
|
||||
async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const {player, row, col} = await game.prompt(
|
||||
@@ -83,7 +83,7 @@ async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: num
|
||||
|
||||
return { winner: null };
|
||||
}
|
||||
const turnCommand = registry.register('turn <player:string> <turnNumber:int>', turn);
|
||||
const turnCommand = registry.asCommand('turn <player:string> <turnNumber:int>', turn);
|
||||
|
||||
function isValidMove(row: number, col: number): boolean {
|
||||
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
||||
|
||||
@@ -18,7 +18,7 @@ type CanRunParsed = {
|
||||
|
||||
type CmdFunc<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
||||
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
||||
register<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
||||
asCommand<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
||||
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
||||
registerCommand(this, {
|
||||
schema: parsedSchema,
|
||||
|
||||
Reference in New Issue
Block a user