That code will work sometimes but if no one has yet scored you will get an error and if multiple players have the same score you will get an inaccurate result.
The below function will return accurate results based on all scenerios:
- If one player has highest frags
- If multiple players tied for having highest frags
- If no one has any frags (frags > 0)
Results
Code:
bugsy has the most frags [ 1 frags ]
The following players tied for the most frags: bugsy,Kimbo Slice,Ricardo Almeida [ 1 frags ]
No players have any frags.
I made a little plugin to show how it works.
PHP Code:
#include <amxmodx>
new const Version[] = "0.1";
public plugin_init()
{
register_plugin( "Most Frags" , Version , "bugsy" );
register_clcmd( "say /mostfrags" , "MostFrags" );
}
public MostFrags( id )
{
new iPlayer , iPlayers , iFrags , bool:bTie;
if ( ( iPlayers = GetMostFrags( iPlayer , iFrags , bTie ) ) )
{
if ( bTie )
{
new szNames[ 192 ] , iPos;
for ( new id = 1 ; id <= 32 ; id++ )
{
if ( iPlayers & ( 1 << ( id & 31 ) ) )
{
iPos += get_user_name( id , szNames[ iPos ] , charsmax( szNames ) - iPos );
szNames[ iPos++ ] = ',';
}
}
szNames[ iPos - 1 ] = EOS;
client_print( id , print_chat , "The following players tied for the most frags: %s [ %d frags ]" , szNames , iFrags );
return;
}
new szName[ 33 ];
get_user_name( iPlayer , szName , charsmax( szName ) );
client_print( id , print_chat , "%s has the most frags [ %d frags ]" , szName , iFrags );
}
else
{
client_print( id , print_chat , "No players have any frags." );
}
}
GetMostFrags( &iPlayer , &iFrags , &bool:bTie )
{
new iPlayers[ 32 ] , iNum , iCurPlayer , iTie;
new iCurFrags , iMostFrags , iMostFragsPlayer;
get_players( iPlayers , iNum );
for ( new i = 0 ; i < iNum ; i++ )
{
iCurPlayer = iPlayers[ i ];
if ( ( iCurFrags = get_user_frags( iCurPlayer ) ) > iMostFrags )
{
iMostFragsPlayer = iCurPlayer;
iMostFrags = iCurFrags;
iTie = 0;
}
else if ( iCurFrags == iMostFrags )
{
iTie |= ( 1 << ( iCurPlayer & 31 ) );
iTie |= ( 1 << ( iMostFragsPlayer & 31 ) );
}
}
if ( iMostFrags <= 0 )
return 0;
if ( iTie )
{
iPlayer = 0;
iFrags = iMostFrags;
bTie = true;
return iTie;
}
iPlayer = iMostFragsPlayer;
iFrags = iMostFrags;
return 1;
}
__________________