/// <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;
        }
Exemple #2
0
        public static int GetTotalTestCase(this SpojClient spojClient, string spojProblemCode)
        {
            var text    = spojClient.GetText(string.Format(SpojProblemInfoUrl, spojProblemCode));
            var matches = Regex.Matches(text.Trim(), "<option value=\"(\\d+)\">Modify testcase #\\d+</option>");

            var listNumber = new List <int>();

            foreach (Match match in matches)
            {
                try
                {
                    var value = int.Parse(match.Groups[1].Value);
                    listNumber.Add(value);
                }
                catch (Exception e)
                {
                    LogHepler.WriteCustomErrorLog(e, "Logger/SpojClient");
                }
            }

            return(listNumber.Max());
        }
        /// <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;
        }
        /// <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;
        }
Exemple #5
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();
            }
        }