From fd3d8c07464fa41d5a05340005ab180025dc85d8 Mon Sep 17 00:00:00 2001 From: server Date: Mon, 13 Apr 2026 22:47:06 +0200 Subject: [PATCH] db: prepare login and empire select queries --- src/db/ClientManager.cpp | 75 +++++++++++++++++++++++------------ src/db/ClientManagerLogin.cpp | 17 ++------ 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/db/ClientManager.cpp b/src/db/ClientManager.cpp index b61c65f..1035956 100644 --- a/src/db/ClientManager.cpp +++ b/src/db/ClientManager.cpp @@ -1067,26 +1067,42 @@ void CClientManager::QUERY_SAFEBOX_SAVE(CPeer * pkPeer, TSafeboxTable * pTable) void CClientManager::QUERY_EMPIRE_SELECT(CPeer * pkPeer, DWORD dwHandle, TEmpireSelectPacket * p) { - char szQuery[QUERY_MAX_LEN]; + CStmt updateEmpireStmt; + const std::string updateEmpireQuery = std::string("UPDATE player_index") + GetTablePostfix() + " SET empire=? WHERE id=?"; - snprintf(szQuery, sizeof(szQuery), "UPDATE player_index%s SET empire=%u WHERE id=%u", GetTablePostfix(), p->bEmpire, p->dwAccountID); - CDBManager::instance().DirectQuery(szQuery); - - sys_log(0, "EmpireSelect: %s", szQuery); + if (!PrepareClientPlayerStmt(updateEmpireStmt, updateEmpireQuery) + || !updateEmpireStmt.BindParam(MYSQL_TYPE_TINY, &p->bEmpire) + || !updateEmpireStmt.BindParam(MYSQL_TYPE_LONG, &p->dwAccountID) + || !updateEmpireStmt.Execute()) { - snprintf(szQuery, sizeof(szQuery), - "SELECT pid1, pid2, pid3, pid4 FROM player_index%s WHERE id=%u", GetTablePostfix(), p->dwAccountID); + sys_err("EmpireSelect: failed to update empire for account %u", p->dwAccountID); + pkPeer->EncodeHeader(DG::EMPIRE_SELECT, dwHandle, sizeof(BYTE)); + pkPeer->EncodeBYTE(p->bEmpire); + return; + } - auto pmsg = CDBManager::instance().DirectQuery(szQuery); + sys_log(0, "EmpireSelect: account %u empire %u", p->dwAccountID, p->bEmpire); + { + CStmt playerIndexStmt; + DWORD pids[PLAYER_PER_ACCOUNT] = {}; + const std::string playerIndexQuery = std::string("SELECT pid1, pid2, pid3, pid4 FROM player_index") + + GetTablePostfix() + " WHERE id=?"; - SQLResult * pRes = pmsg->Get(); - - if (pRes->uiNumRows) + if (!PrepareClientPlayerStmt(playerIndexStmt, playerIndexQuery) + || !playerIndexStmt.BindParam(MYSQL_TYPE_LONG, &p->dwAccountID) + || !playerIndexStmt.BindResult(MYSQL_TYPE_LONG, &pids[0]) + || !playerIndexStmt.BindResult(MYSQL_TYPE_LONG, &pids[1]) + || !playerIndexStmt.BindResult(MYSQL_TYPE_LONG, &pids[2]) + || !playerIndexStmt.BindResult(MYSQL_TYPE_LONG, &pids[3]) + || !playerIndexStmt.Execute()) { - sys_log(0, "EMPIRE %lu", pRes->uiNumRows); - - MYSQL_ROW row = mysql_fetch_row(pRes->pSQLResult); - DWORD pids[3]; + sys_err("EmpireSelect: failed to load player_index for account %u", p->dwAccountID); + } + else if (playerIndexStmt.iRows && playerIndexStmt.Fetch()) + { + CStmt moveEmpirePlayerStmt; + const std::string moveEmpirePlayerQuery = std::string("UPDATE player") + GetTablePostfix() + + " SET map_index=?,x=?,y=? WHERE id=?"; UINT g_start_map[4] = { @@ -1105,24 +1121,33 @@ void CClientManager::QUERY_EMPIRE_SELECT(CPeer * pkPeer, DWORD dwHandle, TEmpire { 969600, 278400 } // 진노국 }; - for (int i = 0; i < 3; ++i) + if (!PrepareClientPlayerStmt(moveEmpirePlayerStmt, moveEmpirePlayerQuery)) { - str_to_number(pids[i], row[i]); - sys_log(0, "EMPIRE PIDS[%d]", pids[i]); + sys_err("EmpireSelect: failed to prepare player move query"); + } + else for (int i = 0; i < 3; ++i) + { + sys_log(0, "EMPIRE PIDS[%u]", pids[i]); if (pids[i]) { sys_log(0, "EMPIRE move to pid[%d] to villiage of %u, map_index %d", pids[i], p->bEmpire, g_start_map[p->bEmpire]); - snprintf(szQuery, sizeof(szQuery), "UPDATE player%s SET map_index=%u,x=%u,y=%u WHERE id=%u", - GetTablePostfix(), - g_start_map[p->bEmpire], - g_start_position[p->bEmpire][0], - g_start_position[p->bEmpire][1], - pids[i]); + uint32_t mapIndex = g_start_map[p->bEmpire]; + uint32_t x = g_start_position[p->bEmpire][0]; + uint32_t y = g_start_position[p->bEmpire][1]; + uint32_t playerId = pids[i]; - auto pmsg2 = CDBManager::instance().DirectQuery(szQuery); + if (!moveEmpirePlayerStmt.BindParam(MYSQL_TYPE_LONG, &mapIndex) + || !moveEmpirePlayerStmt.BindParam(MYSQL_TYPE_LONG, &x) + || !moveEmpirePlayerStmt.BindParam(MYSQL_TYPE_LONG, &y) + || !moveEmpirePlayerStmt.BindParam(MYSQL_TYPE_LONG, &playerId) + || !moveEmpirePlayerStmt.Execute()) + { + sys_err("EmpireSelect: failed to move pid %u", playerId); + break; + } } } } diff --git a/src/db/ClientManagerLogin.cpp b/src/db/ClientManagerLogin.cpp index 3078ae8..e511f83 100644 --- a/src/db/ClientManagerLogin.cpp +++ b/src/db/ClientManagerLogin.cpp @@ -404,22 +404,13 @@ void CClientManager::RESULT_LOGIN_BY_KEY(CPeer * peer, SQLMsg * msg) if (msg->Get()->uiNumRows == 0) { DWORD account_id = info->pAccountTable->id; - char szQuery[QUERY_MAX_LEN]; - snprintf(szQuery, sizeof(szQuery), "SELECT pid1, pid2, pid3, pid4, empire FROM player_index%s WHERE id=%u", GetTablePostfix(), account_id); - auto pMsg = CDBManager::instance().DirectQuery(szQuery, SQL_PLAYER); sys_log(0, "RESULT_LOGIN_BY_KEY FAIL player_index's NULL : ID:%d", account_id); - if (pMsg->Get()->uiNumRows == 0) - { - sys_log(0, "RESULT_LOGIN_BY_KEY FAIL player_index's NULL : ID:%d", account_id); - - // PLAYER_INDEX_CREATE_BUG_FIX - //snprintf(szQuery, sizeof(szQuery), "INSERT IGNORE INTO player_index%s (id) VALUES(%lu)", GetTablePostfix(), info->pAccountTable->id); - snprintf(szQuery, sizeof(szQuery), "INSERT INTO player_index%s (id) VALUES(%u)", GetTablePostfix(), info->pAccountTable->id); - CDBManager::instance().ReturnQuery(szQuery, QID_PLAYER_INDEX_CREATE, peer->GetHandle(), info); - // END_PLAYER_INDEX_CREATE_BUF_FIX - } + // PLAYER_INDEX_CREATE_BUG_FIX + snprintf(szQuery, sizeof(szQuery), "INSERT INTO player_index%s (id) VALUES(%u)", GetTablePostfix(), info->pAccountTable->id); + CDBManager::instance().ReturnQuery(szQuery, QID_PLAYER_INDEX_CREATE, peer->GetHandle(), info); + // END_PLAYER_INDEX_CREATE_BUF_FIX return; }