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.
 
 
 
 
 
 

1081 lines
48 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Sog;
using ProtoCSStruct;
using MySql.Data.MySqlClient;
using Sog.IO;
namespace GameDB
{
public class MysqlGameDBOperator : GameDBOperator
{
private const int RoleDataBuffLengthMax = 500 * 1024;
private MySqlDB m_mySqlDb;
//销毁的时候置空
public override void Dispose()
{
m_mySqlDb.Dispose();
m_mySqlDb = null;
}
public MysqlGameDBOperator(string db, string ip, string user, string password)
{
m_mySqlDb = new MySqlDB(db, ip, user, password);
}
public override void KeepAlive()
{
string strSql = "select realm from tbuser_1 where uid=0";
//TraceLog.Trace("MysqlGameDBOperator.KeepAlive exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.KeepAlive no record in account table, return reader is null!");
return;
}
//记住一定要Close
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.KeepAlive exec at {0}", DateTime.Now);
}
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;
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.QueryFreezeTime uid {0} table {1}", uid, tableName);
string strSql = string.Format("select freezeTime,freezeReasonStr,freezeReason,enableMsgTime from {0} where uid = {1}", tableName, uid);
TraceLog.Trace("MysqlGameDBOperator.QueryFreezeTime exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryFreezeTime no record in table {0}, return reader is null!", tableName);
return -1;
}
// 没记录也是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryFreezeTime no record in table {0}", tableName);
reader.Close();
return 0;
}
try
{
reader.Read();
freezeTime = reader.GetInt64("freezeTime");
freezeReasonStr = reader.GetString("freezeReasonStr");
freezeType = reader.GetInt32("freezeReason");
enableMsgTime = reader.GetInt32("enableMsgTime");
return 0;
}
catch (System.Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return -1;
}
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;
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.QueryRole uid {0} table {1}", uid, tableName);
string strSql = string.Format("select * from {0} where uid={1}", tableName, uid);
TraceLog.Trace("MysqlGameDBOperator.QueryRole exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryRole no record in table {0}, return reader is null!", tableName);
return false;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryRole no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return false;
}
try
{
reader.Read();
realmId = reader.GetInt32("realm");
roleBase.Uid = reader.GetInt64("uid");
roleBase.Nick.SetString(reader.GetString("nick"));
roleBase.Gender = reader.GetInt32("gender");
roleBase.Icon.SetString(reader.GetString("icon"));
roleBase.Exp = reader.GetInt32("exp");
roleBase.Level = reader.GetInt32("level");
roleBase.VipLevel = reader.GetInt32("vipLevel");
roleBase.Chip = reader.GetInt64("chip");
roleBase.Diamond = reader.GetInt64("diamond");
roleBase.LastLoginTime = reader.GetInt64("lastLoginTime");
roleBase.CreateTime = reader.GetInt64("createTime");
roleBase.OnlineTime = reader.GetInt64("onlineTime");
firebasePushId = reader.GetString("firebasePushId");
roleBase.VipExp = reader.GetInt32("vipExp");
roleBase.Lang.SetString(reader.GetString("lang"));
roleBase.HeroExpPool = reader.GetInt32("heroExpPool");
roleBase.RoleCreateComplete = reader.GetInt32("roleCreateComplete");
roleBase.Power = reader.GetInt64("power");
//请封装
int gameDataIndex = reader.GetOrdinal("gameData");
byte[] buffer = new byte[RoleDataBuffLengthMax];
long gameDataLength = reader.GetBytes(gameDataIndex, 0, buffer, 0, buffer.Length);
//这里之所以要new byte[gameDataLength]并不是多此一举,而是为了兼容gameDataLength==0的情况
byte[] gameDataByte = new byte[gameDataLength];
Buffer.BlockCopy(buffer, 0, gameDataByte, 0, (int)gameDataLength);
gameDataByte = UnzipData(gameDataByte);
StructMessageParseUtils.ParseFrom(ref roleData, gameDataByte);
//请封装
int unimportanceDataIndex = reader.GetOrdinal("unimportanceData");
long unimportanceDataLength = reader.GetBytes(unimportanceDataIndex, 0, buffer, 0, buffer.Length);
byte[] unimportanceDataByte = new byte[unimportanceDataLength];
Buffer.BlockCopy(buffer, 0, unimportanceDataByte, 0, (int)unimportanceDataLength);
unimportanceDataByte = UnzipData(unimportanceDataByte);
StructMessageParseUtils.ParseFrom(ref unimportanceData, unimportanceDataByte);
int dropDataIndex = reader.GetOrdinal("dropData");
long dropDataLength = reader.GetBytes(dropDataIndex, 0, buffer, 0, buffer.Length);
byte[] dropDataByte = new byte[dropDataLength];
Buffer.BlockCopy(buffer, 0, dropDataByte, 0, (int)dropDataLength);
dropDataByte = UnzipData(dropDataByte);
StructMessageParseUtils.ParseFrom(ref dropRateData, dropDataByte);
TraceLog.Trace("MysqlGameDBOperator.QueryRole query success uid {0} level {1},roleData.LoginCount {2} realm {3}", uid, roleBase.Level, roleData.LoginCount, realmId);
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool QueryOfflineRole(long uid, ref DBRoleBase roleBase, ref DBRoleData roleData, out int realmId)
{
realmId = 0;
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.QueryOfflineRole uid {0} table {1}", uid, tableName);
string strSql = string.Format("select * from {0} where uid={1}", tableName, uid);
TraceLog.Trace("MysqlGameDBOperator.QueryOfflineRole exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryOfflineRole no record in table {0}, return reader is null!", tableName);
return false;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryOfflineRole no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return false;
}
try
{
reader.Read();
realmId = reader.GetInt32("realm");
roleBase.Uid = reader.GetInt64("uid");
roleBase.Nick.SetString(reader.GetString("nick"));
roleBase.Gender = reader.GetInt32("gender");
roleBase.Icon.SetString(reader.GetString("icon"));
roleBase.Exp = reader.GetInt32("exp");
roleBase.Level = reader.GetInt32("level");
roleBase.VipLevel = reader.GetInt32("vipLevel");
roleBase.Chip = reader.GetInt64("chip");
roleBase.LastLoginTime = reader.GetInt64("lastLoginTime");
roleBase.CreateTime = reader.GetInt64("createTime");
roleBase.OnlineTime = reader.GetInt64("onlineTime");
roleBase.VipExp = reader.GetInt32("vipExp");
roleBase.Lang.SetString(reader.GetString("lang"));
roleBase.HeroExpPool = reader.GetInt32("heroExpPool");
roleBase.RoleCreateComplete = reader.GetInt32("roleCreateComplete");
roleBase.Power = reader.GetInt64("power");
//请封装
int gameDataIndex = reader.GetOrdinal("gameData");
byte[] buffer = new byte[RoleDataBuffLengthMax];
long gameDataLength = reader.GetBytes(gameDataIndex, 0, buffer, 0, buffer.Length);
byte[] gameDataByte = new byte[gameDataLength];
Buffer.BlockCopy(buffer, 0, gameDataByte, 0, (int)gameDataLength);
gameDataByte = UnzipData(gameDataByte);
StructMessageParseUtils.ParseFrom(ref roleData, gameDataByte);
TraceLog.Trace("MysqlGameDBOperator.QueryOfflineRole query success uid {0} level {1} roleData.LoginCount {2} realm {3}", uid, roleBase.Level, roleData.LoginCount, realmId);
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool InsertRole(int reamId, ref DBRoleBase roleBase, ref DBRoleData roleData, ref DBRoleUnimportanceData unimportanceData,ref DropRateData dropRateData)
{
string tableName = GameDBServerUtils.GetUserTableName(roleBase.Uid);
TraceLog.Trace("MysqlGameDBOperator.InsertRole uid {0} to table {1}", roleBase.Uid, tableName);
string strSql = "insert into " + tableName + "(uid,realm,nick,gender,icon,exp,level,vipLevel,vipExp,chip,lastLoginTime,createTime,onlineTime,lang,heroExpPool,roleCreateComplete,power,gameData,unimportanceData,dropData)"
+ " values(?uid,?realm,?nick,?gender,?icon,?exp,?level,?vipLevel,?vipExp,?chip,?lastLoginTime,?createTime,?onlineTime,?lang,?heroExpPool,?roleCreateComplete,?power,?gameData,?unimportanceData,?dropData)";
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) { Value = roleBase.Uid };
MySqlParameter p_realm = new MySqlParameter("?realm", MySqlDbType.Int32) { Value = reamId };
MySqlParameter p_nick = new MySqlParameter("?nick", MySqlDbType.VarChar, 64) { Value = roleBase.Nick.GetString() };
MySqlParameter p_gender = new MySqlParameter("?gender", MySqlDbType.Int32) { Value = roleBase.Gender };
MySqlParameter p_icon = new MySqlParameter("?icon", MySqlDbType.VarChar, 200) { Value = roleBase.Icon.GetString() };
MySqlParameter p_exp = new MySqlParameter("?exp", MySqlDbType.Int32) { Value = roleBase.Exp };
MySqlParameter p_vipExp = new MySqlParameter("?vipExp", MySqlDbType.Int32) { Value = roleBase.VipExp };
MySqlParameter p_level = new MySqlParameter("?level", MySqlDbType.Int32) { Value = roleBase.Level };
MySqlParameter p_vipLevel = new MySqlParameter("?vipLevel", MySqlDbType.Int32) { Value = roleBase.VipLevel };
MySqlParameter p_chip = new MySqlParameter("?chip", MySqlDbType.Int64) { Value = roleBase.Chip };
//钻石托管了,这里不能更新
//MySqlParameter p_diamaond = new MySqlParameter("?diamond", MySqlDbType.Int64) { Value = roleBase.Diamond };
MySqlParameter p_lastLoginTime = new MySqlParameter("?lastLoginTime", MySqlDbType.Int64) { Value = roleBase.LastLoginTime };
MySqlParameter p_createTime = new MySqlParameter("?createTime", MySqlDbType.Int64) { Value = roleBase.CreateTime };
MySqlParameter p_onlineTime = new MySqlParameter("?onlineTime", MySqlDbType.Int64) { Value = roleBase.OnlineTime };
MySqlParameter p_lang = new MySqlParameter("?lang", MySqlDbType.VarChar, 16) { Value = roleBase.Lang.GetString() };
MySqlParameter p_heroExpPool = new MySqlParameter("?heroExpPool", MySqlDbType.Int32) { Value = roleBase.HeroExpPool };
MySqlParameter p_roleCreateComplete = new MySqlParameter("?roleCreateComplete", MySqlDbType.Int32) { Value = roleBase.RoleCreateComplete };
MySqlParameter p_power = new MySqlParameter("?power", MySqlDbType.Int64) { Value = roleBase.Power };
byte[] gameDataByte = StructMessageParseUtils.ToByteArray(ref roleData);
gameDataByte = ZipData(gameDataByte);
MySqlParameter p_gameData = new MySqlParameter("?gameData", MySqlDbType.MediumBlob) { Value = gameDataByte };
byte[] unimportanceDataByte = StructMessageParseUtils.ToByteArray(ref unimportanceData);
unimportanceDataByte = ZipData(unimportanceDataByte);
MySqlParameter p_unimportanceData = new MySqlParameter("?unimportanceData", MySqlDbType.MediumBlob) { Value = unimportanceDataByte };
byte[] dropRateDataByte = StructMessageParseUtils.ToByteArray(ref dropRateData);
dropRateDataByte = ZipData(dropRateDataByte);
MySqlParameter p_ropRateDataByte = new MySqlParameter("?dropData", MySqlDbType.MediumBlob) { Value = dropRateDataByte };
TraceLog.Trace("MysqlGameDBOperator.InsertRole exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql
, p_uid, p_realm, p_nick, p_gender, p_icon, p_exp,
p_level, p_vipLevel, p_vipExp, p_chip, p_lastLoginTime,
p_createTime, p_onlineTime, p_lang, p_heroExpPool,
p_roleCreateComplete, p_power, p_gameData, p_unimportanceData,p_ropRateDataByte);
if (reader == null)
{
TraceLog.Error("MysqlGameDBOperator.InsertRole no record in table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.InsertRole uid {0} to table {1} success", roleBase.Uid, tableName);
return true;
}
public override bool UpdateRole(ref DBRoleBase roleBase, ref DBRoleData roleData, ref DBRoleUnimportanceData unimportanceData,ref DropRateData dropRateData)
{
string tableName = GameDBServerUtils.GetUserTableName(roleBase.Uid);
TraceLog.Trace("MysqlGameDBOperator.UpdateRole uid {0} to table {1}", roleBase.Uid, tableName);
string strSql = "update " + tableName + " set nick=?nick,gender=?gender,icon=?icon,exp=?exp,level=?level,vipLevel=?vipLevel,vipExp=?vipExp,chip=?chip,diamond=?diamond,lastLoginTime=?lastLoginTime," +
"createTime=?createTime,onlineTime=?onlineTime,lang=?lang,heroExpPool=?heroExpPool,roleCreateComplete=?roleCreateComplete,power=?power," +
"gameData=?gameData,unimportanceData=?unimportanceData,dropData=?dropData where uid=?uid";
MySqlParameter p_nick = new MySqlParameter("?nick", MySqlDbType.VarChar, 64) { Value = roleBase.Nick.GetString() };
MySqlParameter p_gender = new MySqlParameter("?gender", MySqlDbType.Int32) { Value = roleBase.Gender };
MySqlParameter p_icon = new MySqlParameter("?icon", MySqlDbType.VarChar, 200) { Value = roleBase.Icon.GetString() };
MySqlParameter p_exp = new MySqlParameter("?exp", MySqlDbType.Int32) { Value = roleBase.Exp };
MySqlParameter p_vipExp = new MySqlParameter("?vipExp", MySqlDbType.Int32) { Value = roleBase.VipExp };
MySqlParameter p_level = new MySqlParameter("?level", MySqlDbType.Int32) { Value = roleBase.Level };
MySqlParameter p_vipLevel = new MySqlParameter("?vipLevel", MySqlDbType.Int32) { Value = roleBase.VipLevel };
MySqlParameter p_chip = new MySqlParameter("?chip", MySqlDbType.Int64) { Value = roleBase.Chip };
MySqlParameter p_diamaond = new MySqlParameter("?diamond", MySqlDbType.Int64) { Value = roleBase.Diamond };
MySqlParameter p_lastLoginTime = new MySqlParameter("?lastLoginTime", MySqlDbType.Int64) { Value = roleBase.LastLoginTime };
MySqlParameter p_createTime = new MySqlParameter("?createTime", MySqlDbType.Int64) { Value = roleBase.CreateTime };
MySqlParameter p_onlineTime = new MySqlParameter("?onlineTime", MySqlDbType.Int64) { Value = roleBase.OnlineTime };
MySqlParameter p_lang = new MySqlParameter("?lang", MySqlDbType.VarChar, 16) { Value = roleBase.Lang.GetString() };
MySqlParameter p_heroExpPool = new MySqlParameter("?heroExpPool", MySqlDbType.Int32) { Value = roleBase.HeroExpPool };
MySqlParameter p_roleCreateComplete = new MySqlParameter("?roleCreateComplete", MySqlDbType.Int32) { Value = roleBase.RoleCreateComplete };
MySqlParameter p_power = new MySqlParameter("?power", MySqlDbType.Int64) { Value = roleBase.Power };
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) { Value = roleBase.Uid };
byte[] gameDataByte = StructMessageParseUtils.ToByteArray(ref roleData);
gameDataByte = ZipData(gameDataByte);
MySqlParameter p_gameData = new MySqlParameter("?gameData", MySqlDbType.MediumBlob) { Value = gameDataByte };
byte[] unimportanceDataByte = StructMessageParseUtils.ToByteArray(ref unimportanceData);
unimportanceDataByte = ZipData(unimportanceDataByte);
MySqlParameter p_unimportanceData = new MySqlParameter("?unimportanceData", MySqlDbType.MediumBlob) { Value = unimportanceDataByte };
byte[] dbRoleDataBytes = StructMessageParseUtils.ToByteArray(ref dropRateData);
dbRoleDataBytes = ZipData(dbRoleDataBytes);
MySqlParameter dropData = new MySqlParameter("?dropData", MySqlDbType.MediumBlob) { Value = dbRoleDataBytes };
TraceLog.Trace("MysqlGameDBOperator.UpdateRole exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql
, p_uid, p_nick, p_gender, p_icon, p_exp,
p_level, p_vipLevel, p_vipExp, p_chip, p_diamaond,
p_lastLoginTime, p_createTime, p_onlineTime,
p_lang, p_heroExpPool, p_roleCreateComplete,
p_power, p_gameData, p_unimportanceData,dropData);
if (reader == null)
{
TraceLog.Error("MysqlGameDBOperator.UpdateRole no record in table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.UpdateRole uid {0} to table {1} success", roleBase.Uid, tableName);
return true;
}
// 删除角色信息, 仅供性能测试调用!!!
public override void DeleteRoleForDBTest(long uid)
{
string tableName = GameDBServerUtils.GetUserTableName(uid);
string strSql = $"delete from {tableName} where uid={uid};";
TraceLog.Trace("MysqlGameDBOperator.DeleteRoleForDBTest exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.DeleteRoleForDBTest no reader");
return;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.DeleteRoleForDBTest uid {0} succ", uid);
}
public override bool UpdateRoleFirebasePushId(long uid, string firebasePushId)
{
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.UpdateRoleFirebasePushId uid {0} to table {1} firebasePushId {2}"
, uid, tableName, firebasePushId);
string strSql = "update " + tableName + " set firebasePushId=?firebasePushId where uid=?uid";
MySqlParameter p_firbaseId = new MySqlParameter("?firebasePushId", MySqlDbType.VarChar, 256) { Value = firebasePushId };
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) { Value = uid };
TraceLog.Trace("MysqlGameDBOperator.UpdateRoleFirebasePushId exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_uid, p_firbaseId);
if (reader == null)
{
TraceLog.Error("MysqlGameDBOperator.UpdateRoleFirebasePushId no record in table {0} uid {1}, return reader is null!"
, tableName, uid);
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.UpdateRoleFirebasePushId uid {0} to table {1} firebasePushId {2} success"
, uid, tableName, firebasePushId);
return true;
}
public override string QueryRoleFirebaseId(long uid)
{
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.QueryRoleFirebaseId uid {0} table {1}", uid, tableName);
string strSql = string.Format("select firebasePushId from {0} where uid={1}", tableName, uid);
TraceLog.Trace("MysqlGameDBOperator.QueryRoleFirebaseId exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryRoleFirebaseId no record in table {0}, return reader is null!", tableName);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryRoleFirebaseId no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return null;
}
try
{
reader.Read();
string firebasePushId = reader.GetString("firebasePushId");
TraceLog.Trace("MysqlGameDBOperator.QueryRoleFirebaseId query success uid {0} firebasePushId {1}", uid, firebasePushId);
return firebasePushId;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return null;
}
public override bool SetFreezeTime(long uid, long freezeTime, int freezeReasonType, string freezeResonStr)
{
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.SetFreezeTime uid {0} to table {1}", uid, tableName);
string strSql = string.Format("select * from {0} where uid={1}", tableName, uid);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.SetFreezeTime no record in table {0} uid {1} return reader = null", tableName, uid);
return false;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.SetFreezeTime no record in table {0} uid {1} return reader no HasRows", tableName, uid);
reader.Close();
return false;
}
reader.Close();
strSql = string.Format("update {0} set freezeTime={1},freezeReasonStr='{3}',freezeReason={4} where uid={2}", tableName, freezeTime, uid, freezeResonStr, freezeReasonType);
reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.SetFreezeTime no record in table {0} uid {1} return reader = null", tableName, uid);
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.SetFreezeTime uid {0} to freezetime {1} success", uid, freezeTime);
return true;
}
public override bool AddBattleReplayData(long battleId, int createDay, ref CSBattleReplay replay)
{
string tableName = GameDBServerUtils.GetBattleReplayTableName(battleId);
string strSql = "replace into " + tableName + " set battleId=?battleId,mainlandId=?mainlandId,battleVersion=?battleVersion,data=?data,createDay=?createDay";
MySqlParameter p_battleId = new MySqlParameter("?battleId", MySqlDbType.Int64) { Value = battleId };
MySqlParameter p_mainlandId = new MySqlParameter("?mainlandId", MySqlDbType.Int32) { Value = replay.BattleInfoAll.MainlandId };
MySqlParameter p_battleVersion = new MySqlParameter("?battleVersion", MySqlDbType.Int64) { Value = replay.BattleVersion };
byte[] dataByte = StructMessageParseUtils.ToByteArray(ref replay);
dataByte = ZipData(dataByte);
MySqlParameter p_data = new MySqlParameter("?data", MySqlDbType.MediumBlob) { Value = dataByte };
MySqlParameter p_createDay = new MySqlParameter("?createDay", MySqlDbType.Int32) { Value = createDay };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_battleId, p_mainlandId, p_battleVersion, p_data, p_createDay);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.AddBattleReplayData no record in table, return reader is null");
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.AddBattleReplayData succ, battleId {0} ", battleId);
return true;
}
public override bool QueryBattleReplay(long battleId, ref SSQueryBattleReplayDbRes res)
{
string tableName = GameDBServerUtils.GetBattleReplayTableName(battleId);
string strSql = string.Format("select * from {0} where battleId={1};", tableName, battleId);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryBattleReplay return reader is null");
return false;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryBattleReplay no record in table {0}", battleId);
reader.Close();
return false;
}
try
{
reader.Read();
int index = reader.GetOrdinal("data");
byte[] buffer = new byte[100 * 1024];
long dataLen = reader.GetBytes(index, 0, buffer, 0, buffer.Length);
byte[] data = new byte[dataLen];
Buffer.BlockCopy(buffer, 0, data, 0, (int)dataLen);
data = UnzipData(data);
CSBattleReplay dbData = new CSBattleReplay();
StructMessageParseUtils.ParseFrom(ref dbData, data);
res.Replay.CopyFrom(ref dbData);
TraceLog.Trace("MysqlGameDBOperator.QueryBattleReplay succ,battleid {0}", battleId);
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool DeleteBattleReplay(long battleId)
{
string tableName = GameDBServerUtils.GetBattleReplayTableName(battleId);
string strSql = string.Format("delete from {0} where battleId={1};", tableName, battleId);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.DeleteBattleReplay return reader is null");
return false;
}
reader.Close();
return true;
}
public override bool QueryGameDbRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rank)
{
TraceLog.Trace("MysqlGameDBOperator.QueryGameDbRank rankId {0} realmId {1} groupId {2}", rankId, realmId, groupId);
int sqlIndex = realmId % 10;
if (sqlIndex == 0)
{
sqlIndex = 10;
}
string strSql = string.Format("select * from tbrank_{0} where rankId={1} and realmid={2} and groupId={3} and beginTime={4}",
sqlIndex, rankId, realmId, groupId, beginTime);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryGameDbRank return reader is null");
return false;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryGameDbRank no record in table");
reader.Close();
return true;
}
try
{
reader.Read();
int index = reader.GetOrdinal("rankData");
byte[] buffer = new byte[102400];
long dataLen = reader.GetBytes(index, 0, buffer, 0, buffer.Length);
byte[] data = new byte[dataLen];
Buffer.BlockCopy(buffer, 0, data, 0, (int)dataLen);
data = UnzipData(data);
StructMessageParseUtils.ParseFrom(ref rank, data);
TraceLog.Trace("MysqlGameDBOperator.QueryRank succ, rankId {0} realmId {1}", rankId, realmId);
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool ReplaceGameRank(int rankId, int realmId, int groupId, int beginTime, ref DBRankData rankData)
{
int sqlIndex = realmId % 10;
if (sqlIndex == 0)
{
sqlIndex = 10;
}
TraceLog.Trace("MysqlGameDBOperator.ReplaceRank rankId {0} realmId {1} groupId {2}", rankId, realmId, groupId);
string strSql = "replace into tbrank_" + sqlIndex.ToString() + " set rankId=?rankId, realmId=?realmId,groupId=?groupId,beginTime=?beginTime,rankData=?rankData";
MySqlParameter p_rankId = new MySqlParameter("?rankId", MySqlDbType.Int32) { Value = rankId };
MySqlParameter p_realmId = new MySqlParameter("?realmId", MySqlDbType.Int32) { Value = realmId };
MySqlParameter p_groupId = new MySqlParameter("?groupId", MySqlDbType.Int32) { Value = groupId };
MySqlParameter p_beginTime = new MySqlParameter("?beginTime", MySqlDbType.Int32) { Value = beginTime };
byte[] rankByte = StructMessageParseUtils.ToByteArray(ref rankData);
rankByte = ZipData(rankByte);
MySqlParameter p_rankData = new MySqlParameter("?rankData", MySqlDbType.MediumBlob) { Value = rankByte };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_rankId, p_realmId, p_groupId, p_beginTime, p_rankData);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.ReplaceRank no record in table, return reader is null");
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.ReplaceRank succ, rankId {0} realmId {1} groupId {2}", rankId, realmId, groupId);
return true;
}
public override bool ReplaceActRank(ref SSSaveActRankDataReq req)
{
TraceLog.Trace("MysqlGameDBOperator.ReplaceActRank rankId {0} realmId {1}", req.ActRankId, req.RealmId);
var remRankName = GameDBServerUtils.GetactRemRankId(req.RealmId, req.ActRankId);
string strSql = "replace into tbactRank set actRemRankId=?actRemRankId, endClear=?endClear,beginTime=?beginTime,endTime=?endTime,rankData=?rankData";
MySqlParameter p_remRankId = new MySqlParameter("?actRemRankId", MySqlDbType.Int64) { Value = remRankName };
MySqlParameter p_endClear = new MySqlParameter("?endClear", MySqlDbType.Int32) { Value = req.EndClear };
MySqlParameter p_beginTime = new MySqlParameter("?beginTime", MySqlDbType.Int64) { Value = req.BeginTime };
MySqlParameter p_endTime = new MySqlParameter("?endTime", MySqlDbType.Int32) { Value = req.EndTime };
byte[] rankByte = StructMessageParseUtils.ToByteArray(ref req.RankData);
rankByte = ZipData(rankByte);
MySqlParameter p_rankData = new MySqlParameter("?rankData", MySqlDbType.MediumBlob) { Value = rankByte };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_remRankId, p_endClear, p_beginTime, p_endTime, p_rankData);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.ReplaceActRank no record in table, return reader is null");
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.ReplaceActRank succ, rankId {0} realmId {1} ", req.ActRankId, req.RealmId);
return true;
}
public override int QueryActRank(ref SSQueryActRankDbReq req, ref SSQueryActRankDbRes res)
{
TraceLog.Trace("MysqlGameDBOperator.QueryActRank rankId {0} realmId {1}", req.ActRankId, req.RealmId);
var remRankName = GameDBServerUtils.GetactRemRankId(req.RealmId, req.ActRankId);
string strSql = string.Format("select * from tbactRank where actRemRankId={0}", remRankName);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryActRank return reader is null");
return -1;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryActRank no record in table");
reader.Close();
return 1;
}
try
{
reader.Read();
res.ActRankId = req.ActRankId;
res.RealmId = req.RealmId;
res.EndClear = reader.GetInt32("endClear");
res.BeginTime = reader.GetInt64("beginTime");
res.EndTime = reader.GetInt64("endTime");
int index = reader.GetOrdinal("rankData");
byte[] buffer = new byte[4500016];
long dataLen = reader.GetBytes(index, 0, buffer, 0, buffer.Length);
byte[] data = new byte[dataLen];
Buffer.BlockCopy(buffer, 0, data, 0, (int)dataLen);
data = UnzipData(data);
StructMessageParseUtils.ParseFrom(ref res.RankData, data);
TraceLog.Trace("MysqlGameDBOperator.QueryRank succ, rankId {0} realmId {1}", req.ActRankId, req.RealmId);
return 0;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return -1;
}
public override bool SearchRoleByName(long fromUid, string condition, ref SSPlayerSearchRoleDBByNameRes res)
{
StringBuilder strSql = new StringBuilder("select uid from (");
for (int i = 1; i <= TableIndexCalc.RoleTableCount; i++)
{
strSql.Append("select uid from tbuser_" + i + " where " + condition);
if (i != TableIndexCalc.RoleTableCount)
{
strSql.Append(" union all ");
}
}
strSql.Append(") as tbuser where uid != " + fromUid + " limit " + res.TargetUid.GetMaxCount());
TraceLog.Trace("MysqlGameDBOperator.SearchRoleByName exec sql: {0}", strSql.ToString());
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql.ToString());
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.SearchRoleByName no record in tbuser , return reader is null!");
return false;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.SearchRoleByName no record in tbuser");
//记住一定要Close
reader.Close();
return false;
}
try
{
while (reader.Read())
{
res.TargetUid.Add(reader.GetInt64("uid"));
}
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool QueryFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list, ref DBFriendOpData opData)
{
string tableName = GameDBServerUtils.GetFriendTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.QueryFriendList uid {0} to table {1}", uid, tableName);
string strSql = string.Format("select * from {0} where uid={1}", tableName, uid);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.QueryFriendList no record in table {0}, return reader is null!", tableName);
return false;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.QueryFriendList no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return true;
}
try
{
reader.Read();
int selfIndex = reader.GetOrdinal("self");
byte[] buffer = new byte[100 * 1024];
long dataLength = reader.GetBytes(selfIndex, 0, buffer, 0, buffer.Length);
byte[] dataByte = new byte[dataLength];
Buffer.BlockCopy(buffer, 0, dataByte, 0, (int)dataLength);
dataByte = UnzipData(dataByte);
StructMessageParseUtils.ParseFrom(ref self, dataByte);
int listIndex = reader.GetOrdinal("list");
dataLength = reader.GetBytes(listIndex, 0, buffer, 0, buffer.Length);
if (dataLength > 0)
{
dataByte = new byte[dataLength];
Buffer.BlockCopy(buffer, 0, dataByte, 0, (int)dataLength);
dataByte = UnzipData(dataByte);
StructMessageParseUtils.ParseFrom(ref list, dataByte);
}
int opIndex = reader.GetOrdinal("op");
dataLength = reader.GetBytes(opIndex, 0, buffer, 0, buffer.Length);
if (dataLength > 0)
{
dataByte = new byte[dataLength];
Buffer.BlockCopy(buffer, 0, dataByte, 0, (int)dataLength);
dataByte = UnzipData(dataByte);
StructMessageParseUtils.ParseFrom(ref opData, dataByte);
}
TraceLog.Trace("MysqlGameDBOperator.QueryFriendList query success uid {0} ", uid);
return true;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return false;
}
public override bool UpdateFriendList(long uid, ref DBFriendSelf self, ref DBFriendList list, ref DBFriendOpData op, bool binsert)
{
string tableName = GameDBServerUtils.GetFriendTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.UpdateFriendList uid {0} to table {1}", uid, tableName);
string strSql = "update " + tableName + " set self=?self,list=?list,op=?op where uid=" + uid.ToString();
if (binsert)
{
strSql = string.Format("replace into {0} (uid,self,list,op) values({1},?self,?list,?op)"
, tableName, uid);
}
byte[] selfByte = StructMessageParseUtils.ToByteArray(ref self);
selfByte = ZipData(selfByte);
MySqlParameter p_self = new MySqlParameter("?self", MySqlDbType.Blob) { Value = selfByte };
byte[] listByte = StructMessageParseUtils.ToByteArray(ref list);
listByte = ZipData(listByte);
MySqlParameter p_list = new MySqlParameter("?list", MySqlDbType.Blob) { Value = listByte };
byte[] opByte = StructMessageParseUtils.ToByteArray(ref op);
opByte = ZipData(opByte);
MySqlParameter p_op = new MySqlParameter("?op", MySqlDbType.Blob) { Value = opByte };
TraceLog.Trace("MysqlGameDBOperator.UpdateFriendList exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_self, p_list, p_op);
if (reader == null)
{
TraceLog.Error("MysqlGameDBOperator.UpdateFriendList failed, return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.UpdateFriendList uid {0} to table success", uid);
return true;
}
public override bool SetEnableMsgTime(long uid, long enableMsgTime)
{
string tableName = GameDBServerUtils.GetUserTableName(uid);
TraceLog.Trace("MysqlGameDBOperator.SetEnableMsgTime uid {0} to table {1}", uid, tableName);
string strSql = string.Format("select * from {0} where uid={1}", tableName, uid);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.SetEnableMsgTime no record in table {0} uid {1} return reader = null", tableName, uid);
return false;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.SetEnableMsgTime no record in table {0} uid {1} return reader no HasRows", tableName, uid);
reader.Close();
return false;
}
reader.Close();
strSql = string.Format("update {0} set enableMsgTime={1} where uid={2}", tableName, enableMsgTime, uid);
reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.SetEnableMsgTime no record in table {0} uid {1} return reader = null", tableName, uid);
return false;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.SetEnableMsgTime uid {0} to freezetime {1} success", uid, enableMsgTime);
return true;
}
//查询全局数据
public override CSErrCode OnQueryWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res)
{
TraceLog.Trace("MysqlGameDBOperator.OnQueryWorldGlobalDataDBOp dataType {0} globalId {1} dataVer {2}", req.DataType, req.GlobalId, req.DataVer);
string strSql = string.Format("select * from tb_worldglobaldata where dataType={0} and globalId={1} and dataVer={2}", (int)req.DataType, req.GlobalId, req.DataVer);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MysqlGameDBOperator.OnQueryWorldGlobalDataDBOp return reader is null");
return CSErrCode.SysFailure;
}
//没有记录是正常的
if (reader.HasRows == false)
{
TraceLog.Trace("MysqlGameDBOperator.OnQueryWorldGlobalDataDBOp no record in table");
reader.Close();
return CSErrCode.None;
}
try
{
reader.Read();
int index = reader.GetOrdinal("data");
byte[] buffer = new byte[res.Data.GetMaxCount() * 2];
long dataLen = reader.GetBytes(index, 0, buffer, 0, buffer.Length);
byte[] bdata = new byte[dataLen];
Buffer.BlockCopy(buffer, 0, bdata, 0, (int)dataLen);
for (int i = 0; i < dataLen && i < res.Data.GetMaxCount(); i++)
{
res.Data.Add(bdata[i]);
}
TraceLog.Trace("MysqlGameDBOperator.OnQueryWorldGlobalDataDBOp succ, dataType {0} globalId {1} dataVer {2} data length {3}", req.DataType, req.GlobalId, req.DataVer, dataLen);
return CSErrCode.None;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
return CSErrCode.Fail;
}
finally
{
reader.Close();
}
}
public override CSErrCode OnUpdateWorldGlobalDataDBOp(ref SSWorldGlobalDataDBOpReq req, ref SSWorldGlobalDataDBOpRes res)
{
//REPLACE into tb_worldglobaldata(dataType, globalId, data) VALUES(2, 1, 99999999999);
TraceLog.Trace("MysqlGameDBOperator.OnUpdateWorldGlobalDataDBOp " +
"dataType {0} globalId {1} dataVer {2} data length {3}", req.DataType, req.GlobalId, req.DataVer, req.Data.Count);
string strSql = "REPLACE into tb_worldglobaldata(dataType, globalId, dataVer,data) VALUES (?dataType, ?globalId,?dataVer, ?data)";
MySqlParameter p_dataType = new MySqlParameter("?dataType", MySqlDbType.Int32) { Value = (int)req.DataType };
MySqlParameter p_globalId = new MySqlParameter("?globalId", MySqlDbType.Int32) { Value = req.GlobalId };
MySqlParameter p_dataVer = new MySqlParameter("?dataVer", MySqlDbType.Int32) { Value = req.DataVer };
byte[] buffer = new byte[req.Data.Count];
for (int i = 0; i < req.Data.Count; i++)
{
buffer[i] = req.Data[i];
}
MySqlParameter p_data = new MySqlParameter("?data", MySqlDbType.MediumBlob) { Value = buffer };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_dataType, p_globalId, p_dataVer, p_data);
if (reader == null)
{
TraceLog.Error("MysqlGameDBOperator.OnUpdateWorldGlobalDataDBOp ExecReader error");
return CSErrCode.SysFailure;
}
reader.Close();
TraceLog.Trace("MysqlGameDBOperator.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;
}
}
}