MapRankComponentSystem.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ET.Server
  5. {
  6. [FriendOf(typeof (MapRankComponent))]
  7. [FriendOfAttribute(typeof (Map))]
  8. public static class MapRankComponentSystem
  9. {
  10. public class MapRankComponentAwakeSystem: AwakeSystem<MapRankComponent>
  11. {
  12. protected override void Awake(MapRankComponent self)
  13. {
  14. Log.Info($"创建场景贡献榜组件");
  15. }
  16. }
  17. public class MapRankComponentDestroySystem: DestroySystem<MapRankComponent>
  18. {
  19. protected override void Destroy(MapRankComponent self)
  20. {
  21. Log.Info($"销毁场景贡献榜组件");
  22. }
  23. }
  24. public class MapRankComponentUpdateSystem: UpdateSystem<MapRankComponent>
  25. {
  26. protected override void Update(MapRankComponent self)
  27. {
  28. Map map = self.GetParent<Map>();
  29. if (map.IsGameOver())
  30. {
  31. return;
  32. }
  33. // 未死亡的单位玩家,每秒增加贡献值
  34. self.AddContributeValue();
  35. // 每隔3秒推送排名
  36. long now = TimeHelper.ClientNow();
  37. if (now >= self.NextPushTime)
  38. {
  39. // 排序并推送客户端
  40. self.Sort();
  41. // 设置下一次推送时间戳
  42. self.NextPushTime = now + 3000;
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// 增加贡献值数据
  48. /// </summary>
  49. /// <param name="self"></param>
  50. private static void AddContributeValue(this MapRankComponent self)
  51. {
  52. Map map = self.GetParent<Map>();
  53. foreach (Struct.UnitPlayerData unitPlayer in map.UnitPlayers.Values)
  54. {
  55. if (unitPlayer.DeadState == 1)
  56. {
  57. continue;
  58. }
  59. int value = unitPlayer.Level switch
  60. {
  61. 1 => 1,
  62. 2 => 2,
  63. 3 => 3,
  64. _ => 0
  65. };
  66. map.AddContributeValue(unitPlayer.OpenId, value);
  67. }
  68. }
  69. /// <summary>
  70. /// 排序
  71. /// </summary>
  72. /// <param name="self"></param>
  73. private static void Sort(this MapRankComponent self)
  74. {
  75. Map map = self.GetParent<Map>();
  76. List<Struct.UnitPlayerData> list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList();
  77. if (list is { Count: > 0 })
  78. {
  79. list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue));
  80. }
  81. int ranking = 1;
  82. List<RankInfo> infoListProto = new List<RankInfo>();
  83. foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 3))
  84. {
  85. RankInfo info = new RankInfo();
  86. info.Name = unitPlayerData.Name;
  87. info.Url = unitPlayerData.Url;
  88. // info.Value = unitPlayerData.ContributeValue;
  89. info.Ranking = ranking;
  90. infoListProto.Add(info);
  91. ranking += 1;
  92. }
  93. bool isEqual = self.IsEqual(infoListProto);
  94. if (isEqual)
  95. {
  96. return;
  97. }
  98. // 推送客户端
  99. if (map.Player != null)
  100. {
  101. MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto });
  102. // 记录一下推送列表
  103. self.LastInfoListProto = infoListProto;
  104. }
  105. }
  106. private static bool IsEqual(this MapRankComponent self, List<RankInfo> infoList)
  107. {
  108. if (self.LastInfoListProto.Count != infoList.Count)
  109. {
  110. return false;
  111. }
  112. for (int i = 0; i < infoList.Count; i++)
  113. {
  114. RankInfo info1 = infoList[i];
  115. RankInfo info2 = self.LastInfoListProto[i];
  116. if (info1 != null && info2 != null && (!info1.Name.Trim().Equals(info2.Name.Trim()) || info1.Ranking != info2.Ranking))
  117. {
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. /// <summary>
  124. /// 游戏结束
  125. /// </summary>
  126. /// <param name="self"></param>
  127. public static void OnGameOver(this MapRankComponent self)
  128. {
  129. Map map = self.GetParent<Map>();
  130. List<Struct.UnitPlayerData> list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList();
  131. if (list is { Count: > 0 })
  132. {
  133. list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue));
  134. }
  135. int ranking = 1;
  136. List<RankInfo> infoListProto = new List<RankInfo>();
  137. foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 50))
  138. {
  139. RankInfo info = new RankInfo();
  140. info.Name = unitPlayerData.Name;
  141. info.Url = unitPlayerData.Url;
  142. info.Value = unitPlayerData.ContributeValue;
  143. info.Ranking = ranking;
  144. infoListProto.Add(info);
  145. ranking += 1;
  146. }
  147. // 推送客户端
  148. if (map.Player != null)
  149. {
  150. MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto });
  151. }
  152. }
  153. }
  154. }