Exemple #1
0
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string FolderPath = getHdfAsString(PathFolderSave);
            string inputFile  = FolderPath + "/" + getHdfAsString(ProblemsUrl) + "." + getExtenstion();
            string fileOutPut = FolderPath + "/" + getHdfAsString(ProblemsUrl) + ".exe";
            //Dịch sang exe
            DataTable dtCompiler = objCompiler.LoadByID(getHdfAsLong(CompilerID));

            try
            {
                objSub.Update(getHdfAsLong(SubmissionId), 2, 0); // đang xử lý
                string date = "";
                if (File.Exists(inputFile))                      // đổi tên file nếu đã tồn tại, tên inputFile luôn là tên file được chạy cuối cùng
                {
                    date = File.GetLastWriteTime(inputFile).ToString("HH_mm_ss,dd_MM_yyyy");
                    System.IO.File.Move(inputFile, inputFile.Replace(".", date + "."));
                }
                FileHelper.SaveFile(inputFile, txtInput.Text);



                double timeToCompiler = 0;
                string compilerPath   = dtCompiler.Rows[0]["CompilerPath"].ToString();
                switch (getHdfAsLong(CompilerID))
                {
                case 1:     // C
                    //Biên dịch sang exe
                    timeToCompiler = CompilerC.CompileToExe(SettingsHelper.COMPILER_PATH.CPlusPlusGccCompilerCodeBlockPath, inputFile, fileOutPut);
                    break;

                case 2:     // C++
                    //Biên dịch sang exe
                    timeToCompiler = CompilerC.CompileToExe(compilerPath, inputFile, fileOutPut);
                    break;

                case 3:     // Java
                    timeToCompiler = CompilerJava.CompileToExe(compilerPath, inputFile);
                    break;

                default:
                    break;
                }

                //if (!File.Exists(FolderPath + "/input.txt"))
                //{
                //    File.Create(FolderPath + "/input.txt");
                //}
            }
            catch (Exception ex)
            {
                objSub.Update(getHdfAsLong(SubmissionId), 4, 0); // lỗi biên dịch
                throw new Exception(ex.Message.Replace(inputFile + ":", ""));
            }

            //Chạy exe lấy KQ
            try
            {
                //Lấy tất cả các test case
                BUS_ProblemsTestCases objProblemsTestCases = new BUS_ProblemsTestCases();
                DataTable             dt = objProblemsTestCases.LoadByProblemID(getHdfAsLong(ProblemId));

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    string folderTestCase = getHdfAsString(FolderTestCase);
                    for (int i = 0; i < count; i++)
                    {
                        string FileName = folderTestCase + dt.Rows[i]["FileName"].ToString();
                        if (File.Exists(FileName + ".IN") && File.Exists(FileName + ".OUT"))
                        {
                            //Chạy file exe lấy kết quả
                            // List<string> executablePath = new List<string>();

                            string contentFileIN = File.ReadAllText(FileName + ".IN");

                            string[] inputs = contentFileIN.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                            // executablePath.Add("8");
                            //executablePath.Add("3");
                            //Ghi vào file
                            // File.Copy(FileName + ".IN", FolderPath + "/input.txt", true);\

                            string outputstring   = "";
                            string contentFileOut = "";
                            string excuteablePath = dtCompiler.Rows[0]["ExcutablePath"].ToString();
                            switch (getHdfAsLong(CompilerID))
                            {
                            case 1:     // C

                            case 2:     // C++
                                outputstring = CompilerC.RunExeGetOutput(FolderPath, fileOutPut, inputs);

                                break;

                            case 3:     // Java
                                outputstring = CompilerJava.RunExeGetOutput(excuteablePath, FolderPath, fileOutPut, inputs);
                                break;

                            default:
                                break;
                            }
                            contentFileOut = File.ReadAllText(FileName + ".Out");

                            if (contentFileOut.Equals(outputstring))
                            {
                                long point = getHdfAsLong(Point) + long.Parse(dt.Rows[i][Point].ToString());
                                hdfUrl.Set(Point, point);
                                objSub.Update(getHdfAsLong(SubmissionId), 2, point);
                            }
                        }
                    }
                    //trừ đi điểm lần submiss trước, cộng với điểm submiss lần này(lấy kết quả cuối cùng)
                    //lấy submiss trước
                    long usID = getHdfAsLong(UserID);

                    long pointOld         = objSub.getPoint(getHdfAsLong(ContestId), getHdfAsLong(ProblemId), usID);
                    long pointNew         = getHdfAsLong(Point);
                    long pointUserCurrent = objUser.getPoint(usID);
                    pointUserCurrent = pointUserCurrent - pointOld + pointNew;
                    objUser.UpdatePoint(pointUserCurrent, usID);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.Replace(inputFile + ":", ""));
            }
            if (File.Exists(fileOutPut))
            {
                File.Delete(fileOutPut);
            }
        }