Skip to content

Commit

Permalink
TUI-related fixes
Browse files Browse the repository at this point in the history
Identified/fixed problem with `/teams` command and player ratings working properly for players who changed the case of their name (fred->Fred).

Exit properly when running the `/quit` command.
  • Loading branch information
tra committed Feb 16, 2025
1 parent 4c9e9ec commit 78c51bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/game/CNetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,20 @@ std::vector<CPlayerManager*> CNetManager::AllPlayers() {
return players;
}

// return a lowercase copy of string
inline std::string lowerStr(std::string s)
{
std::string low(s);
std::transform(low.begin(), low.end(), low.begin(), ::tolower);
return low;
}

int CNetManager::PlayerSlot(std::string playerName) {
std::string lowerName1(lowerStr(playerName));
for (int i = 0; i < kMaxAvaraPlayers; i++) {
if (playerTable[i].get()->GetPlayerName() == playerName) {
std::string lowerName2(lowerStr(playerTable[i].get()->GetPlayerName()));
// case-insensitive name comparison ("FreD" == "fred")
if (lowerName1 == lowerName2) {
return i;
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/game/PlayerRatingsSimpleElo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ void PlayerRatingsSimpleElo::UpdateRatings(std::vector<PlayerResult> &playerResu
int outCount = 0;
for (auto result: playerResults) {
auto playerId = result.playerId;

// if a player changed the CASE of their name (FreD to fREd)...
if (playerId != ratingsMap.find(playerId)->first) {
std::cout << "LOOK at " << playerId << std::endl;
// pull the old node out of the map and reinsert with the new name
auto rating = ratingsMap.extract(playerId);
rating.key() = playerId;
ratingsMap.insert(std::move(rating));
}

ratingsMap[playerId].rating += adjustments[playerId].rating;
ratingsMap[playerId].count += adjustments[playerId].count > 0 ? 1 : 0; // don't count game more than once
ratingsMap[playerId].rating = std::roundf(ratingsMap[playerId].rating*4)/4;
Expand All @@ -88,11 +98,14 @@ void PlayerRatingsSimpleElo::UpdateRatings(std::vector<PlayerResult> &playerResu
if (result == playerResults.front()) {
gCurrentGame->itsApp->AddMessageLine("Updated Elo Ratings:");
}
// format results like this:
// PlayerName[111] = 1622(+22)
// LongerPlayerName[22] = 1477( -6)
oss << std::right << std::setw(21)
<< (playerId + "[" + std::to_string(ratingsMap[playerId].count) + "] = ")
// format results like this (stop displaying game count over 99):
// PlayerName = 1622(+22)
// LongPlayerName[22] = 1477( -6)
std::string numPlayed(ratingsMap[playerId].count > 99 ?
"" :
"[" + std::to_string(ratingsMap[playerId].count) + "]");
oss << std::right << std::setw(19)
<< (playerId + numPlayed + " = ")
<< std::fixed << std::setprecision(0)
<< std::setw(4) << ratingsMap[playerId].rating
<< "(" << std::showpos << std::setw(3) << adjustments[playerId].rating << std::noshowpos << ")" ;
Expand Down
1 change: 1 addition & 0 deletions src/gui/CApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void CApplication::Done() {
_prefs[kWindowHeight] = mSize[1];
_prefs.erase(kDefaultClientUDPPort); // don't save client port
WritePrefs(_prefs);
std::exit(EXIT_SUCCESS);
}

void CApplication::BroadcastCommand(int theCommand) {
Expand Down

0 comments on commit 78c51bd

Please sign in to comment.