wesnothd: use lambdas instead of rs_base (#9747)

This commit is contained in:
Pentarctagon 2025-01-15 20:14:00 -06:00 committed by GitHub
parent 5a7bd7c51f
commit d8abcff103
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 159 additions and 444 deletions

View File

@ -109,12 +109,6 @@
<Unit filename="../../src/server/common/server_base.hpp" />
<Unit filename="../../src/server/common/simple_wml.cpp" />
<Unit filename="../../src/server/common/simple_wml.hpp" />
<Unit filename="../../src/server/common/resultsets/ban_check.cpp" />
<Unit filename="../../src/server/common/resultsets/ban_check.hpp" />
<Unit filename="../../src/server/common/resultsets/game_history.cpp" />
<Unit filename="../../src/server/common/resultsets/game_history.hpp" />
<Unit filename="../../src/server/common/resultsets/tournaments.cpp" />
<Unit filename="../../src/server/common/resultsets/tournaments.hpp" />
<Unit filename="../../src/server/common/user_handler.hpp" />
<Unit filename="../../src/server/room.hpp" />
<Unit filename="../../src/server/wesnothd/ban.cpp" />

View File

@ -144,12 +144,6 @@
<Unit filename="../../src/server/common/server_base.hpp" />
<Unit filename="../../src/server/common/simple_wml.cpp" />
<Unit filename="../../src/server/common/simple_wml.hpp" />
<Unit filename="../../src/server/common/resultsets/ban_check.cpp" />
<Unit filename="../../src/server/common/resultsets/ban_check.hpp" />
<Unit filename="../../src/server/common/resultsets/game_history.cpp" />
<Unit filename="../../src/server/common/resultsets/game_history.hpp" />
<Unit filename="../../src/server/common/resultsets/tournaments.cpp" />
<Unit filename="../../src/server/common/resultsets/tournaments.hpp" />
<Unit filename="../../src/server/common/user_handler.hpp" />
<Unit filename="../../src/server/room.hpp" />
<Unit filename="../../src/server/wesnothd/ban.cpp" />

View File

@ -67,10 +67,6 @@
460D89B824DC7863000B1ABC /* fs_commit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89B024DC7862000B1ABC /* fs_commit.cpp */; };
460D89B924DC7863000B1ABC /* blacklist.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89B424DC7862000B1ABC /* blacklist.cpp */; };
460D89BB24DC945F000B1ABC /* apple_video.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46A72F3C24D6F6B30033B5A9 /* apple_video.mm */; };
460D89C224DC95DD000B1ABC /* ban_check.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89BE24DC95DC000B1ABC /* ban_check.cpp */; };
460D89C324DC95DD000B1ABC /* ban_check.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89BE24DC95DC000B1ABC /* ban_check.cpp */; };
460D89C424DC95DD000B1ABC /* tournaments.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89BF24DC95DC000B1ABC /* tournaments.cpp */; };
460D89C524DC95DD000B1ABC /* tournaments.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 460D89BF24DC95DC000B1ABC /* tournaments.cpp */; };
46181DCF2119F73A00908BC2 /* apple_battery_info.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46181DCE2119F73A00908BC2 /* apple_battery_info.mm */; };
46181DD22119F74D00908BC2 /* battery_info.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46181DD12119F74C00908BC2 /* battery_info.cpp */; };
461CE967205C3A8700E6E98F /* base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 469BDB53205C357400DBF748 /* base64.cpp */; };
@ -175,8 +171,6 @@
4685124C24AE1A0B005B6EB1 /* game_config_view.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4685124824AE1535005B6EB1 /* game_config_view.cpp */; };
4685124D24AE1A15005B6EB1 /* game_config_view.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4685124824AE1535005B6EB1 /* game_config_view.cpp */; };
4685A5FB25B4501E006FD3A1 /* match_history.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4685A5F925B4501E006FD3A1 /* match_history.cpp */; };
4685A60225B45064006FD3A1 /* game_history.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4685A60025B45064006FD3A1 /* game_history.cpp */; };
4685A60325B45064006FD3A1 /* game_history.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4685A60025B45064006FD3A1 /* game_history.cpp */; };
468A5B93258CD3B5004A80EF /* libboost_chrono-mt.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 468A5B85258CD3B4004A80EF /* libboost_chrono-mt.dylib */; };
468A5B94258CD3B5004A80EF /* libboost_context-mt.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 468A5B86258CD3B4004A80EF /* libboost_context-mt.dylib */; };
468A5B95258CD3B5004A80EF /* libboost_prg_exec_monitor-mt.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 468A5B87258CD3B4004A80EF /* libboost_prg_exec_monitor-mt.dylib */; };
@ -1648,11 +1642,6 @@
460D89B324DC7862000B1ABC /* blacklist.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = blacklist.hpp; path = ../../src/server/campaignd/blacklist.hpp; sourceTree = SOURCE_ROOT; };
460D89B424DC7862000B1ABC /* blacklist.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = blacklist.cpp; path = ../../src/server/campaignd/blacklist.cpp; sourceTree = SOURCE_ROOT; };
460D89B524DC7862000B1ABC /* addon_utils.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = addon_utils.hpp; path = ../../src/server/campaignd/addon_utils.hpp; sourceTree = SOURCE_ROOT; };
460D89BD24DC95DB000B1ABC /* rs_base.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = rs_base.hpp; path = ../../src/server/common/resultsets/rs_base.hpp; sourceTree = SOURCE_ROOT; };
460D89BE24DC95DC000B1ABC /* ban_check.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ban_check.cpp; path = ../../src/server/common/resultsets/ban_check.cpp; sourceTree = SOURCE_ROOT; };
460D89BF24DC95DC000B1ABC /* tournaments.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tournaments.cpp; path = ../../src/server/common/resultsets/tournaments.cpp; sourceTree = SOURCE_ROOT; };
460D89C024DC95DC000B1ABC /* ban_check.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ban_check.hpp; path = ../../src/server/common/resultsets/ban_check.hpp; sourceTree = SOURCE_ROOT; };
460D89C124DC95DC000B1ABC /* tournaments.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = tournaments.hpp; path = ../../src/server/common/resultsets/tournaments.hpp; sourceTree = SOURCE_ROOT; };
46181DCD2119F73A00908BC2 /* apple_battery_info.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = apple_battery_info.hpp; path = ../../src/desktop/apple_battery_info.hpp; sourceTree = "<group>"; };
46181DCE2119F73A00908BC2 /* apple_battery_info.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = apple_battery_info.mm; path = ../../src/desktop/apple_battery_info.mm; sourceTree = "<group>"; };
46181DD02119F74C00908BC2 /* battery_info.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = battery_info.hpp; sourceTree = "<group>"; };
@ -1711,8 +1700,6 @@
4685124924AE1535005B6EB1 /* game_config_view.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = game_config_view.hpp; sourceTree = "<group>"; };
4685A5F925B4501E006FD3A1 /* match_history.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = match_history.cpp; sourceTree = "<group>"; };
4685A5FA25B4501E006FD3A1 /* match_history.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = match_history.hpp; sourceTree = "<group>"; };
4685A60025B45064006FD3A1 /* game_history.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = game_history.cpp; sourceTree = "<group>"; };
4685A60125B45064006FD3A1 /* game_history.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = game_history.hpp; sourceTree = "<group>"; };
4685E3702417F9BF00CB1ED8 /* data */ = {isa = PBXFileReference; lastKnownFileType = folder; name = data; path = ../../data; sourceTree = "<group>"; };
468A5B85258CD3B4004A80EF /* libboost_chrono-mt.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libboost_chrono-mt.dylib"; path = "lib/libboost_chrono-mt.dylib"; sourceTree = "<group>"; };
468A5B86258CD3B4004A80EF /* libboost_context-mt.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libboost_context-mt.dylib"; path = "lib/libboost_context-mt.dylib"; sourceTree = "<group>"; };
@ -3646,7 +3633,6 @@
460D899624DC7842000B1ABC /* dbconn.hpp */,
460D899324DC7842000B1ABC /* forum_user_handler.cpp */,
460D898D24DC7841000B1ABC /* forum_user_handler.hpp */,
460D89BC24DC95A2000B1ABC /* resultsets */,
460D899824DC7842000B1ABC /* server_base.cpp */,
460D899724DC7842000B1ABC /* server_base.hpp */,
460D899524DC7842000B1ABC /* simple_wml.cpp */,
@ -3656,20 +3642,6 @@
path = common;
sourceTree = "<group>";
};
460D89BC24DC95A2000B1ABC /* resultsets */ = {
isa = PBXGroup;
children = (
460D89BE24DC95DC000B1ABC /* ban_check.cpp */,
460D89C024DC95DC000B1ABC /* ban_check.hpp */,
4685A60025B45064006FD3A1 /* game_history.cpp */,
4685A60125B45064006FD3A1 /* game_history.hpp */,
460D89BD24DC95DB000B1ABC /* rs_base.hpp */,
460D89BF24DC95DC000B1ABC /* tournaments.cpp */,
460D89C124DC95DC000B1ABC /* tournaments.hpp */,
);
path = resultsets;
sourceTree = "<group>";
};
46685C7F219D518A0009CFFE /* schema */ = {
isa = PBXGroup;
children = (
@ -6054,12 +6026,9 @@
464F7FDA25937D74006AB37B /* server_base.cpp in Sources */,
46CAC4CE29BF23CC004763BE /* mt_rng.cpp in Sources */,
91C548E01D886E2C00FE6A7B /* tstring.cpp in Sources */,
460D89C524DC95DD000B1ABC /* tournaments.cpp in Sources */,
460D89B624DC7863000B1ABC /* addon_utils.cpp in Sources */,
460D89C324DC95DD000B1ABC /* ban_check.cpp in Sources */,
91C548F01D88703300FE6A7B /* validation.cpp in Sources */,
46F57085205FCE48007031BF /* crypt_blowfish.c in Sources */,
4685A60325B45064006FD3A1 /* game_history.cpp in Sources */,
91C548F41D8870C700FE6A7B /* game_version.cpp in Sources */,
460D899E24DC7843000B1ABC /* forum_user_handler.cpp in Sources */,
915C68F21DF1F5C600594B07 /* color.cpp in Sources */,
@ -6664,12 +6633,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
460D89C424DC95DD000B1ABC /* tournaments.cpp in Sources */,
EC0341E21ECF4712000F2E2B /* config_attribute_value.cpp in Sources */,
460D89C224DC95DD000B1ABC /* ban_check.cpp in Sources */,
460D898824DC7831000B1ABC /* player_connection.cpp in Sources */,
B5BB6C820F89437400444FBF /* color_range.cpp in Sources */,
4685A60225B45064006FD3A1 /* game_history.cpp in Sources */,
460D898624DC7831000B1ABC /* game.cpp in Sources */,
B5BB6C860F8943F600444FBF /* config_cache.cpp in Sources */,
46685CA1219D52DB0009CFFE /* parser.cpp in Sources */,

View File

@ -7,7 +7,4 @@ server/campaignd/options.cpp
server/campaignd/server.cpp
server/common/dbconn.cpp
server/common/forum_user_handler.cpp
server/common/resultsets/ban_check.cpp
server/common/resultsets/game_history.cpp
server/common/resultsets/tournaments.cpp
server/common/server_base.cpp

View File

@ -1,8 +1,5 @@
server/common/dbconn.cpp
server/common/forum_user_handler.cpp
server/common/resultsets/ban_check.cpp
server/common/resultsets/game_history.cpp
server/common/resultsets/tournaments.cpp
server/common/server_base.cpp
server/wesnothd/ban.cpp
server/wesnothd/game.cpp

View File

@ -15,13 +15,11 @@
#ifdef HAVE_MYSQLPP
#include "server/common/dbconn.hpp"
#include "server/common/resultsets/tournaments.hpp"
#include "server/common/resultsets/ban_check.hpp"
#include "server/common/resultsets/game_history.hpp"
#include "log.hpp"
#include "serialization/unicode.hpp"
#include "serialization/parser.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
static lg::log_domain log_sql_handler("sql_executor");
#define ERR_SQL LOG_STREAM(err, log_sql_handler)
@ -105,11 +103,28 @@ std::string dbconn::get_tournaments()
return "";
}
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
while(rslt->next()) {
config& child = c.add_child("tournament");
child["title"] = rslt->get_string("TITLE");
child["status"] = rslt->get_string("STATUS");
child["url"] = rslt->get_string("URL");
}
return c;
};
try
{
tournaments t;
get_complex_results(connection_, t, db_tournament_query_, {});
return t.str();
config t = get_complex_results(connection_, &cfg_result, db_tournament_query_, {});
std::string text;
for(const auto& child : t.child_range("tournament"))
{
text += "\nThe tournament "+child["title"].str()+" is "+child["status"].str()+". More information can be found at "+child["url"].str();
}
return text;
}
catch(const mariadb::exception::base& e)
{
@ -120,6 +135,42 @@ std::string dbconn::get_tournaments()
std::unique_ptr<simple_wml::document> dbconn::get_game_history(int player_id, int offset, std::string search_game_name, int search_content_type, std::string search_content)
{
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
while(rslt->next())
{
config& child = c.add_child("game");
child["game_name"] = rslt->get_string("GAME_NAME");
child["game_start"] = rslt->get_date_time("START_TIME").str();
child["scenario_name"] = rslt->get_string("SCENARIO_NAME");
child["era_name"] = rslt->get_string("ERA_NAME");
for(const auto& player_info : utils::split(rslt->get_string("PLAYERS")))
{
std::vector<std::string> info = utils::split(player_info, ':');
config& pchild = child.add_child("player");
if(info.size() == 2)
{
pchild["name"] = info[0];
pchild["faction"] = info[1];
}
else
{
ERR_SQL << "Expected player information to split into two fields, instead found the value `" << player_info << "`.";
}
}
for(const std::string& mod : utils::split(rslt->get_string("MODIFICATION_NAMES")))
{
config& mchild = child.add_child("modification");
mchild["name"] = mod;
}
child["replay_url"] = rslt->get_string("REPLAY_URL");
child["version"] = rslt->get_string("VERSION");
}
return c;
};
try
{
// if no parameters populated, return an error
@ -233,12 +284,35 @@ std::unique_ptr<simple_wml::document> dbconn::get_game_history(int player_id, in
DBG_SQL << "game history query text for player " << player_id << ": " << game_history_query;
game_history gh;
get_complex_results(connection, gh, game_history_query, params);
config history = get_complex_results(connection, &cfg_result, game_history_query, params);
DBG_SQL << "after game history query for player " << player_id;
auto doc = gh.to_doc();
auto doc = std::make_unique<simple_wml::document>();
simple_wml::node& results_wml = doc->root().add_child("game_history_results");
for(const auto& result : history.child_range("game"))
{
simple_wml::node& ghr = results_wml.add_child("game_history_result");
ghr.set_attr_dup("game_name", result["game_name"].str().c_str());
ghr.set_attr_dup("game_start", result["game_start"].str().c_str());
ghr.set_attr_dup("scenario_name", result["scenario_name"].str().c_str());
ghr.set_attr_dup("era_name", result["era_name"].str().c_str());
ghr.set_attr_dup("replay_url", result["replay_url"].str().c_str());
ghr.set_attr_dup("version", result["version"].str().c_str());
for(const auto& player : result.child_range("player"))
{
simple_wml::node& p = ghr.add_child("player");
p.set_attr_dup("name", player["name"].str().c_str());
p.set_attr_dup("faction", player["faction"].str().c_str());
}
for(const auto& mod : result.child_range("modification"))
{
simple_wml::node& m = ghr.add_child("modification");
m.set_attr_dup("name", mod["name"].str().c_str());
}
}
DBG_SQL << "after parsing results of game history query for player " << player_id;
@ -315,20 +389,32 @@ bool dbconn::is_user_in_groups(const std::string& name, const std::vector<int>&
}
}
ban_check dbconn::get_ban_info(const std::string& name, const std::string& ip)
config dbconn::get_ban_info(const std::string& name, const std::string& ip)
{
// selected ban_type value must be part of user_handler::BAN_TYPE
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
if(rslt->next()) {
c["ban_type"] = rslt->get_signed32("ban_type");
c["ban_end"] = rslt->get_signed32("ban_end");
c["user_id"] = rslt->get_signed32("ban_userid");
c["email"] = rslt->get_string("ban_email");
}
return c;
};
try
{
// selected ban_type value must be part of user_handler::BAN_TYPE
ban_check b;
get_complex_results(connection_, b, "select ban_userid, ban_email, case when ban_ip != '' then 1 when ban_userid != 0 then 2 when ban_email != '' then 3 end as ban_type, ban_end from `"+db_banlist_table_+"` where (ban_ip = ? or ban_userid = (select user_id from `"+db_users_table_+"` where UPPER(username) = UPPER(?)) or UPPER(ban_email) = (select UPPER(user_email) from `"+db_users_table_+"` where UPPER(username) = UPPER(?))) AND ban_exclude = 0 AND (ban_end = 0 OR ban_end >= ?)",
config f = get_complex_results(connection_, &cfg_result, "select ban_userid, ban_email, case when ban_ip != '' then 1 when ban_userid != 0 then 2 when ban_email != '' then 3 end as ban_type, ban_end from `"+db_banlist_table_+"` where (ban_ip = ? or ban_userid = (select user_id from `"+db_users_table_+"` where UPPER(username) = UPPER(?)) or UPPER(ban_email) = (select UPPER(user_email) from `"+db_users_table_+"` where UPPER(username) = UPPER(?))) AND ban_exclude = 0 AND (ban_end = 0 OR ban_end >= ?)",
{ ip, name, name, std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) });
return b;
return f;
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Failed to check ban info for user '"+name+"' connecting from ip '"+ip+"'!", e);
return ban_check();
return {};
}
}
@ -593,7 +679,7 @@ bool dbconn::do_any_authors_exist(const std::string& instance_version, const std
}
config dbconn::get_addon_downloads_info(const std::string& instance_version, const std::string& id) {
auto cfg_result = [](const mariadb::result_set_ref& rslt) -> config {
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
while(rslt->next()) {
@ -606,12 +692,21 @@ config dbconn::get_addon_downloads_info(const std::string& instance_version, con
return c;
};
return get_complex_results(connection_, &cfg_result, "select ADDON_NAME, VERSION, UPLOADED_ON, DOWNLOAD_COUNT from "+db_addon_info_table_+" where INSTANCE_VERSION = ? and ADDON_ID = ? order by ADDON_NAME, UPLOADED_ON",
{ instance_version, id });
try
{
return get_complex_results(connection_, &cfg_result, "select ADDON_NAME, VERSION, UPLOADED_ON, DOWNLOAD_COUNT from "+db_addon_info_table_+" where INSTANCE_VERSION = ? and ADDON_ID = ? order by ADDON_NAME, UPLOADED_ON",
{ instance_version, id });
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Failed to get addon download info!", e);
return {};
}
}
config dbconn::get_forum_auth_usage(const std::string& instance_version) {
auto cfg_result = [](const mariadb::result_set_ref& rslt) -> config {
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
if(rslt->next()) {
@ -623,13 +718,22 @@ config dbconn::get_forum_auth_usage(const std::string& instance_version) {
return c;
};
return get_complex_results(connection_, &cfg_result, "select (select count(distinct ADDON_ID) from "+db_addon_info_table_+" where INSTANCE_VERSION = ?) as ALL_COUNT, "
"(select count(distinct ADDON_ID) from "+db_addon_info_table_+" where INSTANCE_VERSION = ? and FORUM_AUTH = 1) as FORUM_AUTH_COUNT from dual",
{ instance_version, instance_version });
try
{
return get_complex_results(connection_, &cfg_result, "select (select count(distinct ADDON_ID) from "+db_addon_info_table_+" where INSTANCE_VERSION = ?) as ALL_COUNT, "
"(select count(distinct ADDON_ID) from "+db_addon_info_table_+" where INSTANCE_VERSION = ? and FORUM_AUTH = 1) as FORUM_AUTH_COUNT from dual",
{ instance_version, instance_version });
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Failed to get forum_auth usage!", e);
return {};
}
}
config dbconn::get_addon_admins(int site_admin_group, int forum_admin_group) {
auto cfg_result = [](const mariadb::result_set_ref& rslt) -> config {
auto cfg_result = [](const mariadb::result_set_ref& rslt) {
config c;
while(rslt->next()) {
@ -639,24 +743,27 @@ config dbconn::get_addon_admins(int site_admin_group, int forum_admin_group) {
return c;
};
return get_complex_results(connection_, &cfg_result, "SELECT u.USERNAME FROM `"+db_users_table_+"` u, `"+db_user_group_table_+"` ug WHERE u.USER_ID = ug.USER_ID AND ug.GROUP_ID in (?, ?)",
{ site_admin_group, forum_admin_group });
try
{
return get_complex_results(connection_, &cfg_result, "SELECT u.USERNAME FROM `"+db_users_table_+"` u, `"+db_user_group_table_+"` ug WHERE u.USER_ID = ug.USER_ID AND ug.GROUP_ID in (?, ?)",
{ site_admin_group, forum_admin_group });
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Failed to get addon admins for groups '"+std::to_string(site_admin_group)+"' and '"+std::to_string(forum_admin_group)+"'!", e);
return {};
}
}
//
// handle complex query results
//
void dbconn::get_complex_results(const mariadb::connection_ref& connection, rs_base& base, const std::string& sql, const sql_parameters& params)
{
mariadb::result_set_ref rslt = select(connection, sql, params);
base.read(rslt);
}
template <typename F>
config dbconn::get_complex_results(const mariadb::connection_ref& connection, F* func, const std::string& sql, const sql_parameters& params)
config dbconn::get_complex_results(const mariadb::connection_ref& connection, F* handler, const std::string& sql, const sql_parameters& params)
{
mariadb::result_set_ref rslt = select(connection, sql, params);
config c = (*func)(rslt);
config c = (*handler)(rslt);
return c;
}
//

View File

@ -15,8 +15,6 @@
#pragma once
#include "config.hpp"
#include "server/common/resultsets/rs_base.hpp"
#include "server/common/resultsets/ban_check.hpp"
#include "server/common/simple_wml.hpp"
#include <mysql/mysql.h>
@ -127,7 +125,7 @@ public:
/**
* @see forum_user_handler::user_is_banned().
*/
ban_check get_ban_info(const std::string& name, const std::string& ip);
config get_ban_info(const std::string& name, const std::string& ip);
/**
* @see forum_user_handler::db_insert_game_info().
@ -310,21 +308,14 @@ private:
mariadb::connection_ref create_connection();
/**
* Queries can return data with various types that can't be easily fit into a pre-determined structure.
* Therefore for queries that can return multiple rows with multiple columns, a class that extends @ref rs_base
* handles reading the results.
*
* @param connection The database connection that will be used to execute the query.
* @param base The class that will handle reading the results.
* @param handler The lambda that will handle reading the results into a config.
* @param sql The SQL text to be executed.
* @param params The parameterized values to be inserted into the query.
*/
void get_complex_results(
const mariadb::connection_ref& connection, rs_base& base, const std::string& sql, const sql_parameters& params);
template <typename F>
config get_complex_results(
const mariadb::connection_ref& connection, F* func, const std::string& sql, const sql_parameters& params);
const mariadb::connection_ref& connection, F* handler, const std::string& sql, const sql_parameters& params);
/**
* @param connection The database connection that will be used to execute the query.

View File

@ -145,22 +145,29 @@ void fuh::set_is_moderator(const std::string& name, const bool& is_moderator) {
fuh::ban_info fuh::user_is_banned(const std::string& name, const std::string& addr)
{
ban_check b = conn_.get_ban_info(name, addr);
switch(b.get_ban_type())
config b = conn_.get_ban_info(name, addr);
std::chrono::seconds ban_duration(0);
if(b["ban_end"].to_unsigned() != 0) {
auto time_remaining = chrono::parse_timestamp(b["ban_end"].to_unsigned()) - std::chrono::system_clock::now();
ban_duration = std::chrono::duration_cast<std::chrono::seconds>(time_remaining);
}
switch(b["ban_type"].to_int())
{
case BAN_NONE:
return {};
case BAN_IP:
LOG_UH << "User '" << name << "' ip " << addr << " banned by IP address";
return { BAN_IP, b.get_ban_duration() };
return { BAN_IP, ban_duration };
case BAN_USER:
LOG_UH << "User '" << name << "' uid " << b.get_user_id() << " banned by uid";
return { BAN_USER, b.get_ban_duration() };
LOG_UH << "User '" << name << "' uid " << b["user_id"].str() << " banned by uid";
return { BAN_USER, ban_duration };
case BAN_EMAIL:
LOG_UH << "User '" << name << "' email " << b.get_email() << " banned by email address";
return { BAN_EMAIL, b.get_ban_duration() };
LOG_UH << "User '" << name << "' email " << b["email"].str() << " banned by email address";
return { BAN_EMAIL, ban_duration };
default:
ERR_UH << "Invalid ban type '" << b.get_ban_type() << "' returned for user '" << name << "'";
ERR_UH << "Invalid ban type '" << b["ban_type"].to_int() << "' returned for user '" << name << "'";
return {};
}
}

View File

@ -1,67 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef HAVE_MYSQLPP
#include "server/common/resultsets/ban_check.hpp"
#include "server/common/user_handler.hpp"
#include "serialization/chrono.hpp"
ban_check::ban_check()
: ban_type(user_handler::BAN_TYPE::BAN_NONE)
, ban_duration(0)
, user_id(0)
, email()
{
}
ban_check::ban_check(const mariadb::result_set& rslt)
: ban_type(rslt.get_signed32("ban_type"))
, ban_duration(0)
, user_id(rslt.get_signed32("ban_userid"))
, email(rslt.get_string("ban_email"))
{
auto ban_end = rslt.get_signed32("ban_end");
if(ban_end == 0) return;
auto time_remaining = chrono::parse_timestamp(ban_end) - std::chrono::system_clock::now();
ban_duration = std::chrono::duration_cast<std::chrono::seconds>(time_remaining);
}
void ban_check::read(mariadb::result_set_ref rslt)
{
if(rslt->next()) { *this = ban_check{*rslt}; }
}
long ban_check::get_ban_type()
{
return ban_type;
}
std::chrono::seconds ban_check::get_ban_duration()
{
return ban_duration;
}
int ban_check::get_user_id()
{
return user_id;
}
std::string ban_check::get_email()
{
return email;
}
#endif //HAVE_MYSQLPP

View File

@ -1,39 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "mariadb++/result_set.hpp"
#include "server/common/resultsets/rs_base.hpp"
#include <chrono>
class ban_check : public rs_base
{
public:
ban_check();
explicit ban_check(const mariadb::result_set& rslt);
void read(mariadb::result_set_ref rslt) override;
long get_ban_type();
std::chrono::seconds get_ban_duration();
int get_user_id();
std::string get_email();
private:
long ban_type;
std::chrono::seconds ban_duration;
int user_id;
std::string email;
};

View File

@ -1,82 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef HAVE_MYSQLPP
#include "server/common/resultsets/game_history.hpp"
#include "serialization/string_utils.hpp"
#include "log.hpp"
static lg::log_domain log_sql_handler("sql_executor");
#define ERR_SQL LOG_STREAM(err, log_sql_handler)
void game_history::read(mariadb::result_set_ref rslt)
{
while(rslt->next())
{
result r;
r.game_name = rslt->get_string("GAME_NAME");
r.game_start = rslt->get_date_time("START_TIME").str();
r.scenario_name = rslt->get_string("SCENARIO_NAME");
r.era_name = rslt->get_string("ERA_NAME");
for(const auto& player_info : utils::split(rslt->get_string("PLAYERS")))
{
std::vector<std::string> info = utils::split(player_info, ':');
if(info.size() == 2)
{
r.players.emplace_back(player{ info[0], info[1] });
}
else
{
ERR_SQL << "Expected player information to split into two fields, instead found the value `" << player_info << "`.";
}
}
r.modification_names = utils::split(rslt->get_string("MODIFICATION_NAMES"));
r.replay_url = rslt->get_string("REPLAY_URL");
r.version = rslt->get_string("VERSION");
results.push_back(std::move(r));
}
}
std::unique_ptr<simple_wml::document> game_history::to_doc()
{
auto doc = std::make_unique<simple_wml::document>();
simple_wml::node& results_wml = doc->root().add_child("game_history_results");
for(const auto& result : results)
{
simple_wml::node& ghr = results_wml.add_child("game_history_result");
ghr.set_attr_dup("game_name", result.game_name.c_str());
ghr.set_attr_dup("game_start", result.game_start.c_str());
ghr.set_attr_dup("scenario_name", result.scenario_name.c_str());
ghr.set_attr_dup("era_name", result.era_name.c_str());
ghr.set_attr_dup("replay_url", result.replay_url.c_str());
ghr.set_attr_dup("version", result.version.c_str());
for(const auto& player : result.players)
{
simple_wml::node& p = ghr.add_child("player");
p.set_attr_dup("name", player.name.c_str());
p.set_attr_dup("faction", player.faction.c_str());
}
for(const auto& mod : result.modification_names)
{
simple_wml::node& m = ghr.add_child("modification");
m.set_attr_dup("name", mod.c_str());
}
}
return doc;
}
#endif //HAVE_MYSQLPP

View File

@ -1,50 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include <vector>
#include "mariadb++/result_set.hpp"
#include "server/common/resultsets/rs_base.hpp"
#include "server/common/simple_wml.hpp"
class game_history : public rs_base
{
struct player
{
std::string name;
std::string faction;
};
struct result
{
std::string game_name;
std::string game_start;
std::string scenario_name;
std::string era_name;
std::vector<player> players;
std::vector<std::string> modification_names;
std::string replay_url;
std::string version;
};
public:
void read(mariadb::result_set_ref rslt);
std::unique_ptr<simple_wml::document> to_doc();
private:
std::vector<result> results;
};

View File

@ -1,25 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "mariadb++/result_set.hpp"
class rs_base
{
public:
rs_base() {}
virtual ~rs_base() {}
virtual void read(mariadb::result_set_ref rslt) = 0;
};

View File

@ -1,37 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifdef HAVE_MYSQLPP
#include "server/common/resultsets/tournaments.hpp"
void tournaments::read(mariadb::result_set_ref rslt)
{
while(rslt->next())
{
rows.push_back(data{ rslt->get_string("TITLE"), rslt->get_string("STATUS"), rslt->get_string("URL") });
}
}
std::string tournaments::str()
{
std::string text;
for(const auto& row : rows)
{
text += "\nThe tournament "+row.title+" is "+row.status+". More information can be found at "+row.url;
}
return text;
}
#endif //HAVE_MYSQLPP

View File

@ -1,38 +0,0 @@
/*
Copyright (C) 2020 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "mariadb++/result_set.hpp"
#include "server/common/resultsets/rs_base.hpp"
#include <vector>
class tournaments : public rs_base
{
struct data
{
std::string title;
std::string status;
std::string url;
};
public:
void read(mariadb::result_set_ref rslt);
std::string str();
private:
std::vector<data> rows;
};