예제 #1
0
        public string ToHtml(string player1Name, string player2Name, string battleOutput)
        {
            Logger.Trace("Viewing output as Animation");

            string[] splitters = { "=~=" };
            string[] frames    = battleOutput.Split(splitters, StringSplitOptions.None);
            frames = RemoveEmptyFrames(frames);

            StringBuilder scriptFramesVar = new StringBuilder("var frames{0} = [");

            for (int i = 0; i < frames.Length; ++i)
            {
                string htmlFrame = JarvisEncoding.ToHtmlEncoding(frames[i].TrimEnd(' '));
                scriptFramesVar.AppendFormat("\"{0}\"", htmlFrame);

                if (i < frames.Length - 1)
                {
                    scriptFramesVar.Append(",");
                }
            }

            scriptFramesVar.Append("]; ");

            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(script1, 0);
            builder.AppendFormat(scriptFramesVar.ToString(), 0);
            builder.AppendFormat(script2, 0);
            builder.AppendFormat("<h3>{0} vs. {1}</h3> ", player1Name, player2Name);
            builder.AppendFormat(script3, 0);

            return(builder.ToString());
        }
예제 #2
0
        public string ToHtml(TestCase data)
        {
            Logger.Trace("Viewing output as Animation");

            string[] splitters = { "=~=" };
            string[] frames    = data.StdOutText.Split(splitters, StringSplitOptions.None);
            frames = RemoveEmptyFrames(frames);

            StringBuilder scriptFramesVar = new StringBuilder("var frames{0} = [");

            for (int i = 0; i < frames.Length; ++i)
            {
                foreach (Keyword keyword in keywords)
                {
                    if (frames[i].Contains(keyword.Word))
                    {
                        keyword.Found = true;
                    }
                }

                string htmlFrame = JarvisEncoding.ToHtmlEncoding(frames[i].TrimEnd(' '));
                scriptFramesVar.AppendFormat("\"{0}\"", htmlFrame);

                if (i < frames.Length - 1)
                {
                    scriptFramesVar.Append(",");
                }
            }

            scriptFramesVar.Append("]; ");

            bool foundAllKeywords = true;

            foreach (Keyword keyword in keywords)
            {
                if (!keyword.Found)
                {
                    foundAllKeywords = false;
                    break; // Once we've missed one, then we don't need to keep looking
                }
            }

            data.Passed = foundAllKeywords;

            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(script1, data.Id);
            builder.AppendFormat(scriptFramesVar.ToString(), data.Id);
            builder.AppendFormat(script2, data.Id);
            builder.AppendFormat("<p>Searched for: \"{0}\"</p> ", keywordList);
            builder.AppendFormat(script3, data.Id);

            return(Utilities.BuildDiffBlock("Animation:", builder.ToString(), "", ""));
        }
예제 #3
0
        private string Get5xx(NancyContext context)
        {
            StringBuilder responseText = new StringBuilder();

            responseText.AppendFormat("<img style='display: block; margin: auto;' src='data:image/png;base64,{0}' /><br />", JarvisEncoding.ConvertToBase64(rootPath + "/Content/error.png", false));
            responseText.Append("<p>I'm sorry sir, it appears I have malfunctioned... an email has been sent to my creator.<br /><br />");
            responseText.Append("Error message:<br />" + JarvisEncoding.ToHtmlEncoding(context.Items["ERROR_TRACE"].ToString()));
            responseText.Append("</p>");

            Utilities.SendEmail("*****@*****.**", "Jarvis Internal Server Error!!", context.Items["ERROR_TRACE"].ToString(), "");

            return(responseText.ToString());
        }
예제 #4
0
        private string StyleCheck(Assignment homework)
        {
            StringBuilder result   = new StringBuilder();
            StyleExecutor executor = new StyleExecutor();

            foreach (string file in homework.FileNames)
            {
                string errors = executor.Run(homework.Path + file);

                result.AppendFormat("File: {0}\n", file);
                result.AppendFormat("{0}\n", errors);
            }

            return(JarvisEncoding.ToHtmlEncoding(result.ToString()));
        }
예제 #5
0
        public string ToHtml(TestCase test)
        {
            StringBuilder result = new StringBuilder();

            // Check for std input
            if (!string.IsNullOrEmpty(test.StdInText))
            {
                string htmlStdInput   = JarvisEncoding.ToHtmlEncodingWithNewLines(test.StdInText);
                string caseHeaderText = "Test Input:";

                result.Append(Utilities.BuildInputBlock(caseHeaderText, htmlStdInput));
            }

            // check for std output file
            if (!string.IsNullOrEmpty(test.StdOutputFile))
            {
                if (test.StdOutText.Length < 100000)
                {
                    string expectedStdOutput = Utilities.ReadFileContents(test.TestsPath + test.StdOutputFile);

                    string htmlActualStdOutput   = JarvisEncoding.ToHtmlEncodingWithNewLines(test.StdOutText);
                    string htmlExpectedStdOutput = JarvisEncoding.ToHtmlEncodingWithNewLines(expectedStdOutput);
                    string htmlDiff = JarvisEncoding.GetDiff(htmlActualStdOutput, htmlExpectedStdOutput);

                    string caseHeaderText = "Test Output:";

                    if (string.IsNullOrWhiteSpace(test.StdOutText))
                    {
                        caseHeaderText += "<br/ ><span style=\"color:#ff0000\">Warning: actual output was empty!</span>";
                    }

                    result.Append(Utilities.BuildDiffBlock(caseHeaderText, htmlActualStdOutput, htmlExpectedStdOutput, htmlDiff));

                    test.Passed = htmlDiff.Contains("No difference");
                }
                else
                {
                    test.Passed = false;
                    result.Append("<p>Too much output!</p>");
                }
            }

            return(result.ToString());
        }
예제 #6
0
        public static string CompileCpp(string outputName, string path, List <string> includeDirs, List <string> sourceFiles)
        {
            string result = "";

            using (Process p = new Process())
            {
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError  = true;
                p.StartInfo.FileName  = "g++";
                p.StartInfo.Arguments = "-DJARVIS -g -std=c++11 -Werror -o" + outputName;

                foreach (string include in includeDirs)
                {
                    p.StartInfo.Arguments += " -I" + include;
                }

                // Expects the source to be full name
                foreach (string source in sourceFiles)
                {
                    p.StartInfo.Arguments += " " + source;
                }

                p.Start();

                Logger.Trace("Compilation string: {0} {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

                Jarvis.StudentProcesses.Add(p.Id);

                result = p.StandardError.ReadToEnd();
                result = result.Replace(path, "");
                result = JarvisEncoding.ToHtmlEncoding(result);

                p.WaitForExit();

                p.Close();
            }
            Logger.Trace("Compile result: {0}", result);

            return((!string.IsNullOrEmpty(result)) ? result : "Success!!");
        }
예제 #7
0
        public string ToHtml(TestCase test)
        {
            StringBuilder result = new StringBuilder();

            // check for file output files
            if (test.FileOutputFiles.Count > 0)
            {
                foreach (OutputFile fileout in test.FileOutputFiles)
                {
                    string   expectedOutput = Utilities.ReadFileContents(test.TestsPath + fileout.CourseFile);
                    FileInfo info           = new FileInfo(test.HomeworkPath + fileout.StudentFile);
                    if (File.Exists(test.HomeworkPath + fileout.StudentFile) && info.Length < 1000000)
                    {
                        string actualOutput = Utilities.ReadFileContents(test.HomeworkPath + fileout.StudentFile);

                        string htmlExpectedOutput = JarvisEncoding.ToHtmlEncodingWithNewLines(expectedOutput);
                        string htmlActualOutput   = JarvisEncoding.ToHtmlEncodingWithNewLines(actualOutput);

                        string htmlDiff = JarvisEncoding.GetDiff(htmlActualOutput, htmlExpectedOutput);

                        result.Append(Utilities.BuildDiffBlock("From " + fileout.StudentFile + ":", htmlActualOutput, htmlExpectedOutput, htmlDiff));

                        test.Passed = htmlDiff.Contains("No difference");
                    }
                    else if (!File.Exists(test.HomeworkPath + fileout.StudentFile))
                    {
                        test.Passed = false;
                        result.Append("<p>Cannot find output file: " + fileout.StudentFile + "</p>");
                    }
                    else if (info.Length >= 1000000)
                    {
                        test.Passed = false;
                        result.Append("<p>The file output was too large [" + info.Length.ToString() + "] bytes!!!");
                    }
                }
            }

            return(result.ToString());
        }
예제 #8
0
        public string ToHtml(TestCase test)
        {
            testCase = test;
            StringBuilder result = new StringBuilder();

            foreach (OutputFile file in test.FileOutputFiles)
            {
                if (file.FileType == OutputFile.Type.PPM)
                {
                    string htmlDiff     = string.Empty;
                    string htmlActual   = string.Empty;
                    string htmlExpected = string.Empty;

                    string ppmFile = test.HomeworkPath + file.StudentFile;

                    if (!File.Exists(ppmFile))
                    {
                        htmlActual  = "No image found!";
                        test.Passed = false;
                    }
                    else if (new FileInfo(ppmFile).Length > 6000000)
                    {
                        htmlActual  = string.Format("Generated PPM file is too large! {0} bytes!", new FileInfo(ppmFile).Length);
                        test.Passed = false;
                    }
                    else if (!CheckPpmHeader(ppmFile)) // Invalid header
                    {
                        htmlActual  = "Invalid PPM header!<br />Please check for correct PPM before uploading to Jarvis.";
                        test.Passed = false;
                    }
                    else
                    {
                        string pngExpected       = ConvertPpmToPng(test.TestsPath + file.CourseFile);
                        Bitmap expected          = new Bitmap(pngExpected);
                        string expectedBase64Png = JarvisEncoding.ConvertToBase64(pngExpected);
                        htmlExpected = string.Format("<img src='data:image/png;base64,{0}' />", expectedBase64Png);

                        try
                        {
                            bool   match        = true;
                            bool   sizeMismatch = false;
                            string pngActual    = ConvertPpmToPng(test.HomeworkPath + file.StudentFile);

                            if (File.Exists(pngActual))
                            {
                                Bitmap actual = new Bitmap(pngActual);

                                if ((actual.Width == expected.Width) && (actual.Height == expected.Height))
                                {
                                    for (int i = 0; i < actual.Width; ++i)
                                    {
                                        for (int j = 0; j < actual.Height; ++j)
                                        {
                                            Color actualColor   = expected.GetPixel(i, j);
                                            Color expectedColor = actual.GetPixel(i, j);

                                            if (Math.Abs(actualColor.R - expectedColor.R) > 10)
                                            {
                                                match = false;
                                            }

                                            if (Math.Abs(actualColor.G - expectedColor.G) > 10)
                                            {
                                                match = false;
                                            }

                                            if (Math.Abs(actualColor.B - expectedColor.B) > 10)
                                            {
                                                match = false;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    match        = false;
                                    sizeMismatch = true;
                                }


                                string actualBase64Png = JarvisEncoding.ConvertToBase64(pngActual);
                                htmlActual = string.Format("<img src='data:image/png;base64,{0}' />", actualBase64Png);


                                if (match)
                                {
                                    htmlDiff    = "No difference, or close enough... ;-]";
                                    test.Passed = true;
                                }
                                else
                                {
                                    htmlDiff = "Differences detected!";

                                    if (sizeMismatch)
                                    {
                                        htmlDiff += "<br />Actual Size: " + actual.Width + "x" + actual.Height + ", Expected Size: " + expected.Width + "x" + expected.Height;
                                    }
                                    //htmlDiff += "<br /><a href='data:text/html;base64," + Convert.ToBase64String( Encoding.ASCII.GetBytes()) + "'>Link here</a>";
                                    test.Passed = false;
                                }

                                actual.Dispose();
                            }
                            else
                            {
                                htmlDiff    = "Differences detected!";
                                htmlActual  = "No image found!";
                                test.Passed = false;
                            }
                        }
                        catch
                        {
                            htmlDiff    = "Invalid image!";
                            htmlActual  = "Invalid image!";
                            test.Passed = false;
                        }

                        expected.Dispose();
                    }

                    string diffBlock = Utilities.BuildDiffBlock("From PPM image:", htmlActual, htmlExpected, htmlDiff);
                    result.Append(diffBlock);

                    if (File.Exists(ppmFile))
                    {
                        File.Delete(ppmFile);
                    }
                }
            }

            return(result.ToString());
        }
예제 #9
0
        private string Compile(Assignment homework, List <string> providedFiles)
        {
            // Find all C++ source files
            List <string> sourceFiles = new List <string>();

            foreach (string file in homework.FileNames)
            {
                if (file.EndsWith(".cpp") || file.EndsWith(".cxx") || file.EndsWith(".cc"))
                {
                    sourceFiles.Add(file);
                }
            }

            // Add in provided files
            string testsPath = string.Format("{0}/courses/{1}/tests/hw{2}/", Jarvis.Config.AppSettings.Settings["workingDir"].Value, homework.Course, homework.HomeworkId);

            foreach (string file in providedFiles)
            {
                // Copy all provided files (headers and source)
                Logger.Trace("Copying {0} to {1}", testsPath + file, homework.Path);
                File.Copy(testsPath + file, homework.Path + file, true);

                // Only build source files
                if (file.EndsWith(".cpp") || file.EndsWith(".cxx") || file.EndsWith(".cc"))
                {
                    sourceFiles.Add(file);
                }
            }

            string result = "";

            using (Process p = new Process())
            {
                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError  = true;
                p.StartInfo.FileName  = "g++";
                p.StartInfo.Arguments = "-DJARVIS -std=c++11 -Werror -o" + homework.Path + homework.StudentId + " -I" + homework.Path;
                foreach (string source in sourceFiles)
                {
                    //string temp = source.Replace("_", "\_");
                    p.StartInfo.Arguments += string.Format(" \"{0}{1}\"", homework.Path, source);
                }

                p.Start();

                Logger.Trace("Compilation string: {0} {1}", p.StartInfo.FileName, p.StartInfo.Arguments);

                Jarvis.StudentProcesses.Add(p.Id);

                result = p.StandardError.ReadToEnd();
                result = result.Replace(homework.Path, "");
                result = JarvisEncoding.ToHtmlEncoding(result);

                p.WaitForExit();

                p.Close();
            }
            Logger.Trace("Compile result: {0}", result);

            return((!string.IsNullOrEmpty(result)) ? result : "Success!!");
        }