public IActionResult TrainId(int id) { Puzzle p = puzzleRepository.Get(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)); }
public IActionResult PostComment(string commentBody, string puzzleId) { int puzzleIdI; if (!int.TryParse(puzzleId, out puzzleIdI)) { return(Json(new { success = false, error = "Invalid puzzle ID." })); } Puzzle puzzle = puzzleRepository.Get(puzzleIdI); bool success = false; Comment comment = null; if (puzzle != null) { comment = new Comment(counterRepository.GetAndIncrease(Counter.COMMENT_ID), loginHandler.LoggedInUserId(HttpContext).Value, commentBody, null, puzzleIdI, false, DateTime.UtcNow); success = commentRepository.Add(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); notificationRepository.Add(notificationForParentAuthor); return(Json(new { success = true })); } else { return(Json(new { success = false, error = "Could not post comment." })); } }
public IActionResult Approve(int id) { if (puzzleRepository.Approve(id, loginHandler.LoggedInUserId(HttpContext).Value)) { Puzzle approved = puzzleRepository.Get(id); Notification notif = new Notification(Guid.NewGuid().ToString(), approved.Author, "Your puzzle has been approved!", false, Url.Action("TrainId", "Puzzle", new { id = id }), DateTime.UtcNow); notificationRepository.Add(notif); return(Json(new { success = true })); } else { return(Json(new { success = false, error = "Approval failed." })); } }
public void AdjustRating(int userId, int puzzleId, bool correct, DateTime attemptStarted, DateTime attemptEnded, string variant) { // Glicko-2 library: https://github.com/MaartenStaa/glicko2-csharp User user = userRepository.FindById(userId); Puzzle puzzle = puzzleRepository.Get(puzzleId); if (user.SolvedPuzzles.Contains(puzzle.ID) || puzzle.InReview || puzzle.Author == user.ID || puzzle.Reviewers.Contains(user.ID)) { return; } 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++; } userRepository.Update(user); double newPuzzleRating = puzzleRating.GetRating(); puzzleRepository.UpdateRating(puzzle.ID, new Rating(newPuzzleRating, puzzleRating.GetRatingDeviation(), puzzleRating.GetVolatility())); Attempt attempt = new Attempt(userId, puzzleId, attemptStarted, attemptEnded, newUserRating - oldUserRating, newPuzzleRating - oldPuzzleRating, correct); attemptRepository.Add(attempt); RatingWithMetadata rwm = new RatingWithMetadata(user.Ratings[variant], attemptEnded, user.ID, variant); ratingRepository.Add(rwm); }