Raised This Month: $32 Target: $400
 8% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
mrshark45
Member
Join Date: Jan 2016
Old 09-19-2019 , 13:37   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #11

Quote:
Originally Posted by Natsheh View Post
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?
And I pass the data to load the timeplayed so I need to pass the id so I can acces the variable TimePlayed[id], I can't pass the name.
mrshark45 is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 09-19-2019 , 16:15   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #12

Your data is being rested because player can disconnect before reaching client_putinserver which will result in updating player time with 0 time because it didn't load the user time yet...
to fix this simply check if the server loaded the player data before updating it.

something like this should do it :

PHP Code:
new bool:g_Loaded[33]

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);
    }
    
g_Loaded[id] = true;
}

public 
client_disconnected(id){
    if(
g_Loaded[id] == true) {
        
UpdatePlayerTimePlayed(id);
        
g_Loaded[id] = false;
    }

__________________

Last edited by JusTGo; 09-19-2019 at 16:15.
JusTGo is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 09-19-2019 , 16:32   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #13

Also saving/loading on nick isnt that smart..cuz you will have a lot of duplicated names on the server

EDIT: Doing one big query for updating all players will also avoid the problem which @Natsheh told you (about client_disconnect), i wrote it some months ago, i really suggest you to use that one and not your loop and update every player seperately...:

https://forums.alliedmods.net/showpo...1&postcount=22
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 09-19-2019 at 21:04.
JocAnis is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 09-20-2019 , 00:54   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #14

Also don't use SQL_MoreResult to check if the query has found something use SQL_NumResults(Handle:query)

PHP Code:
loadPlayerTimePlayed(id){
    new 
Name[32];
    
get_user_name(idName,31)

    
formatex(gszQuerycharsmax(gszQuery), "SELECT * FROM `TimePlayed` WHERE Name='%s'"Name);
    
SQL_ThreadQuery(gTuple"handleLoadData"gszQueryNamesizeof(Name));
}
public 
handleLoadData(failstateHandle:queryerror[], errnumdata[], size){
    if(
failstate != TQUERY_SUCCESS){
        
log_amx("SQL Insert error: %s",error);
        return;
    }

        new 
id get_user_index(data);
        if(!
id) return;
    if(
SQL_NumResults(query)){
        
TimePlayed[id] = SQL_ReadResult(query1);
    }else{
        
TimePlayed[id] = 0;

        
formatex(gszQuerycharsmax(gszQuery), "INSERT INTO `TimePlayed` VALUES ('%s', 0)"data);
        
SQL_ThreadQuery(gTuple"handleStandard"gszQuery);
    }

__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 09-20-2019 at 01:07.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
mrshark45
Member
Join Date: Jan 2016
Old 09-20-2019 , 17:20   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #15

Quote:
Originally Posted by JocAnis View Post
Also saving/loading on nick isnt that smart..cuz you will have a lot of duplicated names on the server

EDIT: Doing one big query for updating all players will also avoid the problem which @Natsheh told you (about client_disconnect), i wrote it some months ago, i really suggest you to use that one and not your loop and update every player seperately...:

https://forums.alliedmods.net/showpo...1&postcount=22
Ok, thanks , I understand now what he tried to tell me, I'll do it
mrshark45 is offline
mrshark45
Member
Join Date: Jan 2016
Old 09-20-2019 , 17:21   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #16

Quote:
Originally Posted by JusTGo View Post
Your data is being rested because player can disconnect before reaching client_putinserver which will result in updating player time with 0 time because it didn't load the user time yet...
to fix this simply check if the server loaded the player data before updating it.

something like this should do it :

PHP Code:
new bool:g_Loaded[33]

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);
    }
    
g_Loaded[id] = true;
}

public 
client_disconnected(id){
    if(
g_Loaded[id] == true) {
        
UpdatePlayerTimePlayed(id);
        
g_Loaded[id] = false;
    }

Thanks, I'll use a boolean to check that too, hope it'll work
mrshark45 is offline
Old 09-20-2019, 17:35
mrshark45
This message has been deleted by mrshark45.
mrshark45
Member
Join Date: Jan 2016
Old 09-20-2019 , 18:30   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #17

Ok, thanks all for the info, look what I did, now on client_connect I load the TimePlayed:
PHP Code:
public client_connect(id){
    
loadPlayerTimePlayed(id);
}


public 
loadPlayerTimePlayed(id)
{
    new 
Name[64], szTemp[512]
    
get_user_name(idName63);
    
    new 
Data[1]
    
Data[0] = id
    
    format
(szTemp,charsmax(szTemp),"SELECT * FROM `TimePlayed` WHERE (`TimePlayed`.`Nickname` = '%s')"Name);
    
SQL_ThreadQuery(gTuple,"handleLoadData",szTemp,Data,1)
}

public 
handleLoadData(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("Load - Could not connect to SQL database.  [%d] %s"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }

    new 
id
    id 
Data[0]
    
    if(
SQL_NumResults(Query) < 1
    {
        new 
Name[64], szTemp[512];

        
get_user_name(idName63);
        
        
format(szTemp,charsmax(szTemp),"INSERT INTO `TimePlayed` ( `Nickname` , `TimePlayed`) VALUES ('%s','0');",Name)
        
SQL_ThreadQuery(gTuple,"IgnoreHandle",szTemp)
    } 
    else 
    {
        
TimePlayed[id] = SQL_ReadResult(Query1)
    }
    
    return 
PLUGIN_HANDLED;

And I save the Timeplayed using a big query every minute, when the time is updating:
PHP Code:
public plugin_init(){
       
set_task(15.0,"AddTimePlayed",0,"",0,"b",0)
}

public 
AddTimePlayed(id){
    for(new 
0<= 32i++){
        if(!
is_user_bot(i) && is_user_connected(i)){
            
TimePlayed[i]++;
        }
    }
    
UpdateAllPlayers();
}


public 
UpdateAllPlayers()
{
    new  
buffer[2048], lenplayers32 ], Name[64], pnum
    get_players
playerspnum )

    
len formatex(buffer2047"UPDATE TimePlayed \
        SET TimePlayed = ( case " 
)
        
    if( 
pnum <= )
        return 
PLUGIN_HANDLED
        
    
for( new 1<= pnumi++ )
    {
        
//if( !is_user_connected( i ) ) continue 
        
get_user_name(iName63);
        if( 
TimePlayed] != )
        {
            
len += formatexbuffer[len], 2047-len"when Nickname='%s' then '%d'"NameTimePlayed[i] )
        }
    }
        
    
len += formatexbuffer[len], 2047-len" end )");

    
SQL_ThreadQuerygTuple"IgnoreHandle"buffer 
        
    
log_amx(buffer);
    return 
PLUGIN_HANDLED

I hope it works now , I'll come back with details if it actually works.
mrshark45 is offline
mrshark45
Member
Join Date: Jan 2016
Old 09-20-2019 , 18:31   Re: Loading player's name on client_putinserver returns null (maybe?)
Reply With Quote #18

Quote:
Originally Posted by mrshark45 View Post
Thanks, I'll use a boolean to check that too, hope it'll work
I'll not use the boolean , because I'm not updating the timeplayed on client_disconnected anymore.
mrshark45 is offline
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 23:02.


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