dianesis
dianesis

Reputation: 332

How to Enable Premove in InputHandler

I am using cm-chessboard and I am trying to allow the player to drag his own pieces even though is not his turn. I am trying to accomplish this so I can allow premove. Premove means I am allowed to "premove" the next move in my opponents time. The issue is I cannot understand how to allow the user to drag moves when is not his turn.

const inputHandler = (event) => {
  if (event.type === INPUT_EVENT_TYPE.movingOverSquare) {
    return;
  }

  if (event.type !== INPUT_EVENT_TYPE.moveInputFinished) {
    event.chessboard.removeLegalMovesMarkers()
  }

  if (event.type === INPUT_EVENT_TYPE.moveInputStarted) {
    const moves = game.moves({square: event.squareFrom, verbose: true})
    event.chessboard.addLegalMovesMarkers(moves)
    return moves.length > 0

  } else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
    const move = {from: event.squareFrom, to: event.squareTo, promotion: event.promotion}
    const result = game.move(move)

if (result) {
    event.chessboard.state.moveInputProcess.then(() => { 
      event.chessboard.setPosition(game.fen(), true)
    })
    return true;
} else {
        let possibleMoves = game.moves({square: event.squareFrom, verbose: true})
        for (const possibleMove of possibleMoves) {
            if (possibleMove.promotion && possibleMove.to === event.squareTo) {
                event.chessboard.showPromotionDialog(event.squareTo, COLOR.white, (result) => {
                    if (result.type === PROMOTION_DIALOG_RESULT_TYPE.pieceSelected) {
                        let pmove = game.move({from: event.squareFrom, to: event.squareTo, promotion: result.piece.charAt(1)})
                        event.chessboard.setPosition(game.fen(), true)
                    } else {
                        event.chessboard.setPosition(game.fen(), true)
                    }
                })
                return true
            }
        }
    }
    return result
}

}

Upvotes: 0

Views: 30

Answers (0)

Related Questions