Esempio n. 1
0
        public Submission CreateProblemSubmission(int problem_id, SubmissionFormData submission_data)
        {
            var problem = context.Problems.Find(problem_id);
            var judge   = new JudgeService();

            if (problem == null)
            {
                throw new ObjectNotFoundException("Problem with specified id does not exist");
            }

//            Trace.WriteLine(submission_data.ProgrammingLanguageId);
            var submission = InitializeSubmissionObject(problem, submission_data);

            context.Submissions.Add(submission);
            context.SaveChanges();

            judge.OnSubmissionStatusChange += (sender, e) => {
                var result = e.ExecutionResult;
                submission.Status           = context.SubmissionStatus.Find(result.Verdict);
                submission.RunningTime      = result.RunningTime;
                submission.PeakMemmoryUsage = result.MemmoryUsage;
//                submission.PeakMemmoryUsage = result.Verdict == Verdict.CompilationError ? 0 : 5;  // TODO REPLACE WITH REAL VALUE
                submission.StandardErrorStream = result.ErrorMsg;

                context.SaveChanges();

                // this event must be emitter *after* invoking context.SaveChanges()
                OnSubmissionStatusChange?.Invoke(this, e);
            };

            judge.judge(submission_data, problem);

            return(submission);
        }
Esempio n. 2
0
        /// <summary>
        /// 开始服务
        /// </summary>
        private static void StartUp()
        {
            SetAppHandle();
            LogManager.Info("Starting judger");

            Service = new JudgeService();
            Service.Start();

            LogManager.Info("Judger started");
        }
Esempio n. 3
0
        public static async Task QueueExecutor(CancellationToken token)
        {
            var random = new Random();

            while (!token.IsCancellationRequested)
            {
                await semaphore.WaitAsync(token);

                while (queue.TryDequeue(out var info))
                {
                    using var scope = Program.RootServiceProvider?.CreateScope();
                    try
                    {
                        var judgeService = scope?.ServiceProvider.GetService <IJudgeService>();
                        if (judgeService is null)
                        {
                            throw new InvalidOperationException("IJudgeService was not registed into service collection.");
                        }

                        var dbContext = scope?.ServiceProvider.GetService <WebHostDbContext>();
                        if (dbContext is null)
                        {
                            throw new InvalidOperationException("WebHostDbContext was not registed into service collection.");
                        }

                        if (info.Type == JudgeReportInfo.ReportType.PostJudge)
                        {
                            var userManager = scope?.ServiceProvider.GetService <UserManager <UserInfo> >();
                            if (userManager is null)
                            {
                                throw new InvalidOperationException("UserManager<UserInfo> was not registed into service collection.");
                            }

                            var resultType = JudgeService.ComputeJudgeResultType(info.JudgeResult);

                            var judge = await judgeService.GetJudgeAsync(info.JudgeId);

                            if (judge != null && judge.JudgeCount <= 1 && (int)resultType >= (int)ResultCode.Accepted)
                            {
                                if (resultType == ResultCode.Accepted)
                                {
                                    if (judge.ContestId != null)
                                    {
                                        var problemConfig = await dbContext.ContestProblemConfig
                                                            .Where(i => i.ContestId == judge.ContestId &&
                                                                   i.ProblemId == judge.ProblemId)
                                                            .FirstOrDefaultAsync(token);

                                        problemConfig.AcceptCount++;
                                        dbContext.ContestProblemConfig.Update(problemConfig);
                                    }
                                    else
                                    {
                                        var problem = await dbContext.Problem
                                                      .Where(i => i.Id == judge.ProblemId)
                                                      .FirstOrDefaultAsync(token);

                                        problem.AcceptCount++;
                                        dbContext.Problem.Update(problem);
                                    }
                                }

                                var(coins, experience, accept) = resultType switch
                                {
                                    ResultCode.Accepted => (random.Next(30, 80), random.Next(50, 100), 1),
                                    ResultCode.Presentation_Error => (random.Next(10, 30), random.Next(30, 50), 0),
                                    _ => (0, random.Next(10, 30), 0)
                                };

                                var userInfo = await userManager.FindByIdAsync(judge.UserId);

                                userInfo.Coins         += coins;
                                userInfo.Experience    += experience;
                                userInfo.AcceptedCount += accept;

                                await userManager.UpdateAsync(userInfo);
                            }

                            await dbContext.SaveChangesAsync();
                        }

                        await judgeService.UpdateJudgeResultAsync(info.JudgeId, info.Type, info.JudgeResult);

                        var judgeHub = Program.RootServiceProvider?.GetService <IHubContext <JudgeHub, IJudgeHub> >();
                        if (judgeHub is null)
                        {
                            throw new InvalidOperationException("IHubContext<JudgeHub, IJudgeHub> was not registed into service collection.");
                        }
                        await judgeHub.Clients.Group($"result_{info.JudgeId}").JudgeCompleteSignalReceived(info.JudgeId);
                    }
                    catch (Exception ex)
                    {
                        var logger = scope?.ServiceProvider.GetService <ILogger <JudgeReport> >();
                        if (logger is null)
                        {
                            throw new NullReferenceException();
                        }
                        logger.LogError(ex, "Judge report update error.");
                    }
                }
            }
        }
 public JudgesController()
 {
     JudgeService = new JudgeService(new JudgeRepo(), new ContestJudgeRepo());
 }
Esempio n. 5
0
 private static void StartUp()
 {
     Service = new JudgeService();
     Service.Start();
 }
Esempio n. 6
0
        private UserInfoViewModel GetUser(ApplicationUser user)
        {
            var roles  = UserManager.GetRoles(user.Id);
            var claims = new List <UserClaimViewModel>();

            foreach (var claim in user.Claims)
            {
                claims.Add(new UserClaimViewModel()
                {
                    Type = claim.ClaimType, Value = claim.ClaimValue
                });
            }

            string       firstName   = "";
            string       lastName    = "";
            Organization affiliation = null;

            var personNameIdClaim = claims.FirstOrDefault(n => n.Type == "personNameId");

            if (personNameIdClaim != null)
            {
                var personNameId = Convert.ToInt32(personNameIdClaim.Value);
                var personName   = new PersonNameRepo().Get(personNameId);

                firstName = personName.FirstName;
                lastName  = personName.LastName;
            }

            var organizationIdClaim = claims.FirstOrDefault(n => n.Type == "organizationId");

            if (personNameIdClaim != null)
            {
                var organizationId = Convert.ToInt32(organizationIdClaim.Value);
                affiliation = new OrganizationRepo().Get(organizationId);
            }

            if (roles.Contains("Judge"))
            {
                var judges = new JudgeService(new JudgeRepo(), new ContestJudgeRepo()).GetAll().Where(j => j.UserId == user.Id);

                foreach (var judge in judges)
                {
                    claims.Add(new UserClaimViewModel()
                    {
                        Type = "judgeId", Value = judge.Id.ToString()
                    });
                }
            }

            var contests = new ContestService(new ContestRepo(), new ShowContestRepo()).GetAll().Where(c => c.TimeKeeperId == user.Id);

            if (contests.Any())
            {
                roles.Add("TimeKeeper");
            }

            return(new UserInfoViewModel
            {
                Id = user.Id,
                Email = user.UserName,
                FirstName = firstName,
                LastName = lastName,
                Affiliation = affiliation,
                Roles = roles,
                Claims = claims
            });
        }
 public JudgeController(StvContext db)
 {
     _db     = db;
     service = new JudgeService(_db);
 }
Esempio n. 8
0
 public void Setup()
 {
     this.service = new JudgeService();
 }
Esempio n. 9
0
 public Tests()
 {
     _judgeService = new JudgeService();
 }