MapSystem.cs 17 KB

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