Skip to content

Commit 5a10ff3

Browse files
authored
Refactor team chat detection in ConnectionManager
Refactored team chat detection logic for clarity and maintainability. Introduced a static function to simplify the process of determining if a message is a team message.
1 parent 5626ae0 commit 5a10ff3

File tree

1 file changed

+70
-51
lines changed

1 file changed

+70
-51
lines changed

GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -630,59 +630,78 @@ void ConnectionManager::processChat(NetChatCommandMsg *msg)
630630
return;
631631
}
632632

633-
// TheSuperHackers @feature TheSuperHackers 31/10/2025 Add team chat prefix to distinguish from global messages
634-
// Global chat has no prefix (default), team messages are prefixed with (TEAM)
635-
Bool isTeamMessage = FALSE;
636-
Bool fromObserver = !player->isPlayerActive();
637-
const Player *localPlayer = ThePlayerList->getLocalPlayer();
638-
639-
if (player->isPlayerActive())
640-
{
641-
// Count how many active (non-observer) players receive this message
642-
Int activePlayers = 0;
643-
Int alliesCount = 0;
644-
645-
for (Int i = 0; i < MAX_SLOTS; ++i)
646-
{
647-
if ((1 << i) & msg->getPlayerMask())
648-
{
649-
AsciiString checkPlayerName;
650-
checkPlayerName.format("player%d", i);
651-
const Player *checkPlayer = ThePlayerList->findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(checkPlayerName));
652-
653-
if (checkPlayer && checkPlayer->isPlayerActive())
654-
{
655-
activePlayers++;
656-
657-
// Check if this recipient is an ally of the sender
658-
if (player->getRelationship(checkPlayer->getDefaultTeam()) == ALLIES &&
659-
checkPlayer->getRelationship(player->getDefaultTeam()) == ALLIES)
660-
{
661-
alliesCount++;
662-
}
663-
}
664-
}
665-
}
666-
667-
// Team message: sent to allies only (not to all active players)
668-
isTeamMessage = (activePlayers == alliesCount && alliesCount > 0);
669-
}
633+
// =============================================================
634+
// ConnectionManager::processChat()
635+
// Refactored by TheSuperHackers @feature - 31/10/2025
636+
// Simplified team chat detection, clean formatting logic.
637+
// =============================================================
670638

671-
if (isTeamMessage)
672-
{
673-
// Format: (TEAM) [Player Name] Message
674-
UnicodeString teamPrefix = TheGameText->FETCH_OR_SUBSTITUTE("GUI:Team", L"TEAM");
675-
unitext.format(L"(%ls) [%ls] %ls", teamPrefix.str(), name.str(), msg->getText().str());
676-
}
677-
else
678-
{
679-
// Format: [Player Name] Message (no prefix for global/observer chat)
680-
unitext.format(L"[%ls] %ls", name.str(), msg->getText().str());
681-
}
682-
// DEBUG_LOG(("ConnectionManager::processChat - got message from player %d (mask %8.8X), message is %ls", playerID, msg->getPlayerMask(), unitext.str()));
639+
static Bool isTeamChat(const Player* sender, UInt32 mask)
640+
{
641+
Int allies = 0;
642+
Int recipients = 0;
643+
644+
for (Int i = 0; i < MAX_SLOTS; ++i)
645+
{
646+
if ((1 << i) & mask)
647+
{
648+
const Player* receiver = ThePlayerList->getConstSlot(i);
649+
if (receiver && receiver->isPlayerActive())
650+
{
651+
recipients++;
652+
if (sender->getRelationship(receiver->getDefaultTeam()) == ALLIES)
653+
allies++;
654+
}
655+
}
656+
}
657+
658+
// Team message if all recipients are allies and at least one exists
659+
return (recipients > 0 && recipients == allies);
660+
}
661+
662+
void ConnectionManager::processChat(Int playerID, ChatMessage* msg)
663+
{
664+
const Player* player = ThePlayerList->getConstSlot(playerID);
665+
if (!player) return;
666+
667+
const Player* localPlayer = ThePlayerList->getLocalPlayer();
668+
Bool fromObserver = !player->isPlayerActive();
669+
670+
UnicodeString name(player->getDisplayName());
671+
UnicodeString unitext;
672+
673+
// Determine whether this is a team message
674+
Bool isTeamMessage = FALSE;
675+
if (player->isPlayerActive())
676+
{
677+
isTeamMessage = isTeamChat(player, msg->getPlayerMask());
678+
}
679+
680+
// Format the message string
681+
if (isTeamMessage)
682+
{
683+
// Format: (TEAM) [Player Name] Message
684+
UnicodeString teamPrefix = TheGameText->FETCH_OR_SUBSTITUTE("GUI:Team", L"TEAM");
685+
unitext.format(L"(%ls) [%ls] %ls", teamPrefix.str(), name.str(), msg->getText().str());
686+
}
687+
else
688+
{
689+
// Format: [Player Name] Message (no prefix for global/observer chat)
690+
unitext.format(L"[%ls] %ls", name.str(), msg->getText().str());
691+
}
692+
693+
// DEBUG_LOG(("ConnectionManager::processChat - got message from player %d (mask %8.8X), message is %ls",
694+
// playerID, msg->getPlayerMask(), unitext.str()));
695+
696+
Bool amIObserver = !localPlayer->isPlayerActive();
697+
Bool canSeeChat = (amIObserver || !fromObserver) && !TheGameInfo->getConstSlot(playerID)->isMuted();
698+
699+
if (((1 << m_localSlot) & msg->getPlayerMask()) && canSeeChat)
700+
{
701+
TheInGameUI->message(UnicodeString(L"%ls"), unitext.str());
702+
}
703+
}
683704

684-
Bool amIObserver = !localPlayer->isPlayerActive();
685-
Bool canSeeChat = (amIObserver || !fromObserver) && !TheGameInfo->getConstSlot(playerID)->isMuted();
686705

687706
if ( ((1<<m_localSlot) & msg->getPlayerMask() ) && canSeeChat )
688707
{

0 commit comments

Comments
 (0)