コード例 #1
0
ファイル: GradeController.cs プロジェクト: igi33/online-judge
        public async Task <ActionResult <GradeDto> > Grade([FromBody] GradeSubmissionDto gradeSubmissionDto)
        {
            // Get language info
            ComputerLanguage lang = await _context.ComputerLanguages.FindAsync(gradeSubmissionDto.LangId);

            if (lang == null)
            {
                return(BadRequest(new { Message = "The supplied computer language id doesn't exist!" }));
            }

            string executionRootDir = Grader.GetExecutionRootDir();
            string randomId         = RandomString(32);

            string sourceFileName = $"{randomId}.{lang.Extension}";
            string binaryFileName = randomId;

            string sourceFilePath = $"{executionRootDir}{sourceFileName}";
            string binaryFilePath = $"{executionRootDir}{binaryFileName}";

            // Create file from source code inside rootDir
            System.IO.File.WriteAllText(sourceFilePath, gradeSubmissionDto.SourceCode);

            GradeDto result = new GradeDto(); // this will be returned by the method

            bool readyToRun = true;

            // Check if compiled language
            if (!string.IsNullOrEmpty(lang.CompileCmd))
            {
                // Compile submission
                CompilationOutputDto co = Grader.Compile(lang, sourceFileName, binaryFileName);

                if (co.ExitCode != 0)
                {
                    // Compile error
                    result.Status = "CE";     // Mark status as Compile Error
                    result.Error  = co.Error; // Set message as compile error message
                    readyToRun    = false;
                }
            }

            if (readyToRun)
            {
                // Compiled successfully or interpreted language, so we're ready to run the solution

                string fileName = string.IsNullOrEmpty(lang.CompileCmd) ? sourceFileName : binaryFileName;

                // Grade solution
                result = Grader.Grade(lang, fileName, gradeSubmissionDto.Input, gradeSubmissionDto.ExpectedOutput, gradeSubmissionDto.TimeLimit, gradeSubmissionDto.MemoryLimit);

                // Delete binary file
                System.IO.File.Delete(binaryFilePath);
            }

            // Delete source file
            System.IO.File.Delete(sourceFilePath);

            return(Ok(result));
        }
コード例 #2
0
 public string Grade()
 {
     return(mGrader.Grade(mA, mB, mC));
 }
コード例 #3
0
        public async Task <ActionResult <SubmissionDto> > PostSubmission(int taskId, [FromBody] SubmissionDto submissionDto)
        {
            int currentUserId = int.Parse(User.FindFirst(JwtRegisteredClaimNames.Sub).Value);

            // Get task info
            Entities.Task task = await _context.Tasks.SingleOrDefaultAsync(t => t.Id == taskId && (t.IsPublic || t.UserId == currentUserId));

            if (task == null)
            {
                return(BadRequest());
            }

            // Get language info
            ComputerLanguage lang = await _context.ComputerLanguages.FindAsync(submissionDto.LangId);

            if (lang == null)
            {
                return(BadRequest());
            }

            // Get test cases
            await _context.Entry(task).Collection(t => t.TestCases).LoadAsync();

            Submission submission = new Submission
            {
                SourceCode      = submissionDto.SourceCode,
                LangId          = submissionDto.LangId,
                UserId          = currentUserId,
                TimeSubmitted   = DateTime.Now,
                TaskId          = taskId,
                Status          = "UD",
                ExecutionTime   = 0,
                ExecutionMemory = 0,
            };

            // Save initial submission to DB to get a unique id
            _context.Submissions.Add(submission);
            await _context.SaveChangesAsync();

            string executionRootDir = Grader.GetExecutionRootDir();

            string sourceFileName = $"{submission.Id}.{lang.Extension}";
            string binaryFileName = submission.Id.ToString();

            string sourceFilePath = $"{executionRootDir}{sourceFileName}";
            string binaryFilePath = $"{executionRootDir}{binaryFileName}";

            // Create file from source code inside rootDir
            System.IO.File.WriteAllText(sourceFilePath, submissionDto.SourceCode);

            bool readyToRun = true;

            // Check if compiled language
            if (!string.IsNullOrEmpty(lang.CompileCmd))
            {
                // Compile submission
                CompilationOutputDto co = Grader.Compile(lang, sourceFileName, binaryFileName);

                if (co.ExitCode != 0)
                {
                    // Compile error
                    submission.Status     = "CE";     // Mark status as Compile Error
                    submissionDto.Message = co.Error; // Set message as compile error message
                    readyToRun            = false;
                }
            }

            if (readyToRun)
            {
                // Compiled successfully or interpreted language, so we're ready to run the solution

                submission.Status = "AC"; // Submission status will stay accepted if all test cases pass (or if there aren't any TCs)
                string fileName   = string.IsNullOrEmpty(lang.CompileCmd) ? sourceFileName : binaryFileName;
                int    maxTimeMs  = 0;    // Track max execution time of test cases
                int    maxMemoryB = 0;    // Track max execution memory of test cases

                bool correctSoFar = true;
                for (int i = 0; correctSoFar && i < task.TestCases.Count; ++i)
                {
                    TestCase tc = task.TestCases.ElementAt(i);

                    GradeDto grade = Grader.Grade(lang, fileName, tc.Input, tc.Output, task.TimeLimit, task.MemoryLimit);

                    maxTimeMs         = Math.Max(maxTimeMs, grade.ExecutionTime);
                    maxMemoryB        = Math.Max(maxMemoryB, grade.ExecutionMemory);
                    submission.Status = grade.Status;

                    correctSoFar = grade.Status.Equals("AC");
                }

                submission.ExecutionTime   = maxTimeMs;  // Set submission execution time as max out of all test cases
                submission.ExecutionMemory = maxMemoryB; // Set submission execution memory as max out of all test cases

                // Delete binary file if compiled
                if (!string.IsNullOrEmpty(lang.CompileCmd))
                {
                    System.IO.File.Delete(binaryFilePath);
                }
            }

            // Edit submission object status and stats
            await _context.SaveChangesAsync();

            // Delete source file
            System.IO.File.Delete(sourceFilePath);

            // Prepare response DTO
            SubmissionDto responseDto = mapper.Map <SubmissionDto>(submission);

            responseDto.Message          = submissionDto.Message;
            responseDto.Task             = null;
            responseDto.ComputerLanguage = null;
            responseDto.User             = null;

            return(CreatedAtAction("GetSubmission", new { id = submission.Id }, responseDto));
        }