Skip to content

Commit

Permalink
Store add-on download count in the database.
Browse files Browse the repository at this point in the history
The idea being that this could then also be included as a sheet in the multiplayer activity report, which would then be an easy way to view add-on downloads over time. This would of course not be especially useful until 1.18, since for 1.17 the database count would be starting at 0 even though existing add-ons would have a non-zero download count.
  • Loading branch information
Pentarctagon committed Jun 3, 2022
1 parent 31b508c commit a2679c6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/server/campaignd/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,9 @@ void server::handle_request_campaign(const server::request& req)
if(req.cfg["increase_downloads"].to_bool(true) && !ignore_address_stats(req.addr)) {
addon["downloads"] = 1 + addon["downloads"].to_int();
mark_dirty(name);
if(user_handler_) {
user_handler_->db_update_addon_download_count(server_id_, name, to);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/server/common/dbconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ void dbconn::get_ips_for_user(const std::string& username, std::ostringstream* o
}
}

void dbconn::update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version)
{
try
{
modify(connection_, "UPDATE `"+db_addon_info_table_+"` SET DOWNLOAD_COUNT = DOWNLOAD_COUNT+1 WHERE INSTANCE_VERSION = ? AND ADDON_ID = ? AND VERSION = ?",
instance_version, id, version);
}
catch(const mariadb::exception::base& e)
{
log_sql_exception("Unable to update download count for add-on "+id+" with version "+version+".", e);
}
}

//
// handle complex query results
//
Expand Down
5 changes: 5 additions & 0 deletions src/server/common/dbconn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ class dbconn
*/
void get_ips_for_user(const std::string& username, std::ostringstream* out);

/**
* @see forum_user_handler::db_update_addon_download_count().
*/
void update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version);

private:
/**
* The account used to connect to the database.
Expand Down
4 changes: 4 additions & 0 deletions src/server/common/forum_user_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ std::string fuh::get_user_email(const std::string& user) {
return conn_.get_user_string(db_users_table_, "user_email", user);
}

void fuh::db_update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version) {
return conn_.update_addon_download_count(instance_version, id, version);
}

std::time_t fuh::get_lastlogin(const std::string& user) {
return std::time_t(conn_.get_user_int(db_extra_table_, "user_lastvisit", user));
}
Expand Down
9 changes: 9 additions & 0 deletions src/server/common/forum_user_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ class fuh : public user_handler
*/
std::string get_user_email(const std::string& user);

/**
* Increments the download count for this add-on for the specific version.
*
* @param instance_version The version of campaignd the add-on was uploaded to.
* @param id The add-on's ID (aka directory name).
* @param version The version of the add-on being downloaded. May not be the most recent version.
*/
void db_update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version);

private:
/** An instance of the class responsible for executing the queries and handling the database connection. */
dbconn conn_;
Expand Down
1 change: 1 addition & 0 deletions src/server/common/user_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,5 @@ class user_handler
virtual void db_update_logout(unsigned long long login_id) = 0;
virtual void get_users_for_ip(const std::string& ip, std::ostringstream* out) = 0;
virtual void get_ips_for_user(const std::string& username, std::ostringstream* out) = 0;
virtual void db_update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version) = 0;
};
2 changes: 2 additions & 0 deletions utils/mp-server/table_definitions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ create table game_content_info
-- FORUM_AUTH: whether forum authentication is to be used when uploading
-- UPLOADED_ON: when the addon was uploaded
-- FEEDBACK_TOPIC: the forum topic ID where feedback for the addon can be posted, 0 if not set
-- DOWNLOAD_COUNT: the number of times the add-on has been downloaded by players (does not count downloads from addons.wesnoth.org)
create table addon_info
(
INSTANCE_VERSION VARCHAR(255) NOT NULL,
Expand All @@ -144,6 +145,7 @@ create table addon_info
FORUM_AUTH BIT(1) NOT NULL,
UPLOADED_ON TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FEEDBACK_TOPIC INT UNSIGNED NOT NULL,
DOWNLOAD_COUNT INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (INSTANCE_VERSION, ADDON_ID, VERSION)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Expand Down

0 comments on commit a2679c6

Please sign in to comment.