PHP Code:
#include <amxmodx>
#include <nvault>
#include <nvault_util>
#include <nvault_array>
//Allocate additional memory to plugin to prevent stack error
#pragma dynamic 16384
new const Version[] = "0.2";
new const VaultName[] = "XP_Top15_Example";
//This determines the max number of players that will be included in your top 15 calculation. It is best to keep this at a
//value <= the max players that you expect to have data saved in the vault. If the number of player data saved exceeds this
//value then your top 15 will not be accurate since some players will be left out.
const Max_Player_Support = 3000;
//Components of data that will be saved for each player. SteamID is used as the key so it is not needed within the data.
enum PlayerData
{
PlayerName[ 32 ],
XP
}
new pdData[ MAX_PLAYERS + 1 ][ PlayerData ];
//Components of data used for sorting.
enum _:RankInfo
{
nVault_Offset,
XP_Value
}
new g_SortData[ Max_Player_Support ][ RankInfo ];
new g_AuthID[ MAX_PLAYERS + 1 ][ 34 ] , bool:g_BotOrHLTV[ MAX_PLAYERS + 1 ];
new g_Vault;
//In your plugin, you set a players XP using the below:
//pdData[ id ][ XP ] = 12345;
public plugin_init()
{
register_plugin( "nVault Top 15 and Rank" , Version , "bugsy" );
register_clcmd( "say top15" , "ShowTop15" );
register_clcmd( "say rank" , "ShowRank" );
register_clcmd( "say loadtestdata" , "LoadTestData" );
if ( ( g_Vault = nvault_open( VaultName ) ) == INVALID_HANDLE )
{
set_fail_state( "Failed to open vault" );
}
}
public plugin_end()
{
nvault_close( g_Vault );
}
public client_authorized( id )
{
if ( !( g_BotOrHLTV[ id ] = bool:( is_user_bot( id ) || is_user_hltv( id ) ) ) )
{
//Get players' steam-id so it can be used to retrieve their data from the vault.
get_user_authid( id , g_AuthID[ id ] , charsmax( g_AuthID[] ) );
//Retrieve player data from vault.
nvault_get_array( g_Vault , g_AuthID[ id ] , pdData[ id ][ PlayerData:0 ] , sizeof( pdData[] ) );
}
}
public client_disconnect( id )
{
if ( !g_BotOrHLTV[ id ] )
{
//To avoid having to monitor for name changes in-game, the players name is retrieved and saved when disconnecting.
get_user_name( id , pdData[ id ][ PlayerName ] , charsmax( pdData[][ PlayerName ] ) );
//Save player data to vault.
nvault_set_array( g_Vault , g_AuthID[ id ] , pdData[ id ][ PlayerData:0 ] , sizeof( pdData[] ) );
}
}
public LoadAndSortData( &iVault )
{
new iRow , iCount , iNextOffset , iCurrentOffset , szKey[ 45 ] , pdVal[ PlayerData ];
//Close and re-open vault so the journal writes to the vault so nvault_util gets most up to date data.
nvault_close( g_Vault );
g_Vault = nvault_open( VaultName );
//Open vault for nvault utility usage.
iVault = nvault_util_open( VaultName );
//Get count of total number of records in the vault.
iCount = nvault_util_count( iVault );
//Loop through all records in the vault.
for ( iRow = 0 ; iRow < iCount && iRow < Max_Player_Support ; iRow++ )
{
//Read record from vault. iNextOffset will hold the position of the next record in the vault.
iNextOffset = nvault_util_read_array( iVault , iNextOffset , szKey , charsmax( szKey ) , pdVal[ PlayerData:0 ] , sizeof( pdVal ) );
//Set nVault_Offset to the byte offset for this players data. This will allow for retrieving any data for this player that needs to appear in the top 15 (name, steam id, etc.)
//iPrevOffset is used since iOffset holds the position of the NEXT record, not current.
g_SortData[ iRow ][ nVault_Offset ] = iCurrentOffset;
//Set Player_XP value in array to his XP value. This will be used for sorting.
g_SortData[ iRow ][ XP_Value ] = pdVal[ XP ];
//Since nvault_util_read_array() holds the position of the next record, we have to hold the current offset separately.
iCurrentOffset = iNextOffset;
}
//Sort the array.
SortCustom2D( g_SortData , min( iCount , Max_Player_Support ) , "CompareXP" );
return iCount;
}
public ShowTop15( id )
{
new iVault , iAvailablePlayers , pdVal[ PlayerData ] , szMOTD[ 1501 ] , iPos , iCount , iCurrentOffset , szKey[ 34 ];
iCount = LoadAndSortData( iVault );
//Prepare top 15 MOTD.
iPos = formatex( szMOTD , charsmax( szMOTD ) , "<body bgcolor=#000000><font color=#98f5ff><pre>" );
iPos += formatex( szMOTD[ iPos ] , charsmax( szMOTD ) - iPos , "%2s %-22.22s %3s^n" , "#" , "Name" , "XP" );
//This will account for if the vault has less than 15 player data records stored.
iAvailablePlayers = min( iCount , 15 );
//Build the top 15. iAvailablePlayers is set to the smaller of 15 or the total records in the vault.
for ( new iRow = 0 ; iRow < iAvailablePlayers ; iRow++ )
{
//Get nVault player data offset value which was set in the above loop.
iCurrentOffset = g_SortData[ iRow ][ nVault_Offset ];
//Read data at the players offset so we can retrieve their name to be displayed in the top 15.
nvault_util_read_array( iVault , iCurrentOffset , szKey , charsmax( szKey ) , pdVal[ PlayerData:0 ] , sizeof( pdVal ) );
//Format line in MOTD.
iPos += formatex( szMOTD[ iPos ] , charsmax( szMOTD ) - iPos ,"%2d %-22.22s %3d^n", ( iRow + 1 ) , pdVal[ PlayerName ] , pdVal[ XP ] );
}
//Close nvault utility file.
nvault_util_close( iVault );
formatex( szMOTD[ iPos ], charsmax( szMOTD ) - iPos , "</body></font></pre>" );
show_motd( id , szMOTD , "XP Top 15" );
return PLUGIN_HANDLED;
}
public ShowRank( id )
{
new iVault , iRow , iCount , iCurrentOffset , szKey[ 34 ] , pdVal[ PlayerData ] , bool:bRanked;
iCount = LoadAndSortData( iVault );
//Cycle through all records in the vault to see where the player stands in ranking
for ( iRow = 0 ; iRow < iCount ; iRow++ )
{
//Get nVault player data offset value which was set in the above loop.
iCurrentOffset = g_SortData[ iRow ][ nVault_Offset ];
//Read data at the players offset so we can get their steam id and compare to player requesting their rank.
nvault_util_read_array( iVault , iCurrentOffset , szKey , charsmax( szKey ) , pdVal[ PlayerData:0 ] , sizeof( pdVal ) );
if ( equal( g_AuthID[ id ] , szKey ) )
{
client_print( id , print_chat , "* Your rank is %d" , iRow + 1 );
bRanked = true;
break;
}
}
if ( !bRanked )
{
client_print( id , print_chat , "* You are not yet ranked" );
}
//Close nvault utility file.
nvault_util_close( iVault );
return PLUGIN_HANDLED;
}
public LoadTestData()
{
new szAuthID[ 10 ] , pdData[ PlayerData ] , iRow , iXP;
for ( iRow = 0 ; iRow < 100 ; iRow++ )
{
formatex( szAuthID , charsmax( szAuthID ) , "STEAM_%d" , iRow );
iXP = random( 9999 );
formatex( pdData[ PlayerName ] , charsmax( pdData[ PlayerName ] ) , "Player_%d [%d]" , iRow , iXP );
pdData[ XP ] = iXP;
nvault_set_array( g_Vault , szAuthID , pdData[ PlayerData:0 ] , sizeof( pdData ) );
}
return PLUGIN_HANDLED;
}
public CompareXP( elem1[] , elem2[] )
{
if ( elem1[ 1 ] > elem2[ 1 ] )
return -1;
else if(elem1[ 1 ] < elem2[ 1 ] )
return 1;
return 0;
}