SeaSJRescueMission.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using KYFramework;
  2. using NPOI.SS.Formula.PTG;
  3. using SimulationServer.Utils;
  4. namespace SimulationServer;
  5. public class SeaSJRescueMission : Entity
  6. {
  7. public string MissionId; // 任务ID
  8. public bool Success; // 任务是否成功
  9. public List<AircraftSJ> aircrafts = new List<AircraftSJ>();
  10. public bool IsRunning;
  11. public double SimulationTime;
  12. public Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> singleReport = new();
  13. // 灭火的 整体能力
  14. // <sheet,<指标名,值列表>>
  15. public Dictionary<string, Dictionary<string, List<string>>> totalReport = new();
  16. public Dictionary<string, Dictionary<string, List<string>>> aircraftSJDatas = new();
  17. public bool IsOver = false;
  18. private int currentExecuteCount = 1;
  19. public int ExecutionContext = 0;
  20. public string date;
  21. public void Start()
  22. {
  23. IsRunning = true;
  24. aircrafts.ForEach(a => a.Start());
  25. Log.Info($"{MissionId} 任务开始!");
  26. }
  27. public void Reset()
  28. {
  29. SimulationTime = 0;
  30. aircrafts?.ForEach(a => a.Reset());
  31. }
  32. public void EndMission()
  33. {
  34. IsRunning = false;
  35. Log.Info($"{MissionId} 任务结束!");
  36. //SaveSJ();
  37. //SaveTotalMH();
  38. //ReportSJ();
  39. if (currentExecuteCount > ExecutionContext)
  40. {
  41. return;
  42. }
  43. if (currentExecuteCount < ExecutionContext)
  44. {
  45. SaveSJ();
  46. //SaveTotalMH();
  47. SaveAircraftSJDatas();
  48. }
  49. if (currentExecuteCount == ExecutionContext)
  50. {
  51. SaveSJ();
  52. SaveAircraftSJDatas();
  53. //SaveTotalMH();
  54. ReportSJ();
  55. //this.StartAsyncMH();
  56. IsOver = true;
  57. return;
  58. }
  59. this.Reset();
  60. this.Start();
  61. currentExecuteCount++;
  62. // var readyTime = AircraftXHs.First().TaskReadyTime;
  63. // Task.Delay(TimeSpan.FromSeconds(readyTime)).ContinueWith(t => this.StartAsyncXH());
  64. }
  65. public void SaveAircraftSJDatas()
  66. {
  67. foreach (AircraftSJ aircraftEntity in aircrafts)
  68. {
  69. string key = aircraftEntity.AircraftId;
  70. if (!aircraftSJDatas.ContainsKey(key))
  71. {
  72. aircraftSJDatas[key] = new Dictionary<string, List<string>>();
  73. }
  74. if (!aircraftSJDatas[key].ContainsKey("识别成功率"))
  75. aircraftSJDatas[key]["识别成功率"] = new List<string>();
  76. aircraftSJDatas[key]["识别成功率"].Add(aircraftEntity.isseePerson ? aircraftEntity.targetCount.ToString() : "0");
  77. if (!aircraftSJDatas[key].ContainsKey("人员数量"))
  78. aircraftSJDatas[key]["人员数量"] = new List<string>();
  79. aircraftSJDatas[key]["人员数量"].Add(aircraftEntity.targetCount.ToString());
  80. if (!aircraftSJDatas[key].ContainsKey("任务准备时间"))
  81. aircraftSJDatas[key]["任务准备时间"] = new List<string>();
  82. aircraftSJDatas[key]["任务准备时间"].Add(aircraftEntity.TaskReadyTime.ToString());
  83. if (!aircraftSJDatas[key].ContainsKey("平均搜索时间"))
  84. aircraftSJDatas[key]["平均搜索时间"] = new List<string>();
  85. aircraftSJDatas[key]["平均搜索时间"].Add(aircraftEntity.SearchTime.ToString("f4"));
  86. if (!aircraftSJDatas[key].ContainsKey("平均救助时间"))
  87. aircraftSJDatas[key]["平均救助时间"] = new List<string>();
  88. aircraftSJDatas[key]["平均救助时间"].Add(aircraftEntity.resulttime.ToString());
  89. if (!aircraftSJDatas[key].ContainsKey("总飞行时间"))
  90. aircraftSJDatas[key]["总飞行时间"] = new List<string>();
  91. aircraftSJDatas[key]["总飞行时间"].Add(aircraftEntity.TotalTime.ToString("f4"));
  92. if (!aircraftSJDatas[key].ContainsKey("人员存活率"))
  93. aircraftSJDatas[key]["人员存活率"] = new List<string>();
  94. aircraftSJDatas[key]["人员存活率"].Add(aircraftEntity.Success ? aircraftEntity.targetCount.ToString() : "0");
  95. }
  96. }
  97. public void SaveSJ()
  98. {
  99. foreach (AircraftSJ aircraftEntity in aircrafts)
  100. {
  101. var staticCapacity = aircraftEntity.GetComponent<SJStaticCapacityComponent>();
  102. if (staticCapacity == null) continue;
  103. staticCapacity.FillData(aircraftEntity.Db);
  104. string key = aircraftEntity.AircraftId;
  105. if (!singleReport.ContainsKey(key))
  106. {
  107. singleReport[key] = new Dictionary<string, Dictionary<string, List<string>>>();
  108. }
  109. Dictionary<string, Dictionary<string, string>> staticReport = staticCapacity.GetReport();
  110. foreach (var kv in staticReport)
  111. {
  112. if (!singleReport[key].ContainsKey(kv.Key)) singleReport[key][kv.Key] = new Dictionary<string, List<string>>();
  113. foreach (var kv2 in kv.Value)
  114. {
  115. if (!singleReport[key][kv.Key].ContainsKey(kv2.Key)) singleReport[key][kv.Key][kv2.Key] = new List<string>();
  116. singleReport[key][kv.Key][kv2.Key].Add(kv2.Value);
  117. }
  118. }
  119. }
  120. if (currentExecuteCount == ExecutionContext)
  121. {
  122. foreach (var kv in singleReport)
  123. {
  124. foreach (var kv2 in kv.Value)
  125. {
  126. foreach (var kv3 in kv2.Value)
  127. {
  128. var sum = 0f;
  129. foreach (var kv4 in kv3.Value)
  130. {
  131. bool isfloat = float.TryParse(kv4, out float f);
  132. if (isfloat)
  133. sum += float.Parse(kv4);
  134. else
  135. sum = -1f;
  136. }
  137. if (sum != -1f)
  138. kv3.Value.Add((sum / kv3.Value.Count).ToString());
  139. else
  140. kv3.Value.Add("");
  141. }
  142. }
  143. }
  144. }
  145. }
  146. public void SaveTotalMH()
  147. {
  148. var totalPerformance = GetComponent<SJTotalTaskPerformanceComponent>();
  149. totalPerformance.FillData();
  150. var totalPerformanceReport = totalPerformance.GetReport();
  151. foreach (var kv in totalPerformanceReport)
  152. {
  153. if (!totalReport.ContainsKey(kv.Key)) totalReport[kv.Key] = new Dictionary<string, List<string>>();
  154. foreach (var kv2 in kv.Value)
  155. {
  156. if (!totalReport[kv.Key].ContainsKey(kv2.Key)) totalReport[kv.Key][kv2.Key] = new List<string>();
  157. totalReport[kv.Key][kv2.Key].Add(kv2.Value);
  158. }
  159. }
  160. }
  161. public void ReportSJ()
  162. {
  163. //string data = DateTime.Now.ToString("yyyy-MM-dd");
  164. string path = $"Reports/{date}/{MissionId}";
  165. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  166. foreach (var kv in singleReport)
  167. {
  168. string filePath = $"{path}/{kv.Key}搜救任务单机指标报告.xls";
  169. DataTableExtensions.SaveToExcel(filePath, kv.Value, true);
  170. }
  171. //foreach (KeyValuePair<string, Dictionary<string, List<string>>> keyValuePair in totalReport)
  172. //{
  173. // foreach (KeyValuePair<string, List<string>> kv in keyValuePair.Value)
  174. // {
  175. // List<string> values = kv.Value;
  176. // double sum = 0;
  177. // foreach (string value in values)
  178. // {
  179. // if (double.TryParse(value, out double num))
  180. // {
  181. // sum += num;
  182. // }
  183. // }
  184. // double average = sum / values.Count;
  185. // values.Add(average.ToString("#0.00"));
  186. // }
  187. //}
  188. //string totalPath = $"{path}/{"搜救任务总体指标报告"}.xls";
  189. //DataTableExtensions.SaveToExcel(totalPath, totalReport, true);
  190. }
  191. }
  192. [ObjectSystem]
  193. public class SeaSJRescueMissionUpdateSystem : UpdateSystem<SeaSJRescueMission>
  194. {
  195. public override void Update(SeaSJRescueMission self)
  196. {
  197. if (!self.IsRunning) return;
  198. self.aircrafts?.ForEach(a => a.Update(self.SimulationTime));
  199. if (self.aircrafts.All(a => a.IsOver))
  200. {
  201. self.EndMission();
  202. }
  203. }
  204. }