示例#1
0
        public async Task <IActionResult> OnPostAsync()
        {
            ApplicationUser partialUser = await _userManager.GetUserAsync(User);

            ApplicationUser user = await _userManager.Users
                                   .Include(u => u.ProblemResults)
                                   .ThenInclude(r => r.FirstResult)
                                   .Include(u => u.ProblemResults)
                                   .ThenInclude(r => r.BestResult)
                                   .FirstOrDefaultAsync(u => u == partialUser);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);

            if (RequirePassword)
            {
                if (!await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    ModelState.AddModelError(string.Empty, _localizer["Password not correct"]);
                    return(Page());
                }
            }

            foreach (var problemResult in user.ProblemResults)
            {
                _dbContext.Remove(problemResult.BestResult);
                _dbContext.Remove(problemResult.FirstResult);
                _dbContext.Remove(problemResult);
            }

            var result = await _userManager.DeleteAsync(user);

            var userId = await _userManager.GetUserIdAsync(user);

            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Unexpected error occurred deleteing user with ID '{userId}'.");
            }

            await _signInManager.SignOutAsync();

            _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);

            return(Redirect("~/"));
        }
示例#2
0
        public async Task <IActionResult> Result(int id, string filePath)
        {
            Problem problem = await dbContext.Problems.Include(m => m.Tests).FirstOrDefaultAsync(p => p.ProblemID == id);

            if (problem == null)
            {
                return(RedirectToAction(nameof(List)));
            }

            if (!System.IO.File.Exists(filePath))
            {
                return(RedirectToAction(nameof(Solve), new { id }));
            }

            string parentFolderPath = Directory.GetParent(filePath).FullName;

            ResultViewModel viewModel = new ResultViewModel()
            {
                ProblemID           = problem.ProblemID,
                Name                = problem.Name,
                ShowFailedTestCases = problem.ShowFailedTestCases
            };
            UserProgram userProgram = new UserProgram();

            var result = userProgram.SetSource(filePath);

            viewModel.CompilationResult = result;

            if (result.Status == UserProgram.Result.StatusType.Failed)
            {
                Directory.Delete(parentFolderPath, true);

                return(View(viewModel));
            }

            result = userProgram.Compile();
            viewModel.CompilationResult = result;

            if (result.Status == UserProgram.Result.StatusType.Failed)
            {
                Directory.Delete(parentFolderPath, true);

                viewModel.CompilationResult = result;
                return(View(viewModel));
            }

            int passed = 0;

            foreach (Test t in problem.Tests)
            {
                result = userProgram.Execute(t.GivenInput);
                ResultViewModel.TestResult testResult = new ResultViewModel.TestResult()
                {
                    Test = t, ExecutionResult = result
                };

                if (result.Status == UserProgram.Result.StatusType.Successful)
                {
                    // Format expected output lines
                    string[] expectedOutputLines = t.ExpectedOutput.Split('\n');
                    for (int i = 0; i < expectedOutputLines.Length; i++)
                    {
                        expectedOutputLines[i] = expectedOutputLines[i].Trim('\u202c').Trim();
                    }

                    result = userProgram.EvaluateAndGetResultIfFailed(expectedOutputLines);

                    if (result.Status == UserProgram.Result.StatusType.Successful)
                    {
                        passed++;
                    }

                    testResult.EvaluationResult = result;
                    viewModel.TestResults.Add(testResult);
                }
                else
                {
                    viewModel.TestResults.Add(testResult);
                }
            }

            viewModel.PassedTests = passed;

            try
            {
                Directory.Delete(parentFolderPath, true);
            }
            catch (UnauthorizedAccessException) // Sometimes the process isn't killed fast enough
            {
                Thread.Sleep(100);
                Directory.Delete(parentFolderPath, true);
            }

            ApplicationUser partialUser = await userManager.GetUserAsync(User);

            ApplicationUser currentUser = await userManager.Users
                                          .Include(u => u.ProblemResults)
                                          .ThenInclude(r => r.FirstResult)
                                          .Include(u => u.ProblemResults)
                                          .ThenInclude(r => r.BestResult)
                                          .FirstOrDefaultAsync(u => u == partialUser);

            if (currentUser != null)
            {
                bool hasProblem           = currentUser.ProblemResults.Exists(r => r.ProblemID == problem.ProblemID);
                bool gotPerfectPercentage = false;
                if (hasProblem)
                {
                    ProblemResult problemResult  = currentUser.ProblemResults.Where(r => r.ProblemID == problem.ProblemID).First();
                    int           bestPercentage = problemResult.BestResult.PercentageResult;
                    gotPerfectPercentage = bestPercentage < 100;
                }

                bool isAdmin = await userManager.IsInRoleAsync(currentUser, "Admin");

                // A unique user solved the problem and is not an Admin
                if (passed == problem.Tests.Count && (!hasProblem || (hasProblem && gotPerfectPercentage)) && !isAdmin)
                {
                    problem.TimesSolved++;

                    if (await TryUpdateModelAsync(problem))
                    {
                        try
                        {
                            await dbContext.SaveChangesAsync();
                        }
                        catch (DbUpdateException)
                        {
                            return(View(viewModel));
                        }
                    }
                }

                ProgramResult programResult = viewModel.GetProgramResult();
                if (currentUser.ProblemResults.Exists(r => r.ProblemID == problem.ProblemID))
                {
                    // Tried to solve the problem again
                    ProblemResult problemResult = currentUser.ProblemResults.First(r => r.ProblemID == problem.ProblemID);
                    if (programResult.PercentageResult > problemResult.BestResult.PercentageResult)
                    {
                        if (problemResult.BestResult != problemResult.FirstResult)
                        {
                            dbContext.Remove(problemResult.BestResult);
                        }
                        problemResult.BestResult = programResult;
                    }
                }
                else
                {
                    // Solved this problem for the first time
                    ProblemResult problemResult = new ProblemResult()
                    {
                        ProblemID   = problem.ProblemID,
                        FirstResult = programResult,
                        BestResult  = programResult
                    };
                    currentUser.ProblemResults.Add(problemResult);
                }

                await userManager.UpdateAsync(currentUser);
            }

            return(View(viewModel));
        }