refactor: don't use this
This commit is contained in:
parent
6e1c42015f
commit
af9254603a
|
|
@ -1,6 +1,6 @@
|
||||||
import {
|
import {
|
||||||
createGameCommandRegistry, Part, createRegion, createPart, isCellOccupied as isCellOccupiedUtil,
|
createGameCommandRegistry, Part, createRegion, createPart, isCellOccupied as isCellOccupiedUtil,
|
||||||
registerGameCommand, IGameContext
|
IGameContext
|
||||||
} from '@/index';
|
} from '@/index';
|
||||||
|
|
||||||
const BOARD_SIZE = 3;
|
const BOARD_SIZE = 3;
|
||||||
|
|
@ -37,28 +37,29 @@ export type TicTacToeState = ReturnType<typeof createInitialState>;
|
||||||
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
||||||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
export const registry = createGameCommandRegistry<TicTacToeState>();
|
||||||
|
|
||||||
registerGameCommand(registry, 'setup', async function() {
|
async function setup(game: TicTacToeGame) {
|
||||||
while (true) {
|
while (true) {
|
||||||
const currentPlayer = this.value.currentPlayer;
|
const currentPlayer = game.value.currentPlayer;
|
||||||
const turnNumber = this.value.turn + 1;
|
const turnNumber = game.value.turn + 1;
|
||||||
const turnOutput = await this.run<{winner: WinnerType}>(`turn ${currentPlayer} ${turnNumber}`);
|
const turnOutput = await turnCommand(game, currentPlayer, turnNumber);
|
||||||
if (!turnOutput.success) throw new Error(turnOutput.error);
|
if (!turnOutput.success) throw new Error(turnOutput.error);
|
||||||
|
|
||||||
this.produce(state => {
|
game.produce(state => {
|
||||||
state.winner = turnOutput.result.winner;
|
state.winner = turnOutput.result.winner;
|
||||||
if (!state.winner) {
|
if (!state.winner) {
|
||||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
||||||
state.turn = turnNumber;
|
state.turn = turnNumber;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (this.value.winner) break;
|
if (game.value.winner) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.value;
|
return game.value;
|
||||||
});
|
}
|
||||||
|
registry.register('setup', setup);
|
||||||
|
|
||||||
registerGameCommand(registry, 'turn <player:string> <turnNumber:int>', async function turn(turnPlayer: PlayerType, turnNumber: number) {
|
async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||||
const playCmd = await this.prompt(
|
const playCmd = await game.prompt(
|
||||||
'play <player> <row:number> <col:number>',
|
'play <player> <row:number> <col:number>',
|
||||||
(command) => {
|
(command) => {
|
||||||
const [player, row, col] = command.params as [PlayerType, number, number];
|
const [player, row, col] = command.params as [PlayerType, number, number];
|
||||||
|
|
@ -69,23 +70,24 @@ registerGameCommand(registry, 'turn <player:string> <turnNumber:int>', async fun
|
||||||
if (!isValidMove(row, col)) {
|
if (!isValidMove(row, col)) {
|
||||||
return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||||
}
|
}
|
||||||
if (isCellOccupied(this, row, col)) {
|
if (isCellOccupied(game, row, col)) {
|
||||||
return `Cell (${row}, ${col}) is already occupied.`;
|
return `Cell (${row}, ${col}) is already occupied.`;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
this.value.currentPlayer
|
game.value.currentPlayer
|
||||||
);
|
);
|
||||||
const [player, row, col] = playCmd.params as [PlayerType, number, number];
|
const [player, row, col] = playCmd.params as [PlayerType, number, number];
|
||||||
|
|
||||||
placePiece(this, row, col, turnPlayer);
|
placePiece(game, row, col, turnPlayer);
|
||||||
|
|
||||||
const winner = checkWinner(this);
|
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 };
|
||||||
});
|
}
|
||||||
|
const turnCommand = registry.register('turn <player:string> <turnNumber:int>', turn);
|
||||||
|
|
||||||
function isValidMove(row: number, col: number): boolean {
|
function isValidMove(row: number, col: number): boolean {
|
||||||
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ type CanRunParsed = {
|
||||||
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
|
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdFunc<TContext> = (this: TContext, ...args: any[]) => Promise<unknown>;
|
type CmdFunc<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
||||||
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
||||||
register<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
register<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
||||||
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
||||||
|
|
@ -17,12 +17,13 @@ export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContex
|
||||||
schema: parsedSchema,
|
schema: parsedSchema,
|
||||||
async run(this: CommandRunnerContext<TContext>, command: Command){
|
async run(this: CommandRunnerContext<TContext>, command: Command){
|
||||||
const params = command.params;
|
const params = command.params;
|
||||||
return await run.call(this.context, ...params);
|
return await run.call(this.context, this.context, ...params);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
type TResult = TFunc extends (this: TContext, ...args: any[]) => Promise<infer X> ? X : null;
|
type TParams = TFunc extends (ctx: TContext, ...args: infer X) => Promise<unknown> ? X : null;
|
||||||
return function(ctx: TContext & CanRunParsed, ...args: Parameters<TFunc>){
|
type TResult = TFunc extends (ctx: TContext, ...args: any[]) => Promise<infer X> ? X : null;
|
||||||
|
return function(ctx: TContext & CanRunParsed, ...args: TParams){
|
||||||
return ctx.runParsed({
|
return ctx.runParsed({
|
||||||
options: {},
|
options: {},
|
||||||
params: args,
|
params: args,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue