Exemple #1
0
        public async Task <IActionResult> SubmitTrainingMove(string id, string trainingSessionId, string origin, string destination, string promotion = null)
        {
            PuzzleTrainingSession session  = puzzleTrainingSessionRepository.Get(trainingSessionId);
            SubmittedMoveResponse response = session.ApplyMove(origin, destination, promotion);

            return(await JsonAfterMove(response, session));
        }
Exemple #2
0
        IActionResult JsonAfterMove(SubmittedMoveResponse response, PuzzleTrainingSession session)
        {
            dynamic jsonResp = new ExpandoObject();

            if (response.Correct == 1 || response.Correct == -1)
            {
                int?loggedInUser = loginHandler.LoggedInUserId(HttpContext);
                if (loggedInUser.HasValue)
                {
                    ratingUpdater.AdjustRating(loggedInUser.Value, session.Current.ID, response.Correct == 1, session.CurrentPuzzleStartedUtc.Value, session.CurrentPuzzleEndedUtc.Value, session.Current.Variant);
                }
                jsonResp.rating = (int)session.Current.Rating.Value;
            }
            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.ExplanationSafe != null)
            {
                jsonResp.explanation = response.ExplanationSafe;
            }
            if (response.Play != null)
            {
                jsonResp.play               = response.Play;
                jsonResp.fenAfterPlay       = response.FenAfterPlay;
                jsonResp.checkAfterAutoMove = response.CheckAfterAutoMove;
            }
            if (response.Moves != null)
            {
                jsonResp.dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(response.Moves);
            }
            if (response.ReplayFENs != null)
            {
                jsonResp.replayFens    = response.ReplayFENs;
                jsonResp.replayChecks  = response.ReplayChecks;
                jsonResp.replayMoves   = response.ReplayMoves;
                jsonResp.replayPockets = response.ReplayPockets;
            }
            if (response.Pocket != null)
            {
                jsonResp.pocket = response.Pocket;
            }
            if (response.PocketAfterAutoMove != null)
            {
                jsonResp.pocketAfterAutoMove = response.PocketAfterAutoMove;
            }
            if (response.AnalysisUrl != null)
            {
                jsonResp.analysisUrl = response.AnalysisUrl;
            }
            return(Json(jsonResp));
        }
Exemple #3
0
        public IActionResult SetupTraining(string id, string trainingSessionId = null)
        {
            int puzzleId;

            if (!int.TryParse(id, out puzzleId))
            {
                return(Json(new { success = false, error = "Invalid puzzle ID." }));
            }
            Puzzle puzzle = puzzleRepository.Get(puzzleId);

            if (puzzle == null)
            {
                return(Json(new { success = false, error = "Puzzle not found." }));
            }
            puzzle.Game = gameConstructor.Construct(puzzle.Variant, puzzle.InitialFen);
            PuzzleTrainingSession session;

            if (trainingSessionId == null)
            {
                string g;
                do
                {
                    g = Guid.NewGuid().ToString();
                } while (puzzleTrainingSessionRepository.ContainsTrainingSessionId(g));
                session = new PuzzleTrainingSession(g, gameConstructor);
                puzzleTrainingSessionRepository.Add(session);
            }
            else
            {
                session = puzzleTrainingSessionRepository.Get(trainingSessionId);
                if (session == null)
                {
                    return(Json(new { success = false, error = "Puzzle training session ID not found." }));
                }
            }
            session.Setup(puzzle);
            string additionalInfo = null;

            if (puzzle.Variant == "ThreeCheck")
            {
                ThreeCheckChessGame tccg = puzzle.Game as ThreeCheckChessGame;
                additionalInfo = string.Format("At the puzzle's initial position, white delivered {0} checks and black delivered {1} checks.", tccg.ChecksByWhite, tccg.ChecksByBlack);
            }
            return(Json(new
            {
                success = true,
                trainingSessionId = session.SessionID,
                author = userRepository.FindById(session.Current.Author).Username,
                fen = session.Current.InitialFen,
                dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(session.Current.Game.GetValidMoves(session.Current.Game.WhoseTurn)),
                whoseTurn = session.Current.Game.WhoseTurn.ToString().ToLowerInvariant(),
                variant = puzzle.Variant,
                additionalInfo = additionalInfo,
                authorUrl = Url.Action("Profile", "User", new { id = session.Current.Author }),
                pocket = session.Current.Game.GenerateJsonPocket(),
                check = session.Current.Game.IsInCheck(Player.White) ? "white" : (session.Current.Game.IsInCheck(Player.Black) ? "black" : null)
            }));
        }
Exemple #4
0
        public async Task <IActionResult> SubmitTrainingDrop(string id, string trainingSessionId, string role, string pos)
        {
            PuzzleTrainingSession session  = puzzleTrainingSessionRepository.Get(trainingSessionId);
            SubmittedMoveResponse response = session.ApplyDrop(role, pos);

            if (response.Success && response.Correct == SubmittedMoveResponse.INVALID_MOVE)
            {
                return(Json(new { success = true, invalidDrop = true, pos }));
            }
            return(await JsonAfterMove(response, session));
        }
Exemple #5
0
        public IActionResult SubmitTrainingMove(string id, string trainingSessionId, string origin, string destination, string promotion = null)
        {
            PuzzleTrainingSession session  = puzzleTrainingSessionRepository.Get(trainingSessionId);
            SubmittedMoveResponse response = session.ApplyMove(origin, destination, promotion);
            dynamic jsonResp = new ExpandoObject();

            if (response.Correct == 1 || response.Correct == -1)
            {
                int?loggedInUser = loginHandler.LoggedInUserId(HttpContext);
                if (loggedInUser.HasValue)
                {
                    ratingUpdater.AdjustRating(loggedInUser.Value, session.Current.ID, response.Correct == 1, session.CurrentPuzzleStartedUtc.Value, session.CurrentPuzzleEndedUtc.Value, session.Current.Variant);
                }
                jsonResp.rating = (int)session.Current.Rating.Value;
            }
            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.ExplanationSafe != null)
            {
                jsonResp.explanation = response.ExplanationSafe;
            }
            if (response.Play != null)
            {
                jsonResp.play               = response.Play;
                jsonResp.fenAfterPlay       = response.FenAfterPlay;
                jsonResp.checkAfterAutoMove = response.CheckAfterAutoMove;
            }
            if (response.Moves != null)
            {
                jsonResp.dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(response.Moves);
            }
            if (response.ReplayFENs != null)
            {
                jsonResp.replayFens   = response.ReplayFENs;
                jsonResp.replayChecks = response.ReplayChecks;
                jsonResp.replayMoves  = response.ReplayMoves;
            }
            return(Json(jsonResp));
        }
Exemple #6
0
        public IActionResult SetupTraining(string id, string trainingSessionId = null)
        {
            int puzzleId;

            if (!int.TryParse(id, out puzzleId))
            {
                return(Json(new { success = false, error = "Invalid puzzle ID." }));
            }
            Puzzle puzzle = puzzleRepository.Get(puzzleId);

            if (puzzle == null)
            {
                return(Json(new { success = false, error = "Puzzle not found." }));
            }
            puzzle.Game = gameConstructor.Construct(puzzle.Variant, puzzle.InitialFen);
            PuzzleTrainingSession session;

            if (trainingSessionId == null)
            {
                string g;
                do
                {
                    g = Guid.NewGuid().ToString();
                } while (puzzleTrainingSessionRepository.ContainsTrainingSessionId(g));
                session = new PuzzleTrainingSession(g, gameConstructor);
                puzzleTrainingSessionRepository.Add(session);
            }
            else
            {
                session = puzzleTrainingSessionRepository.Get(trainingSessionId);
                if (session == null)
                {
                    return(Json(new { success = false, error = "Puzzle training session ID not found." }));
                }
            }
            session.Setup(puzzle);
            return(Json(new
            {
                success = true,
                trainingSessionId = session.SessionID,
                author = userRepository.FindById(session.Current.Author).Username,
                fen = session.Current.InitialFen,
                dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(session.Current.Game.GetValidMoves(session.Current.Game.WhoseTurn)),
                whoseTurn = session.Current.Game.WhoseTurn.ToString().ToLowerInvariant(),
                variant = puzzle.Variant
            }));
        }
 public void Add(PuzzleTrainingSession session)
 {
     sessions.Add(session);
 }