summaryrefslogtreecommitdiff
path: root/src/server/scripts
diff options
context:
space:
mode:
authorExitare <Exitare@users.noreply.github.com>2024-12-15 10:50:02 -0800
committerGitHub <noreply@github.com>2024-12-15 19:50:02 +0100
commit7fd8b04a564be4f9f5cebfaf9fa7e261de0733ac (patch)
treef52fe2dafd1b846863aedcc0eaf89af2369c6bb2 /src/server/scripts
parent7732e1a5b2b383ff6905fd13e7ede5a697fe7647 (diff)
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>
Diffstat (limited to 'src/server/scripts')
-rw-r--r--src/server/scripts/Commands/cs_reload.cpp7
-rw-r--r--src/server/scripts/Commands/cs_server.cpp79
2 files changed, 74 insertions, 12 deletions
diff --git a/src/server/scripts/Commands/cs_reload.cpp b/src/server/scripts/Commands/cs_reload.cpp
index 575e30ac8a..a8544c2180 100644
--- a/src/server/scripts/Commands/cs_reload.cpp
+++ b/src/server/scripts/Commands/cs_reload.cpp
@@ -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;
}
diff --git a/src/server/scripts/Commands/cs_server.cpp b/src/server/scripts/Commands/cs_server.cpp
index 795734d6fc..2e5f3ff34a 100644
--- a/src/server/scripts/Commands/cs_server.cpp
+++ b/src/server/scripts/Commands/cs_server.cpp
@@ -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;
}