game: prepare messenger list queries

This commit is contained in:
server
2026-04-13 21:57:59 +02:00
parent 8a5f6e70ee
commit 74827a1907
2 changed files with 116 additions and 31 deletions

View File

@@ -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<std::string>& 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<char*>(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<char*>(account.c_str()))
|| !stmt.BindParam(MYSQL_TYPE_STRING, const_cast<char*>(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<char*>(account.c_str()))
|| !stmt.BindParam(MYSQL_TYPE_STRING, const_cast<char*>(companion.c_str()))
|| !stmt.BindParam(MYSQL_TYPE_STRING, const_cast<char*>(companion.c_str()))
|| !stmt.BindParam(MYSQL_TYPE_STRING, const_cast<char*>(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<char*>(account.c_str()))
|| !stmt.BindParam(MYSQL_TYPE_STRING, const_cast<char*>(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<std::string> 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<keyT>::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());
}

View File

@@ -50,7 +50,7 @@ class MessengerManager : public singleton<MessengerManager>
void SendLogin(keyA account, keyA companion);
void SendLogout(keyA account, keyA companion);
void LoadList(SQLMsg * pmsg);
void LoadList(keyA account);
void Destroy();