MapEventComponentSystem.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace ET.Server
  7. {
  8. [FriendOf(typeof (MapEventComponent))]
  9. [FriendOfAttribute(typeof (Map))]
  10. public static class MapEventComponentSystem
  11. {
  12. public class MapEventComponentAwakeSystem: AwakeSystem<MapEventComponent>
  13. {
  14. /// <summary>
  15. /// 场景事件组件创建
  16. /// </summary>
  17. /// <param name="self"></param>
  18. protected override void Awake(MapEventComponent self)
  19. {
  20. Log.Info($"创建事件组件...");
  21. }
  22. }
  23. public class MapEventComponentDestroySystem: DestroySystem<MapEventComponent>
  24. {
  25. /// <summary>
  26. /// 场景事件组件销毁
  27. /// </summary>
  28. /// <param name="self"></param>
  29. protected override void Destroy(MapEventComponent self)
  30. {
  31. Log.Info($"销毁事件组件...");
  32. }
  33. }
  34. /** 玩家进场景后推的消息 **/
  35. public static void OnReady(this MapEventComponent self, WNPlayer player)
  36. {
  37. }
  38. /** 玩家登录事件 **/
  39. public static void OnPlayerLogin(this MapEventComponent self, WNPlayer player)
  40. {
  41. }
  42. /** 角色成功进入场景 **/
  43. public static void OnPlayerEntered(this MapEventComponent self, WNPlayer player)
  44. {
  45. }
  46. /** 单位死亡事件 **/
  47. public static void OnUnitDead(this MapEventComponent self, Map map, JObject msg)
  48. {
  49. int unitType = Convert.ToInt32(msg.SelectToken("unitType"));
  50. int objId = Convert.ToInt32(msg.SelectToken("objId"));
  51. int posX = Convert.ToInt32(msg.SelectToken("posX"));
  52. int posY = Convert.ToInt32(msg.SelectToken("posY"));
  53. // 攻击者
  54. long hitFinalPlayerId = msg.SelectToken("hitFinal").ToString() == ""? 0 : Convert.ToInt64(msg.SelectToken("hitFinal"));
  55. long belongPlayerId = msg.SelectToken("belongPlayerId").ToString() == ""? 0 : Convert.ToInt64(msg.SelectToken("belongPlayerId"));
  56. long[] atkAssistantList = JsonConvert.DeserializeObject<long[]>(Convert.ToString(msg.SelectToken("atkAssistantList")) ?? string.Empty);
  57. WNPlayer hitFinalPlayer = null;
  58. // 默认使用第一个摸怪玩家
  59. if (belongPlayerId > 0)
  60. {
  61. hitFinalPlayer = map.GetPlayer(belongPlayerId);
  62. }
  63. if (hitFinalPlayer == null && hitFinalPlayerId > 0)
  64. {
  65. hitFinalPlayer = map.GetPlayer(hitFinalPlayerId);
  66. }
  67. switch (unitType)
  68. {
  69. case 0:
  70. // 怪物死亡(玩家单位死亡, 延迟5秒复活)
  71. int unitTemplateId = Convert.ToInt32(msg.SelectToken("unitTemplateId"));
  72. Monster monsterProp = MonsterCategory.Instance.Get(unitTemplateId);
  73. if (monsterProp == null)
  74. {
  75. Log.Error($"unitDead not fount montster : {unitTemplateId}, {map.MapId}, {msg}");
  76. return;
  77. }
  78. // 如果是塔
  79. if (monsterProp.Atype == 4)
  80. {
  81. if (!map.DeadUnits.Contains(unitTemplateId))
  82. {
  83. map.DeadUnits.Add(unitTemplateId);
  84. }
  85. }
  86. // 如果是单位玩家
  87. if (map.UnitObjIds.Contains(objId) && !map.IsGameOver())
  88. {
  89. Log.Debug($"单位死亡...objId={objId}, unitTemplateId={unitTemplateId}, posX={Convert.ToInt32(msg.SelectToken("posX"))}, posY={Convert.ToInt32(msg.SelectToken("posY"))}");
  90. map.UnitObjIds.Remove(objId);
  91. map.GetComponent<MapReliveTimeComponent>().ReliveDatas.Add(new Struct.UnitPlayerReliveData(map, unitTemplateId, TimeHelper.ServerNow() + 5000, posX, posY));
  92. }
  93. break;
  94. case 1:
  95. // 玩家死亡(主播死亡立即复活)
  96. long unitPlayerId = Convert.ToInt64(msg.SelectToken("unitPlayerId"));
  97. if (hitFinalPlayerId <= 0)
  98. {
  99. // 被boss杀死
  100. int attackerTemplateId = Convert.ToInt32(msg.SelectToken("attackerTemplateId"));
  101. Log.Debug($"玩家死亡...被boss杀死...playerId={unitPlayerId}, bossId={attackerTemplateId}");
  102. }
  103. else
  104. {
  105. // 被玩家杀死
  106. Log.Debug($"玩家死亡...被玩家杀死...playerId={unitPlayerId}, 攻击者={hitFinalPlayerId}");
  107. }
  108. WNPlayer unitPlayer = map.GetPlayer(unitPlayerId);
  109. if (unitPlayer == null)
  110. {
  111. return;
  112. }
  113. // 原地复活
  114. unitPlayer.GetXmdsManager().revivePlayer(unitPlayer.GetId().ToString(), unitPlayer.Map.ReliveData(ReliveType.NOW));
  115. break;
  116. case 2:
  117. // 宠物死亡
  118. break;
  119. }
  120. }
  121. /** 副本消息 **/
  122. public static void OnMessageEvent(this MapEventComponent self, JObject msg)
  123. {
  124. }
  125. /** 场景结算事件 **/
  126. public static void OnGameOver(this MapEventComponent self, Map map)
  127. {
  128. if (map?.Players is not { Count: > 0 })
  129. {
  130. return;
  131. }
  132. foreach (WNPlayer player in map.Players.Values.Where(player => player != null))
  133. {
  134. // 记录玩家历史
  135. map.SyncPlayerHistoryData(player);
  136. // 战斗服场景玩家离开
  137. map.PlayerLeaveRequest(player, false);
  138. // 本地场景移除玩家
  139. map.RemovePlayer(player, false);
  140. // 战斗服结束场景
  141. map.GetZoneManager().destroyZoneRequest(map.Id.ToString());
  142. player.Map = null;
  143. }
  144. }
  145. /** 拾取道具 **/
  146. public static void OnPickItem(this MapEventComponent self, JObject msg)
  147. {
  148. }
  149. /** 击杀boss **/
  150. public static void OnKillBoss(this MapEventComponent self, JObject msg)
  151. {
  152. }
  153. /** 战斗统计 **/
  154. public static void OnBattleReport(this MapEventComponent self, Map map, JObject msg)
  155. {
  156. string data = Convert.ToString(msg.SelectToken("data"));
  157. if (string.IsNullOrEmpty(data))
  158. {
  159. return;
  160. }
  161. List<Struct.BattleReports> list = new List<Struct.BattleReports>();
  162. var battleReports = JsonConvert.DeserializeObject<List<Struct.BattleReports>>(data);
  163. if (battleReports is { Count: > 0 })
  164. {
  165. foreach (Struct.BattleReports report in battleReports)
  166. {
  167. // 过滤一下非我方单位
  168. if (report.Force != 1)
  169. {
  170. continue;
  171. }
  172. uint ID = report.ID;
  173. long PlayerUUID = string.IsNullOrEmpty(report.PlayerUUID)? 0 : Convert.ToInt64(report.PlayerUUID);
  174. int TemplateID = report.TemplateId;
  175. int Force = report.Force;
  176. int TotalDamage = report.TotalDamage;
  177. string name = "";
  178. if (PlayerUUID > 0)
  179. {
  180. name = "玩家-" + TemplateID;
  181. }
  182. else
  183. {
  184. if (TemplateID > 0)
  185. {
  186. Monster prop = MonsterCategory.Instance.Get(TemplateID);
  187. if (prop != null)
  188. {
  189. name = prop.Name;
  190. }
  191. }
  192. }
  193. list.Add(new Struct.BattleReports(ID, report.PlayerUUID, TemplateID, name, Force, TotalDamage));
  194. }
  195. }
  196. map.GetComponent<MapRankComponent>().UpdateRank(list, map);
  197. }
  198. }
  199. }