summaryrefslogtreecommitdiff
path: root/src/server/game/Autobroadcast/AutobroadcastMgr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/game/Autobroadcast/AutobroadcastMgr.cpp')
-rw-r--r--src/server/game/Autobroadcast/AutobroadcastMgr.cpp142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/server/game/Autobroadcast/AutobroadcastMgr.cpp b/src/server/game/Autobroadcast/AutobroadcastMgr.cpp
new file mode 100644
index 0000000000..1ed046685b
--- /dev/null
+++ b/src/server/game/Autobroadcast/AutobroadcastMgr.cpp
@@ -0,0 +1,142 @@
+/*
+ * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as published by the
+ * Free Software Foundation; either version 3 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "AutobroadcastMgr.h"
+#include "Config.h"
+#include "Chat.h"
+#include "Player.h"
+#include "GridNotifiers.h"
+
+AutobroadcastMgr* AutobroadcastMgr::instance()
+{
+ static AutobroadcastMgr instance;
+ return &instance;
+}
+
+void AutobroadcastMgr::LoadAutobroadcasts()
+{
+ uint32 oldMSTime = getMSTime();
+
+ _autobroadcasts.clear();
+ _autobroadcastsWeights.clear();
+
+ uint32 realmId = sConfigMgr->GetOption<int32>("RealmID", 0);
+ LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_AUTOBROADCAST);
+ stmt->SetData(0, realmId);
+ PreparedQueryResult result = LoginDatabase.Query(stmt);
+
+ if (!result)
+ {
+ LOG_WARN("autobroadcast", ">> Loaded 0 autobroadcasts definitions. DB table `autobroadcast` is empty for this realm!");
+ LOG_INFO("autobroadcast", " ");
+ return;
+ }
+
+ _announceType = static_cast<AnnounceType>(sWorld->getIntConfig(CONFIG_AUTOBROADCAST_CENTER));
+
+ if (_announceType < AnnounceType::World || _announceType > AnnounceType::Both)
+ {
+ LOG_ERROR("autobroadcast", "AutobroadcastMgr::LoadAutobroadcasts: Config option AutoBroadcast.Center set to not allowed value {}. Set to default value 0", (int8)_announceType);
+ _announceType = AnnounceType::World;
+ }
+
+ uint32 count = 0;
+
+ do
+ {
+ Field* fields = result->Fetch();
+ uint8 id = fields[0].Get<uint8>();
+
+ _autobroadcasts[id] = fields[2].Get<std::string>();
+ _autobroadcastsWeights[id] = fields[1].Get<uint8>();
+
+ ++count;
+ } while (result->NextRow());
+
+ LOG_INFO("autobroadcast", ">> Loaded {} Autobroadcast Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
+ LOG_INFO("autobroadcast", " ");
+}
+
+void AutobroadcastMgr::SendAutobroadcasts()
+{
+ if (_autobroadcasts.empty())
+ {
+ return;
+ }
+
+ uint32 weight = 0;
+ AutobroadcastsWeightMap selectionWeights;
+
+ std::string msg;
+
+ for (AutobroadcastsWeightMap::const_iterator it = _autobroadcastsWeights.begin(); it != _autobroadcastsWeights.end(); ++it)
+ {
+ if (it->second)
+ {
+ weight += it->second;
+ selectionWeights[it->first] = it->second;
+ }
+ }
+
+ if (weight)
+ {
+ uint32 selectedWeight = urand(0, weight - 1);
+ weight = 0;
+ for (AutobroadcastsWeightMap::const_iterator it = selectionWeights.begin(); it != selectionWeights.end(); ++it)
+ {
+ weight += it->second;
+ if (selectedWeight < weight)
+ {
+ msg = _autobroadcasts[it->first];
+ break;
+ }
+ }
+ }
+ else
+ {
+ msg = _autobroadcasts[urand(0, _autobroadcasts.size())];
+ }
+
+ switch (_announceType)
+ {
+ case AnnounceType::World:
+ SendWorldAnnouncement(msg);
+ break;
+ case AnnounceType::Notification:
+ SendNotificationAnnouncement(msg);
+ break;
+ case AnnounceType::Both:
+ SendWorldAnnouncement(msg);
+ SendNotificationAnnouncement(msg);
+ default:
+ break;
+ }
+
+ LOG_DEBUG("autobroadcast", "AutobroadcastMgr::SendAutobroadcasts: '{}'", msg);
+}
+
+void AutobroadcastMgr::SendWorldAnnouncement(std::string_view msg)
+{
+ sWorld->SendWorldTextOptional(LANG_AUTO_BROADCAST, ANNOUNCER_FLAG_DISABLE_AUTOBROADCAST, msg.data());
+}
+
+void AutobroadcastMgr::SendNotificationAnnouncement(std::string_view msg)
+{
+ WorldPacket data(SMSG_NOTIFICATION, (msg.size() + 1));
+ data << msg.data();
+ sWorld->SendGlobalMessage(&data);
+}