MapRankComponentSystem.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // 未死亡的单位玩家,每秒增加贡献值
  29. self.AddContributeValue();
  30. // 每隔3秒推送排名
  31. long now = TimeHelper.ClientNow();
  32. if (now >= self.NextPushTime)
  33. {
  34. // 排序并推送客户端
  35. self.Sort();
  36. // 设置下一次推送时间戳
  37. self.NextPushTime = now + 3000;
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// 增加贡献值数据
  43. /// </summary>
  44. /// <param name="self"></param>
  45. private static void AddContributeValue(this MapRankComponent self)
  46. {
  47. Map map = self.GetParent<Map>();
  48. foreach (Struct.UnitPlayerData unitPlayer in map.UnitPlayers.Values)
  49. {
  50. if (unitPlayer.DeadState == 1)
  51. {
  52. continue;
  53. }
  54. int value = unitPlayer.Level switch
  55. {
  56. 1 => 1,
  57. 2 => 2,
  58. 3 => 3,
  59. _ => 0
  60. };
  61. map.AddContributeValue(unitPlayer.OpenId, value);
  62. }
  63. }
  64. /// <summary>
  65. /// 排序
  66. /// </summary>
  67. /// <param name="self"></param>
  68. private static void Sort(this MapRankComponent self)
  69. {
  70. Map map = self.GetParent<Map>();
  71. List<Struct.UnitPlayerData> list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList();
  72. if (list is { Count: > 0 })
  73. {
  74. list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue));
  75. }
  76. int ranking = 1;
  77. List<RankInfo> infoListProto = new List<RankInfo>();
  78. foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 3))
  79. {
  80. RankInfo info = new RankInfo();
  81. info.Name = unitPlayerData.Name;
  82. // info.Url = unitPlayerData.Url;
  83. info.Value = unitPlayerData.ContributeValue;
  84. info.Ranking = ranking;
  85. infoListProto.Add(info);
  86. ranking += 1;
  87. }
  88. bool isEqual = self.IsEqual(infoListProto);
  89. if (isEqual)
  90. {
  91. return;
  92. }
  93. // 推送客户端
  94. if (map.Player != null)
  95. {
  96. MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto });
  97. // 记录一下推送列表
  98. self.LastInfoListProto = infoListProto;
  99. }
  100. }
  101. private static bool IsEqual(this MapRankComponent self, List<RankInfo> infoList)
  102. {
  103. if (self.LastInfoListProto.Count != infoList.Count)
  104. {
  105. return false;
  106. }
  107. for (int i = 0; i < infoList.Count; i++)
  108. {
  109. RankInfo info1 = infoList[i];
  110. RankInfo info2 = self.LastInfoListProto[i];
  111. if (info1 != null && info2 != null && (!info1.Name.Trim().Equals(info2.Name.Trim()) || info1.Ranking != info2.Ranking))
  112. {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// 游戏结束
  120. /// </summary>
  121. /// <param name="self"></param>
  122. public static void OnGameOver(this MapRankComponent self)
  123. {
  124. Map map = self.GetParent<Map>();
  125. List<Struct.UnitPlayerData> list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList();
  126. if (list is { Count: > 0 })
  127. {
  128. list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue));
  129. }
  130. int ranking = 1;
  131. List<RankInfo> infoListProto = new List<RankInfo>();
  132. foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 50))
  133. {
  134. RankInfo info = new RankInfo();
  135. info.Name = unitPlayerData.Name;
  136. // info.Url = unitPlayerData.Url;
  137. info.Value = unitPlayerData.ContributeValue;
  138. info.Ranking = ranking;
  139. infoListProto.Add(info);
  140. ranking += 1;
  141. }
  142. // 推送客户端
  143. if (map.Player != null)
  144. {
  145. MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto });
  146. }
  147. }
  148. }
  149. }