Raised This Month: $ Target: $400
 0% 

[Need help] Nvault -> MySql saving


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
andrzN
Senior Member
Join Date: Jan 2010
Location: Bodø / Norway
Old 12-28-2012 , 16:54   [Need help] Nvault -> MySql saving
Reply With Quote #1

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!

Last edited by andrzN; 12-28-2012 at 16:55.
andrzN is offline
Send a message via MSN to andrzN
rastafari
Senior Member
Join Date: May 2011
Old 12-29-2012 , 02:17   Re: [Need help] Nvault -> MySql saving
Reply With Quote #2

This might help you: https://forums.alliedmods.net/showpo...12&postcount=4
__________________
rastafari is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-29-2012 , 03:05   Re: [Need help] Nvault -> MySql saving
Reply With Quote #3

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
    }

__________________
Impossible is Nothing

Last edited by Sylwester; 01-28-2013 at 10:10.
Sylwester is offline
andrzN
Senior Member
Join Date: Jan 2010
Location: Bodø / Norway
Old 12-29-2012 , 10:48   Re: [Need help] Nvault -> MySql saving
Reply With Quote #4

Quote:
Originally Posted by Sylwester View Post
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.
andrzN is offline
Send a message via MSN to andrzN
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-29-2012 , 13:42   Re: [Need help] Nvault -> MySql saving
Reply With Quote #5

I forgot to add +get_user_time(id) in save function. Code edited.
__________________
Impossible is Nothing
Sylwester is offline
andrzN
Senior Member
Join Date: Jan 2010
Location: Bodø / Norway
Old 12-29-2012 , 18:11   Re: [Need help] Nvault -> MySql saving
Reply With Quote #6

Quote:
Originally Posted by Sylwester View Post
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!
__________________
Owner of Gamepark.se - Scandinavian Community

#1 - HideNSeek [Easyblocks] IP: eb.gamepark.se
#2 - HideNSeek [Noblocks] IP: nb.gamepark.se
#3 - HideNSeek [HNS Training Blocks] IP: training.gamepark.se
andrzN is offline
Send a message via MSN to andrzN
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-29-2012 , 18:41   Re: [Need help] Nvault -> MySql saving
Reply With Quote #7

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.
__________________
Impossible is Nothing
Sylwester is offline
andrzN
Senior Member
Join Date: Jan 2010
Location: Bodø / Norway
Old 12-29-2012 , 19:32   Re: [Need help] Nvault -> MySql saving
Reply With Quote #8

Quote:
Originally Posted by Sylwester View Post
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 )    


Last edited by andrzN; 12-29-2012 at 19:32.
andrzN is offline
Send a message via MSN to andrzN
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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