Ejemplo n.º 1
0
        public async Task <IActionResult> TrainId(int id)
        {
            Puzzle p = await puzzleRepository.GetAsync(id);

            if (p == null)
            {
                return(ViewResultForHttpError(HttpContext, new HttpErrors.NotFound("The given puzzle could not be found.")));
            }
            ViewBag.Variant = p.Variant;
            return(View("Train", p));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PostComment(string commentBody, string puzzleId)
        {
            if (!int.TryParse(puzzleId, out int puzzleIdI))
            {
                return(Json(new { success = false, error = "Invalid puzzle ID." }));
            }
            Puzzle puzzle = await puzzleRepository.GetAsync(puzzleIdI);

            bool    success = false;
            Comment comment = null;

            if (puzzle != null)
            {
                comment = new Comment(await counterRepository.GetAndIncreaseAsync(Counter.COMMENT_ID), (await loginHandler.LoggedInUserIdAsync(HttpContext)).Value, commentBody, null, puzzleIdI, false, DateTime.UtcNow);
                success = await commentRepository.AddAsync(comment);
            }
            if (success)
            {
                Notification notificationForParentAuthor = new Notification(Guid.NewGuid().ToString(), puzzle.Author, "You received a comment on your puzzle.", false,
                                                                            string.Format("/Puzzle/{0}?comment={1}", comment.PuzzleID, comment.ID), DateTime.UtcNow);
                await notificationRepository.AddAsync(notificationForParentAuthor);

                return(Json(new { success = true }));
            }
            else
            {
                return(Json(new { success = false, error = "Could not post comment." }));
            }
        }
        public async Task <int?> AdjustRatingAsync(int userId, int puzzleId, bool correct, DateTime attemptStarted, DateTime attemptEnded, string variant)
        {
            // Glicko-2 library: https://github.com/MaartenStaa/glicko2-csharp
            User user = await userRepository.FindByIdAsync(userId);

            Puzzle puzzle = await puzzleRepository.GetAsync(puzzleId);

            if (user.SolvedPuzzles.Contains(puzzle.ID) || puzzle.InReview || puzzle.Author == user.ID || puzzle.Reviewers.Contains(user.ID))
            {
                return(null);
            }
            Glicko2.RatingCalculator calculator = new Glicko2.RatingCalculator();
            double oldUserRating   = user.Ratings[variant].Value;
            double oldPuzzleRating = puzzle.Rating.Value;

            Glicko2.Rating userRating           = new Glicko2.Rating(calculator, oldUserRating, user.Ratings[variant].RatingDeviation, user.Ratings[variant].Volatility);
            Glicko2.Rating puzzleRating         = new Glicko2.Rating(calculator, oldPuzzleRating, puzzle.Rating.RatingDeviation, puzzle.Rating.Volatility);
            Glicko2.RatingPeriodResults results = new Glicko2.RatingPeriodResults();
            results.AddResult(correct ? userRating : puzzleRating, correct ? puzzleRating : userRating);
            calculator.UpdateRatings(results);
            double newUserRating = userRating.GetRating();

            user.Ratings[variant] = new Rating(newUserRating, userRating.GetRatingDeviation(), userRating.GetVolatility());
            user.SolvedPuzzles.Add(puzzle.ID);
            if (correct)
            {
                user.PuzzlesCorrect++;
            }
            else
            {
                user.PuzzlesWrong++;
            }
            await userRepository.UpdateAsync(user);

            double newPuzzleRating = puzzleRating.GetRating();
            Task   urt             = puzzleRepository.UpdateRatingAsync(puzzle.ID, new Rating(newPuzzleRating, puzzleRating.GetRatingDeviation(), puzzleRating.GetVolatility()));


            Attempt attempt = new Attempt(userId, puzzleId, attemptStarted, attemptEnded, newUserRating - oldUserRating, newPuzzleRating - oldPuzzleRating, correct);
            Task    ara     = attemptRepository.AddAsync(attempt);

            RatingWithMetadata rwm = new RatingWithMetadata(user.Ratings[variant], attemptEnded, user.ID, variant);
            Task rra = ratingRepository.AddAsync(rwm);

            await urt;
            await ara;
            await rra;

            return((int)newUserRating - (int)oldUserRating);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Approve(int id)
        {
            if (await puzzleRepository.ApproveAsync(id, (await loginHandler.LoggedInUserIdAsync(HttpContext)).Value))
            {
                Puzzle approved = await puzzleRepository.GetAsync(id);

                Notification notif = new Notification(Guid.NewGuid().ToString(), approved.Author, "Your puzzle has been approved!", false, Url.Action("TrainId", "Puzzle", new { id }), DateTime.UtcNow);
                await notificationRepository.AddAsync(notif);

                return(Json(new { success = true }));
            }
            else
            {
                return(Json(new { success = false, error = "Approval failed." }));
            }
        }