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

sql_threadquery - cannot set user flags


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sebxx4
Senior Member
Join Date: Feb 2013
Old 04-23-2022 , 02:21   sql_threadquery - cannot set user flags
Reply With Quote #1

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?

Last edited by sebxx4; 04-23-2022 at 02:25.
sebxx4 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-23-2022 , 12:15   Re: sql_threadquery - cannot set user flags
Reply With Quote #2

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 ) ); }
__________________
Bugsy is offline
sebxx4
Senior Member
Join Date: Feb 2013
Old 04-24-2022 , 03:06   Re: sql_threadquery - cannot set user flags
Reply With Quote #3

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...

Last edited by sebxx4; 04-24-2022 at 03:07.
sebxx4 is offline
sebxx4
Senior Member
Join Date: Feb 2013
Old 04-24-2022 , 05:50   Re: sql_threadquery - cannot set user flags
Reply With Quote #4

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?

Last edited by sebxx4; 04-24-2022 at 06:02.
sebxx4 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-24-2022 , 15:50   Re: sql_threadquery - cannot set user flags
Reply With Quote #5

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?
__________________

Last edited by Bugsy; 04-24-2022 at 15:50.
Bugsy is offline
sebxx4
Senior Member
Join Date: Feb 2013
Old 04-25-2022 , 00:55   Re: sql_threadquery - cannot set user flags
Reply With Quote #6

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.

Last edited by sebxx4; 04-25-2022 at 02:11.
sebxx4 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-26-2022 , 22:21   Re: sql_threadquery - cannot set user flags
Reply With Quote #7

Ok, you may need to edit those plugins to delay the flag checking.
__________________

Last edited by Bugsy; 04-26-2022 at 22:31.
Bugsy is offline
sebxx4
Senior Member
Join Date: Feb 2013
Old 04-27-2022 , 00:45   Re: sql_threadquery - cannot set user flags
Reply With Quote #8

Well, I'd rather avoid editing every plugin I use because of this one.
sebxx4 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-27-2022 , 07:50   Re: sql_threadquery - cannot set user flags
Reply With Quote #9

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
__________________
Bugsy is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-27-2022 , 08:34   Re: sql_threadquery - cannot set user flags
Reply With Quote #10

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.
__________________








CrazY. 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 06:31.


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