game: prepare direct query callsites

This commit is contained in:
server
2026-04-13 22:01:46 +02:00
parent 74827a1907
commit 4e9b721c44
2 changed files with 85 additions and 32 deletions

View File

@@ -64,6 +64,36 @@ namespace
return true;
}
bool LoadGuildIds(std::vector<DWORD>& guildIds)
{
CStmt stmt;
DWORD guildId = 0;
const std::string query = std::string("SELECT id FROM guild") + get_table_postfix();
guildIds.clear();
if (!PrepareGameStmt(stmt, query))
return false;
if (!stmt.BindResult(MYSQL_TYPE_LONG, &guildId)
|| !stmt.Execute())
{
return false;
}
guildIds.reserve(stmt.iRows);
for (int i = 0; i < stmt.iRows; ++i)
{
if (!stmt.Fetch())
return false;
guildIds.push_back(guildId);
}
return true;
}
struct FGuildNameSender
{
@@ -245,19 +275,12 @@ void CGuildManager::Initialize()
return;
}
auto pmsg = DBManager::instance().DirectQuery("SELECT id FROM guild%s", get_table_postfix());
std::vector<DWORD> vecGuildID;
vecGuildID.reserve(pmsg->Get()->uiNumRows);
if (!LoadGuildIds(vecGuildID))
return;
for (uint i = 0; i < pmsg->Get()->uiNumRows; i++)
{
MYSQL_ROW row = mysql_fetch_row(pmsg->Get()->pSQLResult);
DWORD guild_id = strtoul(row[0], (char**) NULL, 10);
LoadGuild(guild_id);
vecGuildID.push_back(guild_id);
}
for (const auto guildId : vecGuildID)
LoadGuild(guildId);
CGuildMarkManager & rkMarkMgr = CGuildMarkManager::instance();

View File

@@ -6,6 +6,53 @@
#include "guild.h"
#include "db.h"
#include "building.h"
#include "libsql/Statement.h"
#include <string>
namespace
{
bool PrepareGameStmt(CStmt& stmt, const std::string& query)
{
CAsyncSQL* sql = DBManager::instance().GetDirectSQL();
if (!sql)
{
sys_err("game direct SQL handle is not initialized");
return false;
}
return stmt.Prepare(sql, query.c_str());
}
bool HasGuildLand(uint32_t guildId, bool& hasLand)
{
CStmt stmt;
uint32_t count = 0;
const std::string query = std::string("SELECT COUNT(*) FROM land") + get_table_postfix() + " WHERE guild_id=?";
hasLand = true;
if (!PrepareGameStmt(stmt, query))
return false;
if (!stmt.BindParam(MYSQL_TYPE_LONG, &guildId)
|| !stmt.BindResult(MYSQL_TYPE_LONG, &count)
|| !stmt.Execute())
{
return false;
}
if (stmt.iRows == 0)
return true;
if (!stmt.Fetch())
return false;
hasLand = count != 0;
return true;
}
}
namespace quest
{
@@ -97,28 +144,11 @@ namespace quest
lua_pushboolean(L, false);
*/
auto pmsg = DBManager::instance().DirectQuery("SELECT COUNT(*) FROM land%s WHERE guild_id = %d", get_table_postfix(), (DWORD)lua_tonumber(L,1));
bool hasLand = true;
const auto guildId = static_cast<uint32_t>(lua_tonumber(L, 1));
if ( pmsg->Get()->uiNumRows > 0 )
{
MYSQL_ROW row = mysql_fetch_row(pmsg->Get()->pSQLResult);
int count = 0;
str_to_number(count, row[0]);
if (count == 0)
{
lua_pushboolean(L, false);
}
else
{
lua_pushboolean(L, true);
}
}
else
{
lua_pushboolean(L, true);
}
HasGuildLand(guildId, hasLand);
lua_pushboolean(L, hasLand);
return 1;
}