Raised This Month: $ Target: $400
 0% 

[Need help] Nvault -> MySql saving


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



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