Debian Codegraph @

src/bnetd/adbanner.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Ross Combs ([email protected])
3 * Copyright (C) 2005 Dizzy
4 * Copyright (C) 2014 HarpyWar
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 #include "common/setup_before.h"
21 #include "adbanner.h"
22
23 #include <algorithm>
24 #include <cstring>
25 #include <fstream>
26 #include <random>
27 #include <stdexcept>
28 #include <string>
29 #include <vector>
30
31 #include "i18n.h"
32 #include "json/json.hpp"
33
34 #include "common/bn_type.h"
35 #include "common/tag.h"
36 #include "compat/strcasecmp.h"
37 #include "common/eventlog.h"
38 #include "connection.h"
39 #include "common/setup_after.h"
40
41
42 using json = nlohmann::json;
43
44
45 namespace pvpgn
46 {
47 namespace bnetd
48 {
49 AdBannerSelector AdBannerList;
50 std::default_random_engine engine{};
51
52 bool AdBannerSelector::is_loaded() const noexcept
53 {
54 return this->m_loaded;
55 }
56
57 void AdBannerSelector::load(const std::string& filename)
58 {
59 if (this->is_loaded())
60 throw std::runtime_error("An ad banner file is already loaded");
61
62 std::ifstream file_stream(filename, std::ios::in);
63 if (!file_stream.is_open())
64 throw std::runtime_error("Could not open " + filename);
65
66 json j;
67 try
68 {
69 file_stream >> j;
70 }
71 catch (const std::invalid_argument& e)
72 {
73 throw std::runtime_error("Invalid JSON file: " + std::string(e.what()));
74 }
75
76 const std::map<std::string, std::string> extension_map = {
77 { "pcx", EXTENSIONTAG_PCX },
78 { "smk", EXTENSIONTAG_SMK },
79 { "mng", EXTENSIONTAG_MNG },
80 { "png", EXTENSIONTAG_MNG }
81 };
82
83 for (const auto& ad : j["ads"])
84 {
85 try
86 {
87 if (ad["filename"].get<std::string>().find('/') != std::string::npos
88 || ad["filename"].get<std::string>().find('\\') != std::string::npos)
89 throw std::runtime_error("Paths are not supported (" + ad["filename"].get<std::string>() + ")");
90
91 const std::size_t ext = ad["filename"].get<std::string>().find('.');
92 if (ext == std::string::npos)
93 throw std::runtime_error("Filename must contain an extension");
94
95 const std::string file_extension(ad["filename"].get<std::string>().substr(ext + 1));
96 bn_int extensiontag = {};
97 for (const auto& m : extension_map)
98 {
99 if (strcasecmp(file_extension.c_str(), m.first.c_str()) == 0)
100 {
101 bn_int_tag_set(&extensiontag, m.second.c_str());
102 break;
103 }
104 }
105 if (extensiontag == 0)
106 {
107 throw std::runtime_error("Unsupported file extension (" + file_extension + ")");
108 }
109
110 t_clienttag ctag = 0;
111 if (strcasecmp(ad["client"].get<std::string>().c_str(), "NULL") != 0)
112 {
113 ctag = clienttag_str_to_uint(ad["client"].get<std::string>().c_str());
114
115 if (std::strcmp(clienttag_uint_to_str(ctag), "UNKN") == 0)
116 throw std::runtime_error("Unknown client tag (" + ad["client"].get<std::string>() + ")");
117 }
118
119 t_gamelang ltag = 0;
120 if (strcasecmp(ad["lang"].get<std::string>().c_str(), "NULL") != 0)
121 {
122 ltag = tag_str_to_uint(ad["lang"].get<std::string>().c_str());
123
124 bool found;
125 language_find_by_tag(ltag, found);
126 if (!found)
127 throw std::runtime_error("Unknown language (" + ad["lang"].get<std::string>() + ")");
128 }
129
130 this->m_banners.push_back(AdBanner(m_banners.size()+1, extensiontag, ad["filename"].get<std::string>(),
131 ad["url"].get<std::string>(), ctag, ltag));
132 }
133 catch (const std::runtime_error& e)
134 {
135 eventlog(eventlog_level_error, __FUNCTION__, "Could not load ad: {}", e.what());
136 continue;
137 }
138 }
139
140 this->m_loaded = true;
141 }
142
143 void AdBannerSelector::unload() noexcept
144 {
145 this->m_banners.clear();
146 this->m_loaded = false;
147 }
148
149 std::size_t AdBannerSelector::size() const noexcept
150 {
151 return this->m_banners.size();
152 }
153
154 const AdBanner* const AdBannerSelector::pick(t_clienttag client_tag, t_gamelang client_lang, std::size_t prev_ad_id)
155 {
156 switch (this->m_banners.size())
157 {
158 case 0:
159 return nullptr;
160 case 1:
161 return &this->m_banners.at(0);
162 default:
163 {
164 bn_int ext;
165 std::vector<std::size_t> candidates = {};
166
167 std::for_each(this->m_banners.begin(), this->m_banners.end(),
168 [client_tag, client_lang, &candidates, &ext](const AdBanner& ad) -> void
169 {
170 if ((ad.get_client() == client_tag || ad.get_client() == 0)
171 && (ad.get_language() == client_lang || ad.get_language() == 0))
172 {
173
174 bn_int_set(&ext, ad.get_extension_tag());
175 // Warcraft 3
176 if (client_tag == CLIENTTAG_WAR3XP_UINT || client_tag == CLIENTTAG_WARCRAFT3_UINT)
177 {
178 // ignore all formats except MNG for Warcraft 3, cause it's only one supported format
179 if (bn_int_tag_eq(ext, EXTENSIONTAG_MNG) != 0)
180 {
181 return;
182 }
183 }
184 // Starcraft, Warcraft 2, Diablo, Diablo 2
185 else
186 {
187 // ignore MNG, cause it's not supported format for other games
188 if (bn_int_tag_eq(ext, EXTENSIONTAG_MNG) == 0)
189 {
190 return;
191 }
192 }
193 candidates.push_back(ad.get_id());
194 }
195 });
196
197 if (candidates.empty())
198 {
199 return nullptr;
200 }
201
202 const AdBanner* ad;
203 unsigned int idx = 0;
204 if (client_tag == CLIENTTAG_WAR3XP_UINT || client_tag == CLIENTTAG_WARCRAFT3_UINT)
205 {
206 // Warcraft 3 client always send prev_ad_id = 0, because of that we use random selection instead of sequence
207 std::uniform_int_distribution<std::size_t> random(0, candidates.size() - 1);
208 idx = random(engine);
209 }
210 else
211 {
212 // if prev_id in middle (for first and last idx=0)
213 if (prev_ad_id != 0 && prev_ad_id != candidates.at(candidates.size() - 1))
214 {
215 bool prev_found = false;
216 for (unsigned int i = 0; i < candidates.size(); i++)
217 {
218 if (prev_found)
219 {
220 idx = i;
221 break;
222 }
223 if (prev_ad_id == candidates.at(i))
224 {
225 prev_found = true;
226 }
227 }
228 }
229 }
230 ad = this->find(client_tag, client_lang, candidates.at(idx));
231
232 /*
233 char lang[5] = {};
234 eventlog(eventlog_level_trace, __FUNCTION__, "Pick ad idx={} candidates={} id=0x{:08} prev_id=0x{:08} filename=\"{}\" link=\"{}\" client=\"{}\" lang=\"{}\"",
235 idx, candidates.size(), ad->get_id(), prev_ad_id, ad->get_filename(), ad->get_url(), client_tag ? clienttag_uint_to_str(client_tag) : "NULL",
236 client_lang ? tag_uint_to_str(lang, client_lang) : "NULL");
237 */
238 return ad;
239 }
240 }
241 }
242
243 const AdBanner* const AdBannerSelector::find(t_clienttag client_tag, t_gamelang client_lang, std::size_t ad_id)
244 {
245 auto result = std::find_if(m_banners.begin(), m_banners.end(),
246 [client_tag, client_lang, ad_id](const AdBanner& a) -> bool
247 {
248 return (a.get_client() == client_tag || a.get_client() == 0)
249 && (a.get_language() == client_lang || a.get_language() == 0)
250 && a.get_id() == ad_id;
251 });
252
253 return result != m_banners.end() ? &*result : nullptr;
254 }
255
256 /***************************************************************************************************/
257
258 AdBanner::AdBanner(std::size_t id, bn_int extensiontag, const std::string& filename, const std::string& url, t_clienttag clienttag, t_gamelang language)
259 : m_id(id),
260 m_extensiontag(bn_int_get(extensiontag)),
261 m_filename(filename),
262 m_url(url),
263 m_client(clienttag),
264 m_language(language)
265 {
266 char lang[5] = {};
267 eventlog(eventlog_level_info, __FUNCTION__, "Created ad id=0x{:08} filename=\"{}\" link=\"{}\" client=\"{}\" lang=\"{}\"",
268 id, filename.c_str(), url.c_str(), clienttag ? clienttag_uint_to_str(clienttag) : "NULL",
269 language ? tag_uint_to_str(lang, language) : "NULL");
270 }
271
272 bool AdBanner::is_empty() const
273 {
274 return this->m_client == 0
275 && this->m_extensiontag == 0
276 && this->m_filename.empty()
277 && this->m_id == 0
278 && this->m_language == 0
279 && this->m_url.empty();
280 }
281
282 /* id starts from 1 */
283 std::size_t AdBanner::get_id() const noexcept
284 {
285 return this->m_id;
286 }
287
288 std::string AdBanner::get_filename() const noexcept
289 {
290 return this->m_filename;
291 }
292
293 unsigned int AdBanner::get_extension_tag() const noexcept
294 {
295 return this->m_extensiontag;
296 }
297
298 std::string AdBanner::get_url() const noexcept
299 {
300 return this->m_url;
301 }
302
303 t_clienttag AdBanner::get_client() const noexcept
304 {
305 return this->m_client;
306 }
307
308 t_gamelang AdBanner::get_language() const noexcept
309 {
310 return this->m_language;
311 }
312 }
313 }