using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace Game { public static class StatisticsOp { public static bool IDIsValid(int id) { if(id > (int)CSRoleStatisticsID.None && id < (int)CSRoleStatisticsID.MaxID) { return true; } return false; } /// /// 排行榜奖励统计在老服可能存在已经领取完情况,做下特殊处理 /// /// public static void OnPlayerOnline(PlayerOnGame player) { long value = 0; //GetIDTotal(player, (int) CSRoleStatisticsID.RankAward); if (value > 0) //已经有值的就不管了 { return; } int count = player.RoleData.RankData.HaveAwardRankList.Count; if (count == 0) { return; } RoleStatChgOp statChgOp = new RoleStatChgOp(player); //statChgOp.AddIDDaily((int) CSRoleStatisticsID.RankAward, count); statChgOp.NotifyClient(); } //数据库数据加载后需要初始化 internal static void OnDBDataLoaded(PlayerOnGame player) { player.Trace("StatisticsOp.OnDBDataLoaded uid {0} begin", player.UserID); ref DBStatisticsData statData = ref player.RoleData.StatData; //补齐字段,也许新加了 for(int i=1; i < (int)CSRoleStatisticsID.MaxID; i++) { TryAddID(ref statData.Daily, i); TryAddID(ref statData.Total, i); } player.MakeDirty(); player.Trace("StatisticsOp.OnDBDataLoaded uid {0} print stat:", player.UserID); player.Trace("StatisticsOp.OnDBDataLoaded daily stat:{0}", statData.Daily.ToString()); player.Trace("StatisticsOp.OnDBDataLoaded total stat:{0}", statData.Total.ToString()); } private static void TryAddID(ref RepeatedDBIDValue_64 idvalues, int id) { //先判断是否存在 for(int i=0; i < idvalues.Count; i++) { if(idvalues[i].Id == id) { return; } } DBIDValue newIDValue = new DBIDValue(); newIDValue.Id = id; newIDValue.Value = 0; idvalues.Add(ref newIDValue); TraceLog.Trace("StatisticsOp.TryAddID add id {0} to player", id); } internal static void TickDailyReset(PlayerOnGame player, bool notifyClient) { ref DBStatisticsData statData = ref player.RoleData.StatData; //判断时间,是否需要重置 long now = GameServerUtils.GetTimeSecond(); if (AppTime.IsSameDay(now, player.RoleData.StatData.LastDailyTime)) { return; } bool needWeeklyRefresh = false; if(AppTime.IsSameWeek127(now,statData.Weekly.LastWeeklyTime) == false) { player.Trace("StatisticsOp.TickDailyReset uid {0}, not same week lasttime {1} now {2}, reset week stat" , player.UserID, statData.Weekly.LastWeeklyTime, now); statData.Weekly.LastWeeklyTime = now; //不同周了,数据重置 statData.Weekly.WinChip = 0; statData.Weekly.ShareFBSignAddDiamond = 0; statData.Weekly.ReceiveFriendGift = 0; needWeeklyRefresh = true; //派发周刷新事件 //GameServerUtils.GetEventHandlerMgr().TriggerNewWeeklyStart(player, notifyClient); } player.Trace("StatisticsOp.TickDailyReset uid {0}, not same day lasttime {1} now {2}, reset daily stat" , player.UserID, statData.LastDailyTime, now); //这个调用在数据重置之前调用,有些今天的数据会被用到 GameServerUtils.GetEventHandlerMgr().TriggerNewDayStart(player, notifyClient); //为了使新手BP添加任务在每日刷新之后,将每周刷新放在日刷新之后 if (needWeeklyRefresh) { //派发周刷新事件 //GameServerUtils.GetEventHandlerMgr().TriggerNewWeeklyStart(player, notifyClient); } statData.LastDailyTime = now; RoleStatChgOp statChgOp = new RoleStatChgOp(player); //这个要在重置前取值 //int lastDayBrokenCount = (int)GetIDDaily(player, (int)CSRoleStatisticsID.BrokeGrantsCount); for(int i=0; i /// /// /// /// /// /// 成功返回true,没变化返回false internal static bool SetIDDaily_DontCall(PlayerOnGame player, int id, long value) { if (!IDIsValid(id)) { player.Error("StatisticsOp.SetIDDaily_DontCall invalid id {0} uid {1}" , id, player.UserID); return false; } ref DBStatisticsData statData = ref player.RoleData.StatData; for (int i = 0; i < statData.Daily.Count; i++) { ref var idvalue = ref statData.Daily[i]; if (idvalue.Id == id) { if (idvalue.Value == value) { return false; } else { long oldValue = idvalue.Value; long chgValue = value - oldValue; idvalue.Value = value; player.Trace("StatisticsOp.SetIDDaily_DontCall uid {0} id {1} new value {2}" , player.UserID, id, value); player.Trace("StatisticsOp.SetIDDaily_DontCall uid {0}, TriggerDailyStatisticsChanged event" , player.UserID); GameServerUtils.GetEventHandlerMgr().TriggerDailyStatisticsChanged(player, id, value, chgValue); player.MakeDirty(); return true; } } } player.Error("StatisticsOp.SetIDDaily_DontCall can not find id {0} uid {1}", id, player.UserID); return false; } /// /// 不要直接调用 /// internal static long AddIDDaily_DontCall(PlayerOnGame player, int id, long value) { long oldValue = GetIDDaily(player, id); long newValue = oldValue + value; SetIDDaily_DontCall(player, id, newValue); return newValue; } //////////////////////////////////////////////////////////////////////// public static long GetIDTotal(PlayerOnGame player, int id) { if (!IDIsValid(id)) { player.Error("StatisticsOp.GetIDTotal invalid id {0} uid {1}", id, player.UserID); return 0; } ref DBStatisticsData statData = ref player.RoleData.StatData; for (int i = 0; i < statData.Total.Count; i++) { ref var idvalue = ref statData.Total[i]; if (idvalue.Id == id) { return idvalue.Value; } } player.Error("StatisticsOp.GetIDTotal can not find id {0} uid {1}", id, player.UserID); return 0; } public static void OnlySetIDTotal(PlayerOnGame player, int id, long value) { SetIDTotal_DontCall(player, id, value, false); } /// /// 不要直接调用 /// internal static bool SetIDTotal_DontCall(PlayerOnGame player, int id, long value, bool triggerEvent = true) { if (!IDIsValid(id)) { player.Error("StatisticsOp.SetIDTotal_DontCall invalid id {0} uid {1}", id, player.UserID); return false; } try { ref DBStatisticsData statData = ref player.RoleData.StatData; for (int i = 0; i < statData.Total.Count; i++) { ref var idvalue = ref statData.Total[i]; if (idvalue.Id == id) { if (idvalue.Value == value) { return false; } long oldValue = idvalue.Value; long chgValue = value - oldValue; idvalue.Value = value; player.TraceDetail("StatisticsOp.SetIDTotal_DontCall uid {0} statId {1} chg {2} new {3}" , player.UserID, id, chgValue, value); if(triggerEvent) GameServerUtils.GetEventHandlerMgr().TriggerTotalStatisticsChanged(player, id, value, chgValue); player.MakeDirty(); return true; } } } catch (Exception e) { TraceLog.Exception(e); } player.Error("StatisticsOp.SetIDTotal_DontCall can not find id {0} uid {1}", id, player.UserID); return false; } /// /// 不要直接调用 /// internal static long AddIDTotal_DontCall(PlayerOnGame player, int id, long value) { long oldValue = GetIDTotal(player, id); long newValue = oldValue + value; SetIDTotal_DontCall(player, id, newValue); return newValue; } } }