MapSystem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  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, WNPlayer>
  15. {
  16. protected override void Awake(Map self, JObject opts, WNPlayer player)
  17. {
  18. Log.Debug($"创建场景实体...create area opts:{opts.ToString()}");
  19. self.RoomId = player.GetRoomId();
  20. self.createTime = TimeHelper.ServerNow();
  21. self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0;
  22. self.MapId = Convert.ToInt32(opts.SelectToken("areaId"));
  23. self.Prop = MapConfigCategory.Instance.Get(self.MapId);
  24. self.Type = self.Prop.Type;
  25. self.Player = player;
  26. self.UnitPlayers = new Dictionary<string, Struct.UnitPlayerData>();
  27. self.DeadUnits = new List<int>();
  28. self.DeadUnitPlayer = new List<int>();
  29. // 战斗服事件组件
  30. self.AddComponent<MapEventComponent>();
  31. // 场景排行榜组件
  32. self.AddComponent<MapRankComponent>();
  33. // token为空过滤一下抖音相关组件
  34. if (self.TokenIsNull())
  35. {
  36. return;
  37. }
  38. // 抖音直播评论任务组件
  39. self.AddComponent<MapDouyinLiveCommentComponent>();
  40. // 抖音直播礼物任务组件
  41. self.AddComponent<MapDouyinLiveGiftComponent>();
  42. // 抖音直播点赞任务组件
  43. self.AddComponent<MapDouyinLiveLikeComponent>();
  44. // 抖音直播礼物置顶
  45. self.DomainScene().GetComponent<GameDouyinComponent>().TopGifts(self.RoomId);
  46. }
  47. }
  48. public class MapDestroySystem: DestroySystem<Map>
  49. {
  50. protected override void Destroy(Map self)
  51. {
  52. Log.Info($"销毁场景");
  53. }
  54. }
  55. public static ZoneManagerPrx GetZoneManager(this Map self)
  56. {
  57. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
  58. }
  59. public static XmdsManagerPrx GetXmdsManager(this Map self)
  60. {
  61. return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
  62. }
  63. public static GetPlayerData GetPlayerData(this Map self, long playerId)
  64. {
  65. string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true);
  66. return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
  67. }
  68. /// <summary>
  69. /// 从战斗服同步角色数据
  70. /// </summary>
  71. /// <param name="self"></param>
  72. /// <param name="player"></param>
  73. public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
  74. {
  75. GetPlayerData result = self.GetPlayerData(player.GetId());
  76. if (result == null)
  77. {
  78. return;
  79. }
  80. player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
  81. player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
  82. }
  83. /// <summary>
  84. /// 获取场景角色
  85. /// </summary>
  86. /// <param name="self"></param>
  87. /// <param name="playerId"></param>
  88. /// <returns></returns>
  89. public static WNPlayer GetPlayer(this Map self, long playerId)
  90. {
  91. return self.Player;
  92. }
  93. /// <summary>
  94. /// 分配阵营
  95. /// </summary>
  96. /// <param name="self"></param>
  97. /// <param name="player"></param>
  98. public static void SetForce(this Map self, WNPlayer player)
  99. {
  100. player.Force = (int)AreaForce.FORCEA;
  101. }
  102. /// <summary>
  103. /// 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用
  104. /// </summary>
  105. /// <param name="self"></param>
  106. /// <param name="player"></param>
  107. /// <param name="keepObject"></param>
  108. public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
  109. {
  110. Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  111. WNPlayer actorPlayer = self.GetPlayer(player.GetId());
  112. if (actorPlayer != null)
  113. {
  114. // self.Players.Remove(player.GetId());
  115. }
  116. // 重置进入场景标识
  117. player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
  118. }
  119. /// <summary>
  120. /// 玩家进入场景请求
  121. /// </summary>
  122. /// <param name="self"></param>
  123. /// <param name="player"></param>
  124. public static void PlayerEnterRequest(this Map self, WNPlayer player)
  125. {
  126. bool ready = player.GetComponent<PlayerTempDataComponent>().MapData.ready;
  127. if (ready)
  128. {
  129. Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}");
  130. return;
  131. }
  132. try
  133. {
  134. string enterData = player.ToJSON4EnterScene(self);
  135. Log.Debug($"{player.GetName()}, enterSceneData:{enterData}");
  136. self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), enterData);
  137. }
  138. catch (Exception e)
  139. {
  140. Log.Warning(
  141. $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
  142. }
  143. }
  144. /// <summary>
  145. /// 场景添加角色
  146. /// </summary>
  147. /// <param name="self"></param>
  148. /// <param name="player"></param>
  149. public static void AddPlayer(this Map self, WNPlayer player)
  150. {
  151. Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
  152. player.Map = self;
  153. self.SetForce(player);
  154. self.Player = player;
  155. }
  156. /// <summary>
  157. /// 玩家进场景后推的消息
  158. /// </summary>
  159. /// <param name="self"></param>
  160. /// <param name="player"></param>
  161. public static void PlayerReady(this Map self, WNPlayer player)
  162. {
  163. self.CurBattleIndex = 0;
  164. }
  165. /// <summary>
  166. /// 角色成功进入场景
  167. /// </summary>
  168. /// <param name="self"></param>
  169. /// <param name="player"></param>
  170. public static void PlayerEntered(this Map self, WNPlayer player)
  171. {
  172. }
  173. /// <summary>
  174. /// 玩家登录事件
  175. /// </summary>
  176. /// <param name="self"></param>
  177. /// <param name="player"></param>
  178. public static void PlayerLogin(this Map self, WNPlayer player)
  179. {
  180. }
  181. /// <summary>
  182. /// 玩家离开场景请求
  183. /// </summary>
  184. /// <param name="self"></param>
  185. /// <param name="player"></param>
  186. /// <param name="keepObject"></param>
  187. public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
  188. {
  189. try
  190. {
  191. self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject);
  192. }
  193. catch (Exception e)
  194. {
  195. Log.Warning($"playerLeaveRequest: catch - {e}");
  196. }
  197. Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
  198. }
  199. /// <summary>
  200. /// 绑定战斗服
  201. /// </summary>
  202. /// <param name="self"></param>
  203. /// <param name="player"></param>
  204. /// <param name="serverId"></param>
  205. public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
  206. {
  207. self.BattleServerId = serverId;
  208. }
  209. /// <summary>
  210. /// 创建单位
  211. /// </summary>
  212. /// <param name="self"></param>
  213. /// <param name="data"></param>
  214. /// <param name="needReturn"></param>
  215. /// <returns></returns>
  216. public static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
  217. {
  218. if (data.Count <= 0)
  219. {
  220. return 0;
  221. }
  222. int addUnitsResult = 0;
  223. if (needReturn) {
  224. addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  225. Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={data}, add units result={addUnitsResult}");
  226. } else
  227. {
  228. int objId = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
  229. Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
  230. }
  231. await ETTask.CompletedTask;
  232. return addUnitsResult;
  233. }
  234. public static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
  235. {
  236. List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
  237. listData.Add(data);
  238. return await self.AddUnits(listData, needReturn);
  239. }
  240. /// <summary>
  241. /// 移除单位,战斗服创建的unit
  242. /// </summary>
  243. /// <param name="self"></param>
  244. /// <param name="objectId"></param>
  245. public static async ETTask RemovePointUnit(this Map self, int objectId)
  246. {
  247. self.GetXmdsManager().removePointUnit(self.Id.ToString().Trim(), objectId);
  248. await ETTask.CompletedTask;
  249. }
  250. /// <summary>
  251. /// 场景复活数据
  252. /// </summary>
  253. /// <param name="self"></param>
  254. /// <param name="type">1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活.</param>
  255. public static string ReliveData(this Map self, ReliveType type)
  256. {
  257. JObject jsonObject = new ();
  258. jsonObject.Add("type", (int)type);
  259. jsonObject.Add("qty", 0);
  260. jsonObject.Add("itemType", "diamond");
  261. jsonObject.Add("hp", 8);
  262. jsonObject.Add("mp", 0);
  263. return JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
  264. }
  265. /// <summary>
  266. /// 是否结束
  267. /// </summary>
  268. /// <param name="self"></param>
  269. /// <returns></returns>
  270. public static bool IsGameOver(this Map self)
  271. {
  272. return self.IsGameOver;
  273. }
  274. /// <summary>
  275. /// 增加单位玩家本地数据
  276. /// </summary>
  277. /// <param name="self"></param>
  278. /// <param name="openId"></param>
  279. /// <param name="templateId"></param>
  280. /// <param name="objId"></param>
  281. /// <param name="likes"></param>
  282. /// <param name="name"></param>
  283. /// <param name="url"></param>
  284. public static Struct.UnitPlayerData AddUnitPlayer(this Map self, string openId, int templateId, int objId, long likes, string name, string url)
  285. {
  286. if (string.IsNullOrEmpty(openId) || templateId <= 0 || objId <= 0 || self.IsGameOver())
  287. {
  288. return null;
  289. }
  290. Struct.UnitPlayerData unitPlayerData = null;
  291. if (self.UnitPlayers.ContainsKey(openId))
  292. {
  293. unitPlayerData = self.UnitPlayers[openId];
  294. unitPlayerData.TemplateId = templateId;
  295. unitPlayerData.ObjId = objId;
  296. unitPlayerData.Name = name;
  297. unitPlayerData.Url = url;
  298. unitPlayerData.Map = self;
  299. self.UnitPlayers[openId] = unitPlayerData;
  300. }
  301. else
  302. {
  303. unitPlayerData = new Struct.UnitPlayerData();
  304. unitPlayerData.OpenId = openId;
  305. unitPlayerData.TemplateId = templateId;
  306. unitPlayerData.ObjId = objId;
  307. unitPlayerData.Name = name;
  308. unitPlayerData.Url = url;
  309. unitPlayerData.Level = 1;
  310. unitPlayerData.Likes = 0;
  311. unitPlayerData.ReliveTime = 0;
  312. unitPlayerData.x = 0;
  313. unitPlayerData.y = 0;
  314. unitPlayerData.Map = self;
  315. self.UnitPlayers.TryAdd(openId, unitPlayerData);
  316. }
  317. return unitPlayerData;
  318. }
  319. /// <summary>
  320. /// 获取单位玩家
  321. /// </summary>
  322. /// <param name="self"></param>
  323. /// <param name="openId"></param>
  324. /// <returns></returns>
  325. public static Struct.UnitPlayerData GetUnitPlayerByOpenId(this Map self, string openId)
  326. {
  327. if (self.UnitPlayers.TryGetValue(openId, out Struct.UnitPlayerData unitPlayer))
  328. {
  329. return unitPlayer;
  330. }
  331. return null;
  332. }
  333. /// <summary>
  334. /// 获取单位玩家
  335. /// </summary>
  336. /// <param name="self"></param>
  337. /// <param name="objId"></param>
  338. /// <returns></returns>
  339. public static Struct.UnitPlayerData GetUnitPlayerByObjId(this Map self, int objId)
  340. {
  341. return self.UnitPlayers.Values.FirstOrDefault(unitPlayer => unitPlayer != null && unitPlayer.ObjId == objId);
  342. }
  343. /// <summary>
  344. /// 玩家增加贡献值
  345. /// </summary>
  346. /// <param name="self"></param>
  347. /// <param name="openId"></param>
  348. /// <param name="addValue"></param>
  349. public static void AddContributeValue(this Map self, string openId, long addValue)
  350. {
  351. Struct.UnitPlayerData unitPlayerData = self.GetUnitPlayerByOpenId(openId);
  352. if (unitPlayerData == null)
  353. {
  354. return;
  355. }
  356. if (self.IsGameOver())
  357. {
  358. return;
  359. }
  360. long value = unitPlayerData.ContributeValue + addValue;
  361. if (value >= long.MaxValue)
  362. {
  363. value = long.MaxValue;
  364. }
  365. unitPlayerData.ContributeValue = value;
  366. }
  367. /// <summary>
  368. /// 获取抖音直播接口调用凭证
  369. /// </summary>
  370. /// <param name="self"></param>
  371. /// <returns></returns>
  372. public static string GetDouyinAccessToken(this Map self)
  373. {
  374. return self.DomainScene().GetComponent<GameDouyinComponent>().AccessToken;
  375. }
  376. /// <summary>
  377. /// Token 是否为空
  378. /// </summary>
  379. /// <param name="self"></param>
  380. /// <returns></returns>
  381. public static bool TokenIsNull(this Map self)
  382. {
  383. return self.DomainScene().GetComponent<GameDouyinComponent>().TokenIsNull;
  384. }
  385. /// <summary>
  386. /// 获得当前战场(当前塔位置),随机位置
  387. /// </summary>
  388. /// <param name="self"></param>
  389. /// <returns></returns>
  390. public static Vector2 GetRandomPlayerPos(this Map self)
  391. {
  392. Vector2[] TowerPos = { new Vector2() { X = 103, Y = 197 }, new Vector2() { X = 190, Y = 133 }, new Vector2() { X = 104, Y = 69 } };
  393. int index = self.CurBattleIndex;
  394. Vector2 tower = TowerPos[index];
  395. Random rand = new Random();
  396. float r;
  397. double ang;
  398. if (index == 0)
  399. {
  400. r = (float)Math.Sqrt(rand.Next(2500)) + 10;
  401. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 78f / 180f + Math.PI;
  402. }
  403. else if (index == 1)
  404. {
  405. r = (float)Math.Sqrt(rand.Next(3136)) + 10;
  406. ang = rand.Next(20) / 180.0f * Math.PI + Math.PI * 76f / 180f;
  407. }
  408. else
  409. {
  410. r = (float)Math.Sqrt(rand.Next(2500)) + 10;
  411. ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 150f / 180f;
  412. }
  413. return new Vector2(tower.X + (float)(r * Math.Cos(ang)), tower.Y - (float)(r * Math.Sin(ang)));
  414. }
  415. public static void TransferUnitsToNewTower(this Map self)
  416. {
  417. foreach(Struct.UnitPlayerData player in self.UnitPlayers.Values)
  418. {
  419. Vector2 pos = self.GetRandomPlayerPos();
  420. self.GetXmdsManager().transferUnit(self.Id.ToString(), player.ObjId, pos.X, pos.Y);
  421. }
  422. Log.Debug($"transfer unit: {self.UnitPlayers.Count}");
  423. }
  424. }
  425. }