public TCPProcessCmdResults ProcChangeName(DBManager dbMgr, TCPOutPacketPool pool, int nID, byte[] data, int count, out TCPOutPacket tcpOutPacket) { tcpOutPacket = null; string cmdData = null; try { cmdData = new UTF8Encoding().GetString(data, 0, count); } catch (Exception) { LogManager.WriteLog(LogTypes.Error, string.Format("解析指令字符串错误, CMD={0}", (TCPGameServerCmds)nID), null, true); tcpOutPacket = TCPOutPacket.MakeTCPOutPacket(pool, "0", 30767); return(TCPProcessCmdResults.RESULT_DATA); } try { string[] fields = cmdData.Split(new char[] { ':' }); if (fields.Length != 8) { LogManager.WriteLog(LogTypes.Error, string.Format("指令参数个数错误, CMD={0}, Recv={1}, CmdData={2}", (TCPGameServerCmds)nID, fields.Length, cmdData), null, true); tcpOutPacket = TCPOutPacket.MakeTCPOutPacket(pool, "0", 30767); return(TCPProcessCmdResults.RESULT_DATA); } string uid = fields[0]; int zoneId = Convert.ToInt32(fields[1]); int roleId = Convert.ToInt32(fields[2]); string newName = fields[3]; int costZuanShiBase = Convert.ToInt32(fields[4]); int costZuanShiMax = Convert.ToInt32(fields[5]); int canFreeMod = Convert.ToInt32(fields[6]); int canZuanShiMod = Convert.ToInt32(fields[7]); string oldName = ""; string failedMsg = ""; int needZuanShi = 0; DBUserInfo userInfo = null; DBRoleInfo roleInfo = null; ChangeNameType cnt = ChangeNameType.CNT_Unknown; ChangeNameError cne = ChangeNameError.Success; bool bNewNameTakePlaceHolder = false; userInfo = dbMgr.GetDBUserInfo(uid); if (userInfo == null) { failedMsg = "账号找不到 : " + uid; cne = ChangeNameError.DBFailed; } else { lock (userInfo) { int i; for (i = 0; i < userInfo.ListRoleIDs.Count; i++) { if (userInfo.ListRoleZoneIDs[i] == zoneId && userInfo.ListRoleIDs[i] == roleId) { break; } } if (i == userInfo.ListRoleIDs.Count) { failedMsg = string.Concat(new object[] { "账号: ", uid, " 在 ", zoneId.ToString(), " 区,不包含角色 ", roleId }); cne = ChangeNameError.NotContainRole; goto IL_641; } } roleInfo = dbMgr.GetDBRoleInfo(ref roleId); if (null == roleInfo) { failedMsg = "查找不到dbroleinfo,roleid=" + roleId.ToString(); cne = ChangeNameError.DBFailed; } else if (GameDBManager.PreDelRoleMgr.IfInPreDeleteState(roleId)) { failedMsg = "处在预删除状态dbroleinfo,roleid=" + roleId.ToString(); cne = ChangeNameError.DBFailed; } else { oldName = roleInfo.RoleName; int leftFreeTimes = Global.GetRoleParamsInt32(roleInfo, "LeftFreeChangeNameTimes"); if (leftFreeTimes > 0 && 1 == canFreeMod) { cnt = ChangeNameType.CNT_Free; } else { cnt = ChangeNameType.CNT_ZuanShi; } if ((cnt == ChangeNameType.CNT_Free && 1 != canFreeMod) || (cnt == ChangeNameType.CNT_ZuanShi && 1 != canZuanShiMod)) { failedMsg = "服务器没有开放改名功能, " + cnt.ToString(); cne = ChangeNameError.ServerDenied; } else { SingletonTemplate <NameUsedMgr> .Instance().AddCannotUse_Ex(oldName); if (!SingletonTemplate <NameUsedMgr> .Instance().AddCannotUse_Ex(newName) || dbMgr.IsRolenameExist(newName) || !SingletonTemplate <NameManager> .Instance().IsNameCanUseInDb(dbMgr, newName)) { failedMsg = "新名字: " + newName + " 已被占用"; cne = ChangeNameError.NameAlreayUsed; } else { bNewNameTakePlaceHolder = true; if (PreNamesManager.SetUsedPreName(newName)) { DBWriter.UpdatePreNameUsedState(dbMgr, newName, 1); } string updateKey = ""; string updateVal = ""; if (cnt == ChangeNameType.CNT_Free) { LogManager.WriteLog(LogTypes.Error, string.Format("角色请求免费改名,roleid={0}, old name={1}, new name={2}", roleId, oldName, newName), null, true); leftFreeTimes = Math.Max(0, leftFreeTimes - 1); updateKey = "LeftFreeChangeNameTimes"; updateVal = leftFreeTimes.ToString(); } else if (cnt == ChangeNameType.CNT_ZuanShi) { int alreadyZuanShiTimes = Global.GetRoleParamsInt32(roleInfo, "AlreadyZuanShiChangeNameTimes"); needZuanShi = Math.Min(costZuanShiMax, costZuanShiBase * (alreadyZuanShiTimes + 1)); LogManager.WriteLog(LogTypes.Error, string.Format("角色请求钻石改名,roleid={0}, old name={1}, new name={2}, costzuanshi={3}", new object[] { roleId, oldName, newName, needZuanShi }), null, true); lock (userInfo) { if (userInfo.Money < needZuanShi) { failedMsg = "钻石不足"; cne = ChangeNameError.ZuanShiNotEnough; goto IL_641; } int tmpMoney = userInfo.Money; userInfo.Money -= needZuanShi; if (!DBWriter.UpdateUserInfo(dbMgr, userInfo)) { userInfo.Money = tmpMoney; failedMsg = string.Format("改名时更新用户的元宝失败,UserID={0}", userInfo.UserID); cne = ChangeNameError.DBFailed; goto IL_641; } alreadyZuanShiTimes++; updateKey = "AlreadyZuanShiChangeNameTimes"; updateVal = alreadyZuanShiTimes.ToString(); } } Global.UpdateRoleParamByName(dbMgr, roleInfo, updateKey, updateVal, null); string cmdText = string.Format("UPDATE t_roles SET rname='{0}' WHERE rid={1} AND userid='{2}' AND zoneid={3}", new object[] { newName, roleId, uid, zoneId }); if (!this._Util_ExecNonQuery(dbMgr, cmdText)) { failedMsg = "更新t_roles的名字失败"; cne = ChangeNameError.DBFailed; } else { lock (userInfo) { for (int i = 0; i < userInfo.ListRoleIDs.Count; i++) { if (userInfo.ListRoleZoneIDs[i] == zoneId && userInfo.ListRoleIDs[i] == roleId) { userInfo.ListRoleNames[i] = newName; break; } } } lock (roleInfo) { oldName = roleInfo.RoleName; roleInfo.RoleName = newName; } cne = ChangeNameError.Success; } } } } } IL_641: if (cne == ChangeNameError.Success) { LogManager.WriteLog(LogTypes.Error, string.Format("角色改名成功,roleid={0}, old name={1},new name={2}", roleId, oldName, newName), null, true); this.AddChangeNameDBRecord(dbMgr, roleId, oldName, newName, cnt, needZuanShi); this._OnChangeNameSuccess(dbMgr, roleId, zoneId, oldName, newName); } else { LogManager.WriteLog(LogTypes.Error, string.Format("角色改名失败,roleid={0}, name={1}, reason={2}", roleId, oldName, failedMsg), null, true); if (bNewNameTakePlaceHolder) { SingletonTemplate <NameUsedMgr> .Instance().DelCannotUse_Ex(newName); } } tcpOutPacket = TCPOutPacket.MakeTCPOutPacket(pool, string.Format("{0}:{1}:{2}:{3}", new object[] { (int)cne, oldName, needZuanShi, userInfo.Money }), nID); return(TCPProcessCmdResults.RESULT_DATA); } catch (Exception ex) { DataHelper.WriteFormatExceptionLog(ex, "", false, false); } tcpOutPacket = TCPOutPacket.MakeTCPOutPacket(pool, "0", 30767); return(TCPProcessCmdResults.RESULT_DATA); }