|
Senior Member
Join Date: Jan 2010
Location: Bodø / Norway
|

12-29-2012
, 10:48
Re: [Need help] Nvault -> MySql saving
|
#4
|
Quote:
Originally Posted by Sylwester
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", type, sizeof(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=1; id<=g_max_players; id++)
if(_Is(_in_server, id) && _Is(_authed, id))
Player_Load(id)
}
public client_connect(id){
g_p_local_userid[id] = _GenerateUserId()
_UnSet(_authed, id)
}
public client_authorized(id){
if(is_user_bot(id) || is_user_hltv(id))
return
_Set(_authed, id)
if(_Is(_in_server, id))
Player_Load(id)
}
public client_putinserver(id){
TotalPlayedTime[id] = 0
_Set(_in_server, id)
if(_Is(_authed, id))
Player_Load(id)
}
public client_disconnect(id){
Player_Save(id)
_UnSet(_authed, id)
_UnSet(_in_server, id)
_UnSet(_loaded, id)
}
public Player_Load(id){
if(!g_sql_ready)
return
new authid[36]
get_user_authid(id, authid, sizeof(authid)-1)
new cache[128]
formatex(cache, sizeof(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", cache, data, 2)
}
public qh_Player_Load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
new id = Data[0]
if(!_Is(_in_server, id) || 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(_loaded, id)
if(!SQL_MoreResults(Query))
return
TotalPlayedTime[id] = SQL_ReadResult(Query, 0)
}
public Player_Save(id){
if(!_Is(_loaded, id))
return
new authid[36]
get_user_authid(id, authid, sizeof(authid)-1)
new cache[128]
formatex(cache, sizeof(cache)-1, "REPLACE INTO `played_time` (`authid`,`total_time`)VALUES('%s','%d');", authid, TotalPlayedTime[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.
|
|