Re: Change nvault to sql
Untested
PHP Code:
#include <amxmodx> #include <sqlx>
//Uncomment 'IM_USING_ZP50' if your server is running ZP50 and above. //#define IM_USING_ZP50
#if defined IM_USING_ZP50 native zp_ammopacks_get(id) native zp_ammopacks_set(id, amount) #else native zp_get_user_ammo_packs(id) native zp_set_user_ammo_packs(id, amount) #endif
#define MAX_PLAYERS 32
//Max player's ammopack that can be save in vault. #define MAX_AMMOPACK 9999
//Max day player's ammopack can be stay in vault, set 0 = permanent. #define DAY_PRUNE 15
new const szHost[] = ""; new const szUser[] = ""; new const szPass[] = "";
enum QueryTypes { qtCreateTable, qtPruneDB, qtLoadAmmo, qtSaveAmmo }
new const Query_CreateTable[] = "CREATE TABLE IF NOT EXISTS tblStore (ID INTEGER PRIMARY KEY AUTOINCREMENT, SteamID VARCHAR(34) UNIQUE, Time_Stamp INTEGER, Ammo INTEGER);"; new const Query_LoadAmmo[] = "SELECT Ammo FROM tblStore WHERE SteamID='%s';"; new const Query_SaveAmmo[] = "REPLACE INTO tblStore (SteamID, Time_Stamp, Ammo) VALUES ('%s',%d,%d);"; #if DAY_PRUNE > 0 new const Query_PruneDatabase[] = "DELETE FROM tblStore WHERE Time_Stamp < %d;"; #endif
enum _:QueryInfo { QueryTypes:qiQueryType, qiPlayerID }
new bool:bIsLoaded[ MAX_PLAYERS + 1 ], szSteamid[ MAX_PLAYERS + 1 ][ 35 ]; new Handle:g_SQLTuple , g_szBuffer[ 256 ];
public plugin_init() { register_plugin( "ZP AP Store SQL" , "0.0.1" , "wbyokomo/bugsy" ); }
public plugin_cfg() { SQL_SetAffinity( "sqlite" ); g_SQLTuple = SQL_MakeDbTuple( szHost , szUser , szPass , "ZPAPStore" ); new Handle:SQLConnection , ErrorCode; SQLConnection = SQL_Connect( g_SQLTuple , ErrorCode , g_szBuffer , charsmax( g_szBuffer ) ); if( SQLConnection == Empty_Handle ) set_fail_state( g_szBuffer ); RunQuery( SQLConnection , Query_CreateTable ); #if DAY_PRUNE > 0 formatex( g_szBuffer , charsmax( g_szBuffer ) , Query_PruneDatabase , get_systime() - ( 86400 * DAY_PRUNE ) ); RunQuery( SQLConnection , g_szBuffer ); #endif }
public plugin_end() { SQL_FreeHandle( g_SQLTuple ); }
public client_putinserver(id) { if ( !is_user_hltv(id) && !is_user_bot( id ) ) { get_user_authid( id , szSteamid[ id] , charsmax( szSteamid[] ) ); LoadData( id ); } }
#if AMXX_VERSION_NUM < 190 public client_disconnect( id ) #else public client_disconnected( id ) #endif { SaveData( id ); bIsLoaded[ id ] = false; }
LoadData( id ) { new QueryData[ QueryInfo ]; QueryData[ qiQueryType ] = qtLoadAmmo; QueryData[ qiPlayerID ] = id; formatex( g_szBuffer , charsmax( g_szBuffer ) , Query_LoadAmmo , szSteamid[ id ] ); SQL_ThreadQuery( g_SQLTuple , "QueryHandle" , g_szBuffer , QueryData , sizeof( QueryData ) ); }
SaveData( id ) { if ( bIsLoaded[ id ] ) { #if defined IM_USING_ZP50 new ap = min( zp_ammopacks_get( id ) , MAX_AMMOPACK ); #else new ap = min( zp_get_user_ammo_packs( id ), MAX_AMMOPACK ); #endif formatex( g_szBuffer , charsmax( g_szBuffer ) , Query_SaveAmmo , szSteamid[ id ] , get_systime() , ap ); SQL_ThreadQuery( g_SQLTuple , "QueryHandle" , g_szBuffer ); } }
RunQuery( Handle:SQLConnection , const szQuery[] ) { new Handle:Query = SQL_PrepareQuery( SQLConnection , szQuery ); if( !SQL_Execute( Query ) ) { SQL_QueryError( Query , g_szBuffer , charsmax( Query_CreateTable ) ); set_fail_state( g_szBuffer ); } }
public QueryHandle( FailState , Handle:Query , const Error[] , Errcode , const Data[ QueryInfo ] , DataSize ) { if( FailState == TQUERY_CONNECT_FAILED ) { set_fail_state( "Could not connect to SQL database." ); return; } else if( FailState == TQUERY_QUERY_FAILED ) { log_amx( "Query error: %s" , Error ); set_fail_state( g_szBuffer ); return; } new iAmmo , id = Data[ qiPlayerID ]; switch ( Data[ qiQueryType ] ) { case qtLoadAmmo: { if ( SQL_NumResults( Query ) ) { iAmmo = SQL_ReadResult( Query , 0 ); #if defined IM_USING_ZP50 zp_ammopacks_set( id , iAmmo ); #else zp_set_user_ammo_packs( id , iAmmo ); #endif bIsLoaded[ id ] = true; } } } }
|