Veteran Member
Join Date: Feb 2007
Location: Tennessee
|
07-24-2012
, 10:53
Re: [INC] SQLVault
|
#111
|
Here's an example top for SQLVault: Extended.
It is necessary to use extended because you need to save the players' names and whatever you are sorting them by (here will be frags).
Basic plugin structure to use vault:
Spoiler
Code:
#include < amxmodx >
#include < fakemeta >
#include < sqlvault_ex >
#define MAX_PLAYERS 32
new SQLVault:g_hVault;
new KEY_KILLS[ ] = "kills";
new KEY_NAME[ ] = "name";
new g_szSteamID[ MAX_PLAYERS + 1 ][ 35 ];
new g_iKills[ MAX_PLAYERS + 1 ];
public plugin_init( )
{
// Register forward for name changing
register_forward( FM_ClientUserInfoChanged, "FwdClientInfoChanged" );
// Open the vault
g_hVault = sqlv_open_local( "top_example", false ); // No auto-init so we can init for Extended
sqlv_init_ex( g_hVault );
}
public plugin_end( )
{
// Close the vault when the plugin is exiting
sqlv_close( g_hVault );
}
public client_authorized( iPlayer )
{
// Cache player's Steam ID and load their kills
get_user_authid( iPlayer, g_szSteamID[ iPlayer ], charsmax( g_szSteamID[ ] ) );
g_iKills[ iPlayer ] = sqlv_get_num_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_KILLS );
}
public client_disconnect( iPlayer )
{
// Only save player's kills if they were loaded
if( g_szSteamID[ iPlayer ][ 0 ] )
{
sqlv_set_num_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_KILLS, g_iKills[ iPlayer ] );
// Empty out Steam ID so kills cannot be saved until they are loaded again
g_szSteamID[ iPlayer ][ 0 ] = EOS;
}
}
public FwdClientInfoChanged( iPlayer )
{
// Don't do any name checking if player was not loaded for the vault
if( !g_szSteamID[ iPlayer ][ 0 ] )
{
return FMRES_IGNORED;
}
// Code from ConnorMcLeod:
// If you don't have a pev_netname and you have a "name" setinfo value, then this is the first name you send to the server
// Otherwise, you should check for name changing.
new szOldName[ 32 ], szNewName[ 32 ];
pev( iPlayer, pev_netname, szOldName, charsmax( szOldName ) );
get_user_info( iPlayer, "name", szNewName, charsmax( szNewName ) );
if( !szOldName[ 0 ] && szNewName[ 0 ]
|| szOldName[ 0 ] && is_user_alive( iPlayer ) && !equal( szOldName, szNewName ) )
{
// Store new name in the vault
sqlv_set_data_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_NAME, szNewName );
}
return FMRES_IGNORED;
}
Now, let's add the top to it:
Spoiler
Code:
#define MAX_TOP 10
public plugin_init( )
{
// Register top commands
register_clcmd( "say /top" , "CmdTop" );
register_clcmd( "say /top10", "CmdTop" );
register_clcmd( "say /top15", "CmdTop" );
}
public CmdTop( iPlayer )
{
// Cache the database connection since we are calling sqlv_* functions more than once
sqlv_connect( g_hVault );
// Make sure we are only choosing the kills for sorting and we are sorting highest to lowest
new szWhere[ 32 ];
formatex( szWhere, charsmax( szWhere ), "`key2` = '%s'", KEY_KILLS );
new eTopData[ MAX_TOP ][ SQLVaultEntryEx ];
new iNumTop = sqlv_read_set_ex( g_hVault, eTopData, sizeof( eTopData ), _, szWhere, "`data` DESC" );
// Loop through all top for formatting the MOTD
static szMOTD[ 2048 ], szName[ 125 ];
new iLen = copy( szMOTD, charsmax( szMOTD ), "<table><tr><th>#.</th><th>Name</th><th>Kills</th></tr>" );
for( new i = 0; i < iNumTop; i++ )
{
// Grab the name that is associated with this Steam ID
// Note: The Steam ID is always key1 when we are saving/loading in this plugin
sqlv_get_data_ex( g_hVault, eTopData[ i ][ SQLVEx_Key1 ], KEY_NAME, szName, charsmax( szName ) );
// Fix the name for HTML characters
replace_all( szName, charsmax( szName ), "&", "&" );
replace_all( szName, charsmax( szName ), "<", "<" );
replace_all( szName, charsmax( szName ), ">", ">" );
// Display the top
// Note: Vault entries always give data back as strings
iLen += formatex( szMOTD[ iLen ], charsmax( szMOTD ) - iLen, "<tr><td>%d.</td><td>%s</td><td>%s</td></tr>", ( i + 1 ), szName, eTopData[ i ][ SQLVEx_Data ] );
}
copy( szMOTD[ iLen ], charsmax( szMOTD ) - iLen, "</table>" );
show_motd( iPlayer, szMOTD, "Top Players" );
// We are done with the vault's cached connection, so disconnect now
sqlv_disconnect( g_hVault );
}
All together:
Spoiler
Code:
#include < amxmodx >
#include < fakemeta >
#include < sqlvault_ex >
#define MAX_PLAYERS 32
#define MAX_TOP 10
new SQLVault:g_hVault;
new KEY_KILLS[ ] = "kills";
new KEY_NAME[ ] = "name";
new g_szSteamID[ MAX_PLAYERS + 1 ][ 35 ];
new g_iKills[ MAX_PLAYERS + 1 ];
public plugin_init( )
{
// Register forward for name changing
register_forward( FM_ClientUserInfoChanged, "FwdClientInfoChanged" );
// Register top commands
register_clcmd( "say /top" , "CmdTop" );
register_clcmd( "say /top10", "CmdTop" );
register_clcmd( "say /top15", "CmdTop" );
// Open the vault
g_hVault = sqlv_open_local( "top_example", false ); // No auto-init so we can init for Extended
sqlv_init_ex( g_hVault );
}
public plugin_end( )
{
// Close the vault when the plugin is exiting
sqlv_close( g_hVault );
}
public client_authorized( iPlayer )
{
// Cache player's Steam ID and load their kills
get_user_authid( iPlayer, g_szSteamID[ iPlayer ], charsmax( g_szSteamID[ ] ) );
g_iKills[ iPlayer ] = sqlv_get_num_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_KILLS );
}
public client_disconnect( iPlayer )
{
// Only save player's kills if they were loaded
if( g_szSteamID[ iPlayer ][ 0 ] )
{
sqlv_set_num_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_KILLS, g_iKills[ iPlayer ] );
// Empty out Steam ID so kills cannot be saved until they are loaded again
g_szSteamID[ iPlayer ][ 0 ] = EOS;
}
}
public FwdClientInfoChanged( iPlayer )
{
// Don't do any name checking if player was not loaded for the vault
if( !g_szSteamID[ iPlayer ][ 0 ] )
{
return FMRES_IGNORED;
}
// Code from ConnorMcLeod:
// If you don't have a pev_netname and you have a "name" setinfo value, then this is the first name you send to the server
// Otherwise, you should check for name changing.
new szOldName[ 32 ], szNewName[ 32 ];
pev( iPlayer, pev_netname, szOldName, charsmax( szOldName ) );
get_user_info( iPlayer, "name", szNewName, charsmax( szNewName ) );
if( !szOldName[ 0 ] && szNewName[ 0 ]
|| szOldName[ 0 ] && is_user_alive( iPlayer ) && !equal( szOldName, szNewName ) )
{
// Store new name in the vault
sqlv_set_data_ex( g_hVault, g_szSteamID[ iPlayer ], KEY_NAME, szNewName );
}
return FMRES_IGNORED;
}
public CmdTop( iPlayer )
{
// Cache the database connection since we are calling sqlv_* functions more than once
sqlv_connect( g_hVault );
// Make sure we are only choosing the kills for sorting and we are sorting highest to lowest
new szWhere[ 32 ];
formatex( szWhere, charsmax( szWhere ), "`key2` = '%s'", KEY_KILLS );
new eTopData[ MAX_TOP ][ SQLVaultEntryEx ];
new iNumTop = sqlv_read_set_ex( g_hVault, eTopData, sizeof( eTopData ), _, szWhere, "`data` DESC" );
// Loop through all top for formatting the MOTD
static szMOTD[ 2048 ], szName[ 125 ];
new iLen = copy( szMOTD, charsmax( szMOTD ), "<table><tr><th>#.</th><th>Name</th><th>Kills</th></tr>" );
for( new i = 0; i < iNumTop; i++ )
{
// Grab the name that is associated with this Steam ID
// Note: The Steam ID is always key1 when we are saving/loading in this plugin
sqlv_get_data_ex( g_hVault, eTopData[ i ][ SQLVEx_Key1 ], KEY_NAME, szName, charsmax( szName ) );
// Fix the name for HTML characters
replace_all( szName, charsmax( szName ), "&", "&" );
replace_all( szName, charsmax( szName ), "<", "<" );
replace_all( szName, charsmax( szName ), ">", ">" );
// Display the top
// Note: Vault entries always give data back as strings
iLen += formatex( szMOTD[ iLen ], charsmax( szMOTD ) - iLen, "<tr><td>%d.</td><td>%s</td><td>%s</td></tr>", ( i + 1 ), szName, eTopData[ i ][ SQLVEx_Data ] );
}
copy( szMOTD[ iLen ], charsmax( szMOTD ) - iLen, "</table>" );
show_motd( iPlayer, szMOTD, "Top Players" );
// We are done with the vault's cached connection, so disconnect now
sqlv_disconnect( g_hVault );
}
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw
I love you exolent!
|
|
|