Hey, I'm working on a plugin that records the time played by a player, I load his saved timeplayed from a database at client_putinserver , but sometimes it returns 0, and the timeplayed resets , I think the problem may be that get_user_name on putinserver return null and when I try to load his played time from the database , it returns 0.
On client_putinserver , it loads the timeplayed, saved in a database.
PHP Code:
public client_putinserver(id){
loadPlayerTimePlayed(id);
//Sets up a tag based on the played time, doesn't modify timeplayed.
SetUserTag(id);
//Load his inventory based on his name, doesn't modify timeplayed.
Load(id);
}
loadPlayerTimePlayed(id){
new Name[32];
get_user_name(id, Name,31)
new data[1];
data[0] = id;
formatex(gszQuery, charsmax(gszQuery), "SELECT * FROM `TimePlayed` WHERE Name='%s'", Name);
SQL_ThreadQuery(gTuple, "handleLoadData", gszQuery, data, sizeof(data));
}
public handleLoadData(failstate, Handle:query, error[], errnum, data[], size){
if(failstate != TQUERY_SUCCESS){
log_amx("SQL Insert error: %s",error);
return;
}
new id = data[0];
if(!is_user_connected(id)) return;
new Name[32];
get_user_name(id, Name,31)
if(SQL_MoreResults(query)){
TimePlayed[id] = SQL_ReadResult(query, 1);
}else{
TimePlayed[id] = 0;
formatex(gszQuery, charsmax(gszQuery), "INSERT INTO `TimePlayed` VALUES ('%s', 0)", Name);
SQL_ThreadQuery(gTuple, "handleStandard", gszQuery);
}
}
And it increments the timeplayed every minute for every player
PHP Code:
public plugin_init(){
set_task(60.0,"AddTimePlayed",0,"",0,"b",0)
}
public AddTimePlayed(id){
for(new i = 0; i <= 32; i++){
if(!is_user_bot(i) && is_user_connected(i)){
TimePlayed[i]++;
}
}
}
And it saves the timeplayed of each player on round end
PHP Code:
public EventRoundEnd(){
for(new i = 0; i <= 32; i++){
if(!is_user_bot(i) && is_user_connected(i)){
UpdatePlayerTimePlayed(i);
}
}
}
And on client_disconnected
PHP Code:
public client_disconnected(id){
UpdatePlayerTimePlayed(id);
}
And the code for saving the timeplayed
PHP Code:
UpdatePlayerTimePlayed(id){
new Name[32];
get_user_name(id, Name,31)
formatex(gszQuery, charsmax(gszQuery), "UPDATE `TimePlayed` SET TimePlayed='%d' WHERE Name='%s'", TimePlayed[id], Name);
SQL_ThreadQuery(gTuple, "handleStandard", gszQuery);
}