Exemple #1
0
        public Judge(int problemId, int userId, string code, string type, bool isStdIO, string description,
                     string defaultTime, int competitionId, Action <int> idCallBack = null)
        {
            Cancelled = false;
            if (competitionId != 0)
            {
                var comp      = Connection.GetCompetition(competitionId);
                var judgeLogs = Connection.QueryJudgeLogBelongsToCompetition(competitionId, userId);
                if ((comp.Option & 1) != 0 && comp.SubmitLimit != 0)
                {
                    if (judgeLogs.Count(i => i.ProblemId == problemId) >= comp.SubmitLimit)
                    {
                        Connection.CanPostJudgTask = true;
                        Cancelled = true;
                        return;
                    }
                }
            }

            JudgeResult.JudgeId = Connection.NewJudge(description);
            idCallBack?.Invoke(JudgeResult.JudgeId);
            _problem = Connection.GetProblem(problemId);
            _id      = Guid.NewGuid().ToString().Replace("-", string.Empty);
            JudgeResult.JudgeDate     = defaultTime ?? DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            JudgeResult.ProblemId     = _problem.ProblemId;
            JudgeResult.Code          = code;
            JudgeResult.UserId        = userId;
            JudgeResult.Exitcode      = new int[_problem.DataSets.Length];
            JudgeResult.Result        = new string[_problem.DataSets.Length];
            JudgeResult.Score         = new float[_problem.DataSets.Length];
            JudgeResult.Timeused      = new long[_problem.DataSets.Length];
            JudgeResult.Memoryused    = new long[_problem.DataSets.Length];
            JudgeResult.Type          = type;
            JudgeResult.Description   = description;
            JudgeResult.CompetitionId = competitionId;
            JudgeResult.AdditionInfo  = string.Empty;
            Connection.UpdateJudgeInfo(JudgeResult);

            var textBlock = Connection.UpdateMainPageState(
                $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} 准备评测 #{JudgeResult.JudgeId},题目:{JudgeResult.ProblemName},用户:{JudgeResult.UserName}");

            while (true)
            {
                if (Connection.CurJudgingCnt < (Configuration.Configurations.MutiThreading == 0
                        ? Configuration.ProcessorCount + Connection.IntelligentAdditionWorkingThread
                        : Configuration.Configurations.MutiThreading))
                {
                    lock (Connection.JudgeListCntLock)
                    {
                        Connection.CurJudgingCnt++;
                    }
                    break;
                }
                Thread.Sleep(100);
            }
            Connection.CanPostJudgTask = true;

            JudgeResult.JudgeDate = defaultTime ?? DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            try
            {
                if (Configuration.Configurations.MutiThreading == 0)
                {
                    while (true)
                    {
                        try
                        {
                            lock (Connection.ResourceLoadingLock)
                            {
                                var cpuCounter = new PerformanceCounter
                                {
                                    CategoryName = "Processor",
                                    CounterName  = "% Processor Time",
                                    InstanceName = "_Total"
                                };
                                var ramCounter      = new PerformanceCounter("Memory", "Available KBytes");
                                var maxMemoryNeeded = _problem.DataSets.Select(i => i.MemoryLimit)
                                                      .Concat(new long[] { 0 })
                                                      .Max();
                                if (cpuCounter.NextValue() <= 70 &&
                                    ramCounter.NextValue() > maxMemoryNeeded + 262144)
                                {
                                    break;
                                }
                            }
                        }
                        catch
                        {
                            //ignored
                        }
                        Thread.Sleep(1000);
                    }
                }

                Connection.UpdateJudgeInfo(JudgeResult);
                _workingdir = Environment.GetEnvironmentVariable("temp") + "\\hjudgeWorkingDir\\Judge_hjudge_" + _id;

                var t = Configuration.Configurations.Compiler.FirstOrDefault(i => i.DisplayName == type);
                if (t == null)
                {
                    for (var i = 0; i < JudgeResult.Result.Length; i++)
                    {
                        JudgeResult.Result[i]     = "Compile Error";
                        JudgeResult.Exitcode[i]   = 0;
                        JudgeResult.Score[i]      = 0;
                        JudgeResult.Timeused[i]   = 0;
                        JudgeResult.Memoryused[i] = 0;
                    }
                    Connection.UpdateJudgeInfo(JudgeResult);

                    lock (Connection.JudgeListCntLock)
                    {
                        Connection.CurJudgingCnt--;
                    }
                    Connection.UpdateMainPageState(
                        $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} 评测完毕 #{JudgeResult.JudgeId},题目:{JudgeResult.ProblemName},用户:{JudgeResult.UserName},结果:{JudgeResult.ResultSummary}",
                        textBlock);
                    return;
                }
                var extList = t.ExtName.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (extList.Length == 0)
                {
                    for (var i = 0; i < JudgeResult.Result.Length; i++)
                    {
                        JudgeResult.Result[i]     = "Compile Error";
                        JudgeResult.Exitcode[i]   = 0;
                        JudgeResult.Score[i]      = 0;
                        JudgeResult.Timeused[i]   = 0;
                        JudgeResult.Memoryused[i] = 0;
                    }
                    Connection.UpdateJudgeInfo(JudgeResult);

                    lock (Connection.JudgeListCntLock)
                    {
                        Connection.CurJudgingCnt--;
                    }
                    Connection.UpdateMainPageState(
                        $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} 评测完毕 #{JudgeResult.JudgeId},题目:{JudgeResult.ProblemName},用户:{JudgeResult.UserName},结果:{JudgeResult.ResultSummary}",
                        textBlock);
                    return;
                }

                if (string.IsNullOrEmpty(_problem.CompileCommand))
                {
                    _problem.CompileCommand = t.LinuxComArgs
                        ? GetRealStringWSL(t.CompilerArgs, 0, extList[0])
                        : GetRealString(t.CompilerArgs, 0, extList[0]);
                }
                else
                {
                    var commList = _problem.CompileCommand.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var s in commList)
                    {
                        var comm = s.Split(':');
                        if (comm.Length != 2)
                        {
                            continue;
                        }
                        if (comm[0] == type)
                        {
                            _problem.CompileCommand = t.LinuxComArgs
                                ? GetRealStringWSL(comm[1], 0, extList[0])
                                : GetRealString(comm[1], 0, extList[0]);
                            break;
                        }
                    }
                }

                _problem.SpecialJudge = GetRealString(_problem.SpecialJudge, 0, extList[0]);

                for (var i = 0; i < _problem.ExtraFiles.Length; i++)
                {
                    _problem.ExtraFiles[i] = GetRealString(_problem.ExtraFiles[i], i, extList[0]);
                }
                for (var i = 0; i < _problem.DataSets.Length; i++)
                {
                    _problem.DataSets[i].InputFile  = GetRealString(_problem.DataSets[i].InputFile, i, extList[0]);
                    _problem.DataSets[i].OutputFile = GetRealString(_problem.DataSets[i].OutputFile, i, extList[0]);
                }

                _problem.InputFileName  = isStdIO ? "stdin" : GetRealString(_problem.InputFileName, 0, extList[0]);
                _problem.OutputFileName = GetRealString(_problem.OutputFileName, 0, extList[0]);

                Connection.UpdateMainPageState(
                    $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} 开始评测 #{JudgeResult.JudgeId},题目:{JudgeResult.ProblemName},用户:{JudgeResult.UserName}",
                    textBlock);

                BeginJudge(
                    t.LinuxComExec
                        ? GetRealStringWSL(t.CompilerExec, 0, extList[0])
                        : GetRealString(t.CompilerExec, 0, extList[0]),
                    t.LinuxStaExec
                        ? GetRealStringWSL(t.StaticCheck, 0, extList[0])
                        : GetRealString(t.StaticCheck, 0, extList[0]),
                    t.LinuxStaArgs
                        ? GetRealStringWSL(t.StaticArgs, 0, extList[0])
                        : GetRealString(t.StaticArgs, 0, extList[0]),
                    t.LinuxRunExec
                        ? GetRealStringWSL(t.RunExec, 0, extList[0])
                        : GetRealString(t.RunExec, 0, extList[0]),
                    t.LinuxRunArgs
                        ? GetRealStringWSL(t.RunArgs, 0, extList[0])
                        : GetRealString(t.RunArgs, 0, extList[0]), textBlock);

                Connection.UpdateJudgeInfo(JudgeResult);

                Connection.UpdateMainPageState(
                    $"{DateTime.Now:yyyy/MM/dd HH:mm:ss} 评测完毕 #{JudgeResult.JudgeId},题目:{JudgeResult.ProblemName},用户:{JudgeResult.UserName},结果:{JudgeResult.ResultSummary}",
                    textBlock);

                if (userId > 1)
                {
                    if (JudgeResult.ResultSummary.Contains("Exceeded"))
                    {
                        DeltaCoins      = Connection.RandomNum.Next(4, 16);
                        DeltaExperience = Connection.RandomNum.Next(8, 16);
                    }
                    else if (JudgeResult.ResultSummary.Contains("Accepted"))
                    {
                        DeltaCoins      = Connection.RandomNum.Next(16, 64);
                        DeltaExperience = Connection.RandomNum.Next(32, 64);
                    }
                    else
                    {
                        DeltaExperience = Connection.RandomNum.Next(2, 4);
                    }

                    Connection.UpdateCoins(userId, DeltaCoins);
                    Connection.UpdateExperience(userId, DeltaExperience);
                }
            }
            catch
            {
                //ignored
            }

            try
            {
                DeleteFiles(_workingdir);
            }
            catch
            {
                //ignored
            }
            lock (Connection.JudgeListCntLock)
            {
                Connection.CurJudgingCnt--;
            }
        }