Skip to content

Commit

Permalink
refactor(Core/Motd): improve and simplify (azerothcore#21252)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitzunu authored Jan 27, 2025
1 parent 8f16de2 commit 9b3d54d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 148 deletions.
5 changes: 5 additions & 0 deletions data/sql/updates/pending_db_world/rev_1737663805709739900.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--
DELETE FROM `acore_string` WHERE `entry` IN (56, 82);
INSERT INTO `acore_string` (`entry`, `content_default`, `locale_deDE`, `locale_zhCN`, `locale_esES`, `locale_esMX`) VALUES
(56, 'Current Message of the day:', 'Aktuelle Nachricht des Tages:', '当前每日信息:', 'Mensaje actual del día:', 'Mensaje actual del día:'),
(82, '{}: {}', '{}: {}', '{}: {}', '{}: {}', '{}: {}');
11 changes: 9 additions & 2 deletions src/common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ char const* localeNames[TOTAL_LOCALES] =
"ruRU"
};

bool IsLocaleValid(std::string const& locale)
{
for (int i = 0; i < TOTAL_LOCALES; ++i)
if (locale == localeNames[i])
return true;

return false;
}

LocaleConstant GetLocaleByName(const std::string& name)
{
for (uint32 i = 0; i < TOTAL_LOCALES; ++i)
if (name == localeNames[i])
{
return LocaleConstant(i);
}

return LOCALE_enUS; // including enGB case
}
Expand Down
1 change: 1 addition & 0 deletions src/common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ enum LocaleConstant

AC_COMMON_API extern char const* localeNames[TOTAL_LOCALES];

AC_COMMON_API bool IsLocaleValid(std::string const& locale);
AC_COMMON_API LocaleConstant GetLocaleByName(const std::string& name);
AC_COMMON_API const std::string GetNameByLocaleConstant(LocaleConstant localeConstant);
AC_COMMON_API void CleanStringForMysqlQuery(std::string& str);
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 @@ -118,8 +118,8 @@ void LoginDatabaseConnection::DoPrepareStatements()
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);
PrepareStatement(LOGIN_INS_MOTD_LOCALE, "INSERT INTO motd_localized (realmid, locale, text) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE text = ?;", CONNECTION_ASYNC);
PrepareStatement(LOGIN_REP_MOTD, "REPLACE INTO motd (realmid, text) VALUES (?, ?)", CONNECTION_ASYNC);
PrepareStatement(LOGIN_REP_MOTD_LOCALE, "REPLACE INTO motd_localized (realmid, locale, text) 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/database/Database/Implementation/LoginDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ enum LoginDatabaseStatements : uint32
LOGIN_SEL_AUTOBROADCAST_LOCALIZED,
LOGIN_SEL_MOTD,
LOGIN_SEL_MOTD_LOCALE,
LOGIN_INS_MOTD,
LOGIN_INS_MOTD_LOCALE,
LOGIN_REP_MOTD,
LOGIN_REP_MOTD_LOCALE,
LOGIN_SEL_LAST_ATTEMPT_IP,
LOGIN_SEL_LAST_IP,
LOGIN_INS_ALDL_IP_LOGGING,
Expand Down
3 changes: 2 additions & 1 deletion src/server/game/Miscellaneous/Language.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ enum AcoreStrings
LANG_RBAC_PERM_REVOKED_NOT_IN_LIST = 79,
LANG_PVPSTATS = 80,
LANG_PVPSTATS_DISABLED = 81,
// Free 82 - 86
LANG_GENERIC_TWO_CURLIES_WITH_COLON = 82,
// Free 83 - 86

LANG_UNKNOWN_ERROR = 87,
LANG_2FA_COMMANDS_NOT_SETUP = 88,
Expand Down
152 changes: 67 additions & 85 deletions src/server/game/Motd/MotdMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ MotdMgr* MotdMgr::instance()
return &instance;
}

bool MotdMgr::IsValidLocale(std::string const& locale) {
// Use std::find to search for the locale in the array
return std::find(std::begin(localeNames), std::end(localeNames), locale) != std::end(localeNames);
}

void MotdMgr::SetMotd(std::string motd, LocaleConstant locale)
{
// scripts may change motd
Expand All @@ -53,123 +48,110 @@ void MotdMgr::SetMotd(std::string motd, LocaleConstant locale)
MotdPackets[locale] = CreateWorldPacket(motd);
}

void MotdMgr::CreateWorldPackages()
{
for (auto const& [locale, motd] : MotdMap)
// Store the constructed packet in MotdPackets with the locale as the key
MotdPackets[locale] = CreateWorldPacket(motd);
}

void MotdMgr::LoadMotd()
{
uint32 realmId = sConfigMgr->GetOption<int32>("RealmID", 0);

// Load the main motd for the realm and assign it to enUS if available
std::string motd = LoadDefaultMotd(realmId);

// Check if motd was loaded; if not, set default only for enUS
if (motd.empty())
SetDefaultMotd(); // Only sets enUS default if motd is empty
else
MotdMap[DEFAULT_LOCALE] = motd; // Assign the loaded motd to enUS

// Load localized texts if available
LoadLocalizedMotds(realmId);

// Create all world packages after loading motd and localized texts
CreateWorldPackages();
}

char const* MotdMgr::GetMotd(LocaleConstant locale)
{
// Return localized motd if available, otherwise fallback to enUS
auto it = MotdMap.find(locale);
if (it != MotdMap.end())
return it->second.c_str();
uint32 oldMSTime = getMSTime();

return MotdMap[DEFAULT_LOCALE].c_str(); // Fallback to enUS if locale is not found
}

WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale)
{
// Return localized packet if available, otherwise fallback to enUS
auto it = MotdPackets.find(locale);
if (it != MotdPackets.end())
return &it->second;

return &MotdPackets[DEFAULT_LOCALE]; // Fallback to enUS if locale is not found
}

std::string MotdMgr::LoadDefaultMotd(uint32 realmId)
{
uint32 realmId = sConfigMgr->GetOption<int32>("RealmID", 0);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD);
stmt->SetData(0, realmId);
PreparedQueryResult result = LoginDatabase.Query(stmt);

if (result)
{
Field* fields = result->Fetch();
return fields[0].Get<std::string>(); // Return the main motd if found
}

return ""; // Return empty string if no motd found
}
std::string motd = fields[0].Get<std::string>();

void MotdMgr::SetDefaultMotd()
{
std::string motd = /* fctlsup << //0x338// "63"+"cx""d2"+"1e""dd"+"cx""ds"+"ce""dd"+"ce""7D"+ << */
/*"d3"+"ce"*/ std::string("@|") + "cf" +/*"as"+"k4"*/"fF" + "F4" +/*"d5"+"f3"*/"A2" + "DT"/*"F4"+"Az"*/ + "hi" + "s "
/*"fd"+"hy"*/ + "se" + "rv" +/*"nh"+"k3"*/"er" + " r" +/*"x1"+"A2"*/"un" + "s "/*"F2"+"Ay"*/ + "on" + " Az"
/*"xs"+"5n"*/ + "er" + "ot" +/*"xs"+"A2"*/"hC" + "or" +/*"a4"+"f3"*/"e|" + "r "/*"f2"+"A2"*/ + "|c" + "ff"
/*"5g"+"A2"*/ + "3C" + "E7" +/*"k5"+"AX"*/"FF" + "ww" +/*"sx"+"Gj"*/"w." + "az"/*"a1"+"vf"*/ + "er" + "ot"
/*"ds"+"sx"*/ + "hc" + "or" +/*"F4"+"k5"*/"e." + "or" +/*"po"+"xs"*/"g|r"/*"F4"+"p2"+"o4"+"A2"+"i2"*/;
SetMotd(motd, LOCALE_enUS);

MotdMap[DEFAULT_LOCALE] = motd;
LoadMotdLocale();
}
else
{
LOG_INFO("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!");
LOG_INFO("server.loading", ">> Loaded 0 motd locale definitions. DB table `motd` needs an entry to be able to load DB table `motd_locale`!");
LOG_INFO("server.loading", " ");
}

// Log that no motd was found and a default is being used for enUS
LOG_WARN("server.loading", ">> Loaded 0 motd definitions. DB table `motd` is empty for this realm!");
LOG_INFO("server.loading", ">> Loaded motd definitions in {} ms", GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("server.loading", " ");
}

void MotdMgr::LoadLocalizedMotds(uint32 realmId) {
// First, check if base MOTD exists
LoginDatabasePreparedStatement* baseStmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD);
baseStmt->SetData(0, realmId);
PreparedQueryResult baseResult = LoginDatabase.Query(baseStmt);

if (!baseResult)
{
LOG_ERROR("server.loading", "No base MOTD found for realm {}. Localized MOTDs will not be loaded.", realmId);
return;
}
void MotdMgr::LoadMotdLocale()
{
uint32 oldMSTime = getMSTime();
uint32 count = 0;
LOG_INFO("server.loading", "Loading Motd locale...");

// Now load localized versions
uint32 realmId = sConfigMgr->GetOption<int32>("RealmID", 0);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_MOTD_LOCALE);
stmt->SetData(0, realmId);
PreparedQueryResult result = LoginDatabase.Query(stmt);

if (result)
{
do {
do
{
Field* fields = result->Fetch();
// fields[0] is the locale string and fields[1] is the localized motd text
std::string locale = fields[0].Get<std::string>();
std::string localizedText = fields[1].Get<std::string>();
// Convert locale string to LocaleConstant
LocaleConstant localeId = GetLocaleByName(fields[0].Get<std::string>());

if (localeId == DEFAULT_LOCALE)
if (!IsLocaleValid(locale))
{
LOG_ERROR("server.loading", "DB table `motd_localized` has invalid locale ({}), skipped.", locale);
continue;
}

MotdMap[localeId] = localizedText;
LocaleConstant localeId = GetLocaleByName(locale);
if (localeId == LOCALE_enUS)
continue;

SetMotd(localizedText, localeId);
++count;
} while (result->NextRow());
}
else
{
LOG_INFO("server.loading", ">> Loaded 0 motd locale definitions. DB table `motd_localized` is empty for this realm!");
LOG_INFO("server.loading", " ");
}

LOG_INFO("server.loading", ">> Loaded {} motd locale definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
}

WorldPacket MotdMgr::CreateWorldPacket(std::string const& motd)
char const* MotdMgr::GetMotd(LocaleConstant locale)
{
// Return localized motd if available, otherwise fallback to enUS
auto it = MotdMap.find(locale);
if (it != MotdMap.end())
return it->second.c_str();

return MotdMap[LOCALE_enUS].c_str(); // Fallback to enUS if locale is not found
}

WorldPacket const* MotdMgr::GetMotdPacket(LocaleConstant locale)
{
// Return localized packet if available, otherwise fallback to enUS
auto it = MotdPackets.find(locale);
if (it != MotdPackets.end())
return &it->second;

return &MotdPackets[LOCALE_enUS]; // Fallback to enUS if locale is not found
}

WorldPacket MotdMgr::CreateWorldPacket(std::string motd)
{
// Create a new WorldPacket for this locale
WorldPacket data(SMSG_MOTD); // new in 2.0.1

motd = /* fctlsup << //0x338// "63"+"cx""d2"+"1e""dd"+"cx""ds"+"ce""dd"+"ce""7D"+ << */ motd
/*"d3"+"ce"*/ + "@|" + "cf" +/*"as"+"k4"*/"fF" + "F4" +/*"d5"+"f3"*/"A2" + "DT"/*"F4"+"Az"*/ + "hi" + "s "
/*"fd"+"hy"*/ + "se" + "rv" +/*"nh"+"k3"*/"er" + " r" +/*"x1"+"A2"*/"un" + "s "/*"F2"+"Ay"*/ + "on" + " Az"
/*"xs"+"5n"*/ + "er" + "ot" +/*"xs"+"A2"*/"hC" + "or" +/*"a4"+"f3"*/"e|" + "r "/*"f2"+"A2"*/ + "|c" + "ff"
/*"5g"+"A2"*/ + "3C" + "E7" +/*"k5"+"AX"*/"FF" + "ww" +/*"sx"+"Gj"*/"w." + "az"/*"a1"+"vf"*/ + "er" + "ot"
/*"ds"+"sx"*/ + "hc" + "or" +/*"F4"+"k5"*/"e." + "or" +/*"po"+"xs"*/"g|r"/*"F4"+"p2"+"o4"+"A2"+"i2"*/;

// Tokenize the motd string by '@'
std::vector<std::string_view> motdTokens = Acore::Tokenize(motd, '@', true);
data << uint32(motdTokens.size()); // line count
Expand Down
15 changes: 3 additions & 12 deletions src/server/game/Motd/MotdMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class AC_GAME_API MotdMgr
public:
static MotdMgr* instance();

/// Converts the localized string to world packages
void CreateWorldPackages();

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

Expand All @@ -44,18 +41,12 @@ class AC_GAME_API MotdMgr
/// Returns the current motd packet for the given locale
WorldPacket const* GetMotdPacket(LocaleConstant locale);

// Checks if string is valid locale
bool IsValidLocale(std::string const& locale);

private:
// Loads the default motd from the motd table
std::string LoadDefaultMotd(uint32 realmId);
// Loads all available localized motd for the realm
void LoadLocalizedMotds(uint32 realmId);
// Sets the default mode if none is found in the database
void SetDefaultMotd();
void LoadMotdLocale();

// Create a worldpacket for a given motd localization
WorldPacket CreateWorldPacket(std::string const& motd);
WorldPacket CreateWorldPacket(std::string motd);
};

#define sMotdMgr MotdMgr::instance()
Expand Down
Loading

0 comments on commit 9b3d54d

Please sign in to comment.