AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Weird issue when player joins (https://forums.alliedmods.net/showthread.php?t=334612)

GasmoN 10-08-2021 10:52

Weird issue when player joins
 
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;
    



Bugsy 10-08-2021 10:55

Re: Weird issue when player joins
 
Hi Gasman, is your plugin hitting an error? This would cause it to stop working.

GasmoN 10-08-2021 11:32

Re: Weird issue when player joins
 
No, nothing in logs. That's why it is so weird, I don't know what could possibly cause that.

Bugsy 10-08-2021 12:35

Re: Weird issue when player joins
 
You are not showing all code, we cannot see where Load_MySql is called.

GasmoN 10-08-2021 12:58

Re: Weird issue when player joins
 
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)) 


Bugsy 10-08-2021 15:23

Re: Weird issue when player joins
 
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?

GasmoN 10-08-2021 16:52

Re: Weird issue when player joins
 
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.

Napoleon_be 10-08-2021 17:48

Re: Weird issue when player joins
 
Quote:

Originally Posted by GasmoN (Post 2759999)
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.

GasmoN 10-09-2021 06:10

Re: Weird issue when player joins
 
Quote:

Originally Posted by Napoleon_be (Post 2760003)
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)

Napoleon_be 10-09-2021 09:50

Re: Weird issue when player joins
 
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.


All times are GMT -4. The time now is 20:25.

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