IActionResult StartNewSession(Piece[][] board, string variant)
        {
            GameCreationData gcd = new GameCreationData
            {
                Board          = board,
                EnPassant      = null,
                HalfMoveClock  = 0,
                FullMoveNumber = 1,
                WhoseTurn      = Player.White
            };

            gcd.CanBlackCastleKingSide = gcd.CanBlackCastleQueenSide = gcd.CanWhiteCastleKingSide = gcd.CanWhiteCastleQueenSide = false;

            ChessGame game = variant == "Atomic" ? (ChessGame) new AtomicChessGame(gcd) : new AntichessGame(gcd);


            string sessionId;

            do
            {
                sessionId = Guid.NewGuid().ToString();
            } while (endgameTrainingSessionRepository.Exists(sessionId));

            EndgameTrainingSession session = new EndgameTrainingSession(sessionId, game);

            if (session.WasAlreadyLost)
            {
                return(null);
            }

            endgameTrainingSessionRepository.Add(session);
            string fen = session.InitialFEN;

            return(Json(new { success = true, fen, sessionId }));
        }
        public IActionResult GetValidMoves(string trainingSessionId)
        {
            EndgameTrainingSession session = endgameTrainingSessionRepository.Get(trainingSessionId);

            if (session == null)
            {
                return(Json(new { success = false, error = "Training session ID not found." }));
            }

            return(Json(new { success = true, dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(session.Game.GetValidMoves(session.Game.WhoseTurn)) }));
        }
        public IActionResult SubmitMove(string trainingSessionId, string origin, string destination, string promotion = null)
        {
            if (promotion != null && promotion.Length != 1)
            {
                return(Json(new { success = false, error = "Invalid promotion parameter." }));
            }

            EndgameTrainingSession session = endgameTrainingSessionRepository.Get(trainingSessionId);

            if (session == null)
            {
                return(Json(new { success = false, error = "Training session ID not found." }));
            }

            Move move = new Move(origin, destination, session.Game.WhoseTurn, promotion?[0]);
            SubmittedMoveResponse response = session.SubmitMove(move);
            dynamic jsonResp = new ExpandoObject();

            jsonResp.success = response.Success;
            jsonResp.correct = response.Correct;
            jsonResp.check   = response.Check;
            if (response.Error != null)
            {
                jsonResp.error = response.Error;
            }
            if (response.FEN != null)
            {
                jsonResp.fen = response.FEN;
            }
            if (response.FenAfterPlay != null)
            {
                jsonResp.fenAfterPlay       = response.FenAfterPlay;
                jsonResp.checkAfterAutoMove = response.CheckAfterAutoMove;
            }
            if (response.Moves != null)
            {
                jsonResp.dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(response.Moves);
            }
            if (response.LastMove != null)
            {
                jsonResp.lastMove = response.LastMove;
            }
            jsonResp.drawAfterAutoMove = response.DrawAfterAutoMove;
            jsonResp.winAfterAutoMove  = response.WinAfterAutoMove;
            string jsonStr = JsonConvert.SerializeObject(jsonResp);

            return(Content(jsonStr, "application/json"));
        }
 public void Add(EndgameTrainingSession session)
 {
     sessions.Add(session);
 }