Merge pull request #237 from RElesgoe/master

Use singleton class for ad banner
This commit is contained in:
RElesgoe 2016-08-11 21:37:40 -07:00 committed by GitHub
commit 78516e1db3
8 changed files with 195 additions and 109 deletions

View file

@ -1,8 +1,8 @@
{ {
"ads": "ads":
[ [
{"filename": "ad000001.png", "url": "http://pvpgn.pro", "client": "W3XP", "lang": "any"}, {"filename": "ad000001.png", "url": "http://pvpgn.pro", "client": "W3XP", "lang": "NULL"},
{"filename": "ad000002.mng", "url": "http://pvpgn.pro", "client": "W3XP", "lang": "any"}, {"filename": "ad000002.mng", "url": "http://pvpgn.pro", "client": "W3XP", "lang": "NULL"},
{"filename": "ad000001.smk", "url": "http://pvpgn.pro", "client": "NULL", "lang": "any"} {"filename": "ad000001.smk", "url": "http://pvpgn.pro", "client": "NULL", "lang": "NULL"}
] ]
} }

View file

@ -6,7 +6,8 @@ The configuration file consists of a single array named ````ads```` which can co
- ````filename````: A string containing the filename of the ad banner, should not include a path. - ````filename````: A string containing the filename of the ad banner, should not include a path.
- ````url````: A string containing the URL that users should be directed to when clicking on the ad banner. - ````url````: A string containing the URL that users should be directed to when clicking on the ad banner.
- ````client````: A string containing the 4 character client tag that the ad banner should be shown to, a string of "NULL" will cause the ad banner to be shown to any client. - ````client````: A string containing the 4 character client tag that the ad banner should be shown to, a string of "NULL" will cause the ad banner to be shown to any client.
- ````lang````: A string containing a 4 character language code that will be displayed to users who have enabled that particular language, a string of "any" will cause the ad banner to be shown to any user. (e.g. deDE for German, enUS for English) - ````lang````: A string containing a 4 character language code that will be displayed to users who have enabled that particular language, a string of "NULL" will cause the ad banner to be shown to any user.
- Valid ````lang```` tags: ````enUS````, ````deDE````, ````csCZ````, ````esES````, ````frFR````, ````itIT````, ````jaJA````, ````koKR````, ````plPL````, ````ruRU````, ````zhCN````, ````zhTW````, ````NULL````.
### File Formats ### File Formats
| Client | Banner Format | | Client | Banner Format |

View file

@ -44,33 +44,17 @@ namespace pvpgn
{ {
namespace bnetd namespace bnetd
{ {
std::vector<AdBanner> AdBanner::m_banners; AdBannerSelector AdBannerList;
AdBanner::AdBanner() bool AdBannerSelector::is_loaded() const noexcept
: m_id(0),
m_extensiontag(0),
m_filename(),
m_url(),
m_client(0),
m_lang(0)
{ {
return this->m_loaded;
} }
AdBanner::AdBanner(std::size_t id, bn_int extensiontag, const std::string& filename, const std::string& url, t_clienttag clienttag, t_gamelang lang) void AdBannerSelector::load(const std::string& filename)
: m_id(id),
m_extensiontag(bn_int_get(extensiontag)),
m_filename(filename),
m_url(url),
m_client(clienttag),
m_lang(lang)
{ {
char language[5] = {}; if (this->is_loaded())
eventlog(eventlog_level_debug, __FUNCTION__, "Created ad id=0x%08zu filename=\"%s\" link=\"%s\" client=\"%s\" lang=\"%s\"", id, filename.c_str(), url.c_str(), clienttag ? clienttag_uint_to_str(clienttag) : "NULL", lang ? tag_uint_to_str(language, lang) : "NULL"); throw std::runtime_error("An ad banner file is already loaded");
}
void AdBanner::load(const std::string& filename)
{
m_banners.clear();
std::ifstream file_stream(filename, std::ios::in); std::ifstream file_stream(filename, std::ios::in);
if (!file_stream.is_open()) if (!file_stream.is_open())
@ -86,6 +70,13 @@ namespace pvpgn
throw std::runtime_error("Invalid JSON file: " + std::string(e.what())); throw std::runtime_error("Invalid JSON file: " + std::string(e.what()));
} }
const std::map<std::string, std::string> extension_map = {
{ "pcx", EXTENSIONTAG_PCX },
{ "mng", EXTENSIONTAG_MNG },
{ "png", EXTENSIONTAG_MNG },
{ "smk", EXTENSIONTAG_SMK }
};
for (const auto& ad : j["ads"]) for (const auto& ad : j["ads"])
{ {
try try
@ -94,22 +85,24 @@ namespace pvpgn
|| ad["filename"].get<std::string>().find('\\') != std::string::npos) || ad["filename"].get<std::string>().find('\\') != std::string::npos)
throw std::runtime_error("Paths are not supported (" + ad["filename"].get<std::string>() + ")"); throw std::runtime_error("Paths are not supported (" + ad["filename"].get<std::string>() + ")");
std::size_t ext = ad["filename"].get<std::string>().find('.'); const std::size_t ext = ad["filename"].get<std::string>().find('.');
if (ext == std::string::npos) if (ext == std::string::npos)
throw std::runtime_error("Filename must contain an extension"); throw std::runtime_error("Filename must contain an extension");
std::string file_extension(ad["filename"].get<std::string>().substr(ext + 1)); const std::string file_extension(ad["filename"].get<std::string>().substr(ext + 1));
bn_int extensiontag = {}; bn_int extensiontag = {};
if (strcasecmp(file_extension.c_str(), "pcx") == 0) for (const auto& m : extension_map)
bn_int_tag_set(&extensiontag, EXTENSIONTAG_PCX); {
else if (strcasecmp(file_extension.c_str(), "mng") == 0) if (strcasecmp(file_extension.c_str(), m.first.c_str()) == 0)
bn_int_tag_set(&extensiontag, EXTENSIONTAG_MNG); {
else if (strcasecmp(file_extension.c_str(), "png") == 0) bn_int_tag_set(&extensiontag, m.second.c_str());
bn_int_tag_set(&extensiontag, EXTENSIONTAG_MNG); break;
else if (strcasecmp(file_extension.c_str(), "smk") == 0) }
bn_int_tag_set(&extensiontag, EXTENSIONTAG_SMK); }
else if (extensiontag == 0)
throw std::runtime_error("Unknown file extension (" + file_extension + ")"); {
throw std::runtime_error("Unsupported file extension (" + file_extension + ")");
}
t_clienttag ctag = 0; t_clienttag ctag = 0;
if (strcasecmp(ad["client"].get<std::string>().c_str(), "NULL") != 0) if (strcasecmp(ad["client"].get<std::string>().c_str(), "NULL") != 0)
@ -121,7 +114,7 @@ namespace pvpgn
} }
t_gamelang ltag = 0; t_gamelang ltag = 0;
if (strcasecmp(ad["lang"].get<std::string>().c_str(), "any") != 0) if (strcasecmp(ad["lang"].get<std::string>().c_str(), "NULL") != 0)
{ {
ltag = tag_str_to_uint(ad["lang"].get<std::string>().c_str()); ltag = tag_str_to_uint(ad["lang"].get<std::string>().c_str());
@ -129,7 +122,7 @@ namespace pvpgn
throw std::runtime_error("Unknown language (" + ad["lang"].get<std::string>() + ")"); throw std::runtime_error("Unknown language (" + ad["lang"].get<std::string>() + ")");
} }
m_banners.push_back(AdBanner(m_banners.size(), extensiontag, ad["filename"].get<std::string>(), this->m_banners.push_back(AdBanner(m_banners.size(), extensiontag, ad["filename"].get<std::string>(),
ad["url"].get<std::string>(), ctag, ltag)); ad["url"].get<std::string>(), ctag, ltag));
} }
catch (const std::runtime_error& e) catch (const std::runtime_error& e)
@ -138,85 +131,123 @@ namespace pvpgn
continue; continue;
} }
} }
this->m_loaded = true;
} }
AdBanner AdBanner::pick(t_clienttag client_tag, t_gamelang client_lang, std::size_t prev_ad_id) void AdBannerSelector::unload() noexcept
{ {
switch (m_banners.size()) this->m_banners.clear();
this->m_loaded = false;
}
std::size_t AdBannerSelector::size() const noexcept
{
return this->m_banners.size();
}
const AdBanner* const AdBannerSelector::pick(t_clienttag client_tag, t_gamelang client_lang, std::size_t prev_ad_id)
{
switch (this->m_banners.size())
{ {
case 0: case 0:
return AdBanner(); return nullptr;
case 1: case 1:
return m_banners.at(0); return &this->m_banners.at(0);
default: default:
{ {
std::vector<AdBanner> candidates = {}; std::vector<std::size_t> candidates = {};
std::copy_if(m_banners.begin(), m_banners.end(), std::back_inserter(candidates), [=](const AdBanner& a) -> bool std::for_each(this->m_banners.begin(), this->m_banners.end(),
[client_tag, client_lang, prev_ad_id, &candidates](const AdBanner& ad) -> void
{ {
return a.get_client() == client_tag if ((ad.get_client() == client_tag || ad.get_client() == 0)
&& a.get_language() == client_lang && (ad.get_language() == client_lang || ad.get_language() == 0)
&& a.get_id() != prev_ad_id && (ad.get_id() != prev_ad_id))
? true : false; {
candidates.push_back(ad.get_id());
}
}); });
std::default_random_engine eng{}; if (candidates.empty())
std::uniform_int_distribution<std::size_t> random(0, m_banners.size() - 1); {
return nullptr;
}
return m_banners.at(random(eng)); std::default_random_engine engine {};
std::uniform_int_distribution<std::size_t> random(0, candidates.size() - 1);
return &this->m_banners.at(candidates.at(random(engine)));
} }
} }
} }
AdBanner AdBanner::find(t_clienttag client_tag, t_gamelang client_lang, std::size_t ad_id) const AdBanner* const AdBannerSelector::find(t_clienttag client_tag, t_gamelang client_lang, std::size_t ad_id)
{ {
auto result = std::find_if(m_banners.begin(), m_banners.end(), auto result = std::find_if(m_banners.begin(), m_banners.end(),
[client_tag, client_lang, ad_id](const AdBanner& a) -> bool [client_tag, client_lang, ad_id](const AdBanner& a) -> bool
{ {
return a.get_client() == client_tag && a.get_language() == client_lang && a.get_id() == ad_id ? true : false; return a.get_client() == client_tag
&& a.get_language() == client_lang
&& a.get_id() == ad_id;
}); });
return result != m_banners.end() ? m_banners.at(result - m_banners.begin()) : AdBanner(); return result != m_banners.end() ? &*result : nullptr;
} }
bool AdBanner::empty() const /***************************************************************************************************/
AdBanner::AdBanner(std::size_t id, bn_int extensiontag, const std::string& filename, const std::string& url, t_clienttag clienttag, t_gamelang language)
: m_id(id),
m_extensiontag(bn_int_get(extensiontag)),
m_filename(filename),
m_url(url),
m_client(clienttag),
m_language(language)
{
char lang[5] = {};
eventlog(eventlog_level_info, __FUNCTION__, "Created ad id=0x%08zu filename=\"%s\" link=\"%s\" client=\"%s\" lang=\"%s\"",
id, filename.c_str(), url.c_str(), clienttag ? clienttag_uint_to_str(clienttag) : "NULL",
language ? tag_uint_to_str(lang, language) : "NULL");
}
bool AdBanner::is_empty() const
{ {
return this->m_client == 0 return this->m_client == 0
&& this->m_extensiontag == 0 && this->m_extensiontag == 0
&& this->m_filename.empty() && this->m_filename.empty()
&& this->m_id == 0 && this->m_id == 0
&& this->m_lang == 0 && this->m_language == 0
&& this->m_url.empty() && this->m_url.empty();
? true : false;
} }
std::size_t AdBanner::get_id() const std::size_t AdBanner::get_id() const noexcept
{ {
return this->m_id; return this->m_id;
} }
unsigned int AdBanner::get_extension_tag() const std::string AdBanner::get_filename() const noexcept
{
return this->m_extensiontag;
}
std::string AdBanner::get_filename() const
{ {
return this->m_filename; return this->m_filename;
} }
std::string AdBanner::get_url() const unsigned int AdBanner::get_extension_tag() const noexcept
{
return this->m_extensiontag;
}
std::string AdBanner::get_url() const noexcept
{ {
return this->m_url; return this->m_url;
} }
t_clienttag AdBanner::get_client() const t_clienttag AdBanner::get_client() const noexcept
{ {
return this->m_client; return this->m_client;
} }
t_gamelang AdBanner::get_language() const t_gamelang AdBanner::get_language() const noexcept
{ {
return this->m_lang; return this->m_language;
} }
} }
} }

View file

@ -18,6 +18,8 @@
#ifndef INCLUDED_ADBANNER_H #ifndef INCLUDED_ADBANNER_H
#define INCLUDED_ADBANNER_H #define INCLUDED_ADBANNER_H
#include <array>
#include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -29,36 +31,61 @@ namespace pvpgn
{ {
namespace bnetd namespace bnetd
{ {
/**
* Forward Declarations
*/
class AdBanner;
/***************************************************************************************************/
class AdBannerSelector
{
public:
AdBannerSelector() = default;
~AdBannerSelector() = default;
bool is_loaded() const noexcept;
void load(const std::string& filename);
void unload() noexcept;
std::size_t size() const noexcept;
const AdBanner* const pick(t_clienttag client_tag, t_gamelang client_lang, std::size_t prev_ad_id);
const AdBanner* const find(t_clienttag client_tag, t_gamelang client_lang, std::size_t ad_id);
private:
bool m_loaded;
std::vector<AdBanner> m_banners;
};
extern AdBannerSelector AdBannerList;
/***************************************************************************************************/
class AdBanner class AdBanner
{ {
public: public:
AdBanner(); AdBanner() = delete;
void load(const std::string& filename); bool is_empty() const;
static AdBanner pick(t_clienttag client_tag, t_gamelang client_lang, std::size_t prev_ad_id); std::size_t get_id() const noexcept;
static AdBanner find(t_clienttag client_tag, t_gamelang client_lang, std::size_t ad_id); std::string get_filename() const noexcept;
unsigned int get_extension_tag() const noexcept;
bool empty() const; std::string get_url() const noexcept;
t_clienttag get_client() const noexcept;
std::size_t get_id() const; t_gamelang get_language() const noexcept;
unsigned int get_extension_tag() const;
std::string get_filename() const;
std::string get_url() const;
t_clienttag get_client() const;
t_gamelang get_language() const;
private: private:
AdBanner(std::size_t id, bn_int extensiontag, const std::string& filename, const std::string& url, t_clienttag clienttag, t_gamelang lang); friend void AdBannerSelector::load(const std::string& filename);
static std::vector<AdBanner> m_banners; AdBanner(std::size_t id, bn_int extensiontag, const std::string& filename, const std::string& url, t_clienttag clienttag, t_gamelang language);
const std::size_t m_id; const std::size_t m_id;
const std::string m_filename; const std::string m_filename;
const unsigned int m_extensiontag; const unsigned int m_extensiontag;
const std::string m_url; const std::string m_url;
const t_clienttag m_client; const t_clienttag m_client;
const t_gamelang m_lang; const t_gamelang m_language;
}; };
} }
} }

View file

@ -5148,6 +5148,9 @@ namespace pvpgn
/* Send message to all clients (similar to announce, but in messagebox) */ /* Send message to all clients (similar to announce, but in messagebox) */
static int _handle_alert_command(t_connection * c, char const * text) static int _handle_alert_command(t_connection * c, char const * text)
{ {
return 0;
/*********************************************************************/
t_clienttag clienttag; t_clienttag clienttag;
t_clienttag clienttag_dest; t_clienttag clienttag_dest;

View file

@ -23,6 +23,7 @@
#include "common/setup_before.h" #include "common/setup_before.h"
#include "handle_bnet.h" #include "handle_bnet.h"
#include <cinttypes>
#include <fstream> #include <fstream>
#include <cerrno> #include <cerrno>
#include <sstream> #include <sstream>
@ -3209,22 +3210,29 @@ namespace pvpgn
{ {
if (packet_get_size(packet) < sizeof(t_client_adreq)) if (packet_get_size(packet) < sizeof(t_client_adreq))
{ {
eventlog(eventlog_level_error, __FUNCTION__, "[%d] got bad ADREQ packet (expected %lu bytes, got %u)", conn_get_socket(c), sizeof(t_client_adreq), packet_get_size(packet)); eventlog(eventlog_level_error, __FUNCTION__, "[%d] got bad ADREQ packet (expected %zu bytes, got %u)", conn_get_socket(c), sizeof(t_client_adreq), packet_get_size(packet));
return -1; return -1;
} }
AdBanner ad = AdBanner::pick(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adreq.prev_adid)); /*
if (ad.empty()) eventlog(eventlog_level_debug, __FUNCTION__, "[%d] SID_CHECKAD { %d %d %d %d }", conn_get_socket(c), bn_int_get(packet->u.client_adreq.archtag),
return 0; bn_int_get(packet->u.client_adreq.clienttag), bn_int_get(packet->u.client_adreq.prev_adid), bn_int_get(packet->u.client_adreq.ticks));
*/
t_packet *rpacket = packet_create(packet_class_bnet); const AdBanner* ad = AdBannerList.pick(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adreq.prev_adid));
if (!ad)
{
return 0;
}
t_packet* const rpacket = packet_create(packet_class_bnet);
packet_set_size(rpacket, sizeof(t_server_adreply)); packet_set_size(rpacket, sizeof(t_server_adreply));
packet_set_type(rpacket, SERVER_ADREPLY); packet_set_type(rpacket, SERVER_ADREPLY);
bn_int_set(&rpacket->u.server_adreply.adid, ad.get_id()); bn_int_set(&rpacket->u.server_adreply.adid, ad->get_id());
bn_int_set(&rpacket->u.server_adreply.extensiontag, ad.get_extension_tag()); bn_int_set(&rpacket->u.server_adreply.extensiontag, ad->get_extension_tag());
file_to_mod_time(c, ad.get_filename().c_str(), &rpacket->u.server_adreply.timestamp); file_to_mod_time(c, ad->get_filename().c_str(), &rpacket->u.server_adreply.timestamp);
packet_append_string(rpacket, ad.get_filename().c_str()); packet_append_string(rpacket, ad->get_filename().c_str());
packet_append_string(rpacket, ad.get_url().c_str()); packet_append_string(rpacket, ad->get_url().c_str());
conn_push_outqueue(c, rpacket); conn_push_outqueue(c, rpacket);
packet_del_ref(rpacket); packet_del_ref(rpacket);
@ -3265,22 +3273,28 @@ namespace pvpgn
{ {
if (packet_get_size(packet) < sizeof(t_client_adclick2)) if (packet_get_size(packet) < sizeof(t_client_adclick2))
{ {
eventlog(eventlog_level_error, __FUNCTION__, "[%d] got bad ADCLICK2 packet (expected %lu bytes, got %u)", conn_get_socket(c), sizeof(t_client_adclick2), packet_get_size(packet)); eventlog(eventlog_level_error, __FUNCTION__, "[%d] got bad ADCLICK2 packet (expected %zu bytes, got %u)", conn_get_socket(c), sizeof(t_client_adclick2), packet_get_size(packet));
return -1; return -1;
} }
eventlog(eventlog_level_trace, __FUNCTION__, "[%d] ad click2 for adid 0x%04hx from \"%s\"", conn_get_socket(c), bn_int_get(packet->u.client_adclick2.adid), conn_get_username(c)); eventlog(eventlog_level_trace, __FUNCTION__, "[%d] ad click2 for adid 0x%04" PRIu32 " from \"%s\"", conn_get_socket(c), bn_int_get(packet->u.client_adclick2.adid), conn_get_username(c));
const AdBanner* const ad = AdBannerList.find(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adclick2.adid));
AdBanner ad = AdBanner::find(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adclick2.adid)); if (!ad)
if (ad.empty()) {
return 0; return 0;
}
t_packet *rpacket = packet_create(packet_class_bnet); t_packet* const rpacket = packet_create(packet_class_bnet);
if (!rpacket)
{
eventlog(eventlog_level_error, __FUNCTION__, "Could not create a packet");
return -1;
}
packet_set_size(rpacket, sizeof(t_server_adclickreply2)); packet_set_size(rpacket, sizeof(t_server_adclickreply2));
packet_set_type(rpacket, SERVER_ADCLICKREPLY2); packet_set_type(rpacket, SERVER_ADCLICKREPLY2);
bn_int_set(&rpacket->u.server_adclickreply2.adid, ad.get_id()); bn_int_set(&rpacket->u.server_adclickreply2.adid, ad->get_id());
packet_append_string(rpacket, ad.get_url().c_str()); packet_append_string(rpacket, ad->get_url().c_str());
conn_push_outqueue(c, rpacket); conn_push_outqueue(c, rpacket);
packet_del_ref(rpacket); packet_del_ref(rpacket);

View file

@ -370,7 +370,12 @@ int pre_server_startup(void)
try try
{ {
AdBanner().load(prefs_get_adfile()); if (AdBannerList.is_loaded())
{
AdBannerList.unload();
}
AdBannerList.load(prefs_get_adfile());
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {

View file

@ -1440,9 +1440,14 @@ namespace pvpgn
{ {
try try
{ {
AdBanner().load(prefs_get_adfile()); if (AdBannerList.is_loaded())
{
AdBannerList.unload();
} }
catch (const std::runtime_error& e)
AdBannerList.load(prefs_get_adfile());
}
catch (const std::exception& e)
{ {
eventlog(eventlog_level_error, __FUNCTION__, "%s", e.what()); eventlog(eventlog_level_error, __FUNCTION__, "%s", e.what());
} }