Exemplo n.º 1
0
        public async Task <IActionResult> GetOneRandomly(string variant, string trainingSessionId = null)
        {
            variant = Utilities.NormalizeVariantNameCapitalization(variant);
            List <int> toBeExcluded;
            double     nearRating = randomProvider.RandomRating();
            int?       userId     = await loginHandler.LoggedInUserIdAsync(HttpContext);

            if (userId.HasValue)
            {
                User u = await userRepository.FindByIdAsync(userId.Value);

                toBeExcluded = u.SolvedPuzzles;
                if (variant != "Mixed")
                {
                    nearRating = u.Ratings[variant].Value;
                }
                else
                {
                    nearRating = u.Ratings.Average(x => x.Value.Value);
                }
            }
            else if (trainingSessionId != null)
            {
                toBeExcluded = puzzleTrainingSessionRepository.Get(trainingSessionId)?.PastPuzzleIds ?? new List <int>();
            }
            else
            {
                toBeExcluded = new List <int>();
            }
            Puzzle puzzle = await puzzleRepository.GetOneRandomlyAsync(toBeExcluded, variant, userId, nearRating);

            if (puzzle != null)
            {
                return(Json(new { success = true, id = puzzle.ID }));
            }
            else
            {
                return(Json(new { success = true, allDone = true }));
            }
        }
Exemplo n.º 2
0
        public IActionResult GetOneRandomly(string variant, string trainingSessionId = null)
        {
            variant = Utilities.NormalizeVariantNameCapitalization(variant);
            if (variant == "Mixed")
            {
                variant = supportedVariants[new Random().Next(0, supportedVariants.Length)];
            }
            List <int> toBeExcluded;
            double     nearRating = 1500;
            int?       userId     = loginHandler.LoggedInUserId(HttpContext);

            if (userId.HasValue)
            {
                User u = userRepository.FindById(userId.Value);
                toBeExcluded = u.SolvedPuzzles;
                nearRating   = u.Ratings[variant].Value;
            }
            else if (trainingSessionId != null)
            {
                toBeExcluded = puzzleTrainingSessionRepository.Get(trainingSessionId)?.PastPuzzleIds ?? new List <int>();
            }
            else
            {
                toBeExcluded = new List <int>();
            }
            Puzzle puzzle = puzzleRepository.GetOneRandomly(toBeExcluded, variant, loginHandler.LoggedInUserId(HttpContext));

            if (puzzle != null)
            {
                return(Json(new { success = true, id = puzzle.ID }));
            }
            else
            {
                return(Json(new { success = true, allDone = true }));
            }
        }