mirror of
https://github.com/mod-playerbots/mod-playerbots
synced 2025-12-01 21:12:50 +08:00
Big update.
This commit is contained in:
49
src/PlayerbotTextMgr.cpp
Normal file
49
src/PlayerbotTextMgr.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
|
||||
*/
|
||||
|
||||
#include "PlayerbotTextMgr.h"
|
||||
#include "Playerbots.h"
|
||||
|
||||
void replaceAll(std::string& str, std::string const from, std::string const to);
|
||||
|
||||
void PlayerbotTextMgr::LoadTemplates()
|
||||
{
|
||||
LOG_INFO("playerbots", "Loading playerbots texts...");
|
||||
|
||||
uint32 count = 0;
|
||||
if (PreparedQueryResult result = PlayerbotsDatabase.Query(PlayerbotsDatabase.GetPreparedStatement(PLAYERBOTS_SEL_TEXT)))
|
||||
{
|
||||
do
|
||||
{
|
||||
Field* fields = result->Fetch();
|
||||
std::string const key = fields[0].Get<std::string>();
|
||||
std::string const text = fields[1].Get<std::string>();
|
||||
templates[key].push_back(text);
|
||||
++count;
|
||||
}
|
||||
while (result->NextRow());
|
||||
}
|
||||
|
||||
LOG_INFO("playerbots", "{} playerbots texts loaded", count);
|
||||
}
|
||||
|
||||
std::string const PlayerbotTextMgr::Format(std::string const key, std::map<std::string, std::string> placeholders)
|
||||
{
|
||||
if (templates.empty())
|
||||
LoadTemplates();
|
||||
|
||||
std::vector<std::string>& list = templates[key];
|
||||
if (list.empty())
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "Unknown text: " << key;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
std::string str = list[urand(0, list.size() - 1)];
|
||||
for (std::map<std::string, std::string>::iterator i = placeholders.begin(); i != placeholders.end(); ++i)
|
||||
replaceAll(str, i->first, i->second);
|
||||
|
||||
return str;
|
||||
}
|
||||
Reference in New Issue
Block a user