AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   sql_threadquery - cannot set user flags (https://forums.alliedmods.net/showthread.php?t=337463)

sebxx4 04-23-2022 02:21

sql_threadquery - cannot set user flags
 
Hello everyone.
I got a problem - I want to connect to sql and check if player has acccess flags. Here's my example code:
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>

#define PLUGIN "sqlVIPloader"
#define VERSION "1.0"
#define AUTHOR "Sebxx"

new c_db_hostc_db_userc_db_passc_db_namec_serv_ip
new db_host[33], db_user[33], db_pass[33], db_name[33], serv_ip[23]
new 
Handle:g_SqlTuple

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)

    
c_db_host register_cvar("shop_sql_host""127.0.0.0")
    
c_db_user register_cvar("shop_sql_user""root")
    
c_db_pass register_cvar("shop_sql_pass""")
    
c_db_name register_cvar("shop_sql_db""shop")
    
c_serv_ip register_cvar("this_serv_ip""127.127.127.127:27015")

    new 
cfg_loc[33]
    
get_configsdircfg_loccharsmax(cfg_loc) )
    
server_cmd"exec %s/st_shop_sql.cfg"cfg_loc )
}

public 
client_authorizedid, const authid[] )
{
    if ( !
is_user_bot(id) && !is_user_hltv(id) )
    {
        
get_pcvar_stringc_db_hostdb_host32 )
        
get_pcvar_stringc_db_userdb_user32 )
        
get_pcvar_stringc_db_passdb_pass32 )
        
get_pcvar_stringc_db_namedb_name32 )
        
get_pcvar_stringc_serv_ipserv_ip22 )

        
g_SqlTuple SQL_MakeDbTupledb_hostdb_userdb_passdb_name )

        new 
query[512], user_ip[32], name[32], steamid[35], pw_info[32], pw_hash[34]

        
get_user_ipiduser_ipcharsmax(user_ip), )
        
get_user_nameidnamecharsmax(name) )
        
get_user_authididsteamidcharsmax(steamid) )
        
get_user_infoid"_pw"pw_info31 )
        
hash_stringpw_infoHash_Md5pw_hashcharsmax(pw_hash) )

        
formatex(query511"SELECT sc.access \
                                FROM shop_services AS sc, shop_servers AS sv \
                                WHERE sv.serv_ip = '%s' \
                                AND sc.server_id = sv.serv_id \
                                AND ( ( sc.auth_id = '%s' AND sc.auth = 1 ) OR ( sc.auth_id = '%s' AND sc.password = '%s' AND sc.auth = 2 ) OR ( sc.auth_id = '%s' AND sc.password = '%s' AND sc.auth = 3 ) ) \
                                AND sc.expire > %d"
serv_ipsteamidnamepw_hashuser_ippw_hashget_systime() )

        new 
Data[1]
        
Data[0] = id

        SQL_ThreadQuery
(g_SqlTuple"do_query"queryData1)
    }
}

public 
do_queryFailStateHandle:QueryError[], ErrcodeData[], DataSize )
{
    if ( 
FailState == TQUERY_CONNECT_FAILED )
    {
        
log_amx("Could not connect to SQL database.")

        if ( 
Errcode )
        {
            
log_amx("Error: %s"Error)
        }

        return 
PLUGIN_CONTINUE
    
}
    else if ( 
FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx("Query failed.")

        if ( 
Errcode )
        {
            
log_amx("Error: %s"Error)
        }

        return 
PLUGIN_CONTINUE
    
}

    new 
id Data[0]

    if ( 
SQL_NumResults(Query) )
    {
        new 
flags[32]
        
        while( 
SQL_MoreResultsQuery ) )
        {
            new 
qcolAccess SQL_FieldNameToNumQuery"access" )
            
SQL_ReadResultQueryqcolAccessflags31 )
            
//log_amx( "Flag: %s", flags )
            
set_user_flagsidread_flagsflags ) )
            
SQL_NextRow(Query)
        }
    }

    
SQL_FreeHandle(g_SqlTuple)
    return 
PLUGIN_CONTINUE


The problem is, set_user_flags don't set it. SQL query works well (I see in the console flags get from sql server) but player don't have permissions. What's wrong?

Bugsy 04-23-2022 12:15

Re: sql_threadquery - cannot set user flags
 
If you've confirmed all the plumbing is correct and you see the log_amx() line print, maybe try adding a small delay when setting flags.
Code:
            //set_user_flags( id, read_flags( flags ) )             set_task( 0.2 , "DelayFlags" , id , flags , sizeof( flags ) );                         SQL_NextRow(Query)         }     }         SQL_FreeHandle(g_SqlTuple)         return PLUGIN_CONTINUE } public DelayFlags( const szFlags[] , id ) {     set_user_flags( id , read_flags( szFlags ) ); }

sebxx4 04-24-2022 03:06

Re: sql_threadquery - cannot set user flags
 
Now, I got it like this:

PHP Code:

    new id Data[0]
    new 
flags[32], output[1024]

    if ( 
SQL_NumResults(Query) )
    {
        while( 
SQL_MoreResultsQuery ) )
        {
            new 
qcolAccess SQL_FieldNameToNumQuery"access" )
            
SQL_ReadResultQueryqcolAccessflags31 )
            
addoutputsizeof(output), flagssizeof(flags) )
            
SQL_NextRow(Query)
        }
    }

    
SQL_FreeHandle(g_SqlTuple)

    
set_task0.2"set_flags"idoutputsizeof(output) );

    return 
PLUGIN_CONTINUE
}

public 
set_flags( const flags[], id  )
{
    
set_user_flagsidread_flagsflags ) )
    
log_amx"Set flags: %s to player: #%d"flagsid )


But it still don't set access flags :/ I have logged in console "Set flags: t to player: #1" so it should work...

sebxx4 04-24-2022 05:50

Re: sql_threadquery - cannot set user flags
 
EDIT:
I found there is something wrong with one of my servers. When testing on another one, it works well. And if I use SQL_PrepareQuery instead of SQL_ThreadQuery it works well on all servers too.
I think the problem is in set_user_flags function bacause even if set constant values ( eg: set_user_flags(1, read_flags("t")) ) it doesn't work. What you think about it?

Bugsy 04-24-2022 15:50

Re: sql_threadquery - cannot set user flags
 
Remember that set_user_flags() is actually add user flags. If you want to set what a player has, first use remove_user_flags() then set_user_flags().

For example, if a player has "abcdef" and you call set_user_flags( id , "t" ), his flags will then be "abcdeft"

Is this your issue?

Do you have any other plugins that may be adjusting flags?

sebxx4 04-25-2022 00:55

Re: sql_threadquery - cannot set user flags
 
I know it only adds flags. That's I want it to do.
I think I found the problem, but still don't know how to deal with it. The problem is, most of plugins checks user flags in client_authorized() function. But my plugin do only sql query in client_authorized(), but no set_user_flags() in this function too. And bacause of it, rest of plugins don't see user flags. They're updated in sql function after client_authorized.

Bugsy 04-26-2022 22:21

Re: sql_threadquery - cannot set user flags
 
Ok, you may need to edit those plugins to delay the flag checking.

sebxx4 04-27-2022 00:45

Re: sql_threadquery - cannot set user flags
 
Well, I'd rather avoid editing every plugin I use because of this one.

Bugsy 04-27-2022 07:50

Re: sql_threadquery - cannot set user flags
 
You've really got no choice. The earliest that you can identify a player with their authid is client authorized. Another option is to load flags with something more instantaneous, like nVault, and place this plugin before all others in plugins.ini

CrazY. 04-27-2022 08:34

Re: sql_threadquery - cannot set user flags
 
Rather than putting a delay in every plugin, create a forward and trigger it once the user flags are loaded, you will still need to edit the plugins though, there is no other way around it.


All times are GMT -4. The time now is 08:38.

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