Raised This Month: $ Target: $400
 0% 

Return Boolean through multiple Functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-07-2014 , 05:13   Return Boolean through multiple Functions
Reply With Quote #1

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.
__________________
Kia is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 06-07-2014 , 05:30   Re: Return Boolean through multiple Functions
Reply With Quote #2

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 

__________________
Retired.

Last edited by Xalus; 06-07-2014 at 05:31.
Xalus is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-07-2014 , 05:34   Re: Return Boolean through multiple Functions
Reply With Quote #3

Thanks, I will try that later.
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-07-2014 , 06:33   Re: Return Boolean through multiple Functions
Reply With Quote #4

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.

Last edited by Backstabnoob; 06-07-2014 at 06:34.
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-07-2014 , 07:04   Re: Return Boolean through multiple Functions
Reply With Quote #5

Quote:
Originally Posted by Backstabnoob View Post
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.
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-07-2014 , 07:07   Re: Return Boolean through multiple Functions
Reply With Quote #6

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.
Attached Files
File Type: sma Get Plugin or Get Source (ClassPrototype.sma - 575 views - 9.2 KB)

Last edited by Backstabnoob; 06-07-2014 at 07:18.
Backstabnoob 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 09:44.


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