Skip to content

Commit 1b12cad

Browse files
committed
Improve readability of GameInfoToAsciiString and TruncatePlayerNames
1 parent daa4805 commit 1b12cad

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Core/GameEngine/Source/GameNetwork/GameInfo.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -962,30 +962,34 @@ static Bool TruncatePlayerNames(AsciiStringVec& playerNames, Int truncateAmount)
962962
Int truncateNameAmount = 0;
963963
for (size_t i = 0; i < lengthIndex.size(); ++i)
964964
{
965+
const Int playerIndex = lengthIndex[i].Index;
966+
const Int playerLength = lengthIndex[i].Length;
967+
965968
// round avg name length up, which will penalize the final entry (longest name) as it will have to account for the roundings
966-
Int avgNameLength = ((remainingNamesLength - truncateAmount) + (playerNames.size() - i - 1)) / (playerNames.size() - i);
967-
remainingNamesLength -= lengthIndex[i].Length;
968-
if (lengthIndex[i].Length <= avgNameLength)
969+
const Int avgNameLength = ((remainingNamesLength - truncateAmount) + (playerNames.size() - i - 1)) / (playerNames.size() - i);
970+
remainingNamesLength -= playerLength;
971+
if (playerLength <= avgNameLength)
969972
{
970973
continue;
971974
}
972975

973976
// ensure a longer name is not truncated less than a previous, shorter name
974-
truncateNameAmount = std::max(truncateNameAmount, lengthIndex[i].Length - avgNameLength);
977+
truncateNameAmount = std::max(truncateNameAmount, playerLength - avgNameLength);
975978
if (i == lengthIndex.size() - 1)
976979
{
977980
// ensure we account for rounding errors when truncating the last, longest entry
978981
truncateNameAmount = std::max(truncateAmount, truncateNameAmount);
979982
}
980983

981984
// as the name is UTF-8, make sure we don't truncate part of a multibyte character
982-
while (lengthIndex[i].Length - truncateNameAmount >= MinimumNameLength
983-
&& (playerNames[lengthIndex[i].Index][lengthIndex[i].Length - truncateNameAmount + 1] & 0xC0) == 0x80)
985+
while (playerLength - truncateNameAmount >= MinimumNameLength
986+
&& (playerNames[playerIndex].getCharAt(playerLength - truncateNameAmount + 1) & 0xC0) == 0x80)
984987
{
985-
++truncateNameAmount; // move back to the start of the multibyte character
988+
// move back to the start of the multibyte character
989+
++truncateNameAmount;
986990
}
987991

988-
playerNames[lengthIndex[i].Index].truncateBy(truncateNameAmount);
992+
playerNames[playerIndex].truncateBy(truncateNameAmount);
989993
truncateAmount -= truncateNameAmount;
990994
}
991995

@@ -1068,11 +1072,15 @@ AsciiString GameInfoToAsciiString(const GameInfo *game, const AsciiStringVec& pl
10681072
if (slot && slot->isHuman())
10691073
{
10701074
str.format( "H%s,%X,%d,%c%c,%d,%d,%d,%d,%d:",
1071-
playerNames[i].str(), slot->getIP(),
1072-
slot->getPort(), (slot->isAccepted() ? 'T' : 'F'),
1073-
(slot->hasMap() ? 'T' : 'F'),
1074-
slot->getColor(), slot->getPlayerTemplate(),
1075-
slot->getStartPos(), slot->getTeamNumber(),
1075+
playerNames[i].str(),
1076+
slot->getIP(),
1077+
slot->getPort(),
1078+
slot->isAccepted() ? 'T' : 'F',
1079+
slot->hasMap() ? 'T' : 'F',
1080+
slot->getColor(),
1081+
slot->getPlayerTemplate(),
1082+
slot->getStartPos(),
1083+
slot->getTeamNumber(),
10761084
slot->getNATBehavior());
10771085
}
10781086
else if (slot && slot->isAI())

0 commit comments

Comments
 (0)