Skip to content

Commit a57fd01

Browse files
committed
Implemented index_from_name in C++
1 parent 73b86c1 commit a57fd01

4 files changed

Lines changed: 31 additions & 22 deletions

File tree

‎addons/source-python/packages/source-python/players/helpers.py‎

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from _players._helpers import edict_from_userid
2727
from _players._helpers import index_from_playerinfo
2828
from _players._helpers import index_from_userid
29+
from _players._helpers import index_from_name
2930
from _players._helpers import inthandle_from_playerinfo
3031
from _players._helpers import inthandle_from_userid
3132
from _players._helpers import playerinfo_from_baseentity
@@ -132,28 +133,6 @@ def index_from_uniqueid(uniqueid):
132133
'Conversion from "UniqueID" ({}) to "Index" failed.'.format(uniqueid))
133134

134135

135-
def index_from_name(name):
136-
"""Return an index from the given player name.
137-
138-
:param str name: The player name to get the index of.
139-
:rtype: int
140-
"""
141-
# Loop through all players on the server
142-
for edict in PlayerGenerator():
143-
144-
# Get the PlayerInfo instance of the player...
145-
playerinfo = playerinfo_from_edict(edict)
146-
147-
# Is the current player's name the same as the one given?
148-
if playerinfo.name == name:
149-
150-
# Return the index of the current player
151-
return index_from_playerinfo(playerinfo)
152-
153-
raise ValueError(
154-
'Conversion from "Name" ({}) to "Index" failed.'.format(name))
155-
156-
157136
def uniqueid_from_playerinfo(playerinfo):
158137
"""Return the UniqueID for the given player.
159138

‎src/core/modules/players/players_helpers_wrap.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void export_player_conversion_functions(scope _helpers)
5757
// To Index conversions...
5858
EXPORT_CONVERSION_FUNCTION(unsigned int, Index, unsigned int, Userid);
5959
EXPORT_CONVERSION_FUNCTION(unsigned int, Index, IPlayerInfo *, PlayerInfo);
60+
EXPORT_CONVERSION_FUNCTION(unsigned int, Index, const char *, Name);
6061

6162
// To Edict conversions...
6263
EXPORT_CONVERSION_FUNCTION(edict_t *, Edict, unsigned int, Userid, reference_existing_object_policy());

‎src/core/utilities/conversions.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ bool IndexFromBaseHandle( CBaseHandle hBaseHandle, unsigned int& output );
211211
bool IndexFromIntHandle( unsigned int iEntityHandle, unsigned int& output );
212212
bool IndexFromUserid( unsigned int iUserID, unsigned int& output );
213213
bool IndexFromPlayerInfo( IPlayerInfo *pPlayerInfo, unsigned int& output );
214+
bool IndexFromName( const char *szName, unsigned int& output );
214215

215216
CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, edict_t *, Edict);
216217
CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, CBaseHandle, BaseHandle);
@@ -219,6 +220,7 @@ CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, CPointer *, Pointer);
219220
CREATE_EXC_CONVERSION_FUNCTION_BASE_ENTITY(unsigned int, Index, CBaseEntity *, BaseEntity);
220221
CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, unsigned int, Userid);
221222
CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, IPlayerInfo *, PlayerInfo);
223+
CREATE_EXC_CONVERSION_FUNCTION(unsigned int, Index, const char *, Name);
222224

223225

224226
//-----------------------------------------------------------------------------

‎src/core/utilities/conversions/index_from.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@ bool IndexFromEdict( edict_t *pEdict, unsigned int& output )
5353
}
5454

5555

56+
//-----------------------------------------------------------------------------
57+
// Returns an index from the given player name.
58+
//-----------------------------------------------------------------------------
59+
bool IndexFromName(const char* szName, unsigned int& output )
60+
{
61+
if (!szName || szName[0] == '\0')
62+
return false;
63+
64+
int iEntityIndex = 0;
65+
IPlayerInfo* pInfo = NULL;
66+
while(iEntityIndex < gpGlobals->maxClients)
67+
{
68+
iEntityIndex++;
69+
if (!PlayerInfoFromIndex(iEntityIndex, pInfo))
70+
continue;
71+
72+
if (V_strcmp(pInfo->GetName(), szName) == 0)
73+
{
74+
output = iEntityIndex;
75+
return true;
76+
}
77+
}
78+
79+
return false;
80+
}
81+
82+
5683
//-----------------------------------------------------------------------------
5784
// Returns an index from the given BaseEntity instance.
5885
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)