using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace Game { public class RoleStatChgOp { private PlayerOnGame m_player; private RepeatedDBIDValue_32 m_chgIDValueDaily; private RepeatedDBIDValue_32 m_chgIDValueTotal; public struct RoleStatResult { public long dailyValue; public long totalValue; } private RoleStatResult m_empty; // 返回最近一次变化的统计数据, 提高查询效率 private RoleStatResult m_latestResult; public RoleStatChgOp(PlayerOnGame player) { m_player = player; m_empty = new RoleStatResult {dailyValue = -1, totalValue = -1}; m_latestResult = new RoleStatResult { dailyValue = -1, totalValue = -1 }; } /// /// 改变daily stat,缺省改变total /// /// /// /// public ref RoleStatResult AddIDDaily(int id, long value, bool addTotal = true) { if(value == 0) { return ref m_empty; } if(!StatisticsOp.IDIsValid(id)) { m_player.Error("RoleStatChgOp.AddIDDaily invalid id {0} uid {1}", id, m_player.UserID); return ref m_empty; } long newValue = StatisticsOp.AddIDDaily_DontCall(m_player, id, value); SetChgIDValueDaily(id, newValue); m_latestResult.dailyValue = newValue; m_latestResult.totalValue = -1; if (addTotal) { newValue = StatisticsOp.AddIDTotal_DontCall(m_player, id, value); SetChgIDValueTotal(id, newValue); m_latestResult.dailyValue = newValue; } return ref m_latestResult; } /// /// 改变total stat /// /// /// public ref RoleStatResult AddIDTotal(int id, long value) { if (value == 0) { return ref m_empty; } if (!StatisticsOp.IDIsValid(id)) { m_player.Error("RoleStatChgOp.AddIDTotal invalid id {0} uid {1}", id, m_player.UserID); return ref m_empty; } long newValue = StatisticsOp.AddIDTotal_DontCall(m_player, id, value); SetChgIDValueTotal(id, newValue); m_latestResult.dailyValue = -1; m_latestResult.totalValue = newValue; return ref m_latestResult; } /// /// 改变daily stat /// /// /// /// public void SetIDDaily(int id, long value) { if (!StatisticsOp.IDIsValid(id)) { m_player.Error("RoleStatChgOp.SetIDDaily invalid id {0} uid {1}", id, m_player.UserID); return; } if (StatisticsOp.SetIDDaily_DontCall(m_player, id, value)) { SetChgIDValueDaily(id,value); } } /// /// 改变total stat /// /// /// /// public void SetIDTotal(int id, long value) { if (!StatisticsOp.IDIsValid(id)) { m_player.Error("RoleStatChgOp.SetIDTotal invalid id {0} uid {1}", id, m_player.UserID); return; } if (StatisticsOp.SetIDTotal_DontCall(m_player, id, value)) { SetChgIDValueTotal(id, value); } } private void SetChgIDValueDaily(int id, long value) { for(int i=0; i< m_chgIDValueDaily.Count; i++) { if(m_chgIDValueDaily[i].Id == id) { m_chgIDValueDaily[i].Value = value; return; } } DBIDValue newitem = new DBIDValue(); newitem.Id = id; newitem.Value = value; m_chgIDValueDaily.Add(ref newitem); } private void SetChgIDValueTotal(int id, long value) { for (int i = 0; i < m_chgIDValueTotal.Count; i++) { if (m_chgIDValueTotal[i].Id == id) { m_chgIDValueTotal[i].Value = value; return; } } DBIDValue newitem = new DBIDValue(); newitem.Id = id; newitem.Value = value; m_chgIDValueTotal.Add(ref newitem); } public void NotifyClient(bool isNewDay = false) { if(m_chgIDValueDaily.Count == 0 && m_chgIDValueTotal.Count == 0 && isNewDay == false) { m_player.Error("RoleStatChgOp.NotifyClient no any id changed uid {0}",m_player.UserID); return; } CSStatisticsChg chgNotify = new CSStatisticsChg(); chgNotify.IsNewDay = isNewDay ? 1 : 0; //新的一天的时候,发给客户端newday之前发个ping,对一下时间 if(isNewDay) { long now = GameServerUtils.GetTimeMs(); CSPing ping = new CSPing(); ping.ServerTime = now; m_player.SendToClient((int)CSGameMsgID.Ping, ref ping); } chgNotify.Daily = m_chgIDValueDaily; chgNotify.Total = m_chgIDValueTotal; m_player.Debug("RoleStatChgOp.NotifyClient changed to uid {0}, daily {1} total {2}" , m_player.UserID, m_chgIDValueDaily.Count, m_chgIDValueTotal.Count); m_player.SendToClient((int)CSGameMsgID.StatisticsChg, ref chgNotify); } public void NotifyClient(int id) { if (!StatisticsOp.IDIsValid(id)) { m_player.Error("RoleStatChgOp.AddIDDaily invalid id {0} uid {1}", id, m_player.UserID); return; } var chgNotify = new CSStatisticsChg(); for(int i=0; i< m_player.RoleData.StatData.Daily.Count; i++) { ref var idvalue = ref m_player.RoleData.StatData.Daily[i]; if (idvalue.Id == id) { chgNotify.Daily.Add(ref idvalue); break; } } for (int i = 0; i < m_player.RoleData.StatData.Total.Count; i++) { ref var idvalue = ref m_player.RoleData.StatData.Total[i]; if (idvalue.Id == id) { chgNotify.Total.Add(ref idvalue); break; } } m_player.SendToClient((int)CSGameMsgID.StatisticsChg, ref chgNotify); } } }