Esempio n. 1
0
        public RunResultDTO SendSolution(int id, [FromBody] TaskSolutionDTO taskSolution)
        {
            using (programming_tasksEntities entities = new programming_tasksEntities())
            {
                string username = Thread.CurrentPrincipal.Identity.Name; //get username from authentication

                user userResult = entities.users.First(user => user.username == username);
                task taskResult = entities.tasks.Find(id);

                if (taskResult == null)
                {
                    ExceptionHandler.ThrowException(HttpStatusCode.NotFound, "Task with id " + id + " doesn't exist");
                }

                CodeExecutor codeExecutor = new CodeExecutor();

                RunResultDTO runResult = codeExecutor.RunTask(taskSolution, taskResult);

                entities.users_solutions.Add(new users_solutions()
                {
                    user        = userResult,
                    task        = taskResult,
                    code        = taskSolution.Code,
                    status      = runResult.CorrectExamples == taskResult.examples.Count,
                    description = runResult.CorrectExamples == taskResult.examples.Count ?
                                  "Code executed successfully for all examples" :
                                  "Code failed at some example(s)",
                    date = DateTime.Now
                });

                entities.SaveChanges();

                return(runResult);
            }
        }
Esempio n. 2
0
        public RunResultDTO RunTask(TaskSolutionDTO taskSolution, task task)
        {
            if (taskSolution == null)
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "No body provided");
            }

            if (taskSolution.Code == null || taskSolution.Code.Trim() == "")
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "No code supplied");
            }

            ILanguage languageType = null;
            string    filePath     = codeLocation;

            if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.JAVA)
            {
                filePath += "\\Main.java";

                languageType = new CompiledLanguage(
                    filePath,
                    "javac -d \"" + binLocation + "\" \"" + filePath + "\"",
                    "java -cp \"" + binLocation + "\"; Main");
            }
            else if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.C_PLUS_PLUS)
            {
                filePath += "\\main.cpp";

                /*
                 * languageType = new CompiledLanguage(
                 *  filePath,
                 *  "gcc -cpp \"" + filePath + "\" -o \"" + binLocation + "\\mainCpp\"",
                 *  "\"" + binLocation + "\\mainCpp\"");
                 * */

                languageType = new CompiledLanguage(
                    filePath,
                    "g++ -std=c++1z -c \"" + filePath + "\" -o \"" + binLocation + "\\main.o\" " +        //create o file in bin folder
                    "&& g++ -std=c++1z \"" + binLocation + "\\main.o\" -o \"" + binLocation + "\\main\"", //link o file to exe
                    "\"" + binLocation + "\\main\"");
            }
            else if (taskSolution.ProgrammingLanguage == ProgrammingLanguage.C_SHARP)
            {
                filePath += "\\Main.cs";

                languageType = new CompiledLanguage(
                    filePath,
                    "csc -out:\"" + binLocation + "\\main.exe\" \"" + filePath + "\"",
                    "\"" + binLocation + "\\main\"");
            }
            else
            {
                ExceptionHandler.ThrowException(HttpStatusCode.BadRequest, "Specified language not supported");
            }

            return(languageType.RunSolution(taskSolution.Code, task));
        }