Exemplo n.º 1
0
        public void CreateSubmission(int contest_id, int problem_no, SubmissionFormData submission_data)
        {
            // important: submission_repository must be initialized using this.context
            SubmissionRepository submission_repository = new SubmissionRepository(context);

            Contest contest = context.Contests.Include(x => x.Problems).FirstOrDefault(x => x.Id == contest_id);

            if (contest.EndDate < DateTime.Now)
            {
                throw new InvalidOperationException("Can not create submission to contest that has ended");
            }

            if (contest.StartDate > DateTime.Now)
            {
                throw new InvalidOperationException("Can not create submission to contest that has not started");
            }

            ContestProblem contest_problem = contest.Problems.FirstOrDefault(x => x.Order == problem_no);
            Problem        problem         = contest_problem.Problem;

            submission_repository.OnSubmissionStatusChange += (Object sender, ExecutionResultEventArgs args) => {
                // the submission entry is updated by SubmissionRepository,
                // this handler only updates the contestant

                // replace with real user
                var contestant = context.Contestants.Include(x => x.Submissions).First(x => x.Contest.Id == contest_id);

                contestant.Penalty = contest_service.CalclatePenalty(contestant.Submissions);

                contestant.SolveCount = context.ContestProblems.Count(x => x.Submissions.
                                                                      Count(y => y.Submission.Status.Id == Verdict.Accepted) > 0);

                context.SaveChanges();
            };


            User submitter = context.Users.First();

            Submission submission = submission_repository.CreateProblemSubmission(problem.Id, submission_data);

            ContestSubmission contest_submission = new ContestSubmission()
            {
                Submitter  = context.Contestants.First(x => x.Contest.Id == contest_id && x.User.Id == submitter.Id),
                Problem    = contest_problem,
                Submission = submission,
            };

            contest.Submissions.Add(contest_submission);
            context.SaveChanges();
        }