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.
 
 
 
 
 
 

667 lines
26 KiB

using Sog;
using System;
using ProtoCSStruct;
using System.Linq;
using StackExchange.Redis;
namespace GameDB
{
public partial class RedisGameDBOperator : GameDBOperator
{
public RedisDB m_redisDB = null;
public RedisGameDBOperator(string db, string ip, string user, string password)
{
m_redisDB = new RedisDB(db, ip, password);
}
public override void Dispose()
{
m_redisDB.Dispose();
m_redisDB = null;
}
public override void KeepAlive()
{
var latency = m_redisDB.Ping();
TraceLog.Trace("RedisGameDBOperator.KeepAlive exec at {0} latency {1}ms", DateTime.Now, latency.Milliseconds);
}
//================================================================================ tbuser
private string GetUser_StringKey(long uid)
{
return m_redisDB.CreateKey("string:tbuser", uid.ToString());
}
public override bool QueryRole(long uid, ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData, ref DropRateData dropRateData, out string firebasePushId, out int realmId)
{
firebasePushId = "";
realmId = 0;
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryRole uid {0} no data", uid);
return false;
}
roleBase.Uid = record.Uid;
roleBase.Nick.SetString(record.Nick);
roleBase.Gender = record.Gender;
roleBase.Icon.SetString(record.Icon);
roleBase.Exp = record.Exp;
roleBase.Level = record.Level;
roleBase.VipLevel = record.VipLevel;
roleBase.Chip = record.Chip;
roleBase.Diamond = record.Diamond;
roleBase.LastLoginTime = record.LastLoginTime;
roleBase.CreateTime = record.CreateTime;
roleBase.OnlineTime = record.OnlineTime;
roleBase.VipExp = record.VipExp;
roleBase.Lang.SetString(record.Lang);
roleBase.HeroExpPool = record.HeroExpPool;
roleBase.RoleCreateComplete = record.RoleCreateComplete;
roleBase.Power = record.Power;
ZipData2Protobuf(ref roleData, record.GameData);
ZipData2Protobuf(ref unimportanceData, record.UnimportanceData);
realmId = record.Realm;
firebasePushId = record.FirebasePushId;
TraceLog.Trace("RedisGameDBOperator.QueryRole query success uid {0} level {1} LoginCount {2}"
, uid, roleBase.Level, roleData.LoginCount);
return true;
}
public override bool QueryOfflineRole(long uid, ref DBRoleBase roleBase, ref DBRoleData roleData, out int realmId)
{
realmId = 0;
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryOfflineRole no record. uid {0}", uid);
return false;
}
roleBase.Uid = record.Uid;
roleBase.Nick.SetString(record.Nick);
roleBase.Gender = record.Gender;
roleBase.Icon.SetString(record.Icon);
roleBase.Exp = record.Exp;
roleBase.Level = record.Level;
roleBase.VipLevel = record.VipLevel;
roleBase.Chip = record.Chip;
roleBase.LastLoginTime = record.LastLoginTime;
roleBase.CreateTime = record.CreateTime;
roleBase.OnlineTime = record.OnlineTime;
roleBase.VipExp = record.VipExp;
roleBase.Lang.SetString(record.Lang);
roleBase.HeroExpPool = record.HeroExpPool;
roleBase.RoleCreateComplete = record.RoleCreateComplete;
roleBase.Power = record.Power;
ZipData2Protobuf(ref roleData, record.GameData);
realmId = record.Realm;
TraceLog.Trace("RedisGameDBOperator.QueryRole query success uid {0} level {1} LoginCount {2}"
, uid, roleBase.Level, roleData.LoginCount);
return true;
}
public override int QueryFreezeTime(long uid, out long freezeTime, out long freezeType, out string freezeReasonStr, out long enableMsgTime)
{
freezeTime = 0;
freezeType = 0;
freezeReasonStr = "";
enableMsgTime = 0;
var record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryFreezeTime uid {0} no data", uid);
return 0;
}
freezeTime = record.FreezeTime;
freezeType = record.freezeReason;
freezeReasonStr = record.freezeReasonStr;
enableMsgTime = record.enableMsgTime;
TraceLog.Trace("RedisGameDBOperator.QueryFreezeTime uid {0} freeze time {1} freezeReason {2} freezeReasonStr {3} enableMsgTime {4}", uid, record.FreezeTime, record.freezeReason, record.freezeReasonStr, record.enableMsgTime);
return 0;
}
public override string QueryRoleFirebaseId(long uid)
{
var record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryRoleFirebaseId uid {0} no data", uid);
return null;
}
TraceLog.Trace("RedisGameDBOperator.QueryRoleFirebaseId uid {0} FirebaseId {1}", uid, record.FirebasePushId);
return record.FirebasePushId;
}
public override bool InsertRole(int realmId, ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData,ref DropRateData dropRateData)
{
var record = new tbuser
{
Uid = roleBase.Uid,
Realm = realmId,
Nick = roleBase.Nick.GetString(),
Gender = roleBase.Gender,
Icon = roleBase.Icon.GetString(),
Exp = roleBase.Exp,
Level = roleBase.Level,
VipLevel = roleBase.VipLevel,
VipExp = roleBase.VipExp,
Chip = roleBase.Chip,
LastLoginTime = roleBase.LastLoginTime,
CreateTime = roleBase.CreateTime,
OnlineTime = roleBase.OnlineTime,
GameData = Protobuf2ZipData(ref roleData),
HeroExpPool = roleBase.HeroExpPool,
Lang = roleBase.Lang.GetString(),
Power = roleBase.Power,
RoleCreateComplete = roleBase.RoleCreateComplete,
UnimportanceData = Protobuf2ZipData(ref unimportanceData),
};
if (!m_redisDB.StringSet(GetUser_StringKey(record.Uid), record))
{
TraceLog.Error("RedisGameDBOperator.InsertRole fail, uid {0}", roleBase.Uid);
return false;
}
TraceLog.Trace("RedisGameDBOperator.InsertRole succ, uid {0}", roleBase.Uid);
return true;
}
public override bool UpdateRole(ref DBRoleBase roleBase, ref DBRoleData roleData,
ref DBRoleUnimportanceData unimportanceData,ref DropRateData dbRoleData)
{
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(roleBase.Uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.UpdateRole uid {0} no data", roleBase.Uid);
return false;
}
record.Nick = roleBase.Nick.GetString();
record.Gender = roleBase.Gender;
record.Icon = roleBase.Icon.GetString();
record.Exp = roleBase.Exp;
record.Level = roleBase.Level;
record.VipLevel = roleBase.VipLevel;
record.VipExp = roleBase.VipExp;
record.Chip = roleBase.Chip;
record.Diamond = roleBase.Diamond;
record.LastLoginTime = roleBase.LastLoginTime;
record.CreateTime = roleBase.CreateTime;
record.OnlineTime = roleBase.OnlineTime;
record.GameData = Protobuf2ZipData(ref roleData);
record.HeroExpPool = roleBase.HeroExpPool;
record.Lang = roleBase.Lang.GetString();
record.Power = roleBase.Power;
record.RoleCreateComplete = roleBase.RoleCreateComplete;
record.UnimportanceData = Protobuf2ZipData(ref unimportanceData);
if (!m_redisDB.StringSet(GetUser_StringKey(roleBase.Uid), record))
{
TraceLog.Error("RedisGameDBOperator.UpdateRole fail. uid {0}", roleBase.Uid);
return false;
}
TraceLog.Trace("RedisGameDBOperator.UpdateRole success. uid {0}", roleBase.Uid);
return true;
}
public override bool UpdateRoleFirebasePushId(long uid, string firebasePushId)
{
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.UpdateRoleFirebasePushId uid {0} no data", uid);
return false;
}
record.FirebasePushId = firebasePushId;
if (!m_redisDB.StringSet(GetUser_StringKey(uid), record))
{
TraceLog.Error("RedisGameDBOperator.UpdateRoleFirebasePushId fail. uid {0}", uid);
return false;
}
TraceLog.Trace("RedisGameDBOperator.UpdateRoleFirebasePushId success. uid {0}", uid);
return true;
}
public override bool SetFreezeTime(long uid, long freezeTime, int freezeReasonType, string freezeResonStr)
{
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.SetFreezeTime uid {0} no role data", uid);
return false;
}
record.FreezeTime = freezeTime;
record.freezeReason = freezeReasonType;
record.freezeReasonStr = freezeResonStr;
if (!m_redisDB.StringSet<tbuser>(GetUser_StringKey(uid), record))
{
TraceLog.Error("RedisGameDBOperator.SetFreezeTime fail uid {0}", uid);
return false;
}
TraceLog.Trace("RedisGameDBOperator.SetFreezeTime success. uid {0} to freezetime {1} freezeReasonType {2} freezeResonStr {3}", uid, freezeTime, freezeReasonType, freezeResonStr);
return true;
}
public override bool SetEnableMsgTime(long uid, long enableMsgTime)
{
tbuser record = m_redisDB.StringGet<tbuser>(GetUser_StringKey(uid));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.SetEnableMsgTime uid {0} no role data", uid);
return false;
}
record.enableMsgTime = enableMsgTime;
bool ret = m_redisDB.StringSet<tbuser>(GetUser_StringKey(uid), record);
if (!ret)
{
TraceLog.Error("RedisGameDBOperator.SetEnableMsgTime uid {0} update db fail", uid);
return false;
}
TraceLog.Trace("RedisGameDBOperator.SetEnableMsgTime uid {0} to freezetime {1} success", uid, enableMsgTime);
return true;
}
public override void DeleteRoleForDBTest(long uid)
{
if (m_redisDB.KeyDelete(GetUser_StringKey(uid)))
TraceLog.Trace("RedisGameDBOperator.DeleteRoleForDBTest success. uid {0}", uid);
else
TraceLog.Error("RedisGameDBOperator.DeleteRoleForDBTest fail. uid {0}", uid);
}
public override bool SearchRoleByName(long fromUid, string condition, ref SSPlayerSearchRoleDBByNameRes res)
{
throw new NotImplementedException();
}
//================================================================================ tb_mainland_recording
//================================================================================ tbarena
private string GetArena_StringKey(int rankId, int realmId, int arenaGroupId, int groupId, int seasonNum)
{
return m_redisDB.CreateKey("string:arena", rankId.ToString(), realmId.ToString(), arenaGroupId.ToString(), groupId.ToString(), seasonNum.ToString());
}
//================================================================================ tbarena_defence
private string GetArenaDefence_StringKey(long uid, int arenaType)
{
return m_redisDB.CreateKey("string:arenadefence", uid.ToString(), arenaType.ToString());
}
//================================================================================ tbbattle_replay
private string GetBattleReplay_StringKey(long battleId)
{
return m_redisDB.CreateKey("string:battlereplay", battleId.ToString());
}
public override bool AddBattleReplayData(long battleId, int createDay, ref CSBattleReplay replay)
{
var record = new tbbattle_replay
{
battleId = battleId,
mainlandId = replay.BattleInfoAll.MainlandId,
battleVersion = replay.BattleVersion,
createDay = createDay,
data = Protobuf2ZipData(ref replay),
};
if (!m_redisDB.StringSet<tbbattle_replay>(GetBattleReplay_StringKey(battleId), record))
{
TraceLog.Error("RedisGameDBOperator.AddBattleReplayData fail, battleId {0}", battleId);
return false;
}
TraceLog.Trace("RedisGameDBOperator.AddBattleReplayData succ, battleId {0}", battleId);
return true;
}
public override bool QueryBattleReplay(long battleId, ref SSQueryBattleReplayDbRes res)
{
var record = m_redisDB.StringGet<tbbattle_replay>(GetBattleReplay_StringKey(battleId));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryBattleReplay battleId {0} no data", battleId);
return false;
}
TraceLog.Trace("RedisGameDBOperator.QueryArenaDefenceData battleId {0} succ", battleId);
ZipData2Protobuf(ref res.Replay, record.data);
return true;
}
public override bool DeleteBattleReplay(long battleId)
{
var ret = m_redisDB.KeyDelete(GetBattleReplay_StringKey(battleId));
if (!ret)
{
TraceLog.Error("RedisGameDBOperator.QueryBattleReplay battleId {0} no data", battleId);
return false;
}
TraceLog.Trace("RedisGameDBOperator.QueryBattleReplay battleId {0}", battleId);
return true;
}
//================================================================================ tbarena_battle_record
private string GetArenaBattleRecord_StringKey(long uid, int arenaType)
{
return m_redisDB.CreateKey("string:arenabattlerecord", uid.ToString(), arenaType.ToString());
}
//================================================================================ tb_arena_season
private string GetArenaSeason_StringKey(int arenaType, int arenaRealmId)
{
return m_redisDB.CreateKey("string:arenaseason", arenaType.ToString(), arenaRealmId.ToString());
}
//================================================================================ tb_arena_trials
private string GetArenaTrials_StringKey(int arenaRealmId, int arenaGroupId, int groupId)
{
return m_redisDB.CreateKey("string:arenatrials", arenaRealmId.ToString(), arenaGroupId.ToString(), groupId.ToString());
}
//================================================================================ tbrank
private string GetRank_StringKey(int rankId, int realmId, int groupId, int beginTime)
{
return m_redisDB.CreateKey("string:rank", rankId.ToString(), realmId.ToString(), groupId.ToString(), beginTime.ToString());
}
private string GetActRank_StringKey(int rankId, int realmId)
{
return m_redisDB.CreateKey("string:actRank", rankId.ToString(), realmId.ToString());
}
public override bool ReplaceGameRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rankData)
{
var record = new tbrank
{
rankId = rankId,
realmId = realmId,
groupId = groupId,
beginTime = beginTime,
rankData = Protobuf2ZipData(ref rankData),
};
if (!m_redisDB.StringSet<tbrank>(GetRank_StringKey(rankId, realmId, groupId, beginTime), record))
{
TraceLog.Error("RedisGameDBOperator.ReplaceGameRank fail, rankId {0} realmId {1} groupId {2}"
, rankId, realmId, groupId);
return false;
}
TraceLog.Trace("RedisGameDBOperator.ReplaceGameRank succ, rankId {0} realmId {1} groupId {2}"
, rankId, realmId, groupId);
return true;
}
public override bool QueryGameDbRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rank)
{
var record = m_redisDB.StringGet<tbrank>(GetRank_StringKey(rankId, realmId, groupId, beginTime));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryGameDbRank no data. rankId {0} realmId {1} groupId {2} beginTime {3}"
, rankId, realmId, groupId, beginTime);
return true;
}
TraceLog.Trace("RedisGameDBOperator.QueryGameDbRank success. rankId {0} realmId {1} groupId {2} beginTime {3}"
, rankId, realmId, groupId, beginTime);
ZipData2Protobuf(ref rank, record.rankData);
return true;
}
//活动排行版
public override bool ReplaceActRank(ref SSSaveActRankDataReq req)
{
var record = new tbactRank
{
actRankId = req.ActRankId,
realmId = req.RealmId,
endClear = req.EndClear,
beginTime = req.BeginTime,
endTime = req.EndTime,
rankData = Protobuf2ZipData(ref req.RankData),
};
if (!m_redisDB.StringSet<tbactRank>(GetActRank_StringKey(req.ActRankId, req.RealmId), record))
{
TraceLog.Error("RedisGameDBOperator.ReplaceActRank fail, rankId {0} realmId {1}"
, req.ActRankId, req.RealmId);
return false;
}
TraceLog.Trace("RedisGameDBOperator.ReplaceGameRank succ, rankId {0} realmId {1}"
, req.ActRankId, req.RealmId);
return true;
}
public override int QueryActRank(ref SSQueryActRankDbReq req, ref SSQueryActRankDbRes res)
{
var record = m_redisDB.StringGet<tbactRank>(GetActRank_StringKey(req.ActRankId, req.RealmId));
if (record == null)
{
TraceLog.Trace("RedisGameDBOperator.QueryActRank no data. rankId {0} realmId {1}"
, req.ActRankId, req.RealmId);
return 1;
}
TraceLog.Trace("RedisGameDBOperator.QueryActRank success. rankId {0} realmId {1}"
, req.ActRankId, req.RealmId);
res.ActRankId = record.actRankId;
res.RealmId = record.realmId;
res.EndClear = record.endClear;
res.BeginTime = record.beginTime;
res.EndTime = record.endTime;
ZipData2Protobuf(ref res.RankData, record.rankData);
return 0;
}
//================================================================================ tbunion
private string m_UnionPrefix = "string:union:realmid";
private string GetUnion_Keys_StringKey(int realmId)
{
return m_redisDB.CreateKey(m_UnionPrefix, realmId.ToString(), "*");
}
//================================================================================ tbunion_role
private string m_UnionRolePrefix = "string:unionrole:realmid";
private string GetUnionRole_Keys_StringKey(int realmId)
{
return m_redisDB.CreateKey(m_UnionRolePrefix, realmId.ToString(), "*");
}
private string GetUnionRole_StringKey(int realmId, long uid)
{
return m_redisDB.CreateKey(m_UnionRolePrefix, realmId.ToString(), "uid", uid.ToString());
}
//================================================================================ tbunion_hunt
private string m_UnionHuntPrefix = "string:unionhunt:realmid";
private string GetUnionHunt_Keys_StringKey(int realmId)
{
return m_redisDB.CreateKey(m_UnionHuntPrefix, realmId.ToString(), "*");
}
//================================================================================ tbhundred_tower
//================================================================================ tbrace_horse
private string GetRaceHorse_HashKey(long uid)
{
return m_redisDB.CreateKey("string:racehorse", uid.ToString());
}
//================================================================================ tbworld_global_data_db
private string GetWorldGlobalDataDb_StrngKey(WorldGlobalDataType dataType, int globalId, int dataVer)
{
return m_redisDB.CreateKey("string:worldglobaldatadb", dataType.ToString(), globalId.ToString(), dataVer.ToString());
}
public override CSErrCode OnQueryWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res)
{
var data = m_redisDB.StringGet<Byte[]>(GetWorldGlobalDataDb_StrngKey(req.DataType, req.GlobalId, req.DataVer));
if (data == null)
{
TraceLog.Trace("RedisGameDBOperator.OnQueryWorldGlobalDataDBOp no data, dataType {0} globalId {1} dataVer {2} data length {3}",
req.DataType, req.GlobalId, req.DataVer, data.Count());
return CSErrCode.None;
}
for (int i = 0; i < data.Count() && i < res.Data.GetMaxCount(); i++)
{
res.Data.Add(data[i]);
}
TraceLog.Trace("RedisGameDBOperator.OnQueryWorldGlobalDataDBOp succ, dataType {0} globalId {1} dataVer {2} data length {3}",
req.DataType, req.GlobalId, req.DataVer, data.Count());
return CSErrCode.None;
}
public override CSErrCode OnUpdateWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res)
{
byte[] buffer = new byte[req.Data.Count];
for (int i = 0; i < req.Data.Count; i++)
{
buffer[i] = req.Data[i];
}
if (!m_redisDB.StringSet<Byte[]>(GetWorldGlobalDataDb_StrngKey(req.DataType, req.GlobalId, req.DataVer), buffer))
{
TraceLog.Error("RedisGameDBOperator.OnUpdateWorldGlobalDataDBOp error, dataType {0} globalId {1} dataVer {2} data length {3}",
req.DataType, req.GlobalId, req.DataVer, req.Data.Count);
return CSErrCode.SysFailure;
}
TraceLog.Trace("RedisGameDBOperator.OnUpdateWorldGlobalDataDBOp success, dataType {0} globalId {1} dataVer {2} data length {3}",
req.DataType, req.GlobalId, req.DataVer, req.Data.Count);
res.Data.CopyFrom(ref req.Data);
return CSErrCode.None;
}
//================================================================================ 玩家全局领地数据 tbworld_home
private string GetWorldHome_StrngKey(long uid)
{
return m_redisDB.CreateKey("string:worldhome", uid.ToString());
}
//================================================================================ 机器人全局领地数据 tbworld_robot_home
private string GetWorldRobotHome_StrngKey(long worldId, long robotUid)
{
return m_redisDB.CreateKey("string:worldrobothome", worldId.ToString(), robotUid.ToString());
}
//================================================================================ tbfriend
private string GetFriend_HashKey(long uid)
{
return m_redisDB.CreateKey("hash:friend", uid.ToString());
}
public override bool QueryFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list, ref DBFriendOpData opData)
{
var selfBytes = m_redisDB.HashGet<Byte[]>(GetFriend_HashKey(uid), "self");
var listBytes = m_redisDB.HashGet<Byte[]>(GetFriend_HashKey(uid), "list");
var opBytes = m_redisDB.HashGet<Byte[]>(GetFriend_HashKey(uid), "op");
if (selfBytes != null)
{
ZipData2Protobuf(ref self, selfBytes);
}
if (listBytes != null)
{
ZipData2Protobuf(ref list, listBytes);
}
if (opBytes != null)
{
ZipData2Protobuf(ref opData, opBytes);
}
TraceLog.Trace("RedisGameDBOperator.QueryFriendList query success uid {0} ", uid);
return true;
}
public override bool UpdateFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list, ref DBFriendOpData op, bool binsert)
{
var selfBytes = Protobuf2ZipData(ref self);
var listBytes = Protobuf2ZipData(ref list);
var opBytes = Protobuf2ZipData(ref op);
var setEntrys = new HashEntry[3];
setEntrys[0] = new HashEntry("self", selfBytes);
setEntrys[1] = new HashEntry("list", listBytes);
setEntrys[2] = new HashEntry("op", opBytes);
m_redisDB.HashSetPart(GetFriend_HashKey(uid), setEntrys);
TraceLog.Trace("RedisGameDBOperator.UpdateFriendList uid {0} to table success", uid);
return true;
}
private string GetTradeUser_HashKey(long uid)
{
return m_redisDB.CreateKey("string:trade", uid.ToString());
}
private string m_RaceSeason = "string:RaceSign:Season";
}
}