feat(Core/Motd): Allow localized motd (#20542)

* Initial commit for localized motd

* Rename function that created world packages

* Update to satisfy code check

* Update code to accomodate localized motd

* Update command to support multiple optionales & adjusted db

* Code cleanup

* Update sql name

* Fix codestyle issues

* Remove hardcoded schema

* Add check for valid player in reload command

* Update to better code style

* Add missing include

* Fix redundant code usage

* Add missing include

* Remove sql files and create new rev sql files

* Address minor code reviews

* Fix code style

* Update code to address code revisions.

- Remove two unused functions
- Remove map
- Use available function to resolve LocaleConstant

* Fix code style

* Add check for base motd and update locale to DEFAULT_LOCALE

* Code docs

* Removed some docs, readd defaultd motd formatting

* Fix oversight in variable declaration

* Code style fix

* Update code based on code review

* ready for merge

* Fix set motd command due to changes to DEFAULT_LOCALE

* Fix CI

* Fix trailing whitespace

---------

Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com>
This commit is contained in:
Exitare
2024-12-15 10:50:02 -08:00
committed by GitHub
parent 7732e1a5b2
commit 7fd8b04a56
15 changed files with 267 additions and 82 deletions

View File

@@ -414,7 +414,12 @@ public:
LOG_INFO("server.loading", "Reloading Motd...");
sMotdMgr->LoadMotd();
handler->SendGlobalGMSysMessage("DB table `motd` reloaded.");
handler->SendGlobalSysMessage(sMotdMgr->GetMotd());
LocaleConstant locale = DEFAULT_LOCALE;
if (Player* player = handler->GetPlayer())
locale = player->GetSession()->GetSessionDbLocaleIndex();
handler->SendGlobalSysMessage(sMotdMgr->GetMotd(locale));
return true;
}

View File

@@ -24,6 +24,7 @@
#include "Chat.h"
#include "CommandScript.h"
#include "Common.h"
#include "GameTime.h"
#include "GitRevision.h"
#include "Log.h"
@@ -288,7 +289,11 @@ public:
// Display the 'Message of the day' for the realm
static bool HandleServerMotdCommand(ChatHandler* handler)
{
handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd());
LocaleConstant localeConstant = DEFAULT_LOCALE;
if (Player* player = handler->GetPlayer())
localeConstant = player->GetSession()->GetSessionDbLocaleIndex();
handler->PSendSysMessage(LANG_MOTD_CURRENT, sMotdMgr->GetMotd(localeConstant));
return true;
}
@@ -520,32 +525,84 @@ public:
}
// Define the 'Message of the day' for the realm
static bool HandleServerSetMotdCommand(ChatHandler* handler, Optional<int32> realmId, Tail motd)
static bool HandleServerSetMotdCommand(ChatHandler* handler, Optional<int32> realmId, Optional<std::string> locale, Tail motd)
{
std::wstring wMotd = std::wstring();
std::string strMotd = std::string();
std::wstring wMotd = std::wstring();
std::string strMotd = std::string();
// Default realmId to the current realm if not provided
if (!realmId)
realmId = static_cast<int32>(realm.Id.Realm);
if (motd.empty())
return false;
if (!Utf8toWStr(motd, wMotd))
// Convert Tail (motd) to std::string
std::ostringstream motdStream;
motdStream << motd;
std::string motdString = motdStream.str(); // Convert Tail to std::string
// Determine the locale; default to "enUS" if not provided
LocaleConstant localeConstant = DEFAULT_LOCALE;
if (locale.has_value())
{
if (sMotdMgr->IsValidLocale(locale.value()))
{
localeConstant = GetLocaleByName(locale.value());
}
else
{
motdStream.str("");
motdStream << locale.value() << " " << motd;
motdString = motdStream.str();
localeConstant = DEFAULT_LOCALE;
locale = GetNameByLocaleConstant(localeConstant);
}
}
else
{
// Set to default locale string
localeConstant = DEFAULT_LOCALE;
locale = GetNameByLocaleConstant(localeConstant);
}
// Convert the concatenated motdString to UTF-8 and ensure encoding consistency
if (!Utf8toWStr(motdString, wMotd))
return false;
if (!WStrToUtf8(wMotd, strMotd))
return false;
// Start a transaction for the database operations
LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_REP_MOTD);
stmt->SetData(0, realmId.value());
stmt->SetData(1, strMotd);
trans->Append(stmt);
if (localeConstant == DEFAULT_LOCALE)
{
// Insert or update in the main motd table for enUS
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_MOTD);
stmt->SetData(0, realmId.value()); // realmId for insertion
stmt->SetData(1, strMotd); // motd text for insertion
stmt->SetData(2, strMotd); // motd text for ON DUPLICATE KEY UPDATE
trans->Append(stmt);
}
else
{
// Insert or update in the motd_localized table for other locales
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_MOTD_LOCALE);
stmt->SetData(0, realmId.value()); // realmId for insertion
stmt->SetData(1, locale.value()); // locale for insertion
stmt->SetData(2, strMotd); // motd text for insertion
stmt->SetData(3, strMotd); // motd text for ON DUPLICATE KEY UPDATE
trans->Append(stmt);
}
// Commit the transaction & update db
LoginDatabase.CommitTransaction(trans);
sMotdMgr->LoadMotd();
handler->PSendSysMessage(LANG_MOTD_NEW, realmId.value(), strMotd);
// Update the in-memory maps for the current realm. Otherwise, do not update
if (realmId == -1 || realmId == static_cast<int32>(realm.Id.Realm))
sMotdMgr->SetMotd(strMotd, localeConstant);
handler->PSendSysMessage(LANG_MOTD_NEW, realmId.value(), locale.value(), strMotd);
return true;
}