This commit is contained in:
aozhiwei 2020-12-10 11:39:22 +08:00
parent 206e04d796
commit 6a8b071308
2 changed files with 67 additions and 0 deletions

View File

@ -17,6 +17,18 @@
#define METAMGR_READ_STR(field_name, def_val) MetaMgr::Instance()->field_name = \
a8::XValue(MetaMgr::Instance()->GetSysParamAsString(#field_name, def_val)).GetString();
static bool PureEnglishName(const std::string& role_name)
{
for (size_t i = 0; i < role_name.size(); i++) {
if (role_name[i] >= 0 && role_name[i] <= 127) {
continue;
} else {
return false;
}
}
return true;
}
class MetaDataLoader
{
public:
@ -25,6 +37,8 @@ public:
std::map<int, MetaData::Guild*> guild_hash;
std::map<size_t, std::map<std::string, std::string> > dirty_words;
void Load()
{
if (!f8::IsOnlineEnv()) {
@ -49,12 +63,33 @@ public:
res_path = "../res/";
}
f8::ReadCsvMetaFile(res_path + "Clan@Clan.csv", guild_meta_list);
LoadDirtyWordTable();
BindToMetaData();
Check();
}
private:
void LoadDirtyWordTable()
{
a8::CsvReader reader;
reader.Load(res_path + "dirtyWord@dirtyWord.csv");
while (reader.NextLine()) {
std::string strword = reader.GetValue("word").GetString();
if (strword == " " || strword.empty()) { //忽略空格
continue;
}
auto itr = dirty_words.find(strword.size());
if (dirty_words.find(strword.size()) != dirty_words.end()) {
itr->second[strword] = strword;
} else {
std::map<std::string, std::string> words;
words[strword] = strword;
dirty_words[strword.size()] = words;
}
}
}
void Check()
{
}
@ -103,3 +138,34 @@ MetaData::Guild* MetaMgr::GetGuild(int guild_lv)
return nullptr;
}
}
std::string MetaMgr::ReplaceDirtyWord(const std::string& text, char c)
{
std::string result;
result.reserve(text.size());
for (size_t i = 0; i < text.size(); i++) {
bool found = false;
for (auto itr = loader_->dirty_words.begin(); itr != loader_->dirty_words.end(); ++itr) {
if (itr->first <= text.size() - i) {
std::string substr = text.substr(i, itr->first);
// 如果是纯英文,则先转为小写,根据策划说明,屏蔽字大小写不敏感
if (PureEnglishName(substr)) {
transform(substr.begin(), substr.end(), substr.begin(), ::tolower);
}
if (substr.size() > 0 && itr->second.find(substr) != itr->second.end()) {
int str_len = a8::GetUtf8Length(substr.c_str());
for (int j = 0; j < str_len; j++) {
result.push_back(c);
}
i += substr.size() - 1;
found = true;
break;
}
}
}//endfor map
if (!found) {
result.push_back(text[i]);
}
}
return result;
}

View File

@ -17,6 +17,7 @@ class MetaMgr : public a8::Singleton<MetaMgr>
void Reload();
MetaData::Guild* GetGuild(int guild_lv);
std::string ReplaceDirtyWord(const std::string& text, char c);
private:
MetaDataLoader* loader_ = nullptr;