public ActionResult <ApiFightTextSimulationResult> PostHtml([FromBody] string xmlFight)
        {
            try
            {
                ApiFightTextSimulationResult res  = new ApiFightTextSimulationResult();
                FightSimulation       sim         = new FightSimulation(Program.World, xmlFight);
                StringWriter          log         = new StringWriter();
                StringWriter          html        = new StringWriter();
                FightSimulationResult fightResult = sim.Run(log);
                fightResult.WriteToHtml(html);
                log.Flush();
                html.Flush();
                res.Text = html.ToString();
                res.Log  = log.ToString();

                if (res.Log.Length > (1024 * 1024))
                {
                    res.Log = res.Log.Substring(0, (1024 * 1024)) + " ...";
                }

                return(res);
            }
            catch (Exception e)
            {
                return(StatusCode(400, e));
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                WriteHelp();
                return;
            }



            string definition = File.ReadAllText(args[0]);
            string fight      = File.ReadAllText(args[1]);

            World        world     = new World(definition);
            StringWriter log       = new StringWriter();
            StringWriter outStream = new StringWriter();

            FightSimulation       simulation = new FightSimulation(world, fight);
            FightSimulationResult res        = simulation.Run(log);

            log.Flush();
            System.Console.WriteLine(log.ToString());


            string outFile = "out.";

            switch (args[2])
            {
            case "csv":
                outFile += "csv";
                res.WriteToCsv(outStream);
                break;

            case "html":
                outFile += "html";
                res.WriteToHtml(outStream);
                break;

            case "htmlchart":
                outFile += "htmlchart";
                res.WriteToHtmlChart(outStream);
                break;

            default:
                outFile = String.Empty;
                break;
            }
            if (!String.IsNullOrEmpty(outFile))
            {
                outStream.Flush();
                File.WriteAllText(outFile, outStream.ToString());
            }
        }
        public ActionResult <FileResult> PostCsvFile([FromBody] string xmlFight)
        {
            try
            {
                ApiFightTextSimulationResult res  = new ApiFightTextSimulationResult();
                FightSimulation       sim         = new FightSimulation(Program.World, xmlFight);
                StringWriter          log         = new StringWriter();
                StringWriter          csv         = new StringWriter();
                FightSimulationResult fightResult = sim.Run(log);
                fightResult.WriteToCsv(csv);
                log.Flush();
                csv.Flush();
                res.Text = csv.ToString();
                res.Log  = log.ToString();


                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                    {
                        ZipArchiveEntry outputFile = archive.CreateEntry("homm3-damage-simulation.csv");

                        using (Stream entryStream = outputFile.Open())
                            using (StreamWriter streamWriter = new StreamWriter(entryStream))
                            {
                                entryStream.Write(System.Text.Encoding.Unicode.GetBytes(res.Text));
                                //streamWriter.Write(res.Text);
                            }

                        outputFile = archive.CreateEntry("homm3-damage-simulation-log.txt");
                        using (Stream entryStream = outputFile.Open())
                            using (StreamWriter streamWriter = new StreamWriter(entryStream))
                            {
                                entryStream.Write(System.Text.Encoding.Unicode.GetBytes(res.Log));
                                //streamWriter.Write(res.Log);
                            }
                    }


                    memoryStream.Seek(0, SeekOrigin.Begin);
                    FileResult fr = new FileContentResult(memoryStream.ToArray(), "application/zip");
                    fr.FileDownloadName = "homm3-damage-simulation.zip";
                    return(fr);
                }
            }
            catch (Exception e)
            {
                return(StatusCode(400, e));
            }
        }
        public ActionResult <ApiFightTextSimulationResult> PostCsv([FromBody] string xmlFight)
        {
            ApiFightTextSimulationResult res  = new ApiFightTextSimulationResult();
            FightSimulation       sim         = new FightSimulation(Program.World, xmlFight);
            StringWriter          log         = new StringWriter();
            StringWriter          csv         = new StringWriter();
            FightSimulationResult fightResult = sim.Run(log);

            fightResult.WriteToCsv(csv);
            log.Flush();
            csv.Flush();
            res.Text = csv.ToString();
            res.Log  = log.ToString();

            return(res);
        }