public long StartNewGame(string ticket, StartGameModel startGameModel) { lock (mutex) { var ctx = GetContext(ticket); if (ctx != null && chessboard == null && (IsComputerGame() || userTickets.Count == 2)) { ChessEngineOption engineOpt = null; if (IsComputerGame()) { engineOpt = GetOptions().ChessEngines.SingleOrDefault((opt) => opt.Name == startGameModel.ChessEngineName); if (engineOpt == null) { throw new ArgumentException($"Invalid chess engine '{startGameModel.ChessEngineName}'."); } if (startGameModel.Level < 1 || startGameModel.Level > 9) { throw new ArgumentException($"Invalid level ${startGameModel.Level}."); } } var users = userTickets.Values.OrderBy(ctx => ctx.Created).ToList(); string whitePlayer = users[0].Name; string blackPlayer = !computerGame ? users[1].Name : engineOpt.Name; if (startGameModel.MyColor == "W" && ctx.Name != whitePlayer || startGameModel.MyColor == "B" && ctx.Name != blackPlayer) { (whitePlayer, blackPlayer) = (blackPlayer, whitePlayer); } GameOption gameOption = startGameModel.GameOption switch { "fastchess" => GameOption.FastChess, "chess15" => GameOption.Chess15, "chess30" => GameOption.Chess30, "chess60" => GameOption.Chess60, _ => GameOption.FastChess }; if (IsComputerGame() && gameOption == GameOption.FastChess) { gameOption = GameOption.Chess15; } chessboard = new Chessboard(whitePlayer, blackPlayer, gameOption); ctx.StartGameConfirmed = true; if (IsComputerGame()) { StartChessEngine(engineOpt); chessengine.Level = startGameModel.Level; chessboard.GameStarted = true; PlayChessEngineNewGame(ctx, chessboard); } stateChanged = DateTime.UtcNow; } return(GetStateChanged()); } }
private void StartChessEngine(ChessEngineOption opt) { StopChessEngine(); chessengine = new ChessEngine(opt.File, opt.UseUCI, OnChessEngineMoveCompleted, logger); computerGame = true; }