MapRankComponentSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// <summary>
  13. /// 场景贡献榜组件创建
  14. /// </summary>
  15. /// <param name="self"></param>
  16. protected override void Awake(MapRankComponent self)
  17. {
  18. Log.Info($"创建场景贡献榜组件");
  19. }
  20. }
  21. public class MapRankComponentDestroySystem: DestroySystem<MapRankComponent>
  22. {
  23. /// <summary>
  24. /// 场景贡献榜组件销毁
  25. /// </summary>
  26. /// <param name="self"></param>
  27. protected override void Destroy(MapRankComponent self)
  28. {
  29. Log.Debug($"销毁场景贡献榜组件");
  30. }
  31. }
  32. /// <summary>
  33. /// 更新排行榜数据
  34. /// </summary>
  35. /// <param name="self"></param>
  36. /// <param name="dataList"></param>
  37. /// <param name="map"></param>
  38. public static void UpdateRank(this MapRankComponent self, List<Struct.BattleReports> dataList, Map map)
  39. {
  40. foreach (Struct.BattleReports data in dataList)
  41. {
  42. bool isExist = false;
  43. foreach (Struct.BattleReports br in self.RankList.Where(br => data.ID == br.ID))
  44. {
  45. isExist = true;
  46. br.TotalDamage = data.TotalDamage;
  47. }
  48. if (!isExist)
  49. {
  50. self.RankList.Add(data);
  51. }
  52. }
  53. self.Sort(map);
  54. }
  55. /// <summary>
  56. /// 排序
  57. /// </summary>
  58. /// <param name="self"></param>
  59. /// <param name="map"></param>
  60. private static void Sort(this MapRankComponent self, Map map)
  61. {
  62. List<Struct.BattleReports> list = new List<Struct.BattleReports>();
  63. self.RankList.ForEach(br => list.Add(br));
  64. if (list is { Count: > 0 })
  65. {
  66. list.Sort((r1, r2) => r2.TotalDamage.CompareTo(r1.TotalDamage));
  67. }
  68. int ranking = 1;
  69. List<RankInfo> infoListProto = new List<RankInfo>();
  70. foreach (Struct.BattleReports battleReports in list.Where(battleReports => ranking <= 3))
  71. {
  72. // 客户端消息
  73. RankInfo info = new RankInfo();
  74. info.Name = battleReports.Name;
  75. info.Ranking = ranking;
  76. infoListProto.Add(info);
  77. ranking += 1;
  78. }
  79. bool isEqual = self.IsEqual(infoListProto);
  80. if (isEqual)
  81. {
  82. return;
  83. }
  84. self.LastInfoListProto = infoListProto;
  85. // 推送客户端
  86. if (map.Player != null)
  87. {
  88. MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto });
  89. }
  90. }
  91. private static bool IsEqual(this MapRankComponent self, List<RankInfo> infoList)
  92. {
  93. if (self.LastInfoListProto.Count != infoList.Count)
  94. {
  95. return false;
  96. }
  97. for (int i = 0; i < infoList.Count; i++)
  98. {
  99. RankInfo info1 = infoList[i];
  100. RankInfo info2 = self.LastInfoListProto[i];
  101. if (info1 != null && info2 != null && (!info1.Name.Trim().Equals(info2.Name.Trim()) || info1.Ranking != info2.Ranking))
  102. {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. }
  109. }