MapSystem.cs 13 KB

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