From f405ed137c9ccfdb57dd7211f6d071750ef1fc33 Mon Sep 17 00:00:00 2001 From: Karamel Date: Thu, 22 Feb 2024 20:35:01 +0100 Subject: [PATCH] Add motd to autohost and challenge --- doc/hosting/AutohostConfig.md | 7 ++++++- src/multiint.cpp | 35 +++++++++++++++++++++++++++++++++++ src/multiint.h | 1 + src/multijoin.cpp | 1 + 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/doc/hosting/AutohostConfig.md b/doc/hosting/AutohostConfig.md index 8519bb73292..7100d071be6 100644 --- a/doc/hosting/AutohostConfig.md +++ b/doc/hosting/AutohostConfig.md @@ -45,6 +45,10 @@ Each player slot can be customized, starting from 0. The first slot will be defi * `difficulty` sets the difficulty for an AI. It can be one of `Easy`, `Medium`, `Hard` or `Insane`. * `name` sets a custom name for the AI. +## Message of the Day + +The `motd` is displayed in the chat box when a player joins the game. It is optional and will be truncated if it exceeds 256 characters. + ## Sample file ``` @@ -83,6 +87,7 @@ Each player slot can be customized, starting from 0. The first slot will be defi }, "player_3": { "team": 1 - } + }, + "motd": "Good Luck, Have Fun!" } ``` diff --git a/src/multiint.cpp b/src/multiint.cpp index 238a8b0e818..78dcf166222 100644 --- a/src/multiint.cpp +++ b/src/multiint.cpp @@ -284,6 +284,7 @@ static struct } locked; static bool spectatorHost = false; static uint16_t defaultOpenSpectatorSlots = 0; +static WzString motd = WzString(); struct AIDATA { @@ -6036,6 +6037,23 @@ static void resetPlayerConfiguration(const bool bShouldResetLocal = false) } } +static void loadMapMessageOfTheDay(WzConfig& ini) +{ + if (ini.contains("motd")) + { + motd = ini.value("motd").toWzString(); + if (motd.length() > MAX_CONSOLE_STRING_LENGTH) + { + motd.truncate(MAX_CONSOLE_STRING_LENGTH); + debug(LOG_WARNING, "MOTD was truncated to fit into %d characters.", MAX_CONSOLE_STRING_LENGTH); + } + } + else + { + motd = WzString(""); + } +} + /** * Loads challenge and player configurations from level/autohost/test .json-files. */ @@ -6081,6 +6099,7 @@ static void loadMapChallengeAndPlayerSettings(bool forceLoadPlayers = false) WzConfig ini(ininame, WzConfig::ReadOnly); loadMapChallengeSettings(ini); + loadMapMessageOfTheDay(ini); /* Do not load player settings if we are already hosting an online match */ if (!bIsOnline || forceLoadPlayers) @@ -7665,6 +7684,11 @@ void WzMultiplayerOptionsTitleUI::screenSizeDidChange(unsigned int oldWidth, uns static void printHostHelpMessagesToConsole() { char buf[512] = { '\0' }; + if (!motd.isEmpty()) + { + ssprintf(buf, motd.toUtf8().c_str()); + displayRoomNotifyMessage(buf); + } if (challengeActive) { ssprintf(buf, "%s", _("Hit the ready box to begin your challenge!")); @@ -8614,6 +8638,17 @@ void sendRoomSystemMessageToSingleReceiver(char const *text, uint32_t receiver, message.enqueue(NETnetQueue(receiver)); } +void sendRoomMotdToSingleReceiver(uint32_t receiver) +{ + if (motd.isEmpty()) + { + return; + } + ASSERT_OR_RETURN(, isHumanPlayer(receiver), "Invalid receiver: %" PRIu32 "", receiver); + NetworkTextMessage message(NOTIFY_MESSAGE, motd.toUtf8().c_str()); + message.enqueue(NETnetQueue(receiver)); +} + static void sendRoomChatMessage(char const *text, bool skipLocalDisplay) { NetworkTextMessage message(selectedPlayer, text); diff --git a/src/multiint.h b/src/multiint.h index 605ec4f6be3..4b52bab0c8f 100644 --- a/src/multiint.h +++ b/src/multiint.h @@ -99,6 +99,7 @@ WzString formatGameName(WzString name); void sendRoomSystemMessage(char const *text); void sendRoomNotifyMessage(char const *text); void sendRoomSystemMessageToSingleReceiver(char const *text, uint32_t receiver, bool skipLocalDisplay = false); +void sendRoomMotdToSingleReceiver(uint32_t receiver); void displayRoomSystemMessage(char const *text); void displayRoomNotifyMessage(char const *text); void displayLobbyDisabledNotification(); diff --git a/src/multijoin.cpp b/src/multijoin.cpp index 4025a160a0b..2041c1e89b5 100644 --- a/src/multijoin.cpp +++ b/src/multijoin.cpp @@ -575,6 +575,7 @@ bool MultiPlayerJoin(UDWORD playerIndex) } } } + sendRoomMotdToSingleReceiver(playerIndex); if (lobby_slashcommands_enabled()) { // Inform the new player that this lobby has slash commands enabled.