123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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<MapRankComponent>
- {
- protected override void Awake(MapRankComponent self)
- {
- Log.Info($"创建场景贡献榜组件");
- }
- }
- public class MapRankComponentDestroySystem: DestroySystem<MapRankComponent>
- {
- protected override void Destroy(MapRankComponent self)
- {
- Log.Info($"销毁场景贡献榜组件");
- }
- }
- public class MapRankComponentUpdateSystem: UpdateSystem<MapRankComponent>
- {
- protected override void Update(MapRankComponent self)
- {
- // 未死亡的单位玩家,每秒增加贡献值
- self.AddContributeValue();
- // 每隔3秒推送排名
- long now = TimeHelper.ClientNow();
- if (now >= self.NextPushTime)
- {
- // 排序并推送客户端
- self.Sort();
- // 设置下一次推送时间戳
- self.NextPushTime = now + 3000;
- }
- }
- }
- /// <summary>
- /// 增加贡献值数据
- /// </summary>
- /// <param name="self"></param>
- private static void AddContributeValue(this MapRankComponent self)
- {
- Map map = self.GetParent<Map>();
- 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);
- }
- }
- /// <summary>
- /// 排序
- /// </summary>
- /// <param name="self"></param>
- private static void Sort(this MapRankComponent self)
- {
- Map map = self.GetParent<Map>();
- List<Struct.UnitPlayerData> 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<RankInfo> infoListProto = new List<RankInfo>();
- 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<RankInfo> 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;
- }
- /// <summary>
- /// 游戏结束
- /// </summary>
- /// <param name="self"></param>
- public static void OnGameOver(this MapRankComponent self)
- {
- Map map = self.GetParent<Map>();
- List<Struct.UnitPlayerData> 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<RankInfo> infoListProto = new List<RankInfo>();
- 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 });
- }
- }
- }
- }
|