From 0be125aa07a9d6cebf47b81fc23eb61cc96cd7b6 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Fri, 1 Feb 2013 19:59:58 +0000 Subject: [PATCH] Simplified cWorld::FindAndDoWithPlayer() It should still work the same - call the callback for the player with the most similar name. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1184 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/World.cpp | 19 ++++++++----------- source/World.h | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/source/World.cpp b/source/World.cpp index 084bf8cd2..e5e8977bd 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -1752,33 +1752,30 @@ bool cWorld::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_ -bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback) +bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCallback & a_Callback) { - cPlayer* BestMatch = 0; + cPlayer * BestMatch = NULL; unsigned int BestRating = 0; - unsigned int NumMatches = 0; - unsigned int NameLength = a_PlayerName.length(); + unsigned int NameLength = a_PlayerNameHint.length(); cCSLock Lock(m_CSPlayers); for (cPlayerList::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr) { - unsigned int Rating = RateCompareString (a_PlayerName, (*itr)->GetName()); - if (Rating > 0 && Rating >= BestRating) + unsigned int Rating = RateCompareString (a_PlayerNameHint, (*itr)->GetName()); + if (Rating >= BestRating) { BestMatch = *itr; - if( Rating > BestRating ) NumMatches = 0; BestRating = Rating; - ++NumMatches; } - if (Rating == NameLength) // Perfect match + if (Rating == NameLength) // Perfect match { break; } } // for itr - m_Players[] - if (NumMatches == 1) + if (BestMatch != NULL) { - LOG("Compared %s and %s with rating %i", a_PlayerName.c_str(), BestMatch->GetName().c_str(), BestRating ); + LOG("Compared %s and %s with rating %i", a_PlayerNameHint.c_str(), BestMatch->GetName().c_str(), BestRating); return a_Callback.Item (BestMatch); } return false; diff --git a/source/World.h b/source/World.h index d3ed37aa3..e30fa7981 100644 --- a/source/World.h +++ b/source/World.h @@ -180,7 +180,7 @@ public: bool DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback); // >> EXPORTED IN MANUALBINDINGS << /// Finds a player from a partial or complete player name and calls the callback - case-insensitive - bool FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_Callback); // >> EXPORTED IN MANUALBINDINGS << + bool FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCallback & a_Callback); // >> EXPORTED IN MANUALBINDINGS << unsigned int GetNumPlayers(); // tolua_export