Raised This Month: $ Target: $400
 0% 

Setting state for a player via sql


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bibu
Veteran Member
Join Date: Sep 2010
Old 06-05-2011 , 14:40   Setting state for a player via sql
Reply With Quote #1

Instead of doing:

PHP Code:
if(get_pcvar_num(cvar)) 
I want todo something like this:

PHP Code:
if(user_wants(id)) 
And the player should be able to change his state with a say command, with a menu.

I don't have any idea about sql, and would really appreciate it if someone can show me a pretty good way.
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.

Last edited by bibu; 06-06-2011 at 14:49.
bibu is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-05-2011 , 17:55   Re: Setting state for a player via sql
Reply With Quote #2

Why not use a global array then do if( user_wants[id] ) then update sql when the status changes (via the menu or whatever function).
__________________
fysiks is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 06-05-2011 , 18:42   Re: Setting state for a player via sql
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
then update sql when the status changes
How I told, I don't have any idea for that.
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-05-2011 , 19:35   Re: Setting state for a player via sql
Reply With Quote #4

PHP Code:
#include < amxmodx >
#include < sqlx >

enum _:Item
{
    
Item_Specific
};

new const 
g_szItemSaveNameItem ][ ] =
{
    
"specific"
};

new 
Trie:g_tSaveNameToItem;

new 
g_bItemsItem ];

new const 
SQL_HOST[ ] = "";
new const 
SQL_USER[ ] = "";
new const 
SQL_PASS[ ] = "";
new const 
SQL_DB[ ] = "";

new 
Handle:g_hSqlTuple;

//#define SetPlayerItem(%1,%2,%3) %3 ? (%1 |= (1<<(%2&31))) : (%1 &= ~(1<<(%2&31)))
stock SetPlayerItem(itemidenabledenabled ? (g_bItems[item] |= (1<<(id&31))) : (g_bItems[item] &= ~(1<<(id&31)));
//#define GetPlayerItem(%1,%2) %1 & (1<<(%2&31))
stock GetPlayerItem(itemid) return (g_bItems[item] & (1<<(id&31)));

public 
plugin_init( )
{
    
g_tSaveNameToItem TrieCreate( );
    
    
InitSQL( );
}

InitSQL( )
{
    if( !( 
g_hSqlTuple SQL_MakeDbTupleSQL_HOSTSQL_USERSQL_PASSSQL_DB ) ) )
    {
        
set_fail_state"Could not make SQL tuple" );
    }
    
    new 
iErrorszError128 ];
    new 
Handle:hConnection SQL_Connectg_hSqlTupleiErrorszErrorcharsmaxszError ) );
    
    if( !
hConnection )
    {
        
set_fail_stateszError );
    }
    
    new 
szQuery1024 ], iQueryLen;
    for( new 
0Itemi++ )
    {
        
TrieSetCellg_tSaveNameToItemg_szItemSaveName], );
        
        
iQueryLen += formatexszQueryiQueryLen ], charsmaxszQuery ) - iQueryLen"%s%s INT NOT NULL DEFAULT 0"", " ""g_szItemSaveName] );
    }
    
    new 
Handle:hQuery SQL_PrepareQueryhConnection"CREATE TABLE IF NOT EXISTS items (steamid VARCHAR(35) NOT NULL, %s);"szQuery );
    
    if( !
SQL_ExecutehQuery ) )
    {
        
SQL_QueryErrorhQueryszErrorcharsmaxszError ) );
        
log_amx"Error creating table: %s"szError );
    }
    
    
SQL_FreeHandlehQuery );
    
    
/* Check if any new items were added to the plugin after the table was already created */
    
    
hQuery SQL_PrepareQueryhConnection"SHOW COLUMNS FROM items;" );
    
    if( !
SQL_ExecutehQuery ) )
    {
        
SQL_QueryErrorhQueryszErrorcharsmaxszError ) );
        
log_amx"Error selecting columns: %s"szError );
    }
    else
    {
        new 
Trie:tColumns TrieCreate( );
        
        new 
szColumn64 ];
        
        while( 
SQL_MoreResultshQuery ) )
        {
            
SQL_ReadResulthQuery0szColumncharsmaxszColumn ) );
            
            
TrieSetCelltColumnsszColumn);
            
            
SQL_NextRowhQuery );
        }
        
        
iQueryLen 0;
        
        new 
iNewItemIndex 0;
        
        for( new 
0Itemi++ )
        {
            if( !
TrieKeyExiststColumnsg_szItemSaveName] ) )
            {
                
iQueryLen += formatexszQueryiQueryLen ], charsmaxszQuery ) - iQueryLen"%sADD %s INT NOT NULL DEFAULT 0", ++iNewItemIndex ", " ""g_szItemSaveName] );
            }
        }
        
        if( 
iNewItemIndex )
        {
            
SQL_FreeHandlehQuery );
            
            
hQuery SQL_PrepareQueryhConnection"ALTER TABLE items %s;"szQuery );
            
            if( !
SQL_ExecutehQuery ) )
            {
                
SQL_QueryErrorhQueryszErrorcharsmaxszError ) );
                
log_amx"Error adding new columns: %s"szError );
            }
        }
    }
    
    
SQL_FreeHandlehQuery );
    
    
/* End of checking for new items */
    
    
SQL_FreeHandlehConnection );
}

public 
client_authorizediPlayer )
{
    
LoadItemsiPlayer );
}

LoadItemsiPlayer )
{
    new 
szSteamID35 ];
    
get_user_authidiPlayerszSteamIDcharsmaxszSteamID ) );
    
    new 
szQuery96 ];
    
formatexszQuerycharsmaxszQuery ), "SELECT * FROM items WHERE steamid = ^"%s^";"szSteamID );
    
    new 
iData];
    
iData] = iPlayer;
    
iData] = get_user_useridiPlayer );
    
    
SQL_ThreadQueryg_hSqlTuple"QueryLoadItems"szQueryiDatasizeofiData ) );
}

public 
QueryLoadItemsiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime )
{
    if( 
iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"%s"szError );
    }
    else
    {
        new 
iPlayer iData];
        
        if( !
is_user_connectediPlayer ) || get_user_useridiPlayer ) != iData] )
        {
            return;
        }
        
        new 
szColumn64 ], iItem;
        
        for( new 
0iNumColumns SQL_NumColumnshQuery ); iNumColumnsi++ )
        {
            
SQL_FieldNumToNamehQueryiszColumncharsmaxszColumn ) );
            
            if( 
TrieGetCellg_tSaveNameToItemszColumniItem ) )
            {
                
SetPlayerItemiItemiPlayerSQL_ReadResulthQuery) );
            }
        }
    }
}

public 
MenuHandleriPlayerhMenuiItem )
{
    if( 
/* item was picked */ )
    {
        
ChangeStateForItemiPlayerItem_Specific );
    }
}

public 
CmdHandleriPlayer )
{
    
ChangeStateForItemiPlayerItem_Specific ):
}

ChangeStateForItemiPlayeriItem )
{
    new 
bEnabled = !GetPlayerItemiItemiPlayer );
    
SetPlayerItemiItemiPlayerbEnabled );
    
    new 
szSteamID35 ];
    
get_user_authidiPlayerszSteamIDcharsmaxszSteamID ) );
    
    new 
szQuery128 ];
    
formatexszQuerycharsmaxszQuery ), "REPLACE INTO items (steamid, %s) VALUES (^"%s^", %d);"g_szItemSaveNameiItem ], szSteamIDbEnabled );
    
    
SQL_ThreadQueryg_hSqlTuple"QueryIgnoreResults"szQuery );
}

public 
QueryIgnoreResultsiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime )
{
    if( 
iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"%s"szError );
    }

__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-13-2011 at 05:15.
Exolent[jNr] is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 06-06-2011 , 01:09   Re: Setting state for a player via sql
Reply With Quote #5

And where's that part?

PHP Code:
// update SQL with bEnabled for this specific item 
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2011 , 01:35   Re: Setting state for a player via sql
Reply With Quote #6

Do you even know SQL?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 06-06-2011 , 07:02   Re: Setting state for a player via sql
Reply With Quote #7

Quote:
Originally Posted by bibu View Post
I don't have any idea about sql....
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2011 , 15:36   Re: Setting state for a player via sql
Reply With Quote #8

Then why do you want to use SQL instead of something simpler like a vault?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 06-06-2011 , 17:12   Re: Setting state for a player via sql
Reply With Quote #9

Cause a friend wants it with SQL, that's why I need that. And is it hard todo or what?
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-06-2011 , 18:02   Re: Setting state for a player via sql
Reply With Quote #10

Updated the code in my post.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 00:01.


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