Beispiel #1
0
        public async Task <IActionResult> VerifyAndGetNext(string sessionId, string origin, string destination, string promotion = null)
        {
            TimedTrainingSession session = timedTrainingSessionRepository.Get(sessionId);

            if (session == null)
            {
                return(Json(new { success = false, error = "Training session ID not found." }));
            }
            if (session.Ended)
            {
                if (!session.RecordedInDb && session.Score.Owner.HasValue)
                {
                    await timedTrainingScoreRepository.AddAsync(session.Score);

                    session.RecordedInDb = true;
                }
                return(Json(new { success = true, ended = true }));
            }
            bool correctMove = session.VerifyMove(origin, destination, promotion);

            if (correctMove)
            {
                TrainingPosition randomPosition = await positionRepository.GetRandomAsync(session.Type);

                session.SetPosition(randomPosition);
            }
            else
            {
                session.RetryCurrentPosition();
            }
            return(Json(new { success = true, fen = session.CurrentFen, color = session.AssociatedGame.WhoseTurn.ToString().ToLowerInvariant(),
                              dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(session.AssociatedGame.GetValidMoves(session.AssociatedGame.WhoseTurn)),
                              currentScore = session.Score.Score, lastMove = session.CurrentLastMoveToDisplay }));
        }
Beispiel #2
0
        async Task <IActionResult> StartTimedTraining(string type, string variant)
        {
            string               sessionId = Guid.NewGuid().ToString();
            DateTime             startTime = DateTime.UtcNow;
            DateTime             endTime   = startTime + new TimeSpan(0, 1, 0);
            TimedTrainingSession session   = new TimedTrainingSession(sessionId, startTime, endTime,
                                                                      await loginHandler.LoggedInUserIdAsync(HttpContext), type, variant, gameConstructor);

            timedTrainingSessionRepository.Add(session);
            TrainingPosition randomPosition = await positionRepository.GetRandomAsync(type);

            session.SetPosition(randomPosition);
            return(Json(new { success = true, sessionId, seconds = 60, fen = randomPosition.FEN, color = session.AssociatedGame.WhoseTurn.ToString().ToLowerInvariant(),
                              dests = moveCollectionTransformer.GetChessgroundDestsForMoveCollection(session.AssociatedGame.GetValidMoves(session.AssociatedGame.WhoseTurn)), lastMove = session.CurrentLastMoveToDisplay }));
        }