AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Return Boolean through multiple Functions (https://forums.alliedmods.net/showthread.php?t=241650)

Kia 06-07-2014 05:13

Return Boolean through multiple Functions
 
Hello everybody,

I have a problem which I can not solve.
When you type a command in the chat which opens a menu, I want to make a SQL Check for all Items I have in an Array.
Now, my problem is, I do not know how to return a value through multiple functions, since I only want to show items the player owns.
My current structure looks like this. The Problem is that the SQL is not fast enough so sometimes you can buy a weapon again even when you own it already.

Menu :
PHP Code:

for(new 0g_iTotalItemsi++)
{
        
ArrayGetArray(g_aItemsieItemData)
    
        
HasUserCreditWeapon(ideItemData[ItemConst])
        
        
formatex(szItemcharsmax(szItem), "%s - \y%i %s"eItemData[ItemName], eItemData[ItemCost], CURRENCY_TAG)
        
        
num_to_str(iszNumcharsmax(szNum))
        
        if(
g_iCredits[id] >= eItemData[ItemCost] && !g_iTempTrue[id])
            
menu_additem(menuszItemszNum0)
        else
            
menu_additem(menuszItemszNum1<<31)
            
        
g_iTempTrue[id] = false


PHP Code:

public HasUserCreditWeapon(id, const szConst[])
{
    new 
szSteamId[32], szTemp[512]
    
get_user_authid(idszSteamIdcharsmax(szSteamId))
    
    new 
Data[1]
    
Data[0] = id
    
    formatex
(szTemp,charsmax(szTemp),"SELECT * FROM `%s` WHERE (`%s`.`steamid` = '%s') AND (`%s`.`weap` = '%s');"Table_WeapDataTable_WeapDataszSteamIdTable_WeapDataszConst)
    
console_print(idszConst)
    
SQL_ThreadQuery(g_SqlTuple,"CheckCount",szTemp,Data,1)
}

public 
CheckCount(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)
        
g_iTempTrue[id] = true
        
else
        
g_iTempTrue[id] = false
    
    
return PLUGIN_HANDLED


So, my question is, how do return a value through HasUserCreditWeapon and CheckCount back to the menu so it waits long enough that I get the actual state. Or is there any other way to do it?

Greetz,
Kia.

Xalus 06-07-2014 05:30

Re: Return Boolean through multiple Functions
 
ThreadQuery is way to slow for such things, try out

PHP Code:

public HasUserCreditWeapon(id, const szConst[]) 

    new 
szSteamId[32]
    
get_user_authid(idszSteamIdcharsmax(szSteamId)) 
    
    new 
Handle:Query SQL_PrepareQuery(MySQL_Connection"SELECT * FROM `%s` WHERE (`%s`.`steamid` = '%s') AND (`%s`.`weap` = '%s');"Table_WeapDataTable_WeapDataszSteamIdTable_WeapDataszConst
    
SQL_Execute(Query)
    
    if(
SQL_NumResults(Query) >= 1
        
g_iTempTrue[id] = true 
    
else 
        
g_iTempTrue[id] = false 



Kia 06-07-2014 05:34

Re: Return Boolean through multiple Functions
 
Thanks, I will try that later.

Backstabnoob 06-07-2014 06:33

Re: Return Boolean through multiple Functions
 
Why don't you just cache it in memory? Send the query only once when the player connects. The fastest query is no query at all.
I'm almost done with my MySQL data layer, which I'll probably also release as a standalone code snippet/plugin. You might want to check that out when it's done.

Kia 06-07-2014 07:04

Re: Return Boolean through multiple Functions
 
Quote:

Originally Posted by Backstabnoob (Post 2147909)
Why don't you just cache it in memory? Send the query only once when the player connects. The fastest query is no query at all.
I'm almost done with my MySQL data layer, which I'll probably also release as a standalone code snippet/plugin. You might want to check that out when it's done.

Inventory can change at run-time, so caching does not help here.

Backstabnoob 06-07-2014 07:07

Re: Return Boolean through multiple Functions
 
1 Attachment(s)
Well it does, you just change it in the memory and then save everything when the player disconnects. My data layer does this.

I'm attaching my current version which you can try out if you want. Just hook up a database to the SQL_MakeDbTuple native, go to the server, write addclass to the console a couple of times to add random data, then getclass which will loop through all of the currently cached data and print it to your console and saveclass to save it into the database. That's the functionality the data layer will have, so I really recommend you to wait it out, it's DESIGNED for stuff like player inventories.

It's not made for actual use, but it won't take me more than a few days to completely finish it and release it.


All times are GMT -4. The time now is 09:44.

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