ServerStartEventHandler.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Cysharp.Threading.Tasks;
  2. using KYFramework;
  3. using Model;
  4. using MongoDB.Bson;
  5. using Newtonsoft.Json;
  6. using SimulationServer.Utils;
  7. namespace SimulationServer;
  8. [Event]
  9. public class ServerStartEventHandler : AEvent<ServerStart>
  10. {
  11. protected override UniTask Run(ServerStart a)
  12. {
  13. Log.Info("服务器启动完成!");
  14. var taskSys = Game.Scene.GetComponent<TaskComponent>();
  15. // 读取任务配置文件
  16. string ec_path = "Missions/editor_config.json";
  17. string tc_path = "Missions/task_config.json";
  18. if (!File.Exists(ec_path))
  19. {
  20. Log.Error("editor_config.json文件不存在!");
  21. return UniTask.CompletedTask;
  22. }
  23. if (!File.Exists(tc_path))
  24. {
  25. Log.Error("task_config.json文件不存在!");
  26. return UniTask.CompletedTask;
  27. }
  28. string ec_str = File.ReadAllText("Missions/editor_config.json");
  29. string tc_str = File.ReadAllText("Missions/task_config.json");
  30. EditorConfig? editorConfig = JsonConvert.DeserializeObject<EditorConfig>(ec_str);
  31. TaskConfig? taskConfig = JsonConvert.DeserializeObject<TaskConfig>(tc_str);
  32. if(editorConfig == null || taskConfig == null)
  33. {
  34. Log.Error("配置文件解析失败!");
  35. return UniTask.CompletedTask;
  36. }
  37. //Weather weather = Util.GetWeather("内蒙古", "根河", DateTime.Now.ToString("2024-05-10"));
  38. Weather weather = new Weather();
  39. foreach (MHTaskConfig mHTaskConfig in taskConfig.mHTaskConfigs)
  40. {
  41. RescueMission mission = ComponentFactory.Create<RescueMission>();
  42. mission.AddComponent<TotalTaskPerformanceComponent>();
  43. mission.AddComponent<TotalEconomicalComponent>();
  44. mission.AddComponent<EquipmentComponent>();
  45. taskSys.RescueMissions.Add(mission);
  46. mission.MissionId = $"灭火任务 {mHTaskConfig.TotalMHId}";
  47. mission.MissionType = "灭火";
  48. Log.Info($"创建任务 : {mHTaskConfig.TotalMHId}");
  49. double slope = Util.GetSlope(editorConfig.firePoints[mHTaskConfig.FirePointId - 1].FirePointLongitude,
  50. editorConfig.firePoints[mHTaskConfig.FirePointId - 1].FirePointLatitude, 30);
  51. FireGround fireGround = ComponentFactory.Create<FireGround>();
  52. mission.FireGround = fireGround;
  53. mission.InitArea = editorConfig.firePoints[mHTaskConfig.FirePointId - 1].InitialArea;
  54. Log.Info("初始火场面积:" + fireGround.countArea.burnarea);
  55. fireGround.FirePoint = new DVector { Latitude = editorConfig.firePoints[mHTaskConfig.FirePointId - 1].FirePointLatitude,
  56. Longitude = editorConfig.firePoints[mHTaskConfig.FirePointId - 1].FirePointLongitude,Altitude = editorConfig.firePoints[mHTaskConfig.FirePointId - 1].FirePointHeight};
  57. foreach (MHTask mhTask in mHTaskConfig.mHTask)
  58. {
  59. Log.Info($"创建灭火任务 : {mhTask.missionInformation.MissionName}");
  60. if(mhTask.missionInformation.AircraftInfos == null) continue;
  61. for (int i = 0; i < mhTask.missionInformation.AircraftInfos.Length; i++)
  62. {
  63. FlightPlanEditor? flightPlanEditor = new FlightPlanEditor();
  64. flightPlanEditor.aircraftparameter = editorConfig.aircraftParameters.Find( a => a.AircraftID == mhTask.missionInformation.AircraftInfos[i]);
  65. flightPlanEditor.firepoint = new FirePoint[1];
  66. flightPlanEditor.firepoint[0] = editorConfig.firePoints[mHTaskConfig.FirePointId - 1];
  67. flightPlanEditor.missionpoint = mhTask.MissionPoints[0];
  68. flightPlanEditor.originbase = editorConfig.bases.Find(b => b.BaseId == flightPlanEditor.aircraftparameter.AirportId);
  69. flightPlanEditor.climbsegment = editorConfig.climbSegment;
  70. flightPlanEditor.descentsegment = editorConfig.descentSegment;
  71. flightPlanEditor.cruisesegment = editorConfig.cruiseSegment;
  72. flightPlanEditor.cityweather = editorConfig.cityWeather;
  73. AircraftMH aircraft = ComponentFactory.Create<AircraftMH, FlightPlanEditor>( flightPlanEditor);
  74. aircraft.Name = mhTask.missionInformation.AircraftInfos[i];
  75. aircraft.NextMissionId = mhTask.NextTaskId;
  76. mission.Aircrafts.Add(aircraft);
  77. aircraft.aircraftparameter = flightPlanEditor.aircraftparameter;
  78. aircraft.Airport = flightPlanEditor.originbase.BaseName;
  79. aircraft.Weather = weather;
  80. aircraft.TaskName = mhTask.missionInformation.MissionName;
  81. aircraft.slope = slope;
  82. mission.End += aircraft.End;
  83. aircraft.AddComponent<StaticCapacityComponent>();
  84. aircraft.AddComponent<TaskPerformanceComponent>();
  85. aircraft.AddComponent<EconomicalComponent>();
  86. Log.Info($"创建机型 : {flightPlanEditor.aircraftparameter.AircraftID}");
  87. }
  88. }
  89. foreach (DMMHTask dmmhTask in mHTaskConfig.dMMHTask)
  90. {
  91. Log.Info($"创建地面灭火任务 : {dmmhTask.missionInformation.MissionName}");
  92. if(dmmhTask.missionInformation.AircraftInfos == null) continue;
  93. for (int i = 0; i < dmmhTask.missionInformation.AircraftInfos.Length; i++)
  94. {
  95. FlightPlanEditor? flightPlanEditor = new FlightPlanEditor();
  96. flightPlanEditor.aircraftparameter = editorConfig.aircraftParameters.Find( a => a.AircraftID == dmmhTask.missionInformation.AircraftInfos[i]);
  97. flightPlanEditor.firepoint = new FirePoint[1];
  98. flightPlanEditor.firepoint[0] = editorConfig.firePoints[mHTaskConfig.FirePointId - 1];
  99. flightPlanEditor.missionpoint = dmmhTask.MissionPoints[0];
  100. flightPlanEditor.originbase = editorConfig.bases.Find(b => b.BaseId == flightPlanEditor.aircraftparameter.AirportId);
  101. flightPlanEditor.climbsegment = editorConfig.climbSegment;
  102. flightPlanEditor.descentsegment = editorConfig.descentSegment;
  103. flightPlanEditor.cruisesegment = editorConfig.cruiseSegment;
  104. flightPlanEditor.cityweather = editorConfig.cityWeather;
  105. AircraftMHWithPersion aircraftWithPerson = ComponentFactory.Create<AircraftMHWithPersion, FlightPlanEditor>( flightPlanEditor);
  106. aircraftWithPerson.Weather = weather;
  107. mission.End += aircraftWithPerson.End;
  108. aircraftWithPerson.GroundPersonnel = (int)dmmhTask.GroundPersonnel;
  109. aircraftWithPerson.LandingPersonnel = (int)dmmhTask.LandingPersonnel;
  110. aircraftWithPerson.NextMissionId = dmmhTask.NextTaskId;
  111. aircraftWithPerson.TaskName = dmmhTask.missionInformation.MissionName;
  112. mission.AircraftWithPersions.Add(aircraftWithPerson);
  113. Log.Info($"创建机型 : {flightPlanEditor.aircraftparameter.AircraftID}");
  114. }
  115. }
  116. foreach (SHJTask shjTask in mHTaskConfig.sHJTask)
  117. {
  118. if(shjTask.missionInformation.AircraftInfos == null) continue;
  119. for (int i = 0; i < shjTask.missionInformation.AircraftInfos.Length; i++)
  120. {
  121. FlightPlanEditor? flightPlanEditor = new FlightPlanEditor();
  122. flightPlanEditor.aircraftparameter = editorConfig.aircraftParameters.Find(a =>
  123. a.AircraftID == shjTask.missionInformation.AircraftInfos[i]);
  124. flightPlanEditor.firepoint = new FirePoint[1];
  125. flightPlanEditor.firepoint[0] = editorConfig.firePoints[mHTaskConfig.FirePointId - 1];
  126. flightPlanEditor.missionpoint = shjTask.MissionPoints[0];
  127. flightPlanEditor.originbase =
  128. editorConfig.bases.Find(b => b.BaseId == flightPlanEditor.aircraftparameter.AirportId);
  129. flightPlanEditor.climbsegment = editorConfig.climbSegment;
  130. flightPlanEditor.descentsegment = editorConfig.descentSegment;
  131. flightPlanEditor.cruisesegment = editorConfig.cruiseSegment;
  132. flightPlanEditor.cityweather = editorConfig.cityWeather;
  133. AircraftSHJ aircraftSHJ = ComponentFactory.Create<AircraftSHJ, FlightPlanEditor,SHJTask>( flightPlanEditor, shjTask);
  134. mission.End += aircraftSHJ.End;
  135. mission.AircraftSHJs.Add(aircraftSHJ);
  136. }
  137. }
  138. }
  139. foreach (ZCTask zcTask in taskConfig.zCTask)
  140. {
  141. RescueMission mission = ComponentFactory.Create<RescueMission>();
  142. taskSys.RescueMissions.Add(mission);
  143. //mission.MissionId = zcTask.TotalZCId;
  144. mission.MissionId = $"侦擦任务 {zcTask.missionInformation.MissionId}";
  145. FireGround fireGround = ComponentFactory.Create<FireGround>();
  146. fireGround.countArea.burnarea = editorConfig.firePoints[zcTask.FirePointId - 1].InitialArea;
  147. fireGround.FirePoint = new DVector
  148. {
  149. Latitude = editorConfig.firePoints[zcTask.FirePointId - 1].FirePointLatitude,
  150. Longitude = editorConfig.firePoints[zcTask.FirePointId - 1].FirePointLongitude,
  151. Altitude = editorConfig.firePoints[zcTask.FirePointId - 1].FirePointHeight
  152. };
  153. mission.InitArea = editorConfig.firePoints[zcTask.FirePointId - 1].InitialArea;
  154. if(zcTask.missionInformation.AircraftInfos == null) continue;
  155. for (int i = 0; i < zcTask.missionInformation.AircraftInfos.Length; i++)
  156. {
  157. FlightPlanEditor? flightPlanEditor = new FlightPlanEditor();
  158. flightPlanEditor.aircraftparameter =
  159. editorConfig.aircraftParameters.Find(
  160. a => a.AircraftID == zcTask.missionInformation.AircraftInfos[i]);
  161. flightPlanEditor.firepoint = new FirePoint[1];
  162. flightPlanEditor.firepoint[0] = editorConfig.firePoints[zcTask.FirePointId - 1];
  163. flightPlanEditor.missionpoint = zcTask.MissionPoints[0];
  164. flightPlanEditor.originbase =
  165. editorConfig.bases.Find(b => b.BaseId == flightPlanEditor.aircraftparameter.AirportId);
  166. flightPlanEditor.climbsegment = editorConfig.climbSegment;
  167. flightPlanEditor.descentsegment = editorConfig.descentSegment;
  168. flightPlanEditor.cruisesegment = editorConfig.cruiseSegment;
  169. flightPlanEditor.cityweather = editorConfig.cityWeather;
  170. AircraftZC aircraft = ComponentFactory.Create<AircraftZC, FlightPlanEditor,ZCTask>( flightPlanEditor, zcTask);
  171. //aircraft.Name = zcTask.missionInformation.AircraftInfos[i];
  172. //aircraft.NextMissionId = zcTask.NextTaskId;
  173. mission.End += aircraft.End;
  174. mission.AircraftZCs.Add(aircraft);
  175. }
  176. }
  177. foreach (XHTask xhTask in taskConfig.xHTask)
  178. {
  179. if(xhTask.MissionPoints == null) continue;
  180. RescueMission mission = ComponentFactory.Create<RescueMission>();
  181. mission.AddComponent<XHEconomicalComponent>();
  182. mission.AddComponent<XHTotalTaskPerformanceComponent>();
  183. mission.MissionId = $"巡护任务 {xhTask.missionInformation.MissionId}";
  184. taskSys.RescueMissions.Add(mission);
  185. FireGround fireGround = ComponentFactory.Create<FireGround>();
  186. mission.FireGround = fireGround;
  187. mission.InitArea = editorConfig.firePoints[xhTask.FirePointId - 1].InitialArea;
  188. fireGround.FirePoint = new DVector
  189. {
  190. Latitude = editorConfig.firePoints[xhTask.FirePointId - 1].FirePointLatitude,
  191. Longitude = editorConfig.firePoints[xhTask.FirePointId - 1].FirePointLongitude,
  192. Altitude = editorConfig.firePoints[xhTask.FirePointId - 1].FirePointHeight
  193. };
  194. if(xhTask.missionInformation.AircraftInfos == null) continue;
  195. for (int i = 0; i < xhTask.missionInformation.AircraftInfos.Length; i++)
  196. {
  197. FlightPlanEditor? flightPlanEditor = new FlightPlanEditor();
  198. flightPlanEditor.aircraftparameter = editorConfig.aircraftParameters.Find(a =>
  199. a.AircraftID == xhTask.missionInformation.AircraftInfos[i]);
  200. flightPlanEditor.firepoint = new FirePoint[1];
  201. flightPlanEditor.firepoint[0] = editorConfig.firePoints[xhTask.FirePointId - 1];
  202. flightPlanEditor.missionpoint = xhTask.MissionPoints[0];
  203. flightPlanEditor.originbase =
  204. editorConfig.bases.Find(b => b.BaseId == flightPlanEditor.aircraftparameter.AirportId);
  205. flightPlanEditor.climbsegment = editorConfig.climbSegment;
  206. flightPlanEditor.descentsegment = editorConfig.descentSegment;
  207. flightPlanEditor.cruisesegment = editorConfig.cruiseSegment;
  208. flightPlanEditor.cityweather = editorConfig.cityWeather;
  209. AircraftXH aircraft = ComponentFactory.Create<AircraftXH, FlightPlanEditor>( flightPlanEditor);
  210. //aircraft.Name = zcTask.missionInformation.AircraftInfos[i];
  211. //aircraft.NextMissionId = zcTask.NextTaskId;
  212. mission.AircraftXHs.Add(aircraft);
  213. mission.End += aircraft.End;
  214. aircraft.Weather = weather;
  215. aircraft.AddComponent<XHStaticCapacityComponent>();
  216. aircraft.AddComponent<XHEconomicalComponent>();
  217. }
  218. }
  219. taskSys.Start();
  220. return UniTask.CompletedTask;
  221. }
  222. }