AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Need help] Nvault -> MySql saving (https://forums.alliedmods.net/showthread.php?t=204228)

andrzN 12-28-2012 16:54

[Need help] Nvault -> MySql saving
 
I need help changing a plugin that uses nvault save to make it use mysql save.

I have no knowledge what so ever of how to code with mysql saving, so I need you you to make the code for me. If you need any more information from the plugin to be able to do this, please tell me.

Here is the code for saving/loading.

PHP Code:

public client_disconnect(id

    
TotalPlayedTime[id] = TotalPlayedTime[id] + (get_user_time(id)); 
    
SaveTime(idTotalPlayedTime[id]); 


public 
client_putinserver(id

    
TotalPlayedTime[id] = LoadTime(id);


public 
LoadTimeid )  

    new 
valut nvault_open("Time_played_0.4"
     
    new 
authid[33]; 
    new 
vaultkey[64], vaultdata[64]; 
     
    
get_user_authid(idauthid[id],31); 
     
    
format(vaultkey63"TIMEPLAYED%s"authid[id]); 
     
    
nvault_get(valutvaultkeyvaultdata63); 
    
nvault_close(valut); 
     
    return 
str_to_num(vaultdata); 


public 
SaveTime(id,PlayedTime

    new 
valut nvault_open("Time_played_0.4"
     
    if(
valut == INVALID_HANDLE
        
set_fail_state("nValut returned invalid handle"
     
    new 
authid[33]; 
    new 
vaultkey[64], vaultdata[64]; 
     
    
get_user_authid(idauthid[id],31); 
     
    
format(vaultkey63"TIMEPLAYED%s"authid[id]);  
    
format(vaultdata63"%d"PlayedTime);  
     
    
nvault_set(valutvaultkeyvaultdata); 
    
nvault_close(valut); 


public 
prune() 

    new 
valut nvault_open("Time_played_0.4"); 
     
    if(
valut == INVALID_HANDLE
        
set_fail_state("nValut returned invalid handle"); 
     
    
nvault_prune(valut0get_systime() - PRUNE_TIME); 
    
nvault_close(valut); 


public 
plugin_end() 

    
prune() 


Thank you so much in advance if anyone would help me!

rastafari 12-29-2012 02:17

Re: [Need help] Nvault -> MySql saving
 
This might help you: https://forums.alliedmods.net/showpo...12&postcount=4

Sylwester 12-29-2012 03:05

Re: [Need help] Nvault -> MySql saving
 
try this (works with both sqlite and mysql, configuration in amxmodx/configs/sql.cfg):
PHP Code:

#include <amxmodx>
#include <sqlx>

#define _GenerateUserId() (_pg_last_userid = (_pg_last_userid+1)%0xffffff)
new _pg_last_userid

#define _Set(%1,%2) %1|=1<<%2
#define _UnSet(%1,%2) %1&=~(1<<%2)
#define _Is(%1,%2) (%1&1<<%2)
new _in_server_authed_loaded

new g_max_players
new g_p_local_userid[33
new 
Handle:g_sql_tuple
new g_sql_ready


public plugin_init(){
    
g_max_players get_maxplayers()
    
set_task(0.2"sql_init")
}


public 
sql_init(){
    
g_sql_tuple SQL_MakeStdTuple()
    new 
type[15]
    
get_cvar_string("amx_sql_type"typesizeof(type)-1)
    
SQL_SetAffinity(type)
    new 
cache[] = "CREATE TABLE IF NOT EXISTS `played_time` (`authid` VARCHAR(35) PRIMARY KEY, `total_time` INTEGER);"
    
SQL_ThreadQuery(g_sql_tuple"qh_create_table"cache)
}


public 
qh_create_table(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error (qh_create_table): %s"Error)
        
set_task(10.0"sql_init")
        return
    }
    
    
g_sql_ready true
    
for(new id=1id<=g_max_playersid++)
        if(
_Is(_in_serverid) && _Is(_authedid))
            
Player_Load(id)
}


public 
client_connect(id){
    
g_p_local_userid[id] = _GenerateUserId()
    
_UnSet(_authedid)
}


public 
client_authorized(id){
    if(
is_user_bot(id) || is_user_hltv(id))
        return
    
_Set(_authedid)
    if(
_Is(_in_serverid))
        
Player_Load(id)
}


public 
client_putinserver(id){
    
TotalPlayedTime[id] = 0

    _Set
(_in_serverid)
    if(
_Is(_authedid))
        
Player_Load(id)
}


public 
client_disconnect(id){
    
Player_Save(id)
    
_UnSet(_authedid)
    
_UnSet(_in_serverid)
    
_UnSet(_loadedid)
}


public 
Player_Load(id){
    if(!
g_sql_ready)
        return   
    new 
authid[36]
    
get_user_authid(idauthidsizeof(authid)-1)
    new 
cache[128]
    
formatex(cachesizeof(cache)-1"SELECT `total_time` FROM `played_time` WHERE `authid`='%s';"authid)
    new 
data[2]
    
data[0] = id
    data
[1] = g_p_local_userid[id]
    
SQL_ThreadQuery(g_sql_tuple"qh_Player_Load"cachedata2)
}


public 
qh_Player_Load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    new 
id Data[0]
    if(!
_Is(_in_serverid) || g_p_local_userid[id] != Data[1])
        return
    if(
FailState){
        
log_amx("SQL Error (qh_Player_Load): %s"Error)
        return
    }
    
_Set(_loadedid)
    if(!
SQL_MoreResults(Query))
        return
    
TotalPlayedTime[id] = SQL_ReadResult(Query0)
}
    
    
public 
Player_Save(id){
    if(!
_Is(_loadedid)) 
        return
    new 
authid[36]
    
get_user_authid(idauthidsizeof(authid)-1)
    new 
cache[128]
    
formatex(cachesizeof(cache)-1"REPLACE INTO `played_time` (`authid`,`total_time`)VALUES('%s','%d');"authidTotalPlayedTime[id]+get_user_time(id))
    
SQL_ThreadQuery(g_sql_tuple"qh_Player_Save"cache)
}


public 
qh_Player_Save(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error (qh_Player_Save): %s"Error)
        return
    }



andrzN 12-29-2012 10:48

Re: [Need help] Nvault -> MySql saving
 
Quote:

Originally Posted by Sylwester (Post 1862519)
try this (works with both sqlite and mysql, configuration in amxmodx/configs/sql.cfg):
PHP Code:

#include <amxmodx>
#include <sqlx>

#define _GenerateUserId() (_pg_last_userid = (_pg_last_userid+1)%0xffffff)
new _pg_last_userid

#define _Set(%1,%2) %1|=1<<%2
#define _UnSet(%1,%2) %1&=~(1<<%2)
#define _Is(%1,%2) (%1&1<<%2)
new _in_server_authed_loaded

new g_max_players
new g_p_local_userid[33
new 
Handle:g_sql_tuple
new g_sql_ready


public plugin_init(){
    
g_max_players get_maxplayers()
    
set_task(0.2"sql_init")
}


public 
sql_init(){
    
g_sql_tuple SQL_MakeStdTuple()
    new 
type[15]
    
get_cvar_string("amx_sql_type"typesizeof(type)-1)
    
SQL_SetAffinity(type)
    new 
cache[] = "CREATE TABLE IF NOT EXISTS `played_time` (`authid` VARCHAR(35) PRIMARY KEY, `total_time` INTEGER);"
    
SQL_ThreadQuery(g_sql_tuple"qh_create_table"cache)
}


public 
qh_create_table(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error (qh_create_table): %s"Error)
        
set_task(10.0"sql_init")
        return
    }
    
    
g_sql_ready true
    
for(new id=1id<=g_max_playersid++)
        if(
_Is(_in_serverid) && _Is(_authedid))
            
Player_Load(id)
}


public 
client_connect(id){
    
g_p_local_userid[id] = _GenerateUserId()
    
_UnSet(_authedid)
}


public 
client_authorized(id){
    if(
is_user_bot(id) || is_user_hltv(id))
        return
    
_Set(_authedid)
    if(
_Is(_in_serverid))
        
Player_Load(id)
}


public 
client_putinserver(id){
    
TotalPlayedTime[id] = 0

    _Set
(_in_serverid)
    if(
_Is(_authedid))
        
Player_Load(id)
}


public 
client_disconnect(id){
    
Player_Save(id)
    
_UnSet(_authedid)
    
_UnSet(_in_serverid)
    
_UnSet(_loadedid)
}


public 
Player_Load(id){
    if(!
g_sql_ready)
        return   
    new 
authid[36]
    
get_user_authid(idauthidsizeof(authid)-1)
    new 
cache[128]
    
formatex(cachesizeof(cache)-1"SELECT `total_time` FROM `played_time` WHERE `authid`='%s';"authid)
    new 
data[2]
    
data[0] = id
    data
[1] = g_p_local_userid[id]
    
SQL_ThreadQuery(g_sql_tuple"qh_Player_Load"cachedata2)
}


public 
qh_Player_Load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    new 
id Data[0]
    if(!
_Is(_in_serverid) || g_p_local_userid[id] != Data[1])
        return
    if(
FailState){
        
log_amx("SQL Error (qh_Player_Load): %s"Error)
        
set_task(10.0"Player_Load"id)
        return
    }
    
_Set(_loadedid)
    if(!
SQL_MoreResults(Query))
        return
    
TotalPlayedTime[id] = SQL_ReadResult(Query0)
}
    
    
public 
Player_Save(id){
    if(!
_Is(_loadedid)) 
        return
    new 
authid[36]
    
get_user_authid(idauthidsizeof(authid)-1)
    new 
cache[128]
    
formatex(cachesizeof(cache)-1"REPLACE INTO `played_time` (`authid`,`total_time`)VALUES('%s','%d');"authidTotalPlayedTime[id])
    
SQL_ThreadQuery(g_sql_tuple"qh_Player_Save"cache)
}


public 
qh_Player_Save(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error (qh_Player_Save): %s"Error)
        return
    }



It doesn't seem to be saving the data at all. Are you sure that this code should work? I get no errors or warnings when compiling, also there is no error in the logs of the server even with debug enabled on the plugin.

In the configs/sql.cfg I've written all the nessesary information, at least I think so, otherwise I should have gotten a error, right?

The plugin tracks my time perfectly, but if I reconnect or the server changes map my time is reset.

Sylwester 12-29-2012 13:42

Re: [Need help] Nvault -> MySql saving
 
I forgot to add +get_user_time(id) in save function. Code edited.

andrzN 12-29-2012 18:11

Re: [Need help] Nvault -> MySql saving
 
Quote:

Originally Posted by Sylwester (Post 1862832)
I forgot to add +get_user_time(id) in save function. Code edited.

Yeah now it works perfectly, thank you so extremely much!

Needed this because my server is too weak to handle all the vault files I have. After a point it makes the server bug and lag when it's saving/loading all the data for each and single player.

Again, thank you so much! Merry christmas and happy new year!

Sylwester 12-29-2012 18:41

Re: [Need help] Nvault -> MySql saving
 
That's because you open and close nvault every time you save or load data. Open it in plugin_init and close in plugin_end to get rid of lags.

andrzN 12-29-2012 19:32

Re: [Need help] Nvault -> MySql saving
 
Quote:

Originally Posted by Sylwester (Post 1863075)
That's because you open and close nvault every time you save or load data. Open it in plugin_init and close in plugin_end to get rid of lags.

Oh I didn't know that.

By the way, I feel bad to ask you, but can you change another plugin I have to mysql saving? This one is really small, and I've tried using the way above, but I just fail and it bugs.

Here are the saving/loading codes.

PHP Code:

public client_connectid )
{
    
load_jumpsid )
}

public 
client_disconnectid )
{
    
save_jumpsid )


PHP Code:

public save_jumpsid )
{
    new 
jump_data128 ],sSteam_Id40 ], key_format238 ]
    
    
get_user_authididsSteam_Id39 )
    
    
formatexkey_format237"%s-Jumps"sSteam_Id )
    
num_to_strvJumpid ], jump_data127 )
    
    
fvault_set_datavaultkey_formatjump_data )
}

public 
load_jumpsid )
{
    new 
jump_data128 ],sSteam_Id40 ], key_format238 ]
    
    
get_user_authididsSteam_Id39 )
    
    
formatexkey_format237"%s-Jumps",sSteam_Id )
    
    
fvault_get_datavaultkey_formatjump_data15 )
    
    
vJumpid ] = str_to_numjump_data )    




All times are GMT -4. The time now is 13:36.

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