feat(Core/Autobroacast): Add autobroadcast locale (#20946)

This commit is contained in:
Exitare
2025-01-26 00:37:32 -08:00
committed by GitHub
parent 778742749a
commit a74e06c27c
6 changed files with 112 additions and 30 deletions

View File

@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS `autobroadcast_locale`;
CREATE TABLE `autobroadcast_locale` (
`realmid` INT NOT NULL,
`id` INT NOT NULL,
`locale` VARCHAR(4) NOT NULL,
`text` VARCHAR(45) NULL,
PRIMARY KEY (`realmid`, `id`))
CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci
ENGINE = InnoDB
;

View File

@@ -115,6 +115,7 @@ void LoginDatabaseConnection::DoPrepareStatements()
PrepareStatement(LOGIN_SEL_REALMLIST_SECURITY_LEVEL, "SELECT allowedSecurityLevel from realmlist WHERE id = ?", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_REALMLIST_SECURITY_LEVEL, "SELECT allowedSecurityLevel from realmlist WHERE id = ?", CONNECTION_SYNCH);
PrepareStatement(LOGIN_DEL_ACCOUNT, "DELETE FROM account WHERE id = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_DEL_ACCOUNT, "DELETE FROM account WHERE id = ?", CONNECTION_ASYNC);
PrepareStatement(LOGIN_SEL_AUTOBROADCAST, "SELECT id, weight, text FROM autobroadcast WHERE realmid = ? OR realmid = -1", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_AUTOBROADCAST, "SELECT id, weight, text FROM autobroadcast WHERE realmid = ? OR realmid = -1", CONNECTION_SYNCH);
PrepareStatement(LOGIN_SEL_AUTOBROADCAST_LOCALIZED, "SELECT id, locale, text FROM autobroadcast_locale WHERE realmid = ? OR realmid = -1", CONNECTION_SYNCH);
PrepareStatement(LOGIN_SEL_MOTD, "SELECT text FROM motd WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_MOTD, "SELECT text FROM motd WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH);
PrepareStatement(LOGIN_SEL_MOTD_LOCALE, "SELECT locale, text FROM motd_localized WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH); PrepareStatement(LOGIN_SEL_MOTD_LOCALE, "SELECT locale, text FROM motd_localized WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH);
PrepareStatement(LOGIN_INS_MOTD, "INSERT INTO motd (realmid, text) VALUES (?, ?) ON DUPLICATE KEY UPDATE text = ?", CONNECTION_ASYNC); PrepareStatement(LOGIN_INS_MOTD, "INSERT INTO motd (realmid, text) VALUES (?, ?) ON DUPLICATE KEY UPDATE text = ?", CONNECTION_ASYNC);

View File

@@ -97,6 +97,7 @@ enum LoginDatabaseStatements : uint32
LOGIN_SEL_REALMLIST_SECURITY_LEVEL, LOGIN_SEL_REALMLIST_SECURITY_LEVEL,
LOGIN_DEL_ACCOUNT, LOGIN_DEL_ACCOUNT,
LOGIN_SEL_AUTOBROADCAST, LOGIN_SEL_AUTOBROADCAST,
LOGIN_SEL_AUTOBROADCAST_LOCALIZED,
LOGIN_SEL_MOTD, LOGIN_SEL_MOTD,
LOGIN_SEL_MOTD_LOCALE, LOGIN_SEL_MOTD_LOCALE,
LOGIN_INS_MOTD, LOGIN_INS_MOTD,

View File

@@ -42,7 +42,6 @@ void AutobroadcastMgr::LoadAutobroadcasts()
if (!result) if (!result)
{ {
LOG_WARN("autobroadcast", ">> Loaded 0 autobroadcasts definitions. DB table `autobroadcast` is empty for this realm!"); LOG_WARN("autobroadcast", ">> Loaded 0 autobroadcasts definitions. DB table `autobroadcast` is empty for this realm!");
LOG_INFO("autobroadcast", " ");
return; return;
} }
@@ -54,35 +53,66 @@ void AutobroadcastMgr::LoadAutobroadcasts()
_announceType = AnnounceType::World; _announceType = AnnounceType::World;
} }
uint32 count = 0; do
{
Field* fields = result->Fetch();
uint8 textId = fields[0].Get<uint8>();
ObjectMgr::AddLocaleString(fields[2].Get<std::string>(), DEFAULT_LOCALE, _autobroadcasts[textId]);
_autobroadcastsWeights[textId] = fields[1].Get<uint8>();
} while (result->NextRow());
LOG_INFO("server.loading", ">> Loaded {} Autobroadcast Definitions in {} ms", _autobroadcasts.size(), GetMSTimeDiffToNow(oldMSTime));
}
void AutobroadcastMgr::LoadAutobroadcastsLocalized()
{
uint32 oldMSTime = getMSTime();
uint32 realmId = sConfigMgr->GetOption<int32>("RealmID", 0);
if (_autobroadcasts.empty())
return;
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_AUTOBROADCAST_LOCALIZED);
stmt->SetData(0, realmId);
PreparedQueryResult result = LoginDatabase.Query(stmt);
if (!result)
{
LOG_WARN("server.loading", ">> Loaded 0 localized autobroadcasts definitions. DB table `autobroadcast_localized` is empty for this realm!");
LOG_INFO("server.loading", " ");
return;
}
uint8 count = 0;
do do
{ {
Field* fields = result->Fetch(); Field* fields = result->Fetch();
uint8 id = fields[0].Get<uint8>(); uint8 textId = fields[0].Get<uint8>();
LocaleConstant locale = GetLocaleByName(fields[1].Get<std::string>());
_autobroadcasts[id] = fields[2].Get<std::string>(); if (locale == DEFAULT_LOCALE || ObjectMgr::GetLocaleString(_autobroadcasts[textId], DEFAULT_LOCALE).empty())
_autobroadcastsWeights[id] = fields[1].Get<uint8>(); continue;
++count; ObjectMgr::AddLocaleString(fields[2].Get<std::string>(), locale, _autobroadcasts[textId]);
count++;
} while (result->NextRow()); } while (result->NextRow());
LOG_INFO("autobroadcast", ">> Loaded {} Autobroadcast Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime)); LOG_INFO("server.loading", ">> Loaded {} Localized Autobroadcast Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("autobroadcast", " "); LOG_INFO("server.loading", " ");
} }
void AutobroadcastMgr::SendAutobroadcasts() void AutobroadcastMgr::SendAutobroadcasts()
{ {
if (_autobroadcasts.empty()) if (_autobroadcasts.empty())
{
return; return;
}
uint32 weight = 0; uint32 weight = 0;
uint8 textId = 0;
AutobroadcastsWeightMap selectionWeights; AutobroadcastsWeightMap selectionWeights;
std::string msg;
for (AutobroadcastsWeightMap::const_iterator it = _autobroadcastsWeights.begin(); it != _autobroadcastsWeights.end(); ++it) for (AutobroadcastsWeightMap::const_iterator it = _autobroadcastsWeights.begin(); it != _autobroadcastsWeights.end(); ++it)
{ {
if (it->second) if (it->second)
@@ -101,42 +131,78 @@ void AutobroadcastMgr::SendAutobroadcasts()
weight += it->second; weight += it->second;
if (selectedWeight < weight) if (selectedWeight < weight)
{ {
msg = _autobroadcasts[it->first]; textId = it->first;
break; break;
} }
} }
} }
else else
{ {
msg = _autobroadcasts[urand(0, _autobroadcasts.size())]; textId = urand(0, _autobroadcasts.size());
} }
switch (_announceType) switch (_announceType)
{ {
case AnnounceType::World: case AnnounceType::World:
SendWorldAnnouncement(msg); SendWorldAnnouncement(textId);
break; break;
case AnnounceType::Notification: case AnnounceType::Notification:
SendNotificationAnnouncement(msg); SendNotificationAnnouncement(textId);
break; break;
case AnnounceType::Both: case AnnounceType::Both:
SendWorldAnnouncement(msg); SendWorldAnnouncement(textId);
SendNotificationAnnouncement(msg); SendNotificationAnnouncement(textId);
default: default:
break; break;
} }
LOG_DEBUG("autobroadcast", "AutobroadcastMgr::SendAutobroadcasts: '{}'", msg); LOG_DEBUG("autobroadcast", "AutobroadcastMgr::SendAutobroadcasts: '{}'", textId);
} }
void AutobroadcastMgr::SendWorldAnnouncement(std::string msg) void AutobroadcastMgr::SendWorldAnnouncement(uint8 textId)
{ {
ChatHandler(nullptr).SendWorldTextOptional(LANG_AUTO_BROADCAST, ANNOUNCER_FLAG_DISABLE_AUTOBROADCAST, msg.data()); // Send localized messages to all sessions
ChatHandler(nullptr).DoForAllValidSessions([&](Player* player)
{
// Get player's locale
LocaleConstant locale = player->GetSession()->GetSessionDbLocaleIndex();
if (!_autobroadcasts.empty())
return;
std::string_view localizedMessage = ObjectMgr::GetLocaleString(_autobroadcasts[textId], locale);
// Check if there is a localized message if not use default one.
if (localizedMessage.empty())
localizedMessage = ObjectMgr::GetLocaleString(_autobroadcasts[textId], DEFAULT_LOCALE);
// Send the localized or fallback message
ChatHandler(player->GetSession()).SendWorldTextOptional(localizedMessage, ANNOUNCER_FLAG_DISABLE_AUTOBROADCAST);
});
} }
void AutobroadcastMgr::SendNotificationAnnouncement(std::string msg) void AutobroadcastMgr::SendNotificationAnnouncement(uint8 textId)
{ {
WorldPacket data(SMSG_NOTIFICATION, (msg.size() + 1)); ChatHandler(nullptr).DoForAllValidSessions([&](Player* player)
data << msg.data(); {
sWorld->SendGlobalMessage(&data); // Retrieve player's locale
LocaleConstant locale = player->GetSession()->GetSessionDbLocaleIndex();
if (!_autobroadcasts.count(textId))
return;
// Get localized message
std::string_view localizedMessage = ObjectMgr::GetLocaleString(_autobroadcasts[textId], locale);
// Check if there is a localized message if not use default one.
if (localizedMessage.empty())
localizedMessage = ObjectMgr::GetLocaleString(_autobroadcasts[textId], DEFAULT_LOCALE);
// Prepare the WorldPacket
WorldPacket data(SMSG_NOTIFICATION, (localizedMessage.size() + 1));
data << localizedMessage;
// Send packet to the player
player->GetSession()->SendPacket(&data);
});
} }

View File

@@ -20,6 +20,7 @@
#include "Common.h" #include "Common.h"
#include <map> #include <map>
#include <vector>
enum class AnnounceType : uint8 enum class AnnounceType : uint8
{ {
@@ -34,17 +35,18 @@ public:
static AutobroadcastMgr* instance(); static AutobroadcastMgr* instance();
void LoadAutobroadcasts(); void LoadAutobroadcasts();
void LoadAutobroadcastsLocalized();
void SendAutobroadcasts(); void SendAutobroadcasts();
private: private:
void SendWorldAnnouncement(std::string msg); void SendWorldAnnouncement(uint8 textId);
void SendNotificationAnnouncement(std::string msg); void SendNotificationAnnouncement(uint8 textId);
typedef std::map<uint8, std::string> AutobroadcastsMap; typedef std::map<uint8, std::vector<std::string>> AutobroadcastsMap;
typedef std::map<uint8, uint8> AutobroadcastsWeightMap; typedef std::map<uint8, uint8> AutobroadcastsWeightMap;
AutobroadcastsMap _autobroadcasts; AutobroadcastsMap _autobroadcasts; // autobroadcast messages
AutobroadcastsWeightMap _autobroadcastsWeights; AutobroadcastsWeightMap _autobroadcastsWeights; // Weights for each message
AnnounceType _announceType; AnnounceType _announceType;
}; };

View File

@@ -2028,6 +2028,7 @@ void World::SetInitialWorldSettings()
///- Load AutoBroadCast ///- Load AutoBroadCast
LOG_INFO("server.loading", "Loading Autobroadcasts..."); LOG_INFO("server.loading", "Loading Autobroadcasts...");
sAutobroadcastMgr->LoadAutobroadcasts(); sAutobroadcastMgr->LoadAutobroadcasts();
sAutobroadcastMgr->LoadAutobroadcastsLocalized();
///- Load Motd ///- Load Motd
LOG_INFO("server.loading", "Loading Motd..."); LOG_INFO("server.loading", "Loading Motd...");