Ejemplo n.º 1
0
        private void Load()
        {
            lock (_loadLock)
            {
                Dispatcher.Invoke(() =>
                {
                    for (var i = CompetitionStateColumn.Columns.Count - 1; i >= 0; i--)
                    {
                        if (CompetitionStateColumn.Columns[i] is GridViewColumn t && t.Header is StackPanel)
                        {
                            CompetitionStateColumn.Columns.RemoveAt(i);
                        }
                    }
                });
                for (var i = 0; i < (_competition.ProblemSet?.Length ?? 0); i++)
                {
                    if (_competition.ProblemSet != null)
                    {
                        var t = Properties.Resources.CompetitionDetailsProblemInfoControl.Replace("${index}",
                                                                                                  $"{i}").Replace("${ProblemName}",
                                                                                                                  Connection.GetProblemName(_competition.ProblemSet[i]));
                        var strreader = new StringReader(t);
                        var xmlreader = new XmlTextReader(strreader);
                        Dispatcher.Invoke(() =>
                        {
                            var obj = XamlReader.Load(xmlreader);
                            CompetitionStateColumn.Columns.Add(obj as GridViewColumn);
                        });
                    }
                }
                Dispatcher.Invoke(() => CompetitionState.ItemsSource = _competitionInfo);
                Dispatcher.Invoke(() => CompetitionState.ItemsSource = _competitionInfo);
                var x = Connection.QueryJudgeLogBelongsToCompetition(_competition.CompetitionId, 0);

                for (var i = x.Count - 1; i >= 0; i--)
                {
                    if ((_competition.Option & 32) != 0)
                    {
                        if (x[i].ResultSummary != "Accepted")
                        {
                            for (var j = 0; j < x[i].Score.Length; j++)
                            {
                                x[i].Score[j] = 0;
                            }
                        }
                    }
                }
                Dispatcher.Invoke(() =>
                {
                    _curJudgeInfo.Clear();
                    _competitionInfo.Clear();
                });
                for (var i = x.Count - 1; i >= 0; i--)
                {
                    Dispatcher.Invoke(() => _curJudgeInfo.Add(x[i]));
                }
                Dispatcher.Invoke(() =>
                {
                    foreach (var i in CompetitionStateColumn.Columns)
                    {
                        if (i.Header is StackPanel j)
                        {
                            foreach (var k in j.Children)
                            {
                                if (k is TextBlock l && l.Name.Contains("ProblemColumn"))
                                {
                                    var m  = Convert.ToInt32(l.Name.Substring(13));
                                    l.Text =
                                        $"{x.Count(p => p.ProblemId == _competition.ProblemSet[m] && p.ResultSummary == "Accepted")}/{x.Count(p => p.ProblemId == _competition.ProblemSet[m])}";
                                }
                            }
                        }
                    }
                });
                var tmpList = new List <CompetitionUserInfo>();
                Dispatcher.Invoke(() => _competitionInfo.Clear());
                var user = x.Select(p => p.UserName).Distinct();
                Dispatcher.Invoke(() => ComUserNumber.Text = $"{user.Count()}");
                foreach (var i in user)
                {
                    if (_competition.ProblemSet == null)
                    {
                        continue;
                    }
                    var tmp = new CompetitionUserInfo
                    {
                        UserName    = i,
                        ProblemInfo = new CompetitionProblemInfo[_competition.ProblemSet.Length]
                    };
                    float score   = 0;
                    var   totTime = new TimeSpan(0);
                    for (var j = 0; j < _competition.ProblemSet.Length; j++)
                    {
                        tmp.ProblemInfo[j] = new CompetitionProblemInfo();
                        var ac = x.Count(p => p.UserName == i && p.ResultSummary == "Accepted" &&
                                         p.ProblemId == _competition.ProblemSet[j]);
                        var all = x.Count(p => p.UserName == i && p.ProblemId == _competition.ProblemSet[j]);
                        tmp.ProblemInfo[j].Color = ac != 0 ? Brushes.LightGreen : Brushes.LightPink;
                        var time         = new TimeSpan(0);
                        var cnt          = 0;
                        var tmpScoreBase = x.Where(p => p.UserName == i && p.ProblemId == _competition.ProblemSet[j])
                                           .ToList();
                        var noAccepted = !x.Any(p => p.UserName == i && p.ProblemId == _competition.ProblemSet[j] && p.ResultSummary == "Accepted");
                        if ((_competition.Option & 2) == 0)
                        {
                            if ((_competition.Option & 32) == 0)
                            {
                                if (tmpScoreBase.Any())
                                {
                                    score += tmpScoreBase.Max(p => p.FullScore);
                                }
                            }
                            tmp.ProblemInfo[j].State = $"{ac}/{all}";
                            foreach (var k in x.Where(p =>
                                                      p.UserName == i && p.ProblemId == _competition.ProblemSet[j]))
                            {
                                var tmpTime = Convert.ToDateTime(k.JudgeDate) - _competition.StartTime;
                                time    += tmpTime;
                                totTime += tmpTime;
                                if (k.ResultSummary == "Accepted")
                                {
                                    if ((_competition.Option & 32) != 0)
                                    {
                                        score += k.FullScore;
                                    }
                                    break;
                                }
                                if ((_competition.Option & 4) != 0)
                                {
                                    if (!noAccepted)
                                    {
                                        time    += new TimeSpan(0, 20, 0);
                                        totTime += new TimeSpan(0, 20, 0);
                                    }
                                    cnt++;
                                }
                            }
                        }
                        else
                        {
                            if ((_competition.Option & 32) == 0)
                            {
                                if (tmpScoreBase.Any())
                                {
                                    score += tmpScoreBase.LastOrDefault()?.FullScore ?? 0;
                                }
                            }
                            var y = x.LastOrDefault(p => p.UserName == i && p.ProblemId == _competition.ProblemSet[j]);
                            if (y != null && y.ResultSummary == "Accepted")
                            {
                                if ((_competition.Option & 32) != 0)
                                {
                                    score += y.FullScore;
                                }
                                tmp.ProblemInfo[j].State = "Solved";
                            }
                            else
                            {
                                tmp.ProblemInfo[j].Color = Brushes.LightPink;
                                tmp.ProblemInfo[j].State = "Unsolved";
                            }
                            if (y != null)
                            {
                                var tmpTime = Convert.ToDateTime(y.JudgeDate) - _competition.StartTime;
                                if (!noAccepted)
                                {
                                    time    += tmpTime;
                                    totTime += tmpTime;
                                }
                            }
                        }
                        if ((_competition.Option & 4) != 0 && cnt != 0)
                        {
                            tmp.ProblemInfo[j].State += $" (-{cnt})";
                        }
                        tmp.ProblemInfo[j].Time = $"{time.Days * 24 + time.Hours}:{time.Minutes}:{time.Seconds}";
                    }
                    tmp.Score   = score;
                    tmp.TotTime = totTime;
                    tmpList.Add(tmp);
                }
                tmpList.Sort((x1, x2) =>
                {
                    if (Math.Abs(x1.Score - x2.Score) > 0.00001)
                    {
                        return(x2.Score.CompareTo(x1.Score));
                    }
                    return(x1.TotTime.CompareTo(x2.TotTime));
                });
                for (var i = 0; i < tmpList.Count; i++)
                {
                    tmpList[i].Rank = i + 1;
                    Dispatcher.Invoke(() => _competitionInfo.Add(tmpList[i]));
                }
                Dispatcher.Invoke(() =>
                {
                    _problemFilter.Clear();
                    _userFilter.Clear();
                });
                foreach (var judgeInfo in x)
                {
                    Dispatcher.Invoke(() =>
                    {
                        if (_problemFilter.All(i => i != judgeInfo.ProblemName))
                        {
                            _problemFilter.Add(judgeInfo.ProblemName);
                        }
                        if (_userFilter.All(i => i != judgeInfo.UserName))
                        {
                            _userFilter.Add(judgeInfo.UserName);
                        }
                    });
                }
                if (DateTime.Now < _competition.EndTime)
                {
                    _hasRefreshWhenFinished = false;
                }
            }
        }
Ejemplo n.º 2
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--;
            }
        }