Пример #1
0
        public async Task RunAssessmentsForExercise(ApplicationUser user, string code, int chapter, int exercise)
        {
            var      output = new StringBuilder();
            Exercise exerciseDetails;
            string   userCode;
            var      result = false;

            if (!user.ExercisePermitted(chapter, exercise))
            {
                WriteToConsole("You do not have permission to do this exercise\r\n");
                return;
            }
            if ((exerciseDetails = await rep.GetExerciseAsync(chapter, exercise)) == null)
            {
                WriteToConsole("Invalid exercise details\r\n");
                return;
            }
            if (!code.StartsWith(exerciseDetails.HiddenCodeHeader))
            {
                WriteToConsole("Something went wrong with the pre-prepared code header");
                throw new InvalidOperationException("Start of user's code does not match the hidden code header");
            }
            userCode = code.Substring(exerciseDetails.HiddenCodeHeader.Length + 1);

            try
            {
                var assessmentGroups = await rep.GetAssessmentGroupsForExerciseAsync(chapter, exercise);

                var assessmentRunner = new AssessmentRunner(codeRunner, code);
                assessmentRunner.ConsoleWrite += OnConsoleWrite;
                result = await assessmentRunner.RunAssessmentsAsync(assessmentGroups);

                assessmentRunner.ConsoleWrite -= OnConsoleWrite;
            }
            finally
            {
                var savedAssessment = new Submission
                {
                    UserId             = user.Id,
                    ChapterNo          = chapter,
                    ExerciseNo         = exercise,
                    SubmittedCode      = userCode,
                    Output             = output.ToString(),
                    Success            = result,
                    SubmissionDateTime = DateTimeOffset.UtcNow
                };
                await rep.InsertSubmissionAsync(savedAssessment);
            }

            void WriteToConsole(string message)
            {
                OnConsoleWrite(this, new ConsoleWriteEventArgs(message));
            }

            void OnConsoleWrite(object sender, ConsoleWriteEventArgs e)
            {
                output.Append(e.Message);
                ConsoleWrite?.Invoke(sender, e);
            }
        }
Пример #2
0
 public void ConsoleIn(string text)
 {
     if (text == "Console In")
     {
         ConsoleWrite?.Invoke(this, new ConsoleWriteEventArgs("Received"));
         done.Set();
     }
 }
Пример #3
0
 // Handle console writes to the output stream
 public void OnConsoleWrite(object sender, ConsoleWriteEventArgs e)
 {
     ConsoleWrite?.Invoke(this, e);
 }
 private void DoConsoleWrite(string message)
 {
     ConsoleWrite?.Invoke(this, new ConsoleWriteEventArgs(message));
 }
Пример #5
0
            public async Task CompileAndRunAsync(string code)
            {
                await Task.Run(() => ConsoleWrite?.Invoke(this, new ConsoleWriteEventArgs("Hello Test")));

                done.WaitOne();
            }
Пример #6
0
 protected void WriteToConsole(string s)
 {
     ConsoleWrite?.Invoke(this, new ConsoleWriteEventArgs(s));
 }