/// <summary>
        ///     Get general information
        /// </summary>
        public void GetSpojInfo()
        {
            if (JobLocker.IsDownloadSpojInfoInProcess)
            {
                return;
            }

            JobLocker.IsDownloadSpojInfoInProcess = true;

            try
            {
                var text = "";

                using (var client = new SpojClient())
                {
                    var adminAccountTask = _adminSettingCacheBusiness.GetFullInfo();
                    adminAccountTask.Wait();

                    var rankUrl     = string.Format(_rankUrl, adminAccountTask.Result.ContestName);
                    var downloadUrl = string.Format(_downloadUrl, adminAccountTask.Result.ContestName);

                    var adminAccount = adminAccountTask.Result;
                    if (string.IsNullOrEmpty(adminAccount.Username) || string.IsNullOrEmpty(adminAccount.Password))
                    {
                        JobLocker.IsDownloadSpojInfoInProcess = false;
                        return;
                    }

                    var result = client.LoginAsync(adminAccount.Username, adminAccount.Password);
                    result.Wait();

                    text = client.GetText(rankUrl);
                    Thread.Sleep(1000);
                    text = client.GetText(downloadUrl);
                }

                var tokenizer = new SpojDataTokenizer(text);

                var contest = ParseContest(tokenizer);
                contest.ProblemsInfo = ParseProblems(tokenizer);
                contest.Users        = ParseUsers(tokenizer);

                ParseUserSubmissions(tokenizer, contest.Users, contest.ProblemsInfo);
            }
            catch (Exception e)
            {
                LogHepler.WriteSystemErrorLog(e, ApplicationConfigs.SystemInfo.ErrorLogFolderPath);
            }

            JobLocker.IsDownloadSpojInfoInProcess = false;
        }
        /// <summary>
        ///     Down load submission details
        /// </summary>
        public void GetSubmissionInfo()
        {
            if (JobLocker.IsDownloadSubmissionInfoInProcess)
            {
                return;
            }

            JobLocker.IsDownloadSubmissionInfoInProcess = true;

            try
            {
                using (var client = new SpojClient())
                {
                    var adminAccountTask = _adminSettingCacheBusiness.GetFullInfo();
                    adminAccountTask.Wait();
                    var adminAccount = adminAccountTask.Result;
                    if (string.IsNullOrEmpty(adminAccount.Username) || string.IsNullOrEmpty(adminAccount.Password))
                    {
                        JobLocker.IsDownloadSubmissionInfoInProcess = false;
                        return;
                    }
                    var result = client.LoginAsync(adminAccount.Password, adminAccount.Password);
                    result.Wait();
                    var submissions = _submissionRepository.Get(x => x.IsDownloadedInfo != true && x.IsNotHaveEnoughInfo != true &&
                                                                x.Problem.IsSkip != true).Include(x => x.Problem).OrderByDescending(x => x.SubmitTime).Take(50).ToList();
                    foreach (var submission in submissions)
                    {
                        if (submission == null)
                        {
                            continue;
                        }
                        if (submission.Problem == null)
                        {
                            submission.IsNotHaveEnoughInfo = true;
                            _submissionRepository.Update(submission, x => x.IsNotHaveEnoughInfo);

                            _submissionRepository.SaveChanges();
                            continue;
                        }
                        if (!Regex.IsMatch(submission.Problem.Code, "^EI\\w+"))
                        {
                            submission.Problem.IsSkip = true;
                            _problemRepository.Update(submission.Problem);
                            _problemRepository.SaveChanges();
                            continue;
                        }

                        var plaintext = client.GetText(string.Format(_submissionInfoUrl, adminAccountTask.Result.ContestName, submission.SpojId));
                        var matches   = Regex.Matches(plaintext, "test (\\d+) - (\\w+)");

                        var listResultEnities = new List <ResultEntity>();

                        foreach (Match match in matches)
                        {
                            var resultType = GetResultType(match.Groups[2].Value);
                            listResultEnities.Add(new ResultEntity
                            {
                                SubmissionId = submission.Id,
                                TestCaseSeq  = int.Parse(match.Groups[1].Value),
                                Result       = resultType
                            });
                        }

                        _resultRepository.InsertRange(listResultEnities);
                        _resultRepository.SaveChanges();

                        submission.IsDownloadedInfo = true;
                        submission.DownloadedTime   = DateTime.Now;
                        _submissionRepository.Update(submission, x => x.IsDownloadedInfo, x => x.DownloadedTime);
                        _submissionRepository.SaveChanges();

                        listResultEnities = new List <ResultEntity>();
                    }
                }
            }
            catch (Exception e)
            {
                LogHepler.WriteSystemErrorLog(e, ApplicationConfigs.SystemInfo.ErrorLogFolderPath);
            }

            JobLocker.IsDownloadSubmissionInfoInProcess = false;
        }
Beispiel #3
0
        public void SyncTestCase(string problemCode)
        {
            using (var client = new SpojClient())
            {
                var adminAccount = GetAdminUsernameAndPassword();
                if (string.IsNullOrEmpty(adminAccount.Username) || string.IsNullOrEmpty(adminAccount.Password))
                {
                    return;
                }

                var loginTask = client.LoginAsync(adminAccount.Username, adminAccount.Password);
                loginTask.Wait();
                var thisProblem = _problemRepository.Get(x => x.Code == problemCode).FirstOrDefault();
                if (thisProblem == null)
                {
                    return;
                }
                if (!Regex.IsMatch(thisProblem.Code, "^EI\\w+"))
                {
                    thisProblem.IsSkip = true;
                    _problemRepository.Update(thisProblem, x => x.IsSkip);

                    _problemRepository.SaveChanges();

                    return;
                }
                var numberOfTestCase = client.GetTotalTestCase(thisProblem.Code);
                var path             = Path.Combine(Directory.GetCurrentDirectory(), $"TestCases/{thisProblem.Code}");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var testCaseEntity = _testCaseRepository.Get(x => x.ProblemId == thisProblem.Id).FirstOrDefault();
                if (testCaseEntity == null)
                {
                    testCaseEntity = new TestCaseInfoEntity
                    {
                        ProblemId     = thisProblem.Id,
                        TotalTestCase = numberOfTestCase,
                        Path          = path
                    };
                    _testCaseRepository.Insert(testCaseEntity);
                }

                for (int i = 0; i < numberOfTestCase; i++)
                {
                    var input  = "";
                    var output = "";
                    try
                    {
                        input  = client.GetText(string.Format(_inputTestCaseUrl, thisProblem.Code, i));
                        output = client.GetText(string.Format(_outputTestCaseUrl, thisProblem.Code, i));
                        File.WriteAllText(Path.Combine(path, $"{i}.in"), input);
                        File.WriteAllText(Path.Combine(path, $"{i}.out"), output);
                    }
                    catch (Exception e)
                    {
                        LogHepler.WriteSystemErrorLog(e, ApplicationConfigs.SystemInfo.ErrorLogFolderPath);
                    }
                }

                thisProblem.IsDownloadedTestCase = true;
                thisProblem.DownloadTestCaseTime = DateTime.Now;
                _problemRepository.Update(thisProblem, x => x.IsDownloadedTestCase, x => x.DownloadTestCaseTime);

                _problemRepository.SaveChanges();
            }
        }
        /// <summary>
        ///     Cron job download TestCase
        /// </summary>
        public void DownloadSpojTestCases()
        {
            if (JobLocker.IsDownloadTestCasesInProcess)
            {
                return;
            }

            JobLocker.IsDownloadTestCasesInProcess = true;

            try
            {
                using (var client = new SpojClient())
                {
                    var adminAccountTask = _adminSettingCacheBusiness.GetAdminAccountAsync();
                    adminAccountTask.Wait();
                    var adminAccount = adminAccountTask.Result;
                    if (string.IsNullOrEmpty(adminAccount.Username) || string.IsNullOrEmpty(adminAccount.Password))
                    {
                        JobLocker.IsDownloadTestCasesInProcess = false;
                        return;
                    }

                    var result = client.LoginAsync(adminAccount.Username, adminAccount.Password);
                    result.Wait();
                    var problems = _problemRepository.Get().Where(x => x.IsDownloadedTestCase != true && x.IsSkip != true).Take(100).ToList();
                    foreach (var problem in problems)
                    {
                        if (!Regex.IsMatch(problem.Code, "^EI\\w+"))
                        {
                            problem.IsSkip = true;
                            _problemRepository.Update(problem, x => x.IsSkip);

                            _problemRepository.SaveChanges();

                            continue;
                        }

                        var maxTestCase = 0;
                        maxTestCase = client.GetTotalTestCase(problem.Code);

                        var path = Path.Combine(Directory.GetCurrentDirectory(), $"TestCases/{problem.Code}");
                        Directory.CreateDirectory(path);

                        _testCaseRepository.Insert(new TestCaseInfoEntity
                        {
                            ProblemId     = problem.Id,
                            TotalTestCase = maxTestCase,
                            Path          = path
                        });

                        for (var i = 0; i <= maxTestCase; i++)
                        {
                            var input  = "";
                            var output = "";
                            try
                            {
                                input  = client.GetText(string.Format(_inputTestCaseUrl, problem.Code, i));
                                output = client.GetText(string.Format(_outputTestCaseUrl, problem.Code, i));
                                File.WriteAllText(Path.Combine(path, $"{i}.in"), input);
                                File.WriteAllText(Path.Combine(path, $"{i}.out"), output);
                            }
                            catch (Exception e)
                            {
                                LogHepler.WriteSystemErrorLog(e, ApplicationConfigs.SystemInfo.ErrorLogFolderPath);
                            }
                        }

                        problem.IsDownloadedTestCase = true;
                        problem.DownloadTestCaseTime = DateTime.Now;
                        _problemRepository.Update(problem, x => x.IsDownloadedTestCase, x => x.DownloadTestCaseTime);

                        _problemRepository.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                LogHepler.WriteSystemErrorLog(e, ApplicationConfigs.SystemInfo.DataError);
            }

            JobLocker.IsDownloadTestCasesInProcess = false;
        }
        public async Task InstantDownLoadSubmissionAsync(int accountId, string accountName, int submissionId)
        {
            var listTask = new List <Task <byte[]> >();

            using (var client = new SpojClient())
            {
                var adminAccount = await _adminSettingCacheBusiness.GetFullInfo();

                var loginTask = client.LoginAsync(adminAccount.Username, adminAccount.Password);
                loginTask.Wait();
                for (int i = 0; i < 5; i++)
                {
                    var pageNum     = i * 20;
                    var downloadUrl = string.Format(UserSubmissionHistory, adminAccount.ContestName, accountName, pageNum);
                    listTask.Add(client.GetByteArrayAsync(downloadUrl));
                }

                var allTask = Task.WhenAll(listTask);
                allTask.Wait();

                string   problemCode = null;
                DateTime submitTime  = new DateTime();
                float    runtime     = -1;

                var listResult = allTask.Result;
                foreach (var listByte in listResult)
                {
                    var pattern = $"/{adminAccount.ContestName}/submit/([0-9A-Z]+)/id={submissionId}";
                    var html    = Encoding.UTF8.GetString(listByte);

                    var match = Regex.Match(html, pattern);
                    if (!match.Success)
                    {
                        continue;
                    }
                    problemCode = match.Groups[1].ToString();

                    //Get submit time
                    var dateSubmitPattern = $"{submissionId}" + " \\([A-Z]+\\), (\\d{4}[-]\\d{2}[-]\\d{2} \\d{2}:\\d{2}:\\d{2})\"";
                    var matchTime         = Regex.Match(html, dateSubmitPattern);
                    submitTime = DateTime.ParseExact(matchTime.Groups[1].ToString(), "yyyy-MM-dd HH:mm:ss",
                                                     System.Globalization.CultureInfo.InvariantCulture);
                    break;
                }
                if (problemCode == null)
                {
                    return;
                }
                string plaintext;

                try
                {
                    plaintext = client.GetText(string.Format(_submissionInfoUrl, adminAccount.ContestName, submissionId));
                }
                catch (Exception e)
                {
                    return;
                }

                // Get results
                var matches = Regex.Matches(plaintext, "test (\\d+) - (\\w+)");

                var problemId = _problemCacheBusiness.GetProblemIdByCode(problemCode);
                if (problemId == 0)
                {
                    return;
                }
                var submissionEntity = new SubmissionEntity
                {
                    ProblemId        = problemId,
                    AccountId        = accountId,
                    DownloadedTime   = DateTime.Now,
                    IsDownloadedInfo = true,
                    SpojId           = submissionId,
                    RunTime          = runtime,
                    SubmitTime       = submitTime,
                    TotalResult      = matches.Count
                };
                Repository.Insert(submissionEntity);

                var listResultEnities = new List <ResultEntity>();
                var acCount           = 0;
                foreach (Match match in matches)
                {
                    var resultType = GetResultType(match.Groups[2].Value);
                    if (resultType == Enums.ResultType.Accepted)
                    {
                        acCount++;
                    }
                    listResultEnities.Add(new ResultEntity
                    {
                        SubmissionId = submissionEntity.Id,
                        TestCaseSeq  = int.Parse(match.Groups[1].Value),
                        Result       = resultType,
                    });
                }

                submissionEntity.Score = ((float)acCount / matches.Count) * 100;

                _resultRepository.InsertRange(listResultEnities);
                //_resultRepository.SaveChanges();
                _resultRepository.SaveChanges();

                Repository.SaveChanges();

                _submissionCacheBusiness.AddId(submissionId);
            }
        }