Raised This Month: $51 Target: $400
 12% 

Solved Weird issue when player joins


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GasmoN
Member
Join Date: Jul 2014
Old 10-08-2021 , 10:52   Weird issue when player joins
Reply With Quote #1

Okay, when player joins my server, the function Load_MySql(id); load his data.
When that happen, players who are in the game already can't use /rank command anymore to see their rank.

PHP Code:
public Load_MySql(id)
{
    new 
szSteamId[32];
    new 
szTemp[512];
    
get_user_authid(idszSteamIdcharsmax(szSteamId));
    
    new 
Data[1];
    
Data[0] = id;
    
    
format(szTempcharsmax(szTemp), "SELECT * FROM `amx_ranks` WHERE (`amx_ranks`.`steam_id` = '%s')"szSteamId);
    
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData1);
}

public 
register_client(FailStateHandle:QueryError[], ErrcodeData[], 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 
szSteamId[32];
        new 
szName[33];
        
get_user_name(idszNamecharsmax(szName));
        
get_user_authid(idszSteamIdcharsmax(szSteamId));
        
        if (
equal(szSteamId,"ID_PENDING"))
            return 
PLUGIN_HANDLED;
            
        new 
szTemp[512];
        
format(szTemp,charsmax(szTemp),"INSERT INTO `amx_ranks` (`steam_id`, `name`) VALUES ('%s','%s');"szSteamIdszName);
        
SQL_ThreadQuery(g_SqlTuple"activate_client"szTempData);
    } 
    
    return 
PLUGIN_HANDLED;
}

public 
activate_client(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    new 
id Data[0];
    
Save_MySql(id)

    return 
PLUGIN_HANDLED;
}

public 
Save_MySql(id)
{
    if(!
is_user_connected(id))
    return;

    new 
szSteamId[32];
    new 
szTemp[512];
    
get_user_authid(idszSteamIdcharsmax(szSteamId));
    
format(szTempcharsmax(szTemp), "UPDATE `amx_ranks` SET `kills` = kills + %i, `deaths` = deaths + %i, `headshots` = headshots + %i, `score` = kills - deaths WHERE `amx_ranks`.`steam_id` = '%s';"
        
g_PlayerData[id][pKills],
        
g_PlayerData[id][pDeaths],
        
g_PlayerData[id][pHeadshots],
        
szSteamId
        
);

    
SQL_ThreadQuery(g_SqlTuple"IgnoreHandle"szTemp);
}

public 
IgnoreHandle(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    
SQL_FreeHandle(Query);
    return 
PLUGIN_HANDLED;
}

public 
cmd_rank(id)
{
    new 
szName[33];
    new 
szQuery[200];
    new 
data[1];
    
data[0] = id;
    
get_user_name(idszName32);
    
formatex(szQuerycharsmax(szQuery), "SELECT name, kills, deaths, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY kills DESC, deaths DESC ) FROM amx_ranks ) ) AS rank FROM amx_ranks WHERE name =  '%s';"szName);
    
SQL_ThreadQuery(g_SqlTuple"mysql_rank"szQuerydata);
}

public 
mysql_rank(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode);
        return 
PLUGIN_HANDLED;
    }

    if(
SQL_NumResults(Query) >= 1)
    {
        new 
iRank SQL_FieldNameToNum(Query"rank");
        new 
iKills SQL_FieldNameToNum(Query"kills");
        new 
iDeaths SQL_FieldNameToNum(Query"deaths");

        
ColorChat(Data[0], BLUE"$g%s$t Your rank: $g%d$n |$t Broj kills: $g%d$n |$t Deaths: $g%d "CHAT_PREFIXSQL_ReadResult(QueryiRank), SQL_ReadResult(QueryiKills), SQL_ReadResult(QueryiDeaths));
    }
    else
    {
        
ColorChat(Data[0], GREY"$g%s$t No players in database."CHAT_PREFIX);
        return 
PLUGIN_HANDLED;
    }

    return 
PLUGIN_HANDLED;
    


Last edited by GasmoN; 10-08-2021 at 13:54. Reason: Fixed
GasmoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-08-2021 , 10:55   Re: Weird issue when player joins
Reply With Quote #2

Hi Gasman, is your plugin hitting an error? This would cause it to stop working.
__________________
Bugsy is offline
GasmoN
Member
Join Date: Jul 2014
Old 10-08-2021 , 11:32   Re: Weird issue when player joins
Reply With Quote #3

No, nothing in logs. That's why it is so weird, I don't know what could possibly cause that.
GasmoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-08-2021 , 12:35   Re: Weird issue when player joins
Reply With Quote #4

You are not showing all code, we cannot see where Load_MySql is called.
__________________
Bugsy is offline
GasmoN
Member
Join Date: Jul 2014
Old 10-08-2021 , 12:58   Re: Weird issue when player joins
Reply With Quote #5

It's called at client_authorized(id) and the only function that sits there is Load_MySql(id)

EDIT: FIXED! Can't after 2 days of trying, can't believe how silly issue it was. Basically, the solutin is below:

PHP Code:
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData1
--->
PHP Code:
SQL_ThreadQuery(g_SqlTuple"register_client"szTempDatasizeof(Data)) 

Last edited by GasmoN; 10-08-2021 at 14:35.
GasmoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-08-2021 , 15:23   Re: Weird issue when player joins
Reply With Quote #6

new Data[ 1 ] would result in sizeof(Data) = 1

So the fact that it's now working is coincidental. Do you possibly have Data also defined globally, and the plugin is using the wrong variable?
__________________
Bugsy is offline
GasmoN
Member
Join Date: Jul 2014
Old 10-08-2021 , 16:52   Re: Weird issue when player joins
Reply With Quote #7

In some cases I had new Data[2], in the other I had new Data[1] so I think that was the issue and the reason why I had this problem.
Btw, I was too lazy to search Data size in every function so that's the reason why I put sizeof(Data) everywhere.

Last edited by GasmoN; 10-08-2021 at 16:52.
GasmoN is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 10-08-2021 , 17:48   Re: Weird issue when player joins
Reply With Quote #8

Quote:
Originally Posted by GasmoN View Post
Btw, I was too lazy to search Data size in every function so that's the reason why I put sizeof(Data) everywhere.
And how it should be done.
__________________
Napoleon_be is online now
Send a message via Skype™ to Napoleon_be
GasmoN
Member
Join Date: Jul 2014
Old 10-09-2021 , 06:10   Re: Weird issue when player joins
Reply With Quote #9

Quote:
Originally Posted by Napoleon_be View Post
And how it should be done.
Well that size number should be the same as in this case Data size.
So for example:
PHP Code:
// Data size is 2
new Data[2]

// So I put that exact size here as last argument.
SQL_ThreadQuery(g_SqlTuple"register_client"szTempData2
So I had different sizes of new Data[] in other functions, and in order not to search for all that, I just searched for SQL_ThreadQuery and changed size to sizeof(Data)
GasmoN is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 10-09-2021 , 09:50   Re: Weird issue when player joins
Reply With Quote #10

Just use sizeof() in all those functions. There's a reason why this function got introduced. There's no need for you to go look for the correct size. Whatever size u give the array, sizeof() will always get the correct size, no matter where your array got declared.
__________________
Napoleon_be is online now
Send a message via Skype™ to Napoleon_be
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 05:55.


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