MapSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. self.UnitPlayers = new Dictionary<string, int>();
  28. self.UnitObjIds = new Dictionary<string, int>();
  29. self.UnitPlayerLikes = new Dictionary<string, int>();
  30. // 场景事件组件
  31. self.AddComponent<MapEventComponent>();
  32. // 场景复活组件
  33. self.AddComponent<MapReliveTimeComponent>();
  34. // 场景排行榜组件
  35. self.AddComponent<MapRankComponent>();
  36. }
  37. }
  38. public class MapDestroySystem: DestroySystem<Map>
  39. {
  40. /// <summary>
  41. /// 本地地图场景实体销毁
  42. /// </summary>
  43. /// <param name="self"></param>
  44. protected override void Destroy(Map self)
  45. {
  46. }
  47. }
  48. public static ZoneManagerPrx GetZoneManager(this Map self)
  49. {
  50. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  51. }
  52. public static XmdsManagerPrx GetXmdsManager(this Map self)
  53. {
  54. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  55. }
  56. public static GetPlayerData GetPlayerData(this Map self, long playerId)
  57. {
  58. string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true);
  59. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  60. }
  61. /** 从战斗服同步角色数据 **/
  62. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  63. {
  64. GetPlayerData result = self.GetPlayerData(player.GetId());
  65. if (result == null)
  66. {
  67. return;
  68. }
  69. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  70. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  71. }
  72. /** 场景添加角色 **/
  73. public static void AddPlayer(this Map self, WNPlayer player)
  74. {
  75. Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  76. self.SetForce(player);
  77. self.Players.TryAdd(player.GetId(), player);
  78. player.Map = self;
  79. }
  80. /** 获取场景角色 **/
  81. public static WNPlayer GetPlayer(this Map self, long playerId)
  82. {
  83. return self.Players.TryGetValue(playerId, out WNPlayer player) ? player : null;
  84. }
  85. /** 分配阵营 **/
  86. public static void SetForce(this Map self, WNPlayer player)
  87. {
  88. player.Force = (int)AreaForce.FORCEA;
  89. }
  90. /** 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用 **/
  91. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  92. {
  93. Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  94. WNPlayer actorPlayer = self.GetPlayer(player.GetId());
  95. if (actorPlayer != null)
  96. {
  97. self.Players.Remove(player.GetId());
  98. }
  99. // 重置进入场景标识
  100. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  101. }
  102. /** 玩家进入场景请求 **/
  103. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  104. {
  105. bool ready = player.GetComponent<PlayerTempDataComponent>().MapData.ready;
  106. if (ready)
  107. {
  108. Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}");
  109. return;
  110. }
  111. try
  112. {
  113. string enterData = player.ToJSON4EnterScene(self);
  114. Log.Debug($"{player.GetName()}, enterSceneData:{enterData}");
  115. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), enterData);
  116. }
  117. catch (Exception e)
  118. {
  119. Log.Warning(
  120. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  121. }
  122. }
  123. /** 玩家离开场景请求 **/
  124. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  125. {
  126. try
  127. {
  128. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  129. }
  130. catch (Exception e)
  131. {
  132. Log.Warning($"playerLeaveRequest: catch - {e}");
  133. }
  134. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  135. }
  136. /** 绑定战斗服 **/
  137. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  138. {
  139. self.BattleServerId = serverId;
  140. }
  141. /** 创建单位 **/
  142. public static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
  143. {
  144. if (data.Count <= 0)
  145. {
  146. return 0;
  147. }
  148. int addUnitsResult = 0;
  149. if (needReturn) {
  150. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  151. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={data}, add units result={addUnitsResult}");
  152. } else
  153. {
  154. int objId = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  155. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
  156. }
  157. await ETTask.CompletedTask;
  158. return addUnitsResult;
  159. }
  160. public static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
  161. {
  162. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  163. listData.Add(data);
  164. return await self.AddUnits(listData, needReturn);
  165. }
  166. /// <summary>
  167. /// 移除单位,战斗服创建的unit
  168. /// </summary>
  169. /// <param name="self"></param>
  170. /// <param name="objectId"></param>
  171. public static async ETTask RemovePointUnit(this Map self, int objectId)
  172. {
  173. self.GetXmdsManager().removePointUnit(self.Id.ToString().Trim(), objectId);
  174. await ETTask.CompletedTask;
  175. }
  176. /// <summary>
  177. /// 场景复活数据
  178. /// </summary>
  179. /// <param name="self"></param>
  180. /// <param name="type">1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活.</param>
  181. public static string ReliveData(this Map self, ReliveType type)
  182. {
  183. JObject jsonObject = new ();
  184. jsonObject.Add("type", (int)type);
  185. jsonObject.Add("qty", 0);
  186. jsonObject.Add("itemType", "diamond");
  187. jsonObject.Add("hp", 8);
  188. jsonObject.Add("mp", 0);
  189. return JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  190. }
  191. /// <summary>
  192. /// 是否结束
  193. /// </summary>
  194. /// <param name="self"></param>
  195. /// <returns></returns>
  196. public static bool IsGameOver(this Map self)
  197. {
  198. return self.IsGameOver;
  199. }
  200. /// <summary>
  201. /// 增加单位玩家
  202. /// </summary>
  203. /// <param name="self"></param>
  204. /// <param name="openId"></param>
  205. /// <param name="objId"></param>
  206. public static void AddUnitObjId(this Map self, string openId, int objId)
  207. {
  208. if (objId <= 0 || self.IsGameOver())
  209. {
  210. return;
  211. }
  212. if (!self.UnitObjIds.TryAdd(openId, objId))
  213. {
  214. self.UnitObjIds[openId] = objId;
  215. }
  216. }
  217. /// <summary>
  218. /// 获取单位玩家objId
  219. /// </summary>
  220. /// <param name="self"></param>
  221. /// <param name="openId"></param>
  222. public static int GetUnitObjId(this Map self, string openId)
  223. {
  224. if (self.UnitObjIds.TryGetValue(openId, out int objId))
  225. {
  226. return objId;
  227. }
  228. return 0;
  229. }
  230. /// <summary>
  231. /// 添加单位玩家
  232. /// </summary>
  233. /// <param name="self"></param>
  234. /// <param name="openId"></param>
  235. /// <param name="templateId"></param>
  236. public static void AddUnitPlayer(this Map self, string openId, int templateId)
  237. {
  238. if (templateId <= 0 || self.IsGameOver())
  239. {
  240. return;
  241. }
  242. if (!self.UnitPlayers.TryAdd(openId, templateId))
  243. {
  244. self.UnitPlayers[openId] = templateId;
  245. }
  246. }
  247. /// <summary>
  248. /// 获取单位玩家模板id
  249. /// </summary>
  250. /// <param name="self"></param>
  251. /// <param name="openId"></param>
  252. /// <returns></returns>
  253. public static int GetUnitTemplateId(this Map self, string openId)
  254. {
  255. if (self.UnitPlayers.TryGetValue(openId, out int templateId))
  256. {
  257. return templateId;
  258. }
  259. return 0;
  260. }
  261. /// <summary>
  262. /// 获取当前坐标xy
  263. /// </summary>
  264. /// <param name="self"></param>
  265. /// <returns></returns>
  266. public static string GetCurXY(this Map self)
  267. {
  268. return self.DeadUnits.Count switch
  269. {
  270. // 1塔位置
  271. 0 => "230;85",
  272. // 1塔挂了,2塔位置
  273. 1 when self.DeadUnits.Contains(1001) => "150;85",
  274. // 1塔2塔挂了,3塔位置
  275. 2 when self.DeadUnits.Contains(1001) && self.DeadUnits.Contains(1002) => "70;85",
  276. _ => "230;85"
  277. };
  278. }
  279. }
  280. }