MapSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using BattleIce;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. namespace ET.Server
  9. {
  10. [FriendOf(typeof (Map))]
  11. [FriendOf(typeof (BattleIceAgentComponent))]
  12. public static class MapSystem
  13. {
  14. public class MapAwakeSystem: AwakeSystem<Map, JObject, WNPlayer>
  15. {
  16. protected override void Awake(Map self, JObject opts, WNPlayer player)
  17. {
  18. Log.Debug($"创建场景实体...create area opts:{opts.ToString()}");
  19. self.CreateTime = TimeHelper.ServerNow();
  20. self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0;
  21. self.MapId = Convert.ToInt32(opts.SelectToken("areaId"));
  22. self.Prop = MapConfigCategory.Instance.Get(self.MapId);
  23. self.Type = self.Prop.Type;
  24. self.HasPlayerEntered = false;
  25. self.Actors = new Dictionary<long, Struct.Actor>();
  26. self.DeadUnits = new List<int>();
  27. self.DeadUnitPlayer = new List<int>();
  28. self.IsLeaveKeepObject = false;
  29. // 战斗服事件组件
  30. self.AddComponent<MapEventComponent>();
  31. }
  32. }
  33. public class MapDestroySystem: DestroySystem<Map>
  34. {
  35. protected override void Destroy(Map self)
  36. {
  37. Log.Info($"销毁场景");
  38. }
  39. }
  40. public static ZoneManagerPrx GetZoneManager(this Map self)
  41. {
  42. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  43. }
  44. public static XmdsManagerPrx GetXmdsManager(this Map self)
  45. {
  46. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  47. }
  48. /// <summary>
  49. /// 获取战斗服角色数据
  50. /// </summary>
  51. /// <param name="self"></param>
  52. /// <param name="playerId"></param>
  53. /// <returns></returns>
  54. private static GetPlayerData GetBattleServerPlayerData(this Map self, long playerId)
  55. {
  56. string result = self.GetXmdsManager().getPlayerData(playerId.ToString(), true);
  57. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  58. }
  59. /// <summary>
  60. /// 从战斗服同步角色数据
  61. /// </summary>
  62. /// <param name="self"></param>
  63. /// <param name="player"></param>
  64. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  65. {
  66. GetPlayerData result = self.GetBattleServerPlayerData(player.GetId());
  67. if (result == null)
  68. {
  69. return;
  70. }
  71. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  72. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  73. }
  74. /// <summary>
  75. /// 分配阵营
  76. /// </summary>
  77. /// <param name="self"></param>
  78. /// <param name="player"></param>
  79. public static void SetForce(this Map self, WNPlayer player)
  80. {
  81. player.Force = (int)AreaForce.FORCEA;
  82. }
  83. /// <summary>
  84. /// 场景添加角色
  85. /// </summary>
  86. /// <param name="self"></param>
  87. /// <param name="player"></param>
  88. public static void AddPlayer(this Map self, WNPlayer player)
  89. {
  90. Log.Info($"AddPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  91. self.SetForce(player);
  92. player.Map = self;
  93. if (!self.Actors.ContainsKey(player.GetId()))
  94. {
  95. self.HasPlayerEntered = true;
  96. self.Actors.Add(player.GetId(), new Struct.Actor());
  97. }
  98. }
  99. /// <summary>
  100. /// 玩家进场景后推的消息
  101. /// </summary>
  102. /// <param name="self"></param>
  103. /// <param name="player"></param>
  104. public static void OnReady(this Map self, WNPlayer player)
  105. {
  106. }
  107. /// <summary>
  108. /// 角色成功进入场景
  109. /// </summary>
  110. /// <param name="self"></param>
  111. /// <param name="player"></param>
  112. public static void OnPlayerEntered(this Map self, WNPlayer player)
  113. {
  114. }
  115. /// <summary>
  116. /// 玩家登录事件
  117. /// </summary>
  118. /// <param name="self"></param>
  119. /// <param name="player"></param>
  120. public static void OnPlayerLogin(this Map self, WNPlayer player)
  121. {
  122. }
  123. /// <summary>
  124. /// 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用
  125. /// </summary>
  126. /// <param name="self"></param>
  127. /// <param name="player"></param>
  128. /// <param name="keepObject"></param>
  129. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  130. {
  131. Log.Info($"RemovePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  132. // 重置进入场景标识
  133. // player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  134. if (self.Actors.ContainsKey(player.GetId()))
  135. {
  136. self.PlayerLeaveRequest(player, keepObject);
  137. self.Actors.Remove(player.GetId());
  138. }
  139. }
  140. /// <summary>
  141. /// 玩家离线时触发
  142. /// </summary>
  143. /// <param name="self"></param>
  144. /// <param name="player"></param>
  145. public static void OnPlayerLogout(this Map self, WNPlayer player)
  146. {
  147. if (self.Actors.ContainsKey(player.GetId()))
  148. {
  149. // 本场景移除玩家
  150. self.RemovePlayer(player, self.IsLeaveKeepObject);
  151. }
  152. // 移除本地玩家数据
  153. self.DomainScene().GetComponent<GamePlayerComponent>().Remove(player.GetId());
  154. }
  155. /// <summary>
  156. /// 绑定战斗服
  157. /// </summary>
  158. /// <param name="self"></param>
  159. /// <param name="player"></param>
  160. /// <param name="serverId"></param>
  161. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  162. {
  163. self.BattleServerId = serverId;
  164. }
  165. /// <summary>
  166. /// 创建单位
  167. /// </summary>
  168. /// <param name="self"></param>
  169. /// <param name="data"></param>
  170. /// <param name="needReturn"></param>
  171. /// <returns></returns>
  172. private static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
  173. {
  174. if (data.Count <= 0)
  175. {
  176. return 0;
  177. }
  178. int addUnitsResult = 0;
  179. if (needReturn) {
  180. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString(), JsonConvert.SerializeObject(data, Formatting.Indented));
  181. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString()}, data={data}, add units result={addUnitsResult}");
  182. } else
  183. {
  184. int objId = self.GetXmdsManager().addUnits(self.Id.ToString(), JsonConvert.SerializeObject(data, Formatting.Indented));
  185. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString()}, objId={objId}");
  186. }
  187. await ETTask.CompletedTask;
  188. return addUnitsResult;
  189. }
  190. private static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
  191. {
  192. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  193. listData.Add(data);
  194. return await self.AddUnits(listData, needReturn);
  195. }
  196. /// <summary>
  197. /// 移除单位,战斗服创建的unit
  198. /// </summary>
  199. /// <param name="self"></param>
  200. /// <param name="objectId"></param>
  201. public static async ETTask RemovePointUnit(this Map self, int objectId)
  202. {
  203. self.GetXmdsManager().removePointUnit(self.Id.ToString(), objectId);
  204. await ETTask.CompletedTask;
  205. }
  206. /// <summary>
  207. /// 玩家进入场景请求
  208. /// </summary>
  209. /// <param name="self"></param>
  210. /// <param name="player"></param>
  211. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  212. {
  213. self.Actors.TryGetValue(player.GetId(), out Struct.Actor actor);
  214. if (actor.Ready)
  215. {
  216. Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={actor.Ready}");
  217. return;
  218. }
  219. try
  220. {
  221. string enterData = player.ToJSON4EnterScene(self);
  222. Log.Debug($"{player.GetName()}, EnterSceneData:{enterData}");
  223. self.GetZoneManager().playerEnterRequest(player.GetId().ToString(), self.Id.ToString(), enterData);
  224. }
  225. catch (Exception e)
  226. {
  227. Log.Error($"PlayerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  228. }
  229. }
  230. /// <summary>
  231. /// 玩家离开场景请求
  232. /// </summary>
  233. /// <param name="self"></param>
  234. /// <param name="player"></param>
  235. /// <param name="keepObject"></param>
  236. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  237. {
  238. try
  239. {
  240. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString(), self.InstanceId.ToString(), keepObject);
  241. }
  242. catch (Exception e)
  243. {
  244. Log.Error($"PlayerLeaveRequest: 出错 - {e}");
  245. }
  246. Log.Debug($"PlayerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  247. }
  248. /// <summary>
  249. /// 战斗服结束场景
  250. /// </summary>
  251. /// <param name="self"></param>
  252. public static void DestroyZoneRequest(this Map self)
  253. {
  254. self.GetZoneManager().destroyZoneRequest(self.Id.ToString());
  255. }
  256. }
  257. }