コード例 #1
0
        public async Task <IActionResult> SubmitPuzzle(string id, string solution, string explanation)
        {
            if (!int.TryParse(id, out int puzzleId))
            {
                return(Json(new { success = false, error = "The given ID is invalid." }));
            }

            if (string.IsNullOrWhiteSpace(solution))
            {
                return(Json(new { success = false, error = "There are no accepted variations." }));
            }

            Puzzle puzzle = puzzlesBeingEdited.Get(puzzleId);

            if (puzzle == null)
            {
                return(Json(new { success = false, error = string.Format("The given puzzle (ID: {0}) cannot be published because it isn't being created.", id) }));
            }
            if (puzzle.Author != (await loginHandler.LoggedInUserIdAsync(HttpContext)).Value)
            {
                return(Json(new { success = false, error = "Only the puzzle author can access this right now." }));
            }
            Puzzle possibleDuplicate = await puzzleRepository.FindByFenAndVariantAsync(puzzle.InitialFen, puzzle.Variant);

            if (possibleDuplicate != null && possibleDuplicate.Approved)
            {
                return(Json(new { success = false, error = "Duplicate; same FEN and variant: " + Url.Action("TrainId", "Puzzle", new { id = possibleDuplicate.ID }) }));
            }

            puzzle.Solutions = new List <string>(solution.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)));
            if (puzzle.Solutions.Count == 0)
            {
                return(Json(new { success = false, error = "There are no accepted variations." }));
            }
            Task <int> pid = counterRepository.GetAndIncreaseAsync(Counter.PUZZLE_ID);

            puzzle.Game = null;
            puzzle.ExplanationUnsafe = explanation;
            puzzle.Rating            = new Rating(1500, 350, 0.06);
            puzzle.Reviewers         = new List <int>();
            if (UserRole.HasAtLeastThePrivilegesOf((await loginHandler.LoggedInUserAsync(HttpContext)).Roles, UserRole.PUZZLE_REVIEWER))
            {
                puzzle.InReview = false;
                puzzle.Approved = true;
                puzzle.Reviewers.Add((await loginHandler.LoggedInUserIdAsync(HttpContext)).Value);
            }
            else
            {
                puzzle.InReview = true;
                puzzle.Approved = false;
            }
            puzzle.DateSubmittedUtc = DateTime.UtcNow;
            puzzle.ID = await pid;
            if (await puzzleRepository.AddAsync(puzzle))
            {
                return(Json(new { success = true, link = Url.Action("TrainId", "Puzzle", new { id = puzzle.ID }) }));
            }
            else
            {
                return(Json(new { success = false, error = "Something went wrong." }));
            }
        }