diff --git a/src/game/guild_manager.cpp b/src/game/guild_manager.cpp index 0d74c1b..d48e5aa 100644 --- a/src/game/guild_manager.cpp +++ b/src/game/guild_manager.cpp @@ -64,6 +64,36 @@ namespace return true; } + bool LoadGuildIds(std::vector& 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 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(); diff --git a/src/game/questlua_building.cpp b/src/game/questlua_building.cpp index 2932dcb..f58b7ff 100644 --- a/src/game/questlua_building.cpp +++ b/src/game/questlua_building.cpp @@ -6,6 +6,53 @@ #include "guild.h" #include "db.h" #include "building.h" +#include "libsql/Statement.h" + +#include + +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(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; }