MapEventComponentSystem.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. map.IsGameOver = true;
  129. if (map?.Players is not { Count: > 0 })
  130. {
  131. return;
  132. }
  133. foreach (WNPlayer player in map.Players.Values.Where(player => player != null))
  134. {
  135. // 记录玩家历史
  136. map.SyncPlayerHistoryData(player);
  137. // 战斗服场景玩家离开
  138. map.PlayerLeaveRequest(player, false);
  139. // 本地场景移除玩家
  140. map.RemovePlayer(player, false);
  141. // 战斗服结束场景
  142. map.GetZoneManager().destroyZoneRequest(map.Id.ToString());
  143. player.Map = null;
  144. }
  145. }
  146. /** 拾取道具 **/
  147. public static void OnPickItem(this MapEventComponent self, JObject msg)
  148. {
  149. }
  150. /** 击杀boss **/
  151. public static void OnKillBoss(this MapEventComponent self, JObject msg)
  152. {
  153. }
  154. /** 战斗统计 **/
  155. public static void OnBattleReport(this MapEventComponent self, Map map, JObject msg)
  156. {
  157. string data = Convert.ToString(msg.SelectToken("data"));
  158. if (string.IsNullOrEmpty(data))
  159. {
  160. return;
  161. }
  162. List<Struct.BattleReports> list = new List<Struct.BattleReports>();
  163. var battleReports = JsonConvert.DeserializeObject<List<Struct.BattleReports>>(data);
  164. if (battleReports is { Count: > 0 })
  165. {
  166. foreach (Struct.BattleReports report in battleReports)
  167. {
  168. // 过滤一下非我方单位
  169. if (report.Force != 1)
  170. {
  171. continue;
  172. }
  173. uint ID = report.ID;
  174. long PlayerUUID = string.IsNullOrEmpty(report.PlayerUUID)? 0 : Convert.ToInt64(report.PlayerUUID);
  175. int TemplateID = report.TemplateId;
  176. int Force = report.Force;
  177. int TotalDamage = report.TotalDamage;
  178. string name = "";
  179. if (PlayerUUID > 0)
  180. {
  181. name = "玩家-" + TemplateID;
  182. }
  183. else
  184. {
  185. if (TemplateID > 0)
  186. {
  187. Monster prop = MonsterCategory.Instance.Get(TemplateID);
  188. if (prop != null)
  189. {
  190. name = prop.Name;
  191. }
  192. }
  193. }
  194. list.Add(new Struct.BattleReports(ID, report.PlayerUUID, TemplateID, name, Force, TotalDamage));
  195. }
  196. }
  197. map.GetComponent<MapRankComponent>().UpdateRank(list, map);
  198. }
  199. }
  200. }