Skip to content

Commit

Permalink
Initial commit for localized motd
Browse files Browse the repository at this point in the history
  • Loading branch information
Exitare committed Nov 13, 2024
1 parent f8d53d4 commit 80d3345
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 19 deletions.
17 changes: 17 additions & 0 deletions data/sql/updates/db_auth/2024_11_12_00.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Step 1: Add the new language columns
ALTER TABLE `motd`
ADD COLUMN `enUS` LONGTEXT NULL AFTER `text`,
ADD COLUMN `koKR` LONGTEXT NULL AFTER `enUS`,
ADD COLUMN `frFR` LONGTEXT NULL AFTER `koKR`,
ADD COLUMN `deDE` LONGTEXT NULL AFTER `frFR`,
ADD COLUMN `zhCN` LONGTEXT NULL AFTER `deDE`,
ADD COLUMN `zhTW` LONGTEXT NULL AFTER `zhCN`,
ADD COLUMN `esES` LONGTEXT NULL AFTER `zhTW`,
ADD COLUMN `esMX` LONGTEXT NULL AFTER `esES`,
ADD COLUMN `ruRU` LONGTEXT NULL AFTER `esMX`;

-- Step 2: Move the data from `text` to `enUS`
UPDATE `motd` SET `enUS` = `text` WHERE `text` IS NOT NULL;

-- Step 3: Drop the `text` column now that the data has been moved
ALTER TABLE `motd` DROP COLUMN `text`;
2 changes: 1 addition & 1 deletion src/server/apps/worldserver/RemoteAccess/RASession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void RASession::Start()
LOG_INFO("commands.ra", "User {} (IP: {}) authenticated correctly to RA", username, GetRemoteIpAddress());

// Authentication successful, send the motd
Send(std::string(std::string(sMotdMgr->GetMotd()) + "\r\n").c_str());
Send(std::string(std::string(sMotdMgr->GetMotd(LOCALE_enUS)) + "\r\n").c_str());

// Read commands
for (;;)
Expand Down
4 changes: 2 additions & 2 deletions src/server/database/Database/Implementation/LoginDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ 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_MOTD, "SELECT text FROM motd WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH);
PrepareStatement(LOGIN_REP_MOTD, "REPLACE INTO motd (realmid, text) VALUES (?, ?)", CONNECTION_ASYNC);
PrepareStatement(LOGIN_SEL_MOTD, "SELECT enUS, koKR, frFR, deDE, zhCN, zhTW, esES, esMX, ruRU FROM motd WHERE realmid = ? OR realmid = -1 ORDER BY realmid DESC", CONNECTION_SYNCH);
PrepareStatement(LOGIN_REP_MOTD, "REPLACE INTO motd (realmid, enUS) VALUES (?, ?)", CONNECTION_ASYNC);
PrepareStatement(LOGIN_INS_ACCOUNT_MUTE, "INSERT INTO account_muted VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?)", CONNECTION_ASYNC);
PrepareStatement(LOGIN_SEL_ACCOUNT_MUTE_INFO, "SELECT mutedate, mutetime, mutereason, mutedby FROM account_muted WHERE guid = ? ORDER BY mutedate ASC", CONNECTION_SYNCH);
PrepareStatement(LOGIN_DEL_ACCOUNT_MUTED, "DELETE FROM account_muted WHERE guid = ?", CONNECTION_ASYNC);
Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Handlers/CharacterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ void WorldSession::HandlePlayerLoginFromDB(LoginQueryHolder const& holder)

// Send MOTD
{
SendPacket(sMotdMgr->GetMotdPacket());
SendPacket(sMotdMgr->GetMotdPacket(pCurrChar->GetSession()->GetSessionDbLocaleIndex()));

// send server info
if (sWorld->getIntConfig(CONFIG_ENABLE_SINFO_LOGIN) == 1)
Expand Down Expand Up @@ -1153,7 +1153,7 @@ void WorldSession::HandlePlayerLoginToCharInWorld(Player* pCurrChar)

// Send MOTD
{
SendPacket(sMotdMgr->GetMotdPacket());
SendPacket(sMotdMgr->GetMotdPacket(pCurrChar->GetSession()->GetSessionDbLocaleIndex()));

// send server info
if (sWorld->getIntConfig(CONFIG_ENABLE_SINFO_LOGIN) == 1)
Expand Down
72 changes: 63 additions & 9 deletions src/server/game/Motd/MotdMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

namespace
{
WorldPacket MotdPacket;
std::string FormattedMotd;
std::unordered_map<LocaleConstant, WorldPacket> MotdPackets;
// Define a dictionary to store locale as the key and the message as the value
std::unordered_map<LocaleConstant, std::string> MotdMap;
}

MotdMgr* MotdMgr::instance()
Expand All @@ -37,6 +38,7 @@ MotdMgr* MotdMgr::instance()
return &instance;
}


void MotdMgr::SetMotd(std::string motd)
{
// scripts may change motd
Expand All @@ -50,17 +52,38 @@ void MotdMgr::SetMotd(std::string motd)
for (std::string_view token : motdTokens)
data << token;

MotdPacket = data;
MotdPackets[LOCALE_enUS] = data;

if (!motdTokens.size())
return;

std::ostringstream oss;
std::copy(motdTokens.begin(), motdTokens.end() - 1, std::ostream_iterator<std::string_view>(oss, "\n"));
oss << *(motdTokens.end() - 1); // copy back element
FormattedMotd = oss.str();
//FormattedMotd = oss.str();
}

void MotdMgr::CreateWorldMessages() {
for (const auto& [locale, motd] : MotdMap)
{
// Create a new WorldPacket for this locale
WorldPacket data(SMSG_MOTD); // new in 2.0.1

// Tokenize the motd string by '@'
std::vector<std::string_view> motdTokens = Acore::Tokenize(motd, '@', true);
data << uint32(motdTokens.size()); // line count

// Add each token to the packet
for (std::string_view token : motdTokens)
data << token;

// Store the constructed packet in MotdPackets with the locale as the key
MotdPackets[locale] = data;
}
}



void MotdMgr::LoadMotd()
{
uint32 oldMSTime = getMSTime();
Expand All @@ -73,8 +96,33 @@ void MotdMgr::LoadMotd()

if (result)
{
// Fetch the fields from the result row
Field* fields = result->Fetch();
motd = fields[0].Get<std::string>();

// Always populated field, used as placeholder
std::string defaultText = "Welcome to an AzerothCore server";
uint8_t index = 0;
// List of locale keys corresponding to database fields
std::array<LocaleConstant, TOTAL_LOCALES> localeConstants = {
LOCALE_enUS,
LOCALE_koKR,
LOCALE_frFR,
LOCALE_deDE,
LOCALE_zhCN,
LOCALE_zhTW,
LOCALE_esES,
LOCALE_esMX,
LOCALE_ruRU
};


// Populate the map by iterating through locale keys
for (LocaleConstant locale : localeConstants)
{
// Use text as a fallback if the field is NULL
MotdMap[locale] = fields[index].IsNull() ? defaultText : fields[index].Get<std::string>();
index++;
}
}
else
{
Expand All @@ -90,16 +138,22 @@ void MotdMgr::LoadMotd()
/*"ds"+"sx"*/ + "hc" + "or" +/*"F4"+"k5"*/"e." + "or" +/*"po"+"xs"*/"g|r"/*"F4"+"p2"+"o4"+"A2"+"i2"*/;;
MotdMgr::SetMotd(motd);

CreateWorldMessages();

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

char const* MotdMgr::GetMotd()

char const* MotdMgr::GetMotd(LocaleConstant locale)
{
return FormattedMotd.c_str();
return MotdMap[locale].c_str();
}

WorldPacket const* MotdMgr::GetMotdPacket()


WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale)
{
return &MotdPacket;
return &MotdPackets[locale];
}

9 changes: 6 additions & 3 deletions src/server/game/Motd/MotdMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ class AC_GAME_API MotdMgr
public:
static MotdMgr* instance();


void CreateWorldMessages();


/// Set a new Message of the Day
void SetMotd(std::string motd);

/// Load Message of the Day
void LoadMotd();

/// Get the current Message of the Day
char const* GetMotd();
char const* GetMotd(LocaleConstant locale);

/// Get the motd packet to send at login
WorldPacket const* GetMotdPacket();
WorldPacket const* GetMotdPacket(LocaleConstant locale);
};

#define sMotdMgr MotdMgr::instance()
Expand Down
2 changes: 1 addition & 1 deletion src/server/scripts/Commands/cs_reload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class reload_commandscript : public CommandScript
LOG_INFO("server.loading", "Reloading Motd...");
sMotdMgr->LoadMotd();
handler->SendGlobalGMSysMessage("DB table `motd` reloaded.");
handler->SendGlobalSysMessage(sMotdMgr->GetMotd());
handler->SendGlobalSysMessage(sMotdMgr->GetMotd(handler->GetPlayer()->GetSession()->GetSessionDbLocaleIndex()));
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/server/scripts/Commands/cs_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ class server_commandscript : public CommandScript
// Display the 'Message of the day' for the realm
static bool HandleServerMotdCommand(ChatHandler* handler)
{
handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd());

handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd(handler->GetPlayer()->GetSession()->GetSessionDbLocaleIndex()));
return true;
}

Expand Down

0 comments on commit 80d3345

Please sign in to comment.