Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Core/Autobroacast)Add localized autobroadcast messages #20946

Merged
merged 18 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions data/sql/updates/pending_db_auth/rev_1734400454914932600.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
;
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void LoginDatabaseConnection::DoPrepareStatements()
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_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_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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enum LoginDatabaseStatements : uint32
LOGIN_SEL_REALMLIST_SECURITY_LEVEL,
LOGIN_DEL_ACCOUNT,
LOGIN_SEL_AUTOBROADCAST,
LOGIN_SEL_AUTOBROADCAST_LOCALIZED,
LOGIN_SEL_MOTD,
LOGIN_SEL_MOTD_LOCALE,
LOGIN_INS_MOTD,
Expand Down
112 changes: 91 additions & 21 deletions src/server/game/Autobroadcast/AutobroadcastMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,54 @@ void AutobroadcastMgr::LoadAutobroadcasts()
_announceType = AnnounceType::World;
}

uint32 count = 0;

do
{
Field* fields = result->Fetch();
uint8 id = fields[0].Get<uint8>();

_autobroadcasts[id] = fields[2].Get<std::string>();
ObjectMgr::AddLocaleString(fields[2].Get<std::string>(), DEFAULT_LOCALE, _autobroadcasts[id]);
_autobroadcastsWeights[id] = fields[1].Get<uint8>();

++count;
} while (result->NextRow());

LOG_INFO("autobroadcast", ">> Loaded {} Autobroadcast Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("autobroadcast", " ");
LOG_INFO("server.loading", ">> Loaded {} Autobroadcast Definitions in {} ms", _autobroadcasts.size(), GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("server.loading", " ");
}

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!");
return;
}

uint8 count = 0;

do
{
Field* fields = result->Fetch();
uint8 id = fields[0].Get<uint8>();
LocaleConstant locale = GetLocaleByName(fields[1].Get<std::string>());

Exitare marked this conversation as resolved.
Show resolved Hide resolved
Exitare marked this conversation as resolved.
Show resolved Hide resolved
if (locale == DEFAULT_LOCALE)
Exitare marked this conversation as resolved.
Show resolved Hide resolved
continue;

ObjectMgr::AddLocaleString(fields[2].Get<std::string>(), locale, _autobroadcasts[id]);
count++;
} while (result->NextRow());

LOG_INFO("server.loading", ">> Loaded {} Localized Autobroadcast Definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}

void AutobroadcastMgr::SendAutobroadcasts()
Expand All @@ -80,8 +113,7 @@ void AutobroadcastMgr::SendAutobroadcasts()

uint32 weight = 0;
AutobroadcastsWeightMap selectionWeights;

std::string msg;
uint8 textId = 0;;
Exitare marked this conversation as resolved.
Show resolved Hide resolved

for (AutobroadcastsWeightMap::const_iterator it = _autobroadcastsWeights.begin(); it != _autobroadcastsWeights.end(); ++it)
{
Expand All @@ -101,42 +133,80 @@ void AutobroadcastMgr::SendAutobroadcasts()
weight += it->second;
if (selectedWeight < weight)
{
msg = _autobroadcasts[it->first];
textId = it->first;
break;
}
}
}
else
{
msg = _autobroadcasts[urand(0, _autobroadcasts.size())];
textId = urand(0, _autobroadcasts.size());
}

switch (_announceType)
{
case AnnounceType::World:
SendWorldAnnouncement(msg);
SendWorldAnnouncement(textId);
break;
case AnnounceType::Notification:
SendNotificationAnnouncement(msg);
SendNotificationAnnouncement(textId);
break;
case AnnounceType::Both:
SendWorldAnnouncement(msg);
SendNotificationAnnouncement(msg);
SendWorldAnnouncement(textId);
SendNotificationAnnouncement(textId);
default:
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.count(textId))
Exitare marked this conversation as resolved.
Show resolved Hide resolved
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));
data << msg.data();
sWorld->SendGlobalMessage(&data);
ChatHandler(nullptr).DoForAllValidSessions([&](Player* player)
{
// Retrieve player's locale
LocaleConstant locale = player->GetSession()->GetSessionDbLocaleIndex();

if (!_autobroadcasts.count(textId))
{
return;
}
Exitare marked this conversation as resolved.
Show resolved Hide resolved

// 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);
});
}
12 changes: 7 additions & 5 deletions src/server/game/Autobroadcast/AutobroadcastMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Common.h"
#include <map>
#include <vector>

enum class AnnounceType : uint8
{
Expand All @@ -34,17 +35,18 @@ class AC_GAME_API AutobroadcastMgr
static AutobroadcastMgr* instance();

void LoadAutobroadcasts();
void LoadAutobroadcastsLocalized();
void SendAutobroadcasts();

private:
void SendWorldAnnouncement(std::string msg);
void SendNotificationAnnouncement(std::string msg);
void SendWorldAnnouncement(uint8 textId);
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;

AutobroadcastsMap _autobroadcasts;
AutobroadcastsWeightMap _autobroadcastsWeights;
AutobroadcastsMap _autobroadcasts; // autobroadcast messages
AutobroadcastsWeightMap _autobroadcastsWeights; // Weights for each message

AnnounceType _announceType;
};
Expand Down
1 change: 1 addition & 0 deletions src/server/game/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,7 @@ void World::SetInitialWorldSettings()
///- Load AutoBroadCast
LOG_INFO("server.loading", "Loading Autobroadcasts...");
sAutobroadcastMgr->LoadAutobroadcasts();
sAutobroadcastMgr->LoadAutobroadcastsLocalized();

///- Load Motd
LOG_INFO("server.loading", "Loading Motd...");
Expand Down
Loading