/// <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;
        }
示例#2
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();
            }
        }