AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Save User XP [ MYSQL ] (https://forums.alliedmods.net/showthread.php?t=217488)

hadesownage 06-03-2013 10:55

Save User XP [ MYSQL ]
 
Hi !
I have a problem with my xp system .
The xp.. don't save.. sometimes .

This is my code:

PHP Code:


public SqlInit( )
{
    new 
szHost32 ]
    new 
szUser32 ]
    new 
szPass32 ]
    new 
szDB32 ]
    
    
get_pcvar_stringg_pcvarHostszHostcharsmaxszHost ) )
    
get_pcvar_stringg_pcvaruUserszUsercharsmaxszUser ) )
    
get_pcvar_stringg_pcvarPassszPasscharsmaxszPass ) )
    
get_pcvar_stringg_pcvarDBszDBcharsmaxszDB ) )
    
    
g_SqlTuple SQL_MakeDbTupleszHostszUserszPassszDB )
    
    new 
ErrorCode
    
new Handle:SqlConnection SQL_Connectg_SqlTupleErrorCodeg_Errorcharsmaxg_Error ) )
    
    if( 
SqlConnection == Empty_Handle )
        
set_fail_stateg_Error )
    
    new 
Handle:Queries
    Queries 
SQL_PrepareQuerySqlConnection"CREATE TABLE IF NOT EXISTS %s ( nick VARCHAR( 32 ) PRIMARY KEY, level INT( 11 ), xp INT( 11 ), strenghtpower INT( 11 ), agilitypower INT( 11 ), healthpower INT( 11 ), levitationpower INT( 11 ), useralreadyhavepower INT( 11 ), ultimatepower INT( 11 ), coins INT( 11 ))"SQL_TABLE )
    
    if( !
SQL_ExecuteQueries ) )
    {
        
SQL_QueryErrorQueriesg_Errorcharsmaxg_Error ) )
        
set_fail_stateg_Error )
    }
    
    
SQL_FreeHandleQueries )
    
SQL_FreeHandleSqlConnection 
}

public 
plugin_end( ) {
 
SQL_FreeHandleg_SqlTuple )
}


public 
SaveData(id)
{
    new 
szTemp512 ]
    
formatszTempcharsmaxszTemp ), "UPDATE `%s` SET `level` = '%d' , `xp` = '%d' , `strenghtpower` = '%d' , `agilitypower` = '%d' , `healthpower` = '%d' , `levitationpower` =  '%d' , `useralreadyhavepower` =  '%d' , `ultimatepower` =  '%d' , `coins` =  '%d'  WHERE `%s`.`nick` = '%s';"SQL_TABLE,  Level id ], eXP[id], StrenghtPower id ], AgilityPower id ], HealthPower id ], LevitationPower id ], UserAlreadyHavePower id ], UltimatePower id ], Coins id ], SQL_TABLEg_szNameid ] )
    
    
SQL_ThreadQueryg_SqlTuple"IgnoreHandle"szTemp )
}

public 
LoadDataid )
{
    new 
Data]
    
Data] = id
    
    
new szTemp512 ]
    
formatszTempcharsmaxszTemp ), "SELECT level,xp,strenghtpower,agilitypower,healthpower,levitationpower,useralreadyhavepower,ultimatepower,coins FROM `%s` WHERE (`%s`.`nick` = '%s');"SQL_TABLESQL_TABLEg_szNameid ] )
    
    
SQL_ThreadQueryg_SqlTuple"RegisterClient"szTempData)
}

public 
RegisterClientFailStateHandle:QueryError[ ], ErrcodeData[ ], DataSize )
{
    if( 
SQL_IsFailFailStateErrcodeError ) )
        return 
PLUGIN_HANDLED_MAIN
    
    
new id
    id 
Data]
    
    if( 
SQL_NumResultsQuery ) < )
    {
        new 
szTemp512 ]
        
formatszTempcharsmaxszTemp ), "INSERT INTO %s (nick,level,xp,strenghtpower,agilitypower,healthpower,levitationpower,useralreadyhavepower,ultimatepower,coins) VALUES('%s', '0', '0', '0', '0', '0', '0', '0', '0', '0' );"SQL_TABLEg_szNameid ], Level id ], eXP id ], StrenghtPower id ], AgilityPower id ], HealthPower id ], LevitationPower id ], UserAlreadyHavePower id ], UltimatePower id ], Coins id ] )
        
        
SQL_ThreadQueryg_SqlTuple"IgnoreHandle"szTemp )
    } 
    else
    {        
        
Level[id] = SQL_ReadResultQuery)
        
eXPid ] = SQL_ReadResultQuery)
        
StrenghtPower id ] = SQL_ReadResultQuery)
        
AgilityPower id ] = SQL_ReadResultQuery)
        
HealthPower id ] = SQL_ReadResultQuery)
        
LevitationPower id ] = SQL_ReadResultQuery)
        
UserAlreadyHavePower id ] = SQL_ReadResultQuery)
        
UltimatePower id ] = SQL_ReadResultQuery)
        
Coins id ] = SQL_ReadResultQuery)
    }
    
    return 
PLUGIN_CONTINUE
}


public 
IgnoreHandleFailStateHandle:QueryError[ ], ErrcodeData[ ], DataSize )
    
SQL_FreeHandleQuery )

SQL_IsFail( const FailState, const Errcode, const Error[ ] ) {
    if( 
FailState == TQUERY_CONNECT_FAILED )
    {
        
log_amx"[Error] Could not connect to SQL database: %s"Error )
        return 
true
    
}
    
    else if( 
FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"[Error] Query failed: %s"Error )
        return 
true
    
}
    
    else if( 
Errcode )
    {
        
log_amx"[Error] Error on query: %s"Error )
        return 
true
    
}
    
    return 
false


Where is the problem ?
On nvault.. same problem .. why ? :|

YamiKaitou 06-03-2013 14:40

Re: Save User XP [ MYSQL ]
 
Is the query failing?

Clauu 06-04-2013 06:52

Re: Save User XP [ MYSQL ]
 
Free the connection and tuple only in plugin end and as the documentation threaded queryies do not require freeing the query. Also your code it's kinda bad writing and due to this bugs could occur.

hadesownage 06-04-2013 09:07

Re: Save User XP [ MYSQL ]
 
Xp is saved, the plugin is connected to mysql and the tabels is created.
But, sometimes.. the xp is not loaded, is 0, and the xp table is changed to 0 .

PHP Code:

public plugin_end( ) {
 
SQL_FreeHandleg_SqlTuple )
}

public 
client_connect(id)
{
    
get_user_nameidg_szNameid ], charsmaxg_szName[ ] ) )
    
set_task(1.5,"LoadData",id)
    
set_task(2.0,"LoadData",id)
    
set_task(1.5,"RemoveAll",id)
}

public 
client_disconnect id ) {
    
    
SaveData id );
    
RemoveAll id );


</b>

In client_disconnect is necessary to add get_user_name ?

TheDS1337 06-04-2013 09:30

Re: Save User XP [ MYSQL ]
 
Quote:

Originally Posted by hadesownage (Post 1964300)
Xp is saved, the plugin is connected to mysql and the tabels is created.
But, sometimes.. the xp is not loaded, is 0, and the xp table is changed to 0 .

PHP Code:

public plugin_end( ) {
 
SQL_FreeHandleg_SqlTuple )
}

public 
client_connect(id)
{
    
get_user_nameidg_szNameid ], charsmaxg_szName[ ] ) )
    
set_task(1.5,"LoadData",id)
    
set_task(2.0,"LoadData",id)
    
set_task(1.5,"RemoveAll",id)
}

public 
client_disconnect id ) {
    
    
SaveData id );
    
RemoveAll id );


</b>

In client_disconnect is necessary to add get_user_name ?

it's not needed, you can get client name only when he connects, you don't need to get his name when he disconnects.

hadesownage 06-11-2013 10:51

Re: Save User XP [ MYSQL ]
 
Ok ! Then where is the problem ? :)


All times are GMT -4. The time now is 16:18.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.