Bläddra i källkod

添加多次执行导出Excel功能

liyang 7 månader sedan
förälder
incheckning
095e0749b4

+ 24 - 9
SimulationServer/Component/SJAllTotalTaskPerformance.cs

@@ -1,4 +1,6 @@
-namespace SimulationServer;
+using static Fire;
+
+namespace SimulationServer;
 
 public class SJAllTotalTaskPerformance
 {
@@ -15,6 +17,7 @@ public class SJAllTotalTaskPerformance
 public class SJAllTotalTaskPerformanceComponent
 {
     public SJAllTotalTaskPerformance SJAllTotalTaskPerformance = new SJAllTotalTaskPerformance();
+    public int index;
 
     public void FillData(List<SeaSJRescueMission> missions)
     {
@@ -29,17 +32,28 @@ public class SJAllTotalTaskPerformanceComponent
         double aircreftCount = 0;
         foreach (SeaSJRescueMission mhRescueMission in missions)
         {
-            foreach (var aircraft in mhRescueMission.aircrafts)
+            //foreach (var aircraft in mhRescueMission.aircrafts)
+            //{
+            //    if (aircraft.isseePerson)
+            //        识别成功率 += 1f;
+            //    if (任务准备时间 < aircraft.TaskReadyTime)
+            //        任务准备时间 = aircraft.TaskReadyTime;
+            //    平均搜索时间 += aircraft.SearchTime;
+            //    总飞行时间 += aircraft.TotalTime;
+            //    人员存活率 += aircraft.Success ? 1 : 0;
+            //    aircreftCount++;
+            //}
+            foreach (var item in mhRescueMission.aircraftSJDatas)
             {
-                if (aircraft.isseePerson)
-                    识别成功率 += 1f;
-                if (任务准备时间 < aircraft.TaskReadyTime)
-                    任务准备时间 = aircraft.TaskReadyTime;
-                平均搜索时间 += aircraft.SearchTime;
-                总飞行时间 += aircraft.TotalTime;
-                人员存活率 += aircraft.Success ? 1 : 0;
+                识别成功率 += float.Parse(item.Value["识别成功率"][index]);
+                if (任务准备时间 < float.Parse(item.Value["任务准备时间"][index]))
+                    任务准备时间 = float.Parse(item.Value["任务准备时间"][index]);
+                平均搜索时间 += float.Parse(item.Value["平均搜索时间"][index]);
+                总飞行时间 += float.Parse(item.Value["总飞行时间"][index]);
+                人员存活率 += float.Parse(item.Value["人员存活率"][index]);
                 aircreftCount++;
             }
+
         }
         if (人员存活率 != 0)
             任务是否成功 = 1;
@@ -51,6 +65,7 @@ public class SJAllTotalTaskPerformanceComponent
         SJAllTotalTaskPerformance.海情覆盖率 = "0.6".ToString();//"60%"
         SJAllTotalTaskPerformance.任务是否成功 = 任务是否成功.ToString();
         SJAllTotalTaskPerformance.人员存活率 = (人员存活率 / aircreftCount).ToString();
+        index++;
     }
 
     public Dictionary<string, Dictionary<string, string>> GetReport()

+ 27 - 16
SimulationServer/Component/TaskComponent.cs

@@ -16,6 +16,9 @@ public class TaskComponent : Component
     public SJAllTotalTaskPerformanceComponent SJAllTotalTaskPerformance = new SJAllTotalTaskPerformanceComponent();
     public Dictionary<string, Dictionary<string, List<string>>> alltotalReport = new();
 
+    private int currentExecuteCount = 1;
+    public int ExecutionContext = 0;
+
     public void Start()
     {
         MHRescueMissions?.ForEach(r => r.Reset());
@@ -43,26 +46,31 @@ public class TaskComponent : Component
             }
         }
 
-        // 求一个平均值放到最后一位
-        foreach (var kv in alltotalReport)
+        if (currentExecuteCount == ExecutionContext)
         {
-            foreach (var kv2 in kv.Value)
+            // 求一个平均值放到最后一位
+            foreach (var kv in alltotalReport)
             {
-                var sum = 0f;
-                foreach (var value in kv2.Value)
+                foreach (var kv2 in kv.Value)
                 {
-                    bool isfloat = float.TryParse(value, out float f);
-                    if (isfloat)
-                        sum += float.Parse(value);
+                    var sum = 0f;
+                    foreach (var value in kv2.Value)
+                    {
+                        bool isfloat = float.TryParse(value, out float f);
+                        if (isfloat)
+                            sum += float.Parse(value);
+                        else
+                            sum = -1f;
+                    }
+                    if (sum != -1f)
+                        kv2.Value.Add((sum / kv2.Value.Count).ToString());
                     else
-                        sum = -1f;
+                        kv2.Value.Add("");
                 }
-                if (sum != -1f)
-                    kv2.Value.Add((sum / kv2.Value.Count).ToString());
-                else
-                    kv2.Value.Add("");
             }
         }
+
+        currentExecuteCount++;
     }
 
     public void ReportAllSJ()
@@ -97,9 +105,12 @@ public class TaskComponentUpdateSystem : UpdateSystem<TaskComponent>
 
         if (self.SeaSJRescueMissions.All(m => m.IsOver))
         {
-            self.SJAllTotalTaskPerformance.FillData(self.SeaSJRescueMissions);
-            var report = self.SJAllTotalTaskPerformance.GetReport();
-            self.SaveSJ(report);
+            for (int i = 0; i < self.ExecutionContext; i++)
+            {
+                self.SJAllTotalTaskPerformance.FillData(self.SeaSJRescueMissions);
+                var report = self.SJAllTotalTaskPerformance.GetReport();
+                self.SaveSJ(report);
+            }
             self.ReportAllSJ();
             self.SeaSJRescueMissions.Clear();
         }

+ 2 - 0
SimulationServer/Entity/AircraftSJ.cs

@@ -31,6 +31,8 @@ public class AircraftSJ : AircraftEntity
         IsOver = false;
         isseePerson = false;
         Success = true;
+        SearchTime = 0;
+        TotalTime = 0;
     }
 
     public override void Start()

+ 69 - 27
SimulationServer/Entity/SeaSJRescueMission.cs

@@ -9,14 +9,16 @@ public class SeaSJRescueMission : Entity
     public string MissionId; // 任务ID
     public bool Success; // 任务是否成功
     public List<AircraftSJ> aircrafts = new List<AircraftSJ>();
-    
+
     public bool IsRunning;
     public double SimulationTime;
-    
-    public Dictionary<string,Dictionary<string,Dictionary<string,List<string>>>> singleReport =  new ();
+
+    public Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> singleReport = new();
     // 灭火的 整体能力
     // <sheet,<指标名,值列表>>
-    public Dictionary<string, Dictionary<string, List<string>>> totalReport = new ();
+    public Dictionary<string, Dictionary<string, List<string>>> totalReport = new();
+
+    public Dictionary<string, Dictionary<string, List<string>>> aircraftSJDatas = new();
 
     public bool IsOver = false;
 
@@ -53,11 +55,13 @@ public class SeaSJRescueMission : Entity
         {
             SaveSJ();
             //SaveTotalMH();
+            SaveAircraftSJDatas();
         }
 
         if (currentExecuteCount == ExecutionContext)
         {
             SaveSJ();
+            SaveAircraftSJDatas();
             SaveTotalMH();
             ReportSJ();
             //this.StartAsyncMH();
@@ -74,28 +78,55 @@ public class SeaSJRescueMission : Entity
         // var readyTime = AircraftXHs.First().TaskReadyTime;
         // Task.Delay(TimeSpan.FromSeconds(readyTime)).ContinueWith(t => this.StartAsyncXH());
     }
-    
+
+    public void SaveAircraftSJDatas()
+    {
+        foreach (AircraftSJ aircraftEntity in aircrafts)
+        {
+            string key = aircraftEntity.AircraftId;
+            if (!aircraftSJDatas.ContainsKey(key))
+            {
+                aircraftSJDatas[key] = new Dictionary<string, List<string>>();
+            }
+            if (!aircraftSJDatas[key].ContainsKey("识别成功率"))
+                aircraftSJDatas[key]["识别成功率"] = new List<string>();
+            aircraftSJDatas[key]["识别成功率"].Add(aircraftEntity.isseePerson ? "1" : "0");
+            if (!aircraftSJDatas[key].ContainsKey("任务准备时间"))
+                aircraftSJDatas[key]["任务准备时间"] = new List<string>();
+            aircraftSJDatas[key]["任务准备时间"].Add(aircraftEntity.TaskReadyTime.ToString());
+            if (!aircraftSJDatas[key].ContainsKey("平均搜索时间"))
+                aircraftSJDatas[key]["平均搜索时间"] = new List<string>();
+            aircraftSJDatas[key]["平均搜索时间"].Add(aircraftEntity.SearchTime.ToString());
+            if (!aircraftSJDatas[key].ContainsKey("总飞行时间"))
+                aircraftSJDatas[key]["总飞行时间"] = new List<string>();
+            aircraftSJDatas[key]["总飞行时间"].Add(aircraftEntity.TotalTime.ToString());
+            if (!aircraftSJDatas[key].ContainsKey("人员存活率"))
+                aircraftSJDatas[key]["人员存活率"] = new List<string>();
+            aircraftSJDatas[key]["人员存活率"].Add(aircraftEntity.Success ? "1" : "0");
+        }
+    }
+
     public void SaveSJ()
     {
         foreach (AircraftSJ aircraftEntity in aircrafts)
         {
             var staticCapacity = aircraftEntity.GetComponent<SJStaticCapacityComponent>();
 
-            if(staticCapacity == null) continue;
-            
+            if (staticCapacity == null) continue;
+
             staticCapacity.FillData(aircraftEntity.Db);
 
             string key = aircraftEntity.AircraftId;
-            
+
             if (!singleReport.ContainsKey(key))
             {
                 singleReport[key] = new Dictionary<string, Dictionary<string, List<string>>>();
             }
-           
+
             Dictionary<string, Dictionary<string, string>> staticReport = staticCapacity.GetReport();
             foreach (var kv in staticReport)
             {
-                if(!singleReport[key].ContainsKey(kv.Key)) singleReport[key][kv.Key] = new Dictionary<string, List<string>>();
+                if (!singleReport[key].ContainsKey(kv.Key)) singleReport[key][kv.Key] = new Dictionary<string, List<string>>();
                 foreach (var kv2 in kv.Value)
                 {
                     if (!singleReport[key][kv.Key].ContainsKey(kv2.Key)) singleReport[key][kv.Key][kv2.Key] = new List<string>();
@@ -103,31 +134,42 @@ public class SeaSJRescueMission : Entity
                 }
             }
         }
-        if (currentExecuteCount == -10)
+        if (currentExecuteCount == ExecutionContext)
         {
             foreach (var kv in singleReport)
             {
                 foreach (var kv2 in kv.Value)
                 {
-                    var sum = 0;
-                    foreach (var item in kv2.Value)
+                    foreach (var kv3 in kv2.Value)
                     {
-
+                        var sum = 0f;
+                        foreach (var kv4 in kv3.Value)
+                        {
+                            bool isfloat = float.TryParse(kv4, out float f);
+                            if (isfloat)
+                                sum += float.Parse(kv4);
+                            else
+                                sum = -1f;
+                        }
+                        if (sum != -1f)
+                            kv3.Value.Add((sum / kv3.Value.Count).ToString());
+                        else
+                            kv3.Value.Add("");
                     }
                 }
             }
         }
     }
-    
+
     public void SaveTotalMH()
     {
         var totalPerformance = GetComponent<SJTotalTaskPerformanceComponent>();
-        totalPerformance.FillData();    
+        totalPerformance.FillData();
         var totalPerformanceReport = totalPerformance.GetReport();
         foreach (var kv in totalPerformanceReport)
         {
-            if(!totalReport.ContainsKey(kv.Key)) totalReport[kv.Key] = new Dictionary<string, List<string>>();
-            
+            if (!totalReport.ContainsKey(kv.Key)) totalReport[kv.Key] = new Dictionary<string, List<string>>();
+
             foreach (var kv2 in kv.Value)
             {
                 if (!totalReport[kv.Key].ContainsKey(kv2.Key)) totalReport[kv.Key][kv2.Key] = new List<string>();
@@ -135,21 +177,21 @@ public class SeaSJRescueMission : Entity
             }
         }
     }
-     
+
     public void ReportSJ()
     {
         string data = DateTime.Now.ToString("yyyy-MM-dd");
         string path = $"Reports/SJ/{data}/{MissionId}";
-        if(!Directory.Exists(path)) Directory.CreateDirectory(path);
+        if (!Directory.Exists(path)) Directory.CreateDirectory(path);
         foreach (var kv in singleReport)
         {
             string filePath = $"{path}/{kv.Key}搜救任务单机指标报告.xls";
-            DataTableExtensions.SaveToExcel(filePath, kv.Value);
+            DataTableExtensions.SaveToExcel(filePath, kv.Value, true);
         }
 
-        foreach (KeyValuePair<string,Dictionary<string,List<string>>> keyValuePair in totalReport)
+        foreach (KeyValuePair<string, Dictionary<string, List<string>>> keyValuePair in totalReport)
         {
-            foreach (KeyValuePair<string,List<string>> kv in keyValuePair.Value)
+            foreach (KeyValuePair<string, List<string>> kv in keyValuePair.Value)
             {
                 List<string> values = kv.Value;
                 double sum = 0;
@@ -165,7 +207,7 @@ public class SeaSJRescueMission : Entity
             }
         }
         string totalPath = $"{path}/{"搜救任务总体指标报告"}.xls";
-        DataTableExtensions.SaveToExcel(totalPath, totalReport,true);
+        DataTableExtensions.SaveToExcel(totalPath, totalReport, true);
     }
 }
 
@@ -175,10 +217,10 @@ public class SeaSJRescueMissionUpdateSystem : UpdateSystem<SeaSJRescueMission>
 {
     public override void Update(SeaSJRescueMission self)
     {
-        if(!self.IsRunning) return;
-        
+        if (!self.IsRunning) return;
+
         self.aircrafts?.ForEach(a => a.Update(self.SimulationTime));
-        
+
         if (self.aircrafts.All(a => a.IsOver))
         {
             self.EndMission();

+ 1 - 1
SimulationServer/EventHandler/ServerStartEventHandler.cs

@@ -86,7 +86,7 @@ public class ServerStartEventHandler : AEvent<ServerStart>
             Game.EventSystem.Publish(new CreateSeaSJTask
             { EditorConfig = editorConfig, SeaSJTask = seaSJTask });
         }
-
+        taskSys.ExecutionContext = editorConfig.runCounts;
         taskSys.Start();
         return UniTask.CompletedTask;
     }

+ 1 - 1
SimulationServer/bin/Debug/net7.0/Missions/editor_config.json

@@ -1,5 +1,5 @@
 {
-    "仿真次数": 1,
+    "仿真次数": 100,
     "想定信息": {
         "想定日期": "2024年6月4日",
         "想定时间": "00时00分00秒"