MapSystem.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  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>
  15. {
  16. /// <summary>
  17. /// 本地地图场景实体创建
  18. /// </summary>
  19. /// <param name="self"></param>
  20. /// <param name="opts"></param>
  21. protected override void Awake(Map self, JObject opts)
  22. {
  23. self.createTime = TimeHelper.ServerNow();
  24. Log.Debug($"create area opts:{JsonConvert.SerializeObject(opts, Formatting.Indented)}");
  25. self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0;
  26. self.MapId = Convert.ToInt32(opts.SelectToken("areaId"));
  27. self.Prop = MapConfigCategory.Instance.Get(self.MapId);
  28. self.Type = self.Prop.Type;
  29. // 添加组件
  30. self.AddComponent<MapEventComponent, Map>(self);
  31. }
  32. }
  33. public class MapDestroySystem: DestroySystem<Map>
  34. {
  35. /// <summary>
  36. /// 本地地图场景实体销毁
  37. /// </summary>
  38. /// <param name="self"></param>
  39. protected override void Destroy(Map self)
  40. {
  41. }
  42. }
  43. public static ZoneManagerPrx GetZoneManager(this Map self)
  44. {
  45. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  46. }
  47. public static XmdsManagerPrx GetXmdsManager(this Map self)
  48. {
  49. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  50. }
  51. public static GetPlayerData GetPlayerData(this Map self, long playerId)
  52. {
  53. string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true);
  54. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  55. }
  56. /** 从战斗服同步角色数据 **/
  57. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  58. {
  59. GetPlayerData result = self.GetPlayerData(player.GetId());
  60. if (result == null)
  61. {
  62. return;
  63. }
  64. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  65. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  66. }
  67. /** 场景添加角色 **/
  68. public static void AddPlayer(this Map self, WNPlayer player)
  69. {
  70. Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  71. self.SetForce(player);
  72. self.Players.TryAdd(player.GetId(), player);
  73. player.Map = self;
  74. }
  75. /** 获取场景角色 **/
  76. public static WNPlayer GetPlayer(this Map self, long playerId)
  77. {
  78. return self.Players.TryGetValue(playerId, out WNPlayer player) ? player : null;
  79. }
  80. /** 分配阵营 **/
  81. public static void SetForce(this Map self, WNPlayer player)
  82. {
  83. player.Force = (int)AreaForce.FORCEA;
  84. }
  85. /** 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用 **/
  86. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  87. {
  88. Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  89. WNPlayer actorPlayer = self.GetPlayer(player.GetId());
  90. if (actorPlayer != null)
  91. {
  92. self.Players.Remove(player.GetId());
  93. }
  94. // 重置进入场景标识
  95. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  96. }
  97. /** 玩家进入场景请求 **/
  98. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  99. {
  100. bool ready = player.GetComponent<PlayerTempDataComponent>().MapData.ready;
  101. if (ready)
  102. {
  103. Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}");
  104. return;
  105. }
  106. try
  107. {
  108. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), player.ToJSON4EnterScene(self));
  109. }
  110. catch (Exception e)
  111. {
  112. Log.Warning(
  113. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  114. }
  115. }
  116. /** 玩家离开场景请求 **/
  117. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  118. {
  119. try
  120. {
  121. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  122. }
  123. catch (Exception e)
  124. {
  125. Log.Warning($"playerLeaveRequest: catch - {e}");
  126. }
  127. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  128. }
  129. /** 绑定战斗服 **/
  130. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  131. {
  132. self.BattleServerId = serverId;
  133. }
  134. /** 创建单位 **/
  135. public static async ETTask<int> AddUnits(this Map self, string instanceId, string data)
  136. {
  137. await ETTask.CompletedTask;
  138. return self.GetXmdsManager().addUnits(instanceId, data);
  139. }
  140. /** 创建单位 **/
  141. public static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit unit, bool needReturn)
  142. {
  143. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  144. listData.Add(unit);
  145. int addUnitsResult = 0;
  146. if (needReturn) {
  147. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(listData, Formatting.Indented));
  148. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={listData}, add units result={addUnitsResult}");
  149. } else
  150. {
  151. int objId = await self.AddUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(listData, Formatting.Indented));
  152. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
  153. }
  154. return addUnitsResult;
  155. }
  156. /** 移除单位 **/
  157. public static async ETTask RemoveUnit(this Map self, int unitId)
  158. {
  159. self.GetXmdsManager().removeUnit(self.Id.ToString().Trim(), unitId);
  160. await ETTask.CompletedTask;
  161. }
  162. }
  163. }