using System; using System.Collections.Generic; using System.Linq; namespace ET.Server { [FriendOf(typeof (MapRankComponent))] [FriendOfAttribute(typeof (Map))] public static class MapRankComponentSystem { public class MapRankComponentAwakeSystem: AwakeSystem { /// /// 场景贡献榜组件创建 /// /// protected override void Awake(MapRankComponent self) { Log.Info($"创建场景贡献榜组件"); } } public class MapRankComponentDestroySystem: DestroySystem { /// /// 场景贡献榜组件销毁 /// /// protected override void Destroy(MapRankComponent self) { Log.Debug($"销毁场景贡献榜组件"); } } /// /// 更新排行榜数据 /// /// /// /// public static void UpdateRank(this MapRankComponent self, List dataList, Map map) { foreach (Struct.BattleReports data in dataList) { bool isExist = false; foreach (Struct.BattleReports br in self.RankList.Where(br => data.ID == br.ID)) { isExist = true; br.TotalDamage = data.TotalDamage; } if (!isExist) { self.RankList.Add(data); } } self.Sort(map); } /// /// 排序 /// /// /// private static void Sort(this MapRankComponent self, Map map) { List list = new List(); self.RankList.ForEach(br => list.Add(br)); if (list is { Count: > 0 }) { list.Sort((r1, r2) => r2.TotalDamage.CompareTo(r1.TotalDamage)); } int ranking = 1; List infoListProto = new List(); foreach (Struct.BattleReports battleReports in list.Where(battleReports => ranking <= 3)) { // 客户端消息 RankInfo info = new RankInfo(); info.Name = battleReports.Name; info.Ranking = ranking; infoListProto.Add(info); ranking += 1; } bool isEqual = self.IsEqual(infoListProto); if (isEqual) { return; } self.LastInfoListProto = infoListProto; // 推送客户端 if (map.Player != null) { MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto }); } } private static bool IsEqual(this MapRankComponent self, List infoList) { if (self.LastInfoListProto.Count != infoList.Count) { return false; } for (int i = 0; i < infoList.Count; i++) { RankInfo info1 = infoList[i]; RankInfo info2 = self.LastInfoListProto[i]; if (info1 != null && info2 != null && (!info1.Name.Trim().Equals(info2.Name.Trim()) || info1.Ranking != info2.Ranking)) { return false; } } return true; } } }