Skip to content

Commit

Permalink
Merge pull request #5386 from Pentarctagon/userid-userlist
Browse files Browse the repository at this point in the history
Send the player forum ids to the client on the player list.
  • Loading branch information
Vultraz committed Dec 30, 2020
2 parents e01fa8e + 5f26e9e commit d7a5fa2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/server/common/dbconn.cpp
Expand Up @@ -124,6 +124,19 @@ bool dbconn::user_exists(const std::string& name)
}
}

long dbconn::get_forum_id(const std::string& name)
{
try
{
return get_single_long(connection_, "SELECT IFNULL((SELECT user_id FROM `"+db_users_table_+"` WHERE UPPER(username)=UPPER(?)), 0)", name);
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Unable to get user_id for '"+name+"'!", e);
return 0;
}
}

bool dbconn::extra_row_exists(const std::string& name)
{
try
Expand Down Expand Up @@ -302,9 +315,12 @@ long dbconn::get_single_long(mariadb::connection_ref connection, const std::stri
if(rslt->next())
{
// mariadbpp checks for strict integral equivalence, but we don't care
// so check the type beforehand, call the associated getter, and let it silently get upcast to an int if needed
// so check the type beforehand, call the associated getter, and let it silently get upcast to a long if needed
// subselects also apparently return a decimal
switch(rslt->column_type(0))
{
case mariadb::value::type::decimal:
return static_cast<long>(rslt->get_decimal(0).float32());
case mariadb::value::type::unsigned8:
case mariadb::value::type::signed8:
return rslt->get_signed8(0);
Expand Down
1 change: 1 addition & 0 deletions src/server/common/dbconn.hpp
Expand Up @@ -37,6 +37,7 @@ class dbconn
std::string get_uuid();
std::string get_tournaments();
bool user_exists(const std::string& name);
long get_forum_id(const std::string& name);
bool extra_row_exists(const std::string& name);
bool is_user_in_group(const std::string& name, int group_id);
std::string get_user_string(const std::string& table, const std::string& column, const std::string& name);
Expand Down
4 changes: 4 additions & 0 deletions src/server/common/forum_user_handler.cpp
Expand Up @@ -114,6 +114,10 @@ bool fuh::user_exists(const std::string& name) {
return conn_.user_exists(name);
}

long fuh::get_forum_id(const std::string& name) {
return conn_.get_forum_id(name);
}

bool fuh::user_is_active(const std::string& name) {
int user_type = conn_.get_user_int(db_users_table_, "user_type", name);
return user_type != USER_INACTIVE && user_type != USER_IGNORE;
Expand Down
2 changes: 2 additions & 0 deletions src/server/common/forum_user_handler.hpp
Expand Up @@ -55,6 +55,8 @@ class fuh : public user_handler {

bool user_exists(const std::string& name);

long get_forum_id(const std::string& name);

bool user_is_active(const std::string& name);

bool user_is_moderator(const std::string& name);
Expand Down
3 changes: 3 additions & 0 deletions src/server/common/user_handler.hpp
Expand Up @@ -68,6 +68,9 @@ class user_handler {
/** Returns true if a user with the given name exists. */
virtual bool user_exists(const std::string& name) =0;

/** Returns the forum user id for the given username */
virtual long get_forum_id(const std::string& name) =0;

/** Returns true if the specified user account is usable for logins. */
virtual bool user_is_active(const std::string& name) =0;

Expand Down
3 changes: 2 additions & 1 deletion src/server/wesnothd/player.cpp
Expand Up @@ -15,7 +15,7 @@
#include "server/wesnothd/player.hpp"
#include "lexical_cast.hpp"

wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
wesnothd::player::player(const std::string& n, simple_wml::node& cfg, int id,
bool registered, const std::string& version, const std::string& source, const std::size_t max_messages,
const std::size_t time_period,
const bool moderator)
Expand All @@ -34,6 +34,7 @@ wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
cfg_.set_attr_dup("name", n.c_str());
cfg_.set_attr("registered", registered ? "yes" : "no");
cfg_.set_attr("moderator", moderator ? "yes" : "no");
cfg.set_attr_int("forum_id", id);
mark_available();
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/wesnothd/player.hpp
Expand Up @@ -30,7 +30,7 @@ class player
OBSERVING
};

player(const std::string& n, simple_wml::node& cfg, bool registered, const std::string& version, const std::string& source,
player(const std::string& n, simple_wml::node& cfg, int id, bool registered, const std::string& version, const std::string& source,
const std::size_t max_messages=4, const std::size_t time_period=10,
const bool moderator=false);

Expand Down
2 changes: 2 additions & 0 deletions src/server/wesnothd/server.cpp
Expand Up @@ -804,12 +804,14 @@ bool server::is_login_allowed(socket_ptr socket, const simple_wml::node* const l

simple_wml::document join_lobby_response;
join_lobby_response.root().add_child("join_lobby").set_attr("is_moderator", is_moderator ? "yes" : "no");
join_lobby_response.root().child("join_lobby")->set_attr_dup("profile_url_prefix", "https://r.wesnoth.org/u");

async_send_doc(socket, join_lobby_response, [this, username, registered, version, source](socket_ptr socket) {
simple_wml::node& player_cfg = games_and_users_list_.root().add_child("user");
add_player(socket, wesnothd::player(
username,
player_cfg,
user_handler_ ? user_handler_->get_forum_id(username) : 0,
registered,
version,
source,
Expand Down

0 comments on commit d7a5fa2

Please sign in to comment.