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.
 
 
 
 
 
 

177 lines
6.4 KiB

using System;
using System.Diagnostics;
using Sog;
using ProtoCSStruct;
using Sog.IO;
namespace GameDB
{
public abstract class GameDBOperator
{
private long zipLenOld;
private long zipLenNew;
private long zipCpuTicks;
private int zipCount;
private long zipTimeMs => swMsFreq > 0 ? zipCpuTicks / swMsFreq : 0;
private long unzipLenOld;
private long unzipLenNew;
private long unzipCpuTicks;
private int unzipCount;
private long unzipTimeMs => swMsFreq > 0 ? unzipCpuTicks / swMsFreq : 0;
private Stopwatch sw;
private long swMsFreq;
public GameDBOperator()
{
sw = new Stopwatch();
sw.Start();
// 转换成每毫秒的tick频率
swMsFreq = Stopwatch.Frequency / 1000;
}
public byte[] ZipData(byte[] unzipData)
{
if (unzipData == null || unzipData.Length == 0)
{
return unzipData;
}
long oldTicks = sw.ElapsedTicks;
byte[] zipData = ZipUtils.CompressBytes(unzipData);
long costTicks = sw.ElapsedTicks - oldTicks;
ZipStatistic(unzipData.Length, zipData.Length, costTicks);
return zipData;
}
public byte[] Protobuf2ZipData<T>(ref T message)
where T : struct, IStructMessage<T>
{
byte[] unDataByte = StructMessageParseUtils.ToByteArray(ref message);
return ZipData(unDataByte);
}
public byte[] UnzipData(byte[] zipData)
{
if (zipData == null || zipData.Length == 0)
{
return zipData;
}
long oldTicks = sw.ElapsedTicks;
byte[] unzipData = ZipUtils.DecompressBytes(zipData);
long costTicks = sw.ElapsedTicks - oldTicks;
UnzipStatistic(zipData.Length, unzipData.Length, costTicks);
return unzipData;
}
public void ZipData2Protobuf<T>(ref T message, byte[] zipData)
where T : struct, IStructMessage<T>
{
byte[] dataByte = UnzipData(zipData);
StructMessageParseUtils.ParseFrom(ref message, dataByte);
}
public byte[] UnzipData(byte[] zipData, int start, int length)
{
if (zipData == null || zipData.Length == 0 || length == 0)
{
return zipData;
}
long oldTicks = sw.ElapsedTicks;
byte[] unzipData = ZipUtils.DecompressBytes(zipData, start, length);
long costTicks = sw.ElapsedTicks - oldTicks;
UnzipStatistic(length, unzipData.Length, costTicks);
return unzipData;
}
public void ZipStatistic(int oldLen, int newLen, long costTicks)
{
zipLenOld += oldLen;
zipLenNew += newLen;
zipCpuTicks += costTicks;
zipCount++;
TraceLog.Trace("GameDBOperator.ZipStatistic len {0} -> {1} cpuTicks {2}, total Len {3} -> {4} timeMs {5} count {6}"
, oldLen, newLen, costTicks, zipLenOld, zipLenNew, zipTimeMs, zipCount);
}
public void UnzipStatistic(int oldLen, int newLen, long costTicks)
{
unzipLenOld += oldLen;
unzipLenNew += newLen;
unzipCpuTicks += costTicks;
unzipCount++;
TraceLog.Trace("GameDBOperator.UnzipStatistic len {0} -> {1} cpuTicks {2}, total Len {3} -> {4} timeMs {5} count {6}"
, oldLen, newLen, costTicks, unzipLenOld, unzipLenNew, unzipTimeMs, unzipCount);
}
//销毁的时候置空
public abstract void Dispose();
public abstract void KeepAlive();
public abstract int QueryFreezeTime(long uid, out long freezeTime, out long freezeType, out string freezeReasonStr, out long enableMsgTime);
public abstract bool QueryRole(long uid, ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData, ref DropRateData dropRateData,out string firebasePushId, out int realmId);
public abstract bool QueryOfflineRole(long uid, ref DBRoleBase roleBase, ref DBRoleData roleData, out int realmId);
public abstract bool InsertRole(int reamId, ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData,ref DropRateData dropRateData);
public abstract bool UpdateRole(ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData,ref DropRateData dropRateData);
// 测试删除角色, 游戏逻辑不要调用
public abstract void DeleteRoleForDBTest(long uid);
public abstract bool UpdateRoleFirebasePushId(long uid, string firebasePushId);
public abstract string QueryRoleFirebaseId(long uid);
public abstract bool SetFreezeTime(long uid, long freezeTime, int freezeReasonType, string freezeResonStr);
public abstract bool AddBattleReplayData(long battleId, int createDay, ref CSBattleReplay replay);
public abstract bool QueryBattleReplay(long battleId, ref SSQueryBattleReplayDbRes res);
public abstract bool DeleteBattleReplay(long battleId);
public abstract bool QueryGameDbRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rank);
public abstract bool ReplaceGameRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rankData);
public abstract bool ReplaceActRank(ref SSSaveActRankDataReq req);
public abstract int QueryActRank(ref SSQueryActRankDbReq req, ref SSQueryActRankDbRes res);
public abstract bool SearchRoleByName(long fromUid, string condition, ref SSPlayerSearchRoleDBByNameRes res);
public abstract bool QueryFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list,
ref DBFriendOpData opData);
public abstract bool UpdateFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list,
ref DBFriendOpData op, bool binsert);
public abstract bool SetEnableMsgTime(long uid, long enableMsgTime);
public abstract CSErrCode OnQueryWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res);
public abstract CSErrCode OnUpdateWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res);
}
}