using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Sog; using Sog.Service; using ProtoCSStruct; using System.Text.RegularExpressions; namespace PlayerOp { public class ExchangeCodeManager { // m_groupId - codeDict //public Dictionary m_saveCache; public static int m_saveCount = 100; public bool m_isTick = false; public long m_lastTickMs = 0; public bool m_isSaving = false; // codeId + public Dictionary> m_saveManger; public ExchangeCodeManager() { //m_saveCache = new Dictionary(); m_saveManger = new Dictionary>(); } public bool SetCodeId(int codeId) { if(m_saveManger.ContainsKey(codeId)) { TraceLog.Error("ExchangeCodeManager.SetCodeId m_saveManger contain key codeId:{0}", codeId); return false; } m_saveManger[codeId] = new Dictionary(); return true; } public void SetIsTick(bool isTick) { m_isTick = isTick; } public bool CreateExchangeCodeGroup(int groupId, Dictionary exchangeCodeGroup, string exchangeCodePre, int codeId) { if(!m_saveManger.ContainsKey(codeId)) { TraceLog.Error("ExchangeCodeManager.CreateExchangeCodeGroup m_saveCache - codeId:{0} is not existed!", codeId); return false; } var saveCache = m_saveManger[codeId]; if (saveCache.ContainsKey(groupId)) { TraceLog.Error("ExchangeCodeManager.CreateExchangeCodeGroup m_saveCache - groupId:{0} is existed!", groupId); return false; } ExchangeCodeGroupCache codeGroup = new ExchangeCodeGroupCache(groupId, exchangeCodePre, exchangeCodeGroup, codeId); saveCache.Add(groupId, codeGroup); return true; } public void OnTickSaveCode(long nowMs) { // 每秒 tick 1次 if(!m_isTick) { return; } if (nowMs < (m_lastTickMs + 500)) { return; } m_lastTickMs = nowMs; SSExchangeCodeRecordSaveDbReq retReq = new SSExchangeCodeRecordSaveDbReq(); foreach (var manger in m_saveManger) { var saveCache = manger.Value; foreach (var item in saveCache) { if (!item.Value.IsNeedSave()) { continue; } retReq = item.Value.SaveData(m_saveCount); if (retReq.Data.Count <= 0) { TraceLog.Debug("ExchangeCodeManager.OnTickSaveCode - SSExchangeCodeSaveDbReq retReq.Data.count = 0"); continue; }else { break; } } } if(retReq.Data.Count == 0) { TraceLog.Debug("ExchangeCodeManager.OnTickSaveCode - SSExchangeCodeSaveDbReq retReq.Data.count = 0"); m_isTick = false; return; } StructPacket packet = new StructPacket(); packet.Header.Type = (int)SSGameMsgID.ExchangeRecordSaveDbReq; StructMessageParser parser = new StructMessageParser(); packet.Parser = parser; ref SSExchangeCodeRecordSaveDbReq req = ref packet.GetMessage(); req.Data.CopyFrom(ref retReq.Data); req.ReqId = retReq.ReqId; uint serverId = PlayerOpServerUtils.GetPacketSender().GetPlayerOpServerIDServerID(); PlayerOpMsgHandler.AddMessageTaskDistributor(serverId, packet); } } }