Resolve copy and past mistakes and further work.

This commit is contained in:
Jett 2023-10-10 09:22:31 +00:00
parent 8edfdd48a1
commit 6fb0677bd9
4 changed files with 61 additions and 59 deletions

View File

@ -41,6 +41,9 @@ public:
virtual void CreateCharacterXML(uint32_t id, const std::string& xml) = 0; virtual void CreateCharacterXML(uint32_t id, const std::string& xml) = 0;
virtual void UpdateCharacterXML(uint32_t id, const std::string& xml) = 0; virtual void UpdateCharacterXML(uint32_t id, const std::string& xml) = 0;
virtual void CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) = 0; virtual void CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) = 0;
virtual void ApproveCharacterName(uint32_t id, const std::string& newName) = 0;
virtual void SetPendingCharacterName(uint32_t id, const std::string& pendingName) = 0;
virtual void UpdateCharacterLastLogin(uint32_t id, uint64_t time) = 0;
// Character Delete // Character Delete
virtual void DeleteCharacter(uint32_t id) = 0; virtual void DeleteCharacter(uint32_t id) = 0;

View File

@ -47,21 +47,7 @@ MySQLDatabase::~MySQLDatabase() {
} }
void MySQLDatabase::Connect() { void MySQLDatabase::Connect() {
try { if (this->m_Properties.find("localSocket") != this->m_Properties.end() || this->m_Properties.find("pipe") != this->m_Properties.end()) {
this->m_Driver = get_m_Driver_instance();
this->m_Properties["hostName"] = this->m_Host;
this->m_Properties["userName"] = this->m_Username;
this->m_Properties["password"] = this->m_Password;
this->m_Properties["OPT_RECONNECT"] = true;
this->m_Connection = this->m_Driver->connect(this->props);
this->m_Connection->setSchema(this->m_Database);
} catch (sql::SQLException& e) {
throw MySqlException(e.what());
}
if (this->m_Properties.find("localSocket") != this->m_Properties.end() || this->m_Properties.find("pipe") != Database::props.end()) {
this->m_Connection = m_Driver->connect(this->m_Properties); this->m_Connection = m_Driver->connect(this->m_Properties);
} else { } else {
this->m_Connection = m_Driver->connect( this->m_Connection = m_Driver->connect(
@ -82,7 +68,7 @@ void MySQLDatabase::Destroy() {
} }
sql::Statement* MySQLDatabase::CreateStmt() { sql::Statement* MySQLDatabase::CreateStmt() {
sql::Statement* toReturn = con->createStatement(); sql::Statement* toReturn = this->m_Connection->createStatement();
return toReturn; return toReturn;
} }
@ -96,7 +82,7 @@ sql::PreparedStatement* MySQLDatabase::CreatePreppedStmt(const std::string& quer
Game::logger->Log("Database", "Trying to reconnect to MySQL"); Game::logger->Log("Database", "Trying to reconnect to MySQL");
} }
if (!con->isValid() || con->isClosed()) { if (!this->m_Connection->isValid() || this->m_Connection->isClosed()) {
delete this->m_Connection; delete this->m_Connection;
this->m_Connection = nullptr; this->m_Connection = nullptr;
@ -111,7 +97,8 @@ sql::PreparedStatement* MySQLDatabase::CreatePreppedStmt(const std::string& quer
} }
std::unique_ptr<sql::PreparedStatement> MySQLDatabase::CreatePreppedStmtUnique(const std::string& query) { std::unique_ptr<sql::PreparedStatement> MySQLDatabase::CreatePreppedStmtUnique(const std::string& query) {
return CreatePreppedStmt(query); std::unique_ptr<sql::PreparedStatement> stmt(CreatePreppedStmt(query));
return stmt;
} }
std::unique_ptr<sql::ResultSet> MySQLDatabase::GetResultsOfStatement(sql::Statement* stmt) { std::unique_ptr<sql::ResultSet> MySQLDatabase::GetResultsOfStatement(sql::Statement* stmt) {
@ -121,7 +108,7 @@ std::unique_ptr<sql::ResultSet> MySQLDatabase::GetResultsOfStatement(sql::Statem
void MySQLDatabase::Commit() { void MySQLDatabase::Commit() {
Database::con->commit(); this->m_Connection->commit();
} }
bool MySQLDatabase::GetAutoCommit() { bool MySQLDatabase::GetAutoCommit() {
@ -174,7 +161,7 @@ std::vector<std::string> MySQLDatabase::GetAllCharacterNames() {
std::vector<std::string> names; std::vector<std::string> names;
while (res->next()) { while (res->next()) {
names.push_back(res->getString("name")); names.push_back(res->getString("name").c_str());
} }
return names; return names;
@ -224,7 +211,7 @@ CharacterInfo MySQLDatabase::GetCharacterInfoByID(uint32_t id) {
info.PendingName = res->getString("pending_name"); info.PendingName = res->getString("pending_name");
info.NameRejected = res->getBoolean("needs_rename"); info.NameRejected = res->getBoolean("needs_rename");
info.PropertyCloneID = res->getUInt("prop_clone_id"); info.PropertyCloneID = res->getUInt("prop_clone_id");
info.PermissionMap = res->getUInt("permission_map"); info.PermissionMap = (ePermissionMap)res->getUInt("permission_map");
return info; return info;
} }
@ -239,7 +226,7 @@ CharacterInfo MySQLDatabase::GetCharacterInfoByName(const std::string& name) {
auto res = GetResultsOfStatement(stmt.get()); auto res = GetResultsOfStatement(stmt.get());
while (res->next()) { while (res->next()) {
return GetCharacterByID(res->getUInt("id")); return GetCharacterInfoByID(res->getUInt("id"));
} }
return CharacterInfo{}; return CharacterInfo{};
@ -265,38 +252,61 @@ std::string MySQLDatabase::GetCharacterXMLByID(uint32_t id) {
auto res = GetResultsOfStatement(stmt.get()); auto res = GetResultsOfStatement(stmt.get());
while (res->next()) { while (res->next()) {
return res->getString("xml_data"); return res->getString("xml_data").c_str();
} }
return ""; return "";
} }
void MySQLDatabase::CreateCharacterXML(uint32_t id, const std::string& xml) { void MySQLDatabase::CreateCharacterXML(uint32_t id, const std::string& xml) {
auto replaceStmt = CreatePreppedStmtUnique("INSERT INTO charxml (id, xml_data) VALUES (?, ?);"); auto stmt = CreatePreppedStmtUnique("INSERT INTO charxml (id, xml_data) VALUES (?, ?);");
replaceStmt->setUInt(1, id); stmt->setUInt(1, id);
replaceStmt->setString(2, xml); stmt->setString(2, xml);
stmt->executeUpdate(); stmt->executeUpdate();
} }
void MySQLDatabase::UpdateCharacterXML(uint32_t id, const std::string& xml) { void MySQLDatabase::UpdateCharacterXML(uint32_t id, const std::string& xml) {
auto replaceStmt = CreatePreppedStmtUnique("UPDATE charxml SET xml_data = ? WHERE id = ?;"); auto stmt = CreatePreppedStmtUnique("UPDATE charxml SET xml_data = ? WHERE id = ?;");
replaceStmt->setString(1, xml); stmt->setString(1, xml);
replaceStmt->setUInt(2, id); stmt->setUInt(2, id);
stmt->executeUpdate(); stmt->executeUpdate();
} }
void MySQLDatabase::CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) { void MySQLDatabase::CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) {
auto replaceStmt = CreatePreppedStmtUnique("INSERT INTO charinfo (id, account_id, name, pending_name, needs_rename, last_login) VALUES (?, ?, ?, ?, ?, ?);"); auto stmt = CreatePreppedStmtUnique("INSERT INTO charinfo (id, account_id, name, pending_name, needs_rename, last_login) VALUES (?, ?, ?, ?, ?, ?);");
replaceStmt->setUInt(1, id); stmt->setUInt(1, id);
replaceStmt->setUInt(2, account_id); stmt->setUInt(2, account_id);
replaceStmt->setString(3, name); stmt->setString(3, name);
replaceStmt->setString(4, pending_name); stmt->setString(4, pending_name);
replaceStmt->setBoolean(5, needs_rename); stmt->setBoolean(5, needs_rename);
replaceStmt->setUInt64(6, last_login); stmt->setUInt64(6, last_login);
replaceStmt->executeUpdate(); stmt->execute();
}
void MySQLDatabase::ApproveCharacterName(uint32_t id, const std::string& newName) {
auto stmt = CreatePreppedStmtUnique("UPDATE charinfo SET name = ?, pending_name = '', needs_rename = 0, last_login = ? WHERE id = ? LIMIT 1");
stmt->setString(1, newName);
stmt->setUInt64(2, time(NULL));
stmt->setUInt(3, id);
stmt->execute();
}
void MySQLDatabase::SetPendingCharacterName(uint32_t id, const std::string& pendingName) {
auto stmt = CreatePreppedStmtUnique("UPDATE charinfo SET pending_name=?, needs_rename=0, last_login=? WHERE id=? LIMIT 1;");
stmt->setString(1, pendingName);
stmt->setUInt64(2, time(NULL));
stmt->setUInt(3, id);
stmt->execute();
}
void MySQLDatabase::UpdateCharacterLastLogin(uint32_t id, uint64_t time) {
auto stmt = CreatePreppedStmtUnique("UPDATE charinfo SET last_login = ? WHERE id = ? LIMIT 1;");
stmt->setUInt64(1, time);
stmt->setUInt(2, id);
stmt->execute();
} }
void MySQLDatabase::DeleteCharacter(uint32_t id) { void MySQLDatabase::DeleteCharacter(uint32_t id) {
@ -381,7 +391,7 @@ AccountInfo MySQLDatabase::GetAccountByID(uint32_t id) {
info.ID = id; info.ID = id;
info.Name = res->getString("name"); info.Name = res->getString("name");
info.Password = res->getString("password"); info.Password = res->getString("password");
info.GMLevel = res->getUInt("gm_level"); info.MaxGMLevel = res->getUInt("gm_level");
info.Locked = res->getBoolean("locked"); info.Locked = res->getBoolean("locked");
info.Banned = res->getBoolean("banned"); info.Banned = res->getBoolean("banned");
info.PlayKeyID = res->getUInt("play_key_id"); info.PlayKeyID = res->getUInt("play_key_id");
@ -403,7 +413,7 @@ std::vector<CharacterInfo> MySQLDatabase::GetAllCharactersByAccountID(uint32_t a
std::vector<CharacterInfo> characters; std::vector<CharacterInfo> characters;
while (res->next()) { while (res->next()) {
characters.push_back(GetCharacterByID(res->getUInt("id"))); characters.push_back(GetCharacterInfoByID(res->getUInt("id")));
} }
return characters; return characters;

View File

@ -25,9 +25,9 @@ public:
std::unique_ptr<sql::PreparedStatement> CreatePreppedStmtUnique(const std::string& query); std::unique_ptr<sql::PreparedStatement> CreatePreppedStmtUnique(const std::string& query);
std::unique_ptr<sql::ResultSet> GetResultsOfStatement(sql::Statement* stmt); std::unique_ptr<sql::ResultSet> GetResultsOfStatement(sql::Statement* stmt);
void Commit() override; void Commit();
bool GetAutoCommit() override; bool GetAutoCommit();
void SetAutoCommit(bool value) override; void SetAutoCommit(bool value);
SocketDescriptor GetMasterServerIP() override; SocketDescriptor GetMasterServerIP() override;
@ -47,6 +47,9 @@ public:
void CreateCharacterXML(uint32_t id, const std::string& xml) override; void CreateCharacterXML(uint32_t id, const std::string& xml) override;
void UpdateCharacterXML(uint32_t id, const std::string& xml) override; void UpdateCharacterXML(uint32_t id, const std::string& xml) override;
void CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) override; void CreateCharacter(uint32_t id, uint32_t account_id, const std::string& name, const std::string& pending_name, bool needs_rename, uint64_t last_login) override;
void ApproveCharacterName(uint32_t id, const std::string& newName) override;
void SetPendingCharacterName(uint32_t id, const std::string& pendingName) override;
void UpdateCharacterLastLogin(uint32_t id, uint64_t time) override;
void DeleteCharacter(uint32_t id) override; void DeleteCharacter(uint32_t id) override;

View File

@ -426,23 +426,13 @@ void UserManager::RenameCharacter(const SystemAddress& sysAddr, Packet* packet)
if (IsNameAvailable(newName)) { if (IsNameAvailable(newName)) {
if (IsNamePreapproved(newName)) { if (IsNamePreapproved(newName)) {
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("UPDATE charinfo SET name=?, pending_name='', needs_rename=0, last_login=? WHERE id=? LIMIT 1"); Database::Connection->ApproveCharacterName(character->GetID(), newName);
stmt->setString(1, newName);
stmt->setUInt64(2, time(NULL));
stmt->setUInt(3, character->GetID());
stmt->execute();
delete stmt;
Game::logger->Log("UserManager", "Character %s now known as %s", character->GetName().c_str(), newName.c_str()); Game::logger->Log("UserManager", "Character %s now known as %s", character->GetName().c_str(), newName.c_str());
WorldPackets::SendCharacterRenameResponse(sysAddr, eRenameResponse::SUCCESS); WorldPackets::SendCharacterRenameResponse(sysAddr, eRenameResponse::SUCCESS);
UserManager::RequestCharacterList(sysAddr); UserManager::RequestCharacterList(sysAddr);
} else { } else {
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("UPDATE charinfo SET pending_name=?, needs_rename=0, last_login=? WHERE id=? LIMIT 1"); Database::Connection->SetPendingCharacterName(character->GetID(), newName);
stmt->setString(1, newName);
stmt->setUInt64(2, time(NULL));
stmt->setUInt(3, character->GetID());
stmt->execute();
delete stmt;
Game::logger->Log("UserManager", "Character %s has been renamed to %s and is pending approval by a moderator.", character->GetName().c_str(), newName.c_str()); Game::logger->Log("UserManager", "Character %s has been renamed to %s and is pending approval by a moderator.", character->GetName().c_str(), newName.c_str());
WorldPackets::SendCharacterRenameResponse(sysAddr, eRenameResponse::SUCCESS); WorldPackets::SendCharacterRenameResponse(sysAddr, eRenameResponse::SUCCESS);
@ -473,11 +463,7 @@ void UserManager::LoginCharacter(const SystemAddress& sysAddr, uint32_t playerID
} }
if (hasCharacter && character) { if (hasCharacter && character) {
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("UPDATE charinfo SET last_login=? WHERE id=? LIMIT 1"); Database::Connection->UpdateCharacterLastLogin(playerID, time(NULL));
stmt->setUInt64(1, time(NULL));
stmt->setUInt(2, playerID);
stmt->execute();
delete stmt;
uint32_t zoneID = character->GetZoneID(); uint32_t zoneID = character->GetZoneID();
if (zoneID == LWOZONEID_INVALID) zoneID = 1000; //Send char to VE if (zoneID == LWOZONEID_INVALID) zoneID = 1000; //Send char to VE