From 74827a19076ee9c09cda602e6fe7d7ad34cc7ec6 Mon Sep 17 00:00:00 2001 From: server Date: Mon, 13 Apr 2026 21:57:59 +0200 Subject: [PATCH] game: prepare messenger list queries --- src/game/messenger_manager.cpp | 145 ++++++++++++++++++++++++++------- src/game/messenger_manager.h | 2 +- 2 files changed, 116 insertions(+), 31 deletions(-) diff --git a/src/game/messenger_manager.cpp b/src/game/messenger_manager.cpp index 32a5fae..67447eb 100644 --- a/src/game/messenger_manager.cpp +++ b/src/game/messenger_manager.cpp @@ -11,10 +11,113 @@ #include "char.h" #include "char_manager.h" #include "questmanager.h" +#include "libsql/Statement.h" static char __account[CHARACTER_NAME_MAX_LEN * 2 + 1]; static char __companion[CHARACTER_NAME_MAX_LEN * 2 + 1]; +namespace +{ + bool PrepareMessengerStmt(CStmt& stmt, const std::string& query) + { + CAsyncSQL* sql = DBManager::instance().GetDirectSQL(); + + if (!sql) + { + sys_err("messenger direct SQL handle is not initialized"); + return false; + } + + return stmt.Prepare(sql, query.c_str()); + } + + bool LoadMessengerCompanions(MessengerManager::keyA account, std::vector& companions) + { + CStmt stmt; + char companion[CHARACTER_NAME_MAX_LEN + 1]; + const std::string query = std::string("SELECT companion FROM messenger_list") + get_table_postfix() + " WHERE account=?"; + + memset(companion, 0, sizeof(companion)); + companions.clear(); + + if (!PrepareMessengerStmt(stmt, query)) + return false; + + if (!stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str())) + || !stmt.BindResult(MYSQL_TYPE_STRING, companion, sizeof(companion)) + || !stmt.Execute()) + { + return false; + } + + for (int i = 0; i < stmt.iRows; ++i) + { + memset(companion, 0, sizeof(companion)); + + if (!stmt.Fetch()) + return false; + + companions.emplace_back(companion); + } + + return true; + } + + bool InsertMessengerRelation(MessengerManager::keyA account, MessengerManager::keyA companion) + { + CStmt stmt; + const std::string query = std::string("INSERT INTO messenger_list") + get_table_postfix() + " VALUES (?, ?)"; + + if (!PrepareMessengerStmt(stmt, query)) + return false; + + if (!stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str())) + || !stmt.BindParam(MYSQL_TYPE_STRING, const_cast(companion.c_str()))) + { + return false; + } + + return stmt.Execute(); + } + + bool DeleteMessengerRelation(MessengerManager::keyA account, MessengerManager::keyA companion) + { + CStmt stmt; + const std::string query = std::string("DELETE FROM messenger_list") + get_table_postfix() + + " WHERE (account=? AND companion=?) OR (account=? AND companion=?)"; + + if (!PrepareMessengerStmt(stmt, query)) + return false; + + if (!stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str())) + || !stmt.BindParam(MYSQL_TYPE_STRING, const_cast(companion.c_str())) + || !stmt.BindParam(MYSQL_TYPE_STRING, const_cast(companion.c_str())) + || !stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str()))) + { + return false; + } + + return stmt.Execute(); + } + + bool DeleteAllMessengerRelations(MessengerManager::keyA account) + { + CStmt stmt; + const std::string query = std::string("DELETE FROM messenger_list") + get_table_postfix() + " WHERE account=? OR companion=?"; + + if (!PrepareMessengerStmt(stmt, query)) + return false; + + if (!stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str())) + || !stmt.BindParam(MYSQL_TYPE_STRING, const_cast(account.c_str()))) + { + return false; + } + + return stmt.Execute(); + } +} + MessengerManager::MessengerManager() { } @@ -51,39 +154,26 @@ void MessengerManager::Login(MessengerManager::keyA account) if (account.compare(__account)) return; - DBManager::instance().FuncQuery(std::bind(&MessengerManager::LoadList, this, std::placeholders::_1), - "SELECT account, companion FROM messenger_list%s WHERE account='%s'", get_table_postfix(), account.c_str()); - m_set_loginAccount.insert(account); + LoadList(account); } -void MessengerManager::LoadList(SQLMsg * msg) +void MessengerManager::LoadList(MessengerManager::keyA account) { - if (NULL == msg) + std::vector companions; + + if (!LoadMessengerCompanions(account, companions)) return; - if (NULL == msg->Get()) + if (companions.empty()) return; - if (msg->Get()->uiNumRows == 0) - return; - - std::string account; - sys_log(1, "Messenger::LoadList"); - for (uint i = 0; i < msg->Get()->uiNumRows; ++i) + for (const auto& companion : companions) { - MYSQL_ROW row = mysql_fetch_row(msg->Get()->pSQLResult); - - if (row[0] && row[1]) - { - if (account.length() == 0) - account = row[0]; - - m_Relation[row[0]].insert(row[1]); - m_InverseRelation[row[1]].insert(row[0]); - } + m_Relation[account].insert(companion); + m_InverseRelation[companion].insert(account); } SendList(account); @@ -501,8 +591,7 @@ void MessengerManager::AddToList(MessengerManager::keyA account, MessengerManage sys_log(0, "Messenger Add %s %s", account.c_str(), companion.c_str()); - DBManager::instance().Query("INSERT INTO messenger_list%s VALUES ('%s', '%s')", - get_table_postfix(), account.c_str(), companion.c_str()); + InsertMessengerRelation(account, companion); __AddToList(account, companion); @@ -568,9 +657,7 @@ void MessengerManager::RemoveFromList(MessengerManager::keyA account, MessengerM sys_log(1, "Messenger Remove %s %s", account.c_str(), companion.c_str()); - // Fix - DBManager::instance().Query("DELETE FROM messenger_list%s WHERE (account='%s' AND companion = '%s') OR (account = '%s' AND companion = '%s')", - get_table_postfix(), account.c_str(), companion.c_str(), companion.c_str(), account.c_str()); + DeleteMessengerRelation(account, companion); // MR-3: Remove from messenger Fix LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str()); @@ -601,8 +688,7 @@ void MessengerManager::RemoveAllList(keyA account) return; /* SQL Data 삭제 */ - DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' OR companion='%s'", - get_table_postfix(), account.c_str(), account.c_str()); + DeleteAllMessengerRelations(account); /* 내가 가지고있는 리스트 삭제 */ for (std::set::iterator iter = company.begin(); @@ -739,4 +825,3 @@ void MessengerManager::SendLogout(MessengerManager::keyA account, MessengerManag d->BufferedPacket(&bLen, sizeof(BYTE)); d->Packet(companion.c_str(), companion.size()); } - diff --git a/src/game/messenger_manager.h b/src/game/messenger_manager.h index ca794be..bfb0cd0 100644 --- a/src/game/messenger_manager.h +++ b/src/game/messenger_manager.h @@ -50,7 +50,7 @@ class MessengerManager : public singleton void SendLogin(keyA account, keyA companion); void SendLogout(keyA account, keyA companion); - void LoadList(SQLMsg * pmsg); + void LoadList(keyA account); void Destroy();