You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

1 month ago
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<int, ExchangeCodeGroupCache> m_saveCache;
public static int m_saveCount = 100;
public bool m_isTick = false;
public long m_lastTickMs = 0;
public bool m_isSaving = false;
// codeId + <groupId, groupCodeObj>
public Dictionary<int, Dictionary<int, ExchangeCodeGroupCache>> m_saveManger;
public ExchangeCodeManager()
{
//m_saveCache = new Dictionary<int, ExchangeCodeGroupCache>();
m_saveManger = new Dictionary<int, Dictionary<int, ExchangeCodeGroupCache>>();
}
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<int, ExchangeCodeGroupCache>();
return true;
}
public void SetIsTick(bool isTick)
{
m_isTick = isTick;
}
public bool CreateExchangeCodeGroup(int groupId, Dictionary<int, string> 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<SSExchangeCodeRecordSaveDbReq> parser = new StructMessageParser<SSExchangeCodeRecordSaveDbReq>();
packet.Parser = parser;
ref SSExchangeCodeRecordSaveDbReq req = ref packet.GetMessage<SSExchangeCodeRecordSaveDbReq>();
req.Data.CopyFrom(ref retReq.Data);
req.ReqId = retReq.ReqId;
uint serverId = PlayerOpServerUtils.GetPacketSender().GetPlayerOpServerIDServerID();
PlayerOpMsgHandler.AddMessageTaskDistributor(serverId, packet);
}
}
}