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.Info($"销毁场景贡献榜组件"); } } public class MapRankComponentUpdateSystem: UpdateSystem { protected override void Update(MapRankComponent self) { // 未死亡的单位玩家,每秒增加贡献值 self.AddContributeValue(); // 每隔3秒推送排名 long now = TimeHelper.ClientNow(); if (now >= self.NextPushTime) { // 排序并推送客户端 self.Sort(); // 设置下一次推送时间戳 self.NextPushTime = now + 3000; } } } /// /// 增加贡献值数据 /// /// private static void AddContributeValue(this MapRankComponent self) { Map map = self.GetParent(); foreach (Struct.UnitPlayerData unitPlayer in map.UnitPlayers.Values) { if (unitPlayer.DeadState == 1) { continue; } int value = unitPlayer.Level switch { 1 => 1, 2 => 2, 3 => 3, _ => 0 }; map.AddContributeValue(unitPlayer.OpenId, value); } } /// /// 排序 /// /// private static void Sort(this MapRankComponent self) { Map map = self.GetParent(); List list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList(); if (list is { Count: > 0 }) { list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue)); } int ranking = 1; List infoListProto = new List(); foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 3)) { RankInfo info = new RankInfo(); info.Name = unitPlayerData.Name; // info.Url = unitPlayerData.Url; info.Value = unitPlayerData.ContributeValue; info.Ranking = ranking; infoListProto.Add(info); ranking += 1; } bool isEqual = self.IsEqual(infoListProto); if (isEqual) { return; } // 推送客户端 if (map.Player != null) { MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto }); // 记录一下推送列表 self.LastInfoListProto = 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; } /// /// 游戏结束 /// /// public static void OnGameOver(this MapRankComponent self) { Map map = self.GetParent(); List list = map.UnitPlayers.Values.Where(unitPlayer => unitPlayer != null).ToList(); if (list is { Count: > 0 }) { list.Sort((r1, r2) => r2.ContributeValue.CompareTo(r1.ContributeValue)); } int ranking = 1; List infoListProto = new List(); foreach (Struct.UnitPlayerData unitPlayerData in list.TakeWhile(unitPlayerData => ranking <= 50)) { RankInfo info = new RankInfo(); info.Name = unitPlayerData.Name; // info.Url = unitPlayerData.Url; info.Value = unitPlayerData.ContributeValue; info.Ranking = ranking; infoListProto.Add(info); ranking += 1; } // 推送客户端 if (map.Player != null) { MessageHelper.SendToClient(map.Player, new G2C_RankNotify() { InfoList = infoListProto }); } } } }