MapSystem.cs 17 KB

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