Compare commits
2 Commits
2d5200bdb7
...
129c58fb08
| Author | SHA1 | Date |
|---|---|---|
|
|
129c58fb08 | |
|
|
70b1ac1e43 |
|
|
@ -19,7 +19,7 @@ export type { Region, RegionAxis } from './core/region';
|
|||
export { createRegion, createRegionAxis, applyAlign, shuffle, moveToRegion } from './core/region';
|
||||
|
||||
// Utils
|
||||
export type { Command, CommandResult, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';
|
||||
export type { Command, CommandDef, CommandResult, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';
|
||||
export { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSchema, applyCommandSchema } from './utils/command';
|
||||
|
||||
export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command';
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export const registry = createGameCommandRegistry<BoopState>();
|
|||
/**
|
||||
* 放置棋子到棋盘
|
||||
*/
|
||||
async function place(game: BoopGame, row: number, col: number, player: PlayerType, type: PieceType) {
|
||||
async function handlePlace(game: BoopGame, row: number, col: number, player: PlayerType, type: PieceType) {
|
||||
const value = game.value;
|
||||
// 从玩家supply中找到对应类型的棋子
|
||||
const part = findPartInRegion(game, player, type);
|
||||
|
|
@ -42,12 +42,15 @@ async function place(game: BoopGame, row: number, col: number, player: PlayerTyp
|
|||
|
||||
return { row, col, player, type, partId };
|
||||
}
|
||||
const placeCommand = registry.asCommand( 'place <row:number> <col:number> <player> <type>', place);
|
||||
const place = registry.register({
|
||||
schema: 'place <row:number> <col:number> <player> <type>',
|
||||
run: handlePlace
|
||||
});
|
||||
|
||||
/**
|
||||
* 执行boop - 推动周围棋子
|
||||
*/
|
||||
async function boop(game: BoopGame, row: number, col: number, type: PieceType) {
|
||||
async function handleBoop(game: BoopGame, row: number, col: number, type: PieceType) {
|
||||
const booped: string[] = [];
|
||||
|
||||
await game.produceAsync(state => {
|
||||
|
|
@ -85,12 +88,15 @@ async function boop(game: BoopGame, row: number, col: number, type: PieceType) {
|
|||
|
||||
return { booped };
|
||||
}
|
||||
const boopCommand = registry.asCommand('boop <row:number> <col:number> <type>', boop);
|
||||
const boop = registry.register({
|
||||
schema: 'boop <row:number> <col:number> <type>',
|
||||
run: handleBoop
|
||||
});
|
||||
|
||||
/**
|
||||
* 检查是否有玩家获胜(三个猫连线)
|
||||
*/
|
||||
async function checkWin(game: BoopGame): Promise<WinnerType | null> {
|
||||
async function handleCheckWin(game: BoopGame): Promise<WinnerType | null> {
|
||||
for(const line of getLineCandidates()){
|
||||
let whites = 0;
|
||||
let blacks = 0;
|
||||
|
|
@ -109,12 +115,15 @@ async function checkWin(game: BoopGame): Promise<WinnerType | null> {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
const checkWinCommand = registry.asCommand('check-win', checkWin);
|
||||
const checkWin = registry.register({
|
||||
schema: 'check-win',
|
||||
run: handleCheckWin
|
||||
});
|
||||
|
||||
/**
|
||||
* 检查并执行小猫升级(三个小猫连线变成猫)
|
||||
*/
|
||||
async function checkGraduates(game: BoopGame){
|
||||
async function handleCheckGraduates(game: BoopGame){
|
||||
const toUpgrade = new Set<string>();
|
||||
for(const line of getLineCandidates()){
|
||||
let whites = 0;
|
||||
|
|
@ -145,12 +154,15 @@ async function checkGraduates(game: BoopGame){
|
|||
}
|
||||
});
|
||||
}
|
||||
const checkGraduatesCommand = registry.asCommand('check-graduates', checkGraduates);
|
||||
const checkGraduates = registry.register({
|
||||
schema: 'check-graduates',
|
||||
run: handleCheckGraduates
|
||||
});
|
||||
|
||||
async function setup(game: BoopGame) {
|
||||
async function handleStart(game: BoopGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnOutput = await turnCommand(game, currentPlayer);
|
||||
const turnOutput = await turn(game, currentPlayer);
|
||||
|
||||
await game.produceAsync(state => {
|
||||
state.winner = turnOutput.winner;
|
||||
|
|
@ -163,9 +175,12 @@ async function setup(game: BoopGame) {
|
|||
|
||||
return game.value;
|
||||
}
|
||||
registry.asCommand('setup', setup);
|
||||
const start = registry.register({
|
||||
schema: 'start',
|
||||
run: handleStart
|
||||
});
|
||||
|
||||
async function checkFullBoard(game: BoopGame, turnPlayer: PlayerType){
|
||||
async function handleCheckFullBoard(game: BoopGame, turnPlayer: PlayerType){
|
||||
// 检查8-piece规则: 如果玩家所有8个棋子都在棋盘上且没有获胜,强制升级一个小猫
|
||||
const playerPieces = Object.values(game.value.pieces).filter(
|
||||
p => p.player === turnPlayer && p.regionId === 'board'
|
||||
|
|
@ -201,8 +216,12 @@ async function checkFullBoard(game: BoopGame, turnPlayer: PlayerType){
|
|||
moveToRegion(cat || part, null, state.regions[turnPlayer]);
|
||||
});
|
||||
}
|
||||
const checkFullBoard = registry.register({
|
||||
schema: 'check-full-board <player:string>',
|
||||
run: handleCheckFullBoard
|
||||
});
|
||||
|
||||
async function turn(game: BoopGame, turnPlayer: PlayerType) {
|
||||
async function handleTurn(game: BoopGame, turnPlayer: PlayerType) {
|
||||
const {row, col, type} = await game.prompt(
|
||||
'play <player> <row:number> <col:number> [type:string]',
|
||||
(command) => {
|
||||
|
|
@ -229,13 +248,16 @@ async function turn(game: BoopGame, turnPlayer: PlayerType) {
|
|||
);
|
||||
const pieceType = type === 'cat' ? 'cat' : 'kitten';
|
||||
|
||||
await placeCommand(game, row, col, turnPlayer, pieceType);
|
||||
await boopCommand(game, row, col, pieceType);
|
||||
const winner = await checkWinCommand(game);
|
||||
await place(game, row, col, turnPlayer, pieceType);
|
||||
await boop(game, row, col, pieceType);
|
||||
const winner = await checkWin(game);
|
||||
if(winner) return { winner: winner };
|
||||
|
||||
await checkGraduatesCommand(game);
|
||||
await checkGraduates(game);
|
||||
await checkFullBoard(game, turnPlayer);
|
||||
return { winner: null };
|
||||
}
|
||||
const turnCommand = registry.asCommand('turn <player>', turn);
|
||||
const turn = registry.register({
|
||||
schema: 'turn <player:string>',
|
||||
run: handleTurn
|
||||
});
|
||||
|
|
@ -36,54 +36,58 @@ export type TicTacToeState = ReturnType<typeof createInitialState>;
|
|||
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
||||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
||||
|
||||
async function start(game: TicTacToeGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turnCommand(game, currentPlayer, turnNumber);
|
||||
registry.register({
|
||||
schema: 'start',
|
||||
async run(game: TicTacToeGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||
|
||||
game.produce(state => {
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
||||
state.turn = turnNumber;
|
||||
}
|
||||
});
|
||||
if (game.value.winner) break;
|
||||
game.produce(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;
|
||||
}
|
||||
});
|
||||
|
||||
return game.value;
|
||||
}
|
||||
registry.asCommand('start', start);
|
||||
const turn = registry.register({
|
||||
schema: 'turn <player> <turnNumber:number>',
|
||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const {player, row, col} = await game.prompt(
|
||||
'play <player> <row:number> <col:number>',
|
||||
(command) => {
|
||||
const [player, row, col] = command.params as [PlayerType, number, number];
|
||||
|
||||
async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||
const {player, row, col} = await game.prompt(
|
||||
'play <player> <row:number> <col:number>',
|
||||
(command) => {
|
||||
const [player, row, col] = command.params as [PlayerType, number, number];
|
||||
if (player !== turnPlayer) {
|
||||
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
||||
} else if (!isValidMove(row, col)) {
|
||||
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||
} else if (isCellOccupied(game, row, col)) {
|
||||
throw `Cell (${row}, ${col}) is already occupied.`;
|
||||
} else {
|
||||
return { player, row, col };
|
||||
}
|
||||
},
|
||||
game.value.currentPlayer
|
||||
);
|
||||
|
||||
if (player !== turnPlayer) {
|
||||
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
||||
} else if (!isValidMove(row, col)) {
|
||||
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||
} else if (isCellOccupied(game, row, col)) {
|
||||
throw `Cell (${row}, ${col}) is already occupied.`;
|
||||
} else {
|
||||
return { player, row, col };
|
||||
}
|
||||
},
|
||||
game.value.currentPlayer
|
||||
);
|
||||
placePiece(game, row, col, turnPlayer);
|
||||
|
||||
placePiece(game, row, col, turnPlayer);
|
||||
const winner = checkWinner(game);
|
||||
if (winner) return { winner };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
||||
|
||||
const winner = checkWinner(game);
|
||||
if (winner) return { winner };
|
||||
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
||||
|
||||
return { winner: null };
|
||||
}
|
||||
const turnCommand = registry.asCommand('turn <player:string> <turnNumber:int>', turn);
|
||||
return { winner: null };
|
||||
}
|
||||
});
|
||||
|
||||
function isValidMove(row: number, col: number): boolean {
|
||||
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import type { Command, CommandSchema } from './types';
|
||||
import type {
|
||||
CommandDef, CommandFunction,
|
||||
CommandResult,
|
||||
CommandRunner,
|
||||
CommandRunnerContext,
|
||||
|
|
@ -16,9 +17,17 @@ type CanRunParsed = {
|
|||
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
|
||||
}
|
||||
|
||||
type CmdFunc<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
||||
export class CommandRegistry<TContext> extends Map<string, CommandRunner<TContext>>{
|
||||
asCommand<TFunc extends CmdFunc<TContext> = CmdFunc<TContext>>(schema: CommandSchema | string, run: TFunc) {
|
||||
register<TFunc extends CommandFunction<TContext>>(...args: [schema: CommandSchema | string, run: TFunc] | [CommandDef<TContext, TFunc>]){
|
||||
let schema: CommandSchema | string;
|
||||
let run: TFunc;
|
||||
if(args.length === 1){
|
||||
schema = args[0].schema;
|
||||
run = args[0].run;
|
||||
}else{
|
||||
schema = args[0];
|
||||
run = args[1];
|
||||
}
|
||||
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
||||
registerCommand(this, {
|
||||
schema: parsedSchema,
|
||||
|
|
|
|||
|
|
@ -51,3 +51,9 @@ export type CommandRunner<TContext, TResult = unknown> = {
|
|||
schema: CommandSchema;
|
||||
run: CommandRunnerHandler<TContext, TResult>;
|
||||
};
|
||||
|
||||
export type CommandFunction<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
||||
export type CommandDef<TContext,TFunc extends CommandFunction<TContext>> = {
|
||||
schema: string | CommandSchema,
|
||||
run: TFunc;
|
||||
}
|
||||
|
|
@ -18,5 +18,5 @@ export type {
|
|||
CommandFlagSchema,
|
||||
CommandSchema,
|
||||
} from './types';
|
||||
export type { CommandRunner, CommandResult, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './command-runner';
|
||||
export type { CommandRunner, CommandDef, CommandResult, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './command-runner';
|
||||
export type { CommandRegistry, CommandRunnerContextExport } from './command-registry';
|
||||
|
|
|
|||
Loading…
Reference in New Issue