MapSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), player.ToJSON4EnterScene(self));
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Warning(
  118. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  119. }
  120. }
  121. /** 玩家离开场景请求 **/
  122. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  123. {
  124. try
  125. {
  126. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  127. }
  128. catch (Exception e)
  129. {
  130. Log.Warning($"playerLeaveRequest: catch - {e}");
  131. }
  132. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  133. }
  134. /** 绑定战斗服 **/
  135. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  136. {
  137. self.BattleServerId = serverId;
  138. }
  139. /** 创建单位 **/
  140. public static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
  141. {
  142. if (data.Count <= 0)
  143. {
  144. return 0;
  145. }
  146. int addUnitsResult = 0;
  147. if (needReturn) {
  148. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  149. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={data}, add units result={addUnitsResult}");
  150. } else
  151. {
  152. int objId = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  153. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
  154. }
  155. await ETTask.CompletedTask;
  156. return addUnitsResult;
  157. }
  158. public static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
  159. {
  160. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  161. listData.Add(data);
  162. return await self.AddUnits(listData, needReturn);
  163. }
  164. /// <summary>
  165. /// 移除单位,战斗服创建的unit
  166. /// </summary>
  167. /// <param name="self"></param>
  168. /// <param name="objectId"></param>
  169. public static async ETTask RemovePointUnit(this Map self, int objectId)
  170. {
  171. self.GetXmdsManager().removePointUnit(self.Id.ToString().Trim(), objectId);
  172. await ETTask.CompletedTask;
  173. }
  174. /// <summary>
  175. /// 场景复活数据
  176. /// </summary>
  177. /// <param name="self"></param>
  178. /// <param name="type">1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活.</param>
  179. public static string ReliveData(this Map self, ReliveType type)
  180. {
  181. JObject jsonObject = new ();
  182. jsonObject.Add("type", (int)type);
  183. jsonObject.Add("qty", 0);
  184. jsonObject.Add("itemType", "diamond");
  185. jsonObject.Add("hp", 8);
  186. jsonObject.Add("mp", 0);
  187. return JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  188. }
  189. /// <summary>
  190. /// 是否结束
  191. /// </summary>
  192. /// <param name="self"></param>
  193. /// <returns></returns>
  194. public static bool IsGameOver(this Map self)
  195. {
  196. return self.IsGameOver;
  197. }
  198. /// <summary>
  199. /// 增加单位玩家
  200. /// </summary>
  201. /// <param name="self"></param>
  202. /// <param name="openId"></param>
  203. /// <param name="objId"></param>
  204. public static void AddUnitObjId(this Map self, string openId, int objId)
  205. {
  206. if (objId <= 0 || self.IsGameOver())
  207. {
  208. return;
  209. }
  210. if (!self.UnitObjIds.TryAdd(openId, objId))
  211. {
  212. self.UnitObjIds[openId] = objId;
  213. }
  214. }
  215. /// <summary>
  216. /// 获取单位玩家objId
  217. /// </summary>
  218. /// <param name="self"></param>
  219. /// <param name="openId"></param>
  220. public static int GetUnitObjId(this Map self, string openId)
  221. {
  222. if (self.UnitObjIds.TryGetValue(openId, out int objId))
  223. {
  224. return objId;
  225. }
  226. return 0;
  227. }
  228. /// <summary>
  229. /// 添加单位玩家
  230. /// </summary>
  231. /// <param name="self"></param>
  232. /// <param name="openId"></param>
  233. /// <param name="templateId"></param>
  234. public static void AddUnitPlayer(this Map self, string openId, int templateId)
  235. {
  236. if (templateId <= 0 || self.IsGameOver())
  237. {
  238. return;
  239. }
  240. if (!self.UnitPlayers.TryAdd(openId, templateId))
  241. {
  242. self.UnitPlayers[openId] = templateId;
  243. }
  244. }
  245. /// <summary>
  246. /// 获取单位玩家模板id
  247. /// </summary>
  248. /// <param name="self"></param>
  249. /// <param name="openId"></param>
  250. /// <returns></returns>
  251. public static int GetUnitTemplateId(this Map self, string openId)
  252. {
  253. if (self.UnitPlayers.TryGetValue(openId, out int templateId))
  254. {
  255. return templateId;
  256. }
  257. return 0;
  258. }
  259. /// <summary>
  260. /// 获取当前坐标xy
  261. /// </summary>
  262. /// <param name="self"></param>
  263. /// <returns></returns>
  264. public static string GetCurXY(this Map self)
  265. {
  266. return self.DeadUnits.Count switch
  267. {
  268. // 1塔位置
  269. 0 => "230;85",
  270. // 1塔挂了,2塔位置
  271. 1 when self.DeadUnits.Contains(1001) => "150;85",
  272. // 1塔2塔挂了,3塔位置
  273. 2 when self.DeadUnits.Contains(1001) && self.DeadUnits.Contains(1002) => "70;85",
  274. _ => "230;85"
  275. };
  276. }
  277. }
  278. }