Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 7 additions & 51 deletions src/Game/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
BackgammonDieValue,
BackgammonGame,
BackgammonGameDoubled,
BackgammonGameMoved,
BackgammonGameMoving,
BackgammonGameRolledForStart,
BackgammonGameRolling,
Expand All @@ -33,7 +32,7 @@ import { exportToGnuPositionId } from '../Board/gnuPositionId'
import { Checker } from '../Checker'
import { Cube } from '../Cube'
import { BackgammonMoveDirection } from '../Play'
import { debug, logger } from '../utils/logger'
import { logger } from '../utils/logger'
import {
acceptDouble,
canAcceptDouble,
Expand All @@ -50,6 +49,10 @@ import {
canRollForStart,
} from './guards'
import { restoreState, rollForStart } from './lifecycle'
import {
confirmTurnWithRobotAutomation,
handleRobotMovedState,
} from './robot'
import {
checkAndCompleteTurn,
confirmTurn,
Expand Down Expand Up @@ -362,22 +365,7 @@ export class Game {

public static confirmTurn = confirmTurn

/**
* Handle robot automation for games in 'moved' state
* If the active player is a robot and the game is in 'moved' state, automatically confirm the turn
* @param game - Game in any state
* @returns Game with turn confirmed if robot automation was applied, otherwise unchanged
*/
public static handleRobotMovedState = function handleRobotMovedState(
game: BackgammonGame
): BackgammonGame {
// Only handle games in 'moved' state with robot active player
if (game.stateKind === 'moved' && game.activePlayer.isRobot) {
debug('Robot in moved state, auto-confirming turn')
return Game.confirmTurn(game as BackgammonGameMoved)
}
return game
}
public static handleRobotMovedState = handleRobotMovedState

public static executeRobotTurn = executeRobotTurn

Expand Down Expand Up @@ -516,39 +504,7 @@ export class Game {
}


/**
* Async wrapper for confirmTurn that handles robot automation
* @param game - Game in 'moving' state
* @returns Promise<BackgammonGame> - Updated game state with robot automation if needed
*/
public static confirmTurnWithRobotAutomation =
async function confirmTurnWithRobotAutomation(
game: BackgammonGameMoved
): Promise<BackgammonGame> {
// Call the pure sync function first
const confirmedGame = Game.confirmTurn(game)

// Check if the next player is a robot and handle automation
if (confirmedGame.activePlayer?.isRobot) {
try {
// Dynamic import to avoid circular dependencies
// Robot automation moved to @nodots/backgammon-robots package

// Robot automation is now external - return game as-is
logger.info('🤖 Robot automation is now handled externally')
return confirmedGame
} catch (error) {
logger.error(
'🤖 Robot automation error during turn transition (confirmTurn):',
error
)
// Return original game state if robot automation throws
return confirmedGame
}
}

return confirmedGame
}
public static confirmTurnWithRobotAutomation = confirmTurnWithRobotAutomation

// processRobotTurn method removed - now handled by @nodots/backgammon-robots package

Expand Down
54 changes: 54 additions & 0 deletions src/Game/robot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
BackgammonGame,
BackgammonGameMoved,
} from '@nodots/backgammon-types'
import { debug, logger } from '../utils/logger'
import { confirmTurn } from './turnFlow'

/**
* Handle robot automation for games in 'moved' state
* If the active player is a robot and the game is in 'moved' state, automatically confirm the turn
* @param game - Game in any state
* @returns Game with turn confirmed if robot automation was applied, otherwise unchanged
*/
export function handleRobotMovedState(game: BackgammonGame): BackgammonGame {
// Only handle games in 'moved' state with robot active player
if (game.stateKind === 'moved' && game.activePlayer.isRobot) {
debug('Robot in moved state, auto-confirming turn')
return confirmTurn(game as BackgammonGameMoved)
}
return game
}

/**
* Async wrapper for confirmTurn that handles robot automation
* @param game - Game in 'moving' state
* @returns Promise<BackgammonGame> - Updated game state with robot automation if needed
*/
export async function confirmTurnWithRobotAutomation(
game: BackgammonGameMoved
): Promise<BackgammonGame> {
// Call the pure sync function first
const confirmedGame = confirmTurn(game)

// Check if the next player is a robot and handle automation
if (confirmedGame.activePlayer?.isRobot) {
try {
// Dynamic import to avoid circular dependencies
// Robot automation moved to @nodots/backgammon-robots package

// Robot automation is now external - return game as-is
logger.info('🤖 Robot automation is now handled externally')
return confirmedGame
} catch (error) {
logger.error(
'🤖 Robot automation error during turn transition (confirmTurn):',
error
)
// Return original game state if robot automation throws
return confirmedGame
}
}

return confirmedGame
}