AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Loading player's name on client_putinserver returns null (maybe?) (https://forums.alliedmods.net/showthread.php?t=318746)

mrshark45 09-18-2019 07:18

Loading player's name on client_putinserver returns null (maybe?)
 
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(idName,31)

    new 
data[1];
    
data[0] = id;

    
formatex(gszQuerycharsmax(gszQuery), "SELECT * FROM `TimePlayed` WHERE Name='%s'"Name);
    
SQL_ThreadQuery(gTuple"handleLoadData"gszQuerydatasizeof(data));
}
public 
handleLoadData(failstateHandle:queryerror[], errnumdata[], 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(idName,31)

    if(
SQL_MoreResults(query)){
        
TimePlayed[id] = SQL_ReadResult(query1);
    }else{
        
TimePlayed[id] = 0;

        
formatex(gszQuerycharsmax(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 
0<= 32i++){
        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 
0<= 32i++){
        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(idName,31)
    
    
formatex(gszQuerycharsmax(gszQuery), "UPDATE `TimePlayed` SET TimePlayed='%d' WHERE Name='%s'"TimePlayed[id], Name);
    
SQL_ThreadQuery(gTuple"handleStandard"gszQuery);



OciXCrom 09-18-2019 07:37

Re: Loading player's name on client_putinserver returns null (maybe?)
 
I had this problem too when using client_putinserver.
Try using client_connect instead - I haven't experienced any issues ever since I switched to it.

mrshark45 09-18-2019 09:58

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Quote:

Originally Posted by OciXCrom (Post 2667408)
I had this problem too when using client_putinserver.
Try using client_connect instead - I haven't experienced any issues ever since I switched to it.

Ok, I'll try it

JocAnis 09-18-2019 10:37

Re: Loading player's name on client_putinserver returns null (maybe?)
 
adding a delay of 0.2sec for example on putinserver can fix it, i think
also when doing Update time of the players in the round_end, isnt it better to have one Query for updating all players than 32 queries for each player?

mrshark45 09-18-2019 11:17

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Quote:

Originally Posted by JocAnis (Post 2667441)
adding a delay of 0.2sec for example on putinserver can fix it, i think
also when doing Update time of the players in the round_end, isnt it better to have one Query for updating all players than 32 queries for each player?

I tried with delay , it didn't work, also I'm using one for loop at the round end to save the time played for all the players, not 32...

JocAnis 09-18-2019 11:24

Re: Loading player's name on client_putinserver returns null (maybe?)
 
you are doing loop through all players...i tryed to tell you: do one query with the loop into that query, but whatever..

also about colum which is named Name, if i remember correctly some years ago Bugsy stated to better change its name
reason: mysql by default has something with column 'name' or something like
solution: change column name

mrshark45 09-18-2019 13:38

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Quote:

Originally Posted by JocAnis (Post 2667448)
you are doing loop through all players...i tryed to tell you: do one query with the loop into that query, but whatever..

also about colum which is named Name, if i remember correctly some years ago Bugsy stated to better change its name
reason: mysql by default has something with column 'name' or something like
solution: change column name

Ok, I'll try to change the column name, but I still don't get what you want to say about the query in round end
PHP Code:

public plugin_init(){
     
register_logevent"EventRoundEnd"2"1=Round_End" );
}


public 
EventRoundEnd(){
     for(new 
0<= 32i++){
        if(!
is_user_bot(i) && is_user_connected(i)){
            
UpdatePlayerTimePlayed(i);
        }
    }



fysiks 09-18-2019 23:38

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Any player-specific data should be based on SteamID and then you would use client_authorized().

Natsheh 09-19-2019 01:01

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Loop through using get_players array with these flags ch

Also you should check if player is connected first before checking if he's Bot in standard player loop the thing is the native might result in an error not continuing the rest of the code.

Also instead of passing the id in the data of the thread query you should pass the name directly since if you have too many queries and it needs to take an amount of time to execute and in that time the player leaves (disconnect) the data is longer saved. Unless some1 else joined in his slot.

Also seems like you are getting the value of the wrong column SQL_ReadResult(query, 1) column One is for the name right? If I'm not mistaken?

mrshark45 09-19-2019 13:34

Re: Loading player's name on client_putinserver returns null (maybe?)
 
Quote:

Originally Posted by Natsheh (Post 2667517)
Loop through using get_players array with these flags ch

Also you should check if player is connected first before checking if he's Bot in standard player loop the thing is the native might result in an error not continuing the rest of the code.

Also instead of passing the id in the data of the thread query you should pass the name directly since if you have too many queries and it needs to take an amount of time to execute and in that time the player leaves (disconnect) the data is longer saved. Unless some1 else joined in his slot.

Also seems like you are getting the value of the wrong column SQL_ReadResult(query, 1) column One is for the name right? If I'm not mistaken?

Thanks for all the info, SQL_ReadResult(query, 1) returns the Timeplayed,https://i.imgur.com/so8Rp8I.png, SQL_ReadResult(query, 0) returns the name.
I'll change the column name in nickname , because someone pointed it out that it may be a problem.


All times are GMT -4. The time now is 17:19.

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