MapSystem.cs 7.7 KB

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