Raised This Month: $51 Target: $400
 12% 

Saving Xp data.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 01-17-2010 , 17:11   Saving Xp data.
Reply With Quote #1

Bugzy wrote this for me and i was trying to use this as well as Gun Xp Mod to write a Sql and Nvault savedate function..

PHP Code:
public SaveDataid )
{
    new 
iPos szSave[ ( XPDATA 10 ) + ];
    
    for ( new 
XPDATA i++ )
        
iPos += formatexszSaveiPos ] ,charsmaxszSave ) , "%10d" g_XPid ][ ] );
    
    
nvault_setg_iVault g_szAuthIDid ] , szSave );
}

public 
LoadDataid )
{
    new 
iPos szNum11 ] , szLoad[ ( XPDATA 10 ) + ];
    
    
nvault_getg_iVault g_szAuthIDid ] , szLoad  charsmaxszLoad ) );
    
    for ( new 
XPDATA i++ )
    {    
        
iPos += copyszNum 10 szLoadiPos ] );
        
g_XPid ][ ] = str_to_numszNum ); 
    }

This is what i came up with.. Will most likly be completely wrong lol but im hopeing with your guys help i can figure this out.

Like i have mentioned before i am very new to the whole "Xp mod" type of coding.

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

new Handle:g_hTuple;

new 
g_szAuthID[33][35];

new 
g_szAuthIP[33][35];

new 
mysqlx_hostmysqlx_usermysqlx_dbmysqlx_pass;

new 
g_iVault;

new const 
szTables[][] = 
{
    
"CREATE TABLE IF NOT EXISTS `mytable` ( `player_id` varchar(32) NOT NULL, `player_xp` int(16) default NULL,PRIMARY KEY (`player_id`) ) TYPE=MyISAM;"
}


public 
plugin_init() {
    
register_plugin("Xp Save Sample""1.0""Who knows")
    
    
// SQLx cvars
    
mysqlx_host register_cvar ("bf_host"""); // The host from the db
    
mysqlx_user register_cvar ("bf_user"""); // The username from the db login
    
mysqlx_pass register_cvar ("bf_pass"""); // The password from the db login
    
mysqlx_db register_cvar ("bf_dbname"""); // The database name 
    
    
save_type register_cvar("gxm_savetype","1"); // Save Xp to : 0 = MySQL, 1 = NVault.
    
savexp register_cvar("gxm_save","0");  //Turns On and off xp saving
    
    //Nvault
    
g_iVault nvault_open"Battlefield-XP-Data" );
}



// ============================================================//
//                          [~ Saving datas ~]               //
// ============================================================//
public MySQLx_Init()
{
    if ( !
get_pcvar_num(p_Enabled) || !get_pcvar_num(save_type) )
        return;
    
    new 
szHost[64], szUser[32], szPass[32], szDB[128];
    
    
get_pcvar_stringmysqlx_hostszHostcharsmaxszHost ) );
    
get_pcvar_stringmysqlx_userszUsercharsmaxszUser ) );
    
get_pcvar_stringmysqlx_passszPasscharsmaxszPass ) );
    
get_pcvar_stringmysqlx_dbszDBcharsmaxszDB ) );
    
    
g_hTuple SQL_MakeDbTupleszHostszUserszPassszDB );
    
    for ( new 
0sizeof szTablesi++ )
    {
        
SQL_ThreadQueryg_hTuple"QueryCreateTable"szTables[i])
    }
}
public 
QueryCreateTableiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime 

    if( 
iFailState == TQUERY_CONNECT_FAILED 
    
|| iFailState == TQUERY_QUERY_FAILED 
    { 
        
log_amx"%s"szError ); 
        
        return;
    } 
}

SaveLevel(id)

    if ( 
get_pcvar_num(savexp) )
    {
        if ( 
get_pcvar_num(save_type) )
        {
            new 
iPos szSave[ ( XPDATA 10 ) + ];
        
            for ( new 
XPDATA i++ )
            
iPos += formatexszSaveiPos ] ,charsmaxszSave ) , "%10d" g_XPid ][ ] );
        
            
nvault_setg_iVault g_szAuthIDid ] , szSave );
        }
        
        else
        {    
            static 
szQuery128 ];
            
            for(new 
i=0XPDATA i++)
            {
                
formatexszQuery127"REPLACE INTO `mytable` (`player_id`, `player_xp`) VALUES ('%s', '%d');"g_szAuthID[id] , g_XP[id][i] );
        
                
SQL_ThreadQueryg_hTuple"QuerySetData"szQuery);
            }
        }
    }
}

LoadLevel(id)
{
    if ( 
get_pcvar_num(savexp) )
    {
        if (  
get_pcvar_num(save_type) )
        {
            new 
iPos szNum11 ] , szLoad[ ( XPDATA 10 ) + ];
    
            
nvault_getg_iVault g_szAuthIDid ] , szLoad  charsmaxszLoad ) );
    
            for ( new 
XPDATA i++ )
            {    
                
iPos += copyszNum 10 szLoadiPos ] );
                
g_XPid ][ ] = str_to_numszNum ); 
            } 
        }
        
        else
        {
            for(new 
i=0XPDATAi++)
            {
                static 
szQuery128 ], iData]; 
                
formatexszQuery127"SELECT `player_xp` FROM `mytable` WHERE ( `player_id` = '%s' );"g_szAuthID[id] ); 
                
iData] = id;
                
SQL_ThreadQueryg_hTuple"QuerySelectData"szQueryiData);
            }
        }
    }
}


public 
QuerySelectDataiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime 

    if( 
iFailState == TQUERY_CONNECT_FAILED 
    
|| iFailState == TQUERY_QUERY_FAILED 
    { 
        
log_amx"%s"szError );
        
        return;
    } 
    else 
    { 
        new 
id iData];
        
        new 
ColXp SQL_FieldNameToNum(hQuery"player_xp")
        
        while (
SQL_MoreResults(hQuery)) 
        {
            for(new 
i=0; <XPDATAi++)
            {
                
g_XPid ][ ] = SQL_ReadResult(hQueryColXp);
            }
                    
            
SQL_NextRow(hQuery)
        }
    } 
}
public 
QuerySetDataiFailStateHandle:hQueryszError[ ], iErroriData[ ], iDataSizeFloat:fQueueTime 

    if( 
iFailState == TQUERY_CONNECT_FAILED 
    
|| iFailState == TQUERY_QUERY_FAILED 
    { 
        
log_amx"%s"szError ); 
        
        return;
    } 

Doc-Holiday is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-17-2010 , 17:20   Re: Saving Xp data.
Reply With Quote #2

thought you were using nvault... I can write for sql instead
__________________
Bugsy is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 01-17-2010 , 17:21   Re: Saving Xp data.
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
thought you were using nvault... I can write for sql instead

Id like to have an option since some like sql over nvault.. if possible
Doc-Holiday is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-17-2010 , 17:37   Re: Saving Xp data.
Reply With Quote #4

Do you mind using an include file I wrote for SQLx? It is basically just a wrapper to simplify SQL transactions. The functions closely resemble that of nvault and require no knowledge of SQL.
__________________
Bugsy is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 01-17-2010 , 17:45   Re: Saving Xp data.
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
Do you mind using an include file I wrote for SQLx? It is basically just a wrapper to simplify SQL transactions. The functions closely resemble that of nvault and require no knowledge of SQL.
Thats fine. lol if it wasnt for you id be giving up on xp lol so yeah and again thank you so much
Doc-Holiday is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-17-2010 , 17:49   Re: Saving Xp data.
Reply With Quote #6

The nSQLx include is not complete yet but will work fine for your plugin.

PHP Code:
#define USE_SQL    1

#include <amxmodx> 

#if defined USE_SQL
#include <nsqlx>
#else
#include <nvault>
#endif

enum _:CLASSES 

    
NoClass,
    
Sniper
    
Sport
    
Ace 
}

enum _:RANKS 

    
NoRank,
    
Rookie
    
Chief
    
Captain 
}

enum _:XPDATA 

    
XPTotal,    //Total XP points player has 
    
XPCLASSES ],    //XP points player has in each class 
    
Class,        //Players current class 
    
Rank        //Players current rank 
}

new const 
g_ClassDescCLASSES ][] = 

    
"None",
    
"Sniper"
    
"Sport"
    
"Ace" 
};

new const 
g_RankDescRANKS ][] = 
{
    
"None",
    
"Rookie"
    
"Chief"
    
"Captain" 
};

//XP point-ranges for each rank. A players rank is defined by XP points 
//he has for each class. 
new const g_RankXPRANKS ][ ] = 

    { 
},    //None - 0 xp points
    
50 },    //Rookie - 1-50 xp points 
    
51 100 },    //Chief - 51-100 xp points 
    
101 150 }    //Captain - 101-150 xp points 
}; 

//Array to store all player xp info 
new g_XP33 ][ XPDATA ]; 

new 
g_szAuthID33 ][ 35 ];

#if defined USE_SQL
new g_iSQL;
#else
new g_iVault;
#endif

public plugin_init() 

    
register_plugin"XP Skeleton" "0.1" "bugsy" ); 
    
    
register_clcmd"say /class" "cmd_SayClass" ); 
    
register_clcmd"say /rank" "cmd_SayRank" ); 
    
register_clcmd"say /current" "cmd_SayCurrent" ); 
    
    
#if defined USE_SQL
    
g_iSQL nsqlx_open"player_xp_data" "SQL_Handler" );
    
#else
    
g_iVault nvault_open"player_xp_data" );
    
#endif


#if !defined USE_SQL
public plugin_end()
{
    
nvault_closeg_iVault );
}
#endif

public client_authorizedid )
{
    
get_user_authidid g_szAuthIDid ] , charsmaxg_szAuthID[] ) );
    
LoadDataid );
}

public 
client_disconnectid )
{
    
SaveDataid );
    
    
//Clear all XP data from array
    
for ( new XPDATA i++ )
        
g_XPid ][ ] = 0;
}


public 
cmd_SayClassid 

    new 
iMenu menu_create"Select New Class" "HandleClassMenu" );
    new 
szNum];
    
    for ( new 
sizeofg_ClassDesc ) ; i++ )
    {
        
num_to_strszNum charsmaxszNum ) );
        
menu_additemiMenu g_ClassDesc] , szNum );
    }
    
    
menu_setpropiMenu MPROP_EXIT MEXIT_NEVER ); 
    
menu_displayid iMenu );


//Just for testing. After setting a class (/class), use /rank command (will assign rank = Chief)
//Now switch to a new class (should show rank Rookie). Now switch back to the first class you chose. 
//You should see your rank is back to Chief.
public cmd_SayRankid )
{
    
g_XPid ][ Class ] = Ace;
    
g_XPid ][ Rank ] = Captain;
    
g_XPid ][ XP ][ g_XPid ][ Class ] ] = 130;
}

public 
cmd_SayCurrentid )
{
    
client_printid print_chat "Class: %s | Rank %s -- XP %d / %d" ,     g_ClassDescg_XPid ][ Class ] ] , 
                                        
g_RankDescg_XPid ][ Rank ] ] ,
                                        
g_XPid ][ XP ][ g_XPid ][ Class ] ] ,
                                        ( 
g_XPid ][ Rank ] + )== RANKS ?  g_RankXPg_XPid ][ Rank ] ][ ] : g_RankXPg_XPid ][ Rank ] + ][ ] );
}

public 
HandleClassMenuid iMenu iItem 
{
    new 
szKey] , iSelectedClass Dummy;
    
menu_item_getinfoiMenu iItem Dummy szKey "" Dummy );
    
    
iSelectedClass str_to_numszKey );
    
    if ( 
g_XPid ][ Class ] == iSelectedClass )
    {
        
client_printid print_chat "* You are already a %s!" g_ClassDesciSelectedClass ] );
        return 
PLUGIN_HANDLED;
    }
    
    
ChangeClassid iSelectedClass );
    
    return 
PLUGIN_HANDLED;
}  

//Change a players class and update his rank based on XP he has for the new class. 
public ChangeClassid NewClass 
{
    
//Assign player his new class, set rank to 0 (first rank) 
    
g_XPid ][ Class ] = NewClass
    
g_XPid ][ Rank ] = 0
    
    
//Loop through array g_RankXP and determine which rank player should be placed 
    //in depending on his current XP points he has for the new class. 
    
for ( new sizeofg_RankXP ) ; i++ ) 
    { 
        
//If players XP points for class falls within range for this rank, update players rank 
        
if ( g_RankXP][ ] <= g_XPid ][ XP ][ NewClass ] <= g_RankXP][ ] ) 
        { 
            
g_XPid ][ Rank ] = i
            break;  
        } 
    } 
    
    
client_printid print_chat "* New class: %s - New Rank: %s" g_ClassDescNewClass ] , g_RankDescg_XPid ][ Rank ] ] ); 
    
    return 
PLUGIN_HANDLED;  
}

public 
SaveDataid )
{
    new 
iPos szSave[ ( XPDATA 10 ) + ];
    
    for ( new 
XPDATA i++ )
        
iPos += formatexszSaveiPos ] ,charsmaxszSave ) , "%10d" g_XPid ][ ] );
    
    
#if defined USE_SQL
    
nsqlx_setg_iSQL id g_szAuthIDid ] , szSave );
    
#else
    
nvault_setg_iVault g_szAuthIDid ] , szSave );
    
#endif
}


public 
LoadDataid )
{

    
#if defined USE_SQL
    
nsqlx_getg_iSQL id g_szAuthIDid ] );
    
#else
    
new iPos szNum11 ] , szLoad[ ( XPDATA 10 ) + ];
    
    
nvault_getg_iVault g_szAuthIDid ] , szLoad  charsmaxszLoad ) );
    
    for ( new 
XPDATA i++ )
    {    
        
iPos += copyszNum 10 szLoadiPos ] );
        
g_XPid ][ ] = str_to_numszNum ); 
    }
    
#endif
}

#if defined USE_SQL
public SQL_HandlerszData[] , iDataLen iRequestID iRequestType SQLHandle szError[] , iError )
{
    if ( ( 
iRequestType == QUERY_GET ) && iDataLen )
    {    
        new 
iPos szNum11 ];
        
        for ( new 
XPDATA i++ )
        {
            
iPos += copyszNum 10 szDataiPos ] );
            
g_XPiRequestID ][ ] = str_to_numszNum );     
        }
    }
}
#endif 
Attached Files
File Type: inc nsqlx.inc (5.3 KB, 255 views)
__________________

Last edited by Bugsy; 01-17-2010 at 17:57.
Bugsy is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 01-17-2010 , 18:13   Re: Saving Xp data.
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
The nSQLx include is not complete yet but will work fine for your plugin.

Thanks a bunch going to test.

Hmm some reason i cant connect to Sql db...

i registered the 3 cvars and switched
PHP Code:
g_iSQL nsqlx_open"Db name" "SQL_Handler" ); 
to the database name

Last edited by Doc-Holiday; 01-17-2010 at 19:30.
Doc-Holiday is offline
Jacob
Senior Member
Join Date: Oct 2010
Old 11-27-2010 , 05:10   Re: Saving Xp data.
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
The nSQLx include is not complete yet but will work fine for your plugin.

PHP Code:
#define USE_SQL    1
 
#include <amxmodx> 
 
#if defined USE_SQL
#include <nsqlx>
#else
#include <nvault>
#endif
 
enum _:CLASSES 

    
NoClass,
    
Sniper
    
Sport
    
Ace 
}
 
enum _:RANKS 

    
NoRank,
    
Rookie
    
Chief
    
Captain 
}
 
enum _:XPDATA 

    
XPTotal,    //Total XP points player has 
    
XPCLASSES ],    //XP points player has in each class 
    
Class,        //Players current class 
    
Rank        //Players current rank 
}
 
new const 
g_ClassDescCLASSES ][] = 

    
"None",
    
"Sniper"
    
"Sport"
    
"Ace" 
};
 
new const 
g_RankDescRANKS ][] = 
{
    
"None",
    
"Rookie"
    
"Chief"
    
"Captain" 
};
 
//XP point-ranges for each rank. A players rank is defined by XP points 
//he has for each class. 
new const g_RankXPRANKS ][ ] = 

    { 
},    //None - 0 xp points
    
50 },    //Rookie - 1-50 xp points 
    
51 100 },    //Chief - 51-100 xp points 
    
101 150 }    //Captain - 101-150 xp points 
}; 
 
//Array to store all player xp info 
new g_XP33 ][ XPDATA ]; 
 
new 
g_szAuthID33 ][ 35 ];
 
#if defined USE_SQL
new g_iSQL;
#else
new g_iVault;
#endif
 
public plugin_init() 

    
register_plugin"XP Skeleton" "0.1" "bugsy" ); 
 
    
register_clcmd"say /class" "cmd_SayClass" ); 
    
register_clcmd"say /rank" "cmd_SayRank" ); 
    
register_clcmd"say /current" "cmd_SayCurrent" ); 
 
    
#if defined USE_SQL
    
g_iSQL nsqlx_open"player_xp_data" "SQL_Handler" );
    
#else
    
g_iVault nvault_open"player_xp_data" );
    
#endif

 
#if !defined USE_SQL
public plugin_end()
{
    
nvault_closeg_iVault );
}
#endif
 
public client_authorizedid )
{
    
get_user_authidid g_szAuthIDid ] , charsmaxg_szAuthID[] ) );
    
LoadDataid );
}
 
public 
client_disconnectid )
{
    
SaveDataid );
 
    
//Clear all XP data from array
    
for ( new XPDATA i++ )
        
g_XPid ][ ] = 0;
}
 
 
public 
cmd_SayClassid 

    new 
iMenu menu_create"Select New Class" "HandleClassMenu" );
    new 
szNum];
 
    for ( new 
sizeofg_ClassDesc ) ; i++ )
    {
        
num_to_strszNum charsmaxszNum ) );
        
menu_additemiMenu g_ClassDesc] , szNum );
    }
 
    
menu_setpropiMenu MPROP_EXIT MEXIT_NEVER ); 
    
menu_displayid iMenu );

 
//Just for testing. After setting a class (/class), use /rank command (will assign rank = Chief)
//Now switch to a new class (should show rank Rookie). Now switch back to the first class you chose. 
//You should see your rank is back to Chief.
public cmd_SayRankid )
{
    
g_XPid ][ Class ] = Ace;
    
g_XPid ][ Rank ] = Captain;
    
g_XPid ][ XP ][ g_XPid ][ Class ] ] = 130;
}
 
public 
cmd_SayCurrentid )
{
    
client_printid print_chat "Class: %s | Rank %s -- XP %d / %d" ,     g_ClassDescg_XPid ][ Class ] ] , 
                                        
g_RankDescg_XPid ][ Rank ] ] ,
                                        
g_XPid ][ XP ][ g_XPid ][ Class ] ] ,
                                        ( 
g_XPid ][ Rank ] + )== RANKS ?  g_RankXPg_XPid ][ Rank ] ][ ] : g_RankXPg_XPid ][ Rank ] + ][ ] );
}
 
public 
HandleClassMenuid iMenu iItem 
{
    new 
szKey] , iSelectedClass Dummy;
    
menu_item_getinfoiMenu iItem Dummy szKey "" Dummy );
 
    
iSelectedClass str_to_numszKey );
 
    if ( 
g_XPid ][ Class ] == iSelectedClass )
    {
        
client_printid print_chat "* You are already a %s!" g_ClassDesciSelectedClass ] );
        return 
PLUGIN_HANDLED;
    }
 
    
ChangeClassid iSelectedClass );
 
    return 
PLUGIN_HANDLED;
}  
 
//Change a players class and update his rank based on XP he has for the new class. 
public ChangeClassid NewClass 
{
    
//Assign player his new class, set rank to 0 (first rank) 
    
g_XPid ][ Class ] = NewClass
    
g_XPid ][ Rank ] = 0
 
    
//Loop through array g_RankXP and determine which rank player should be placed 
    //in depending on his current XP points he has for the new class. 
    
for ( new sizeofg_RankXP ) ; i++ ) 
    { 
        
//If players XP points for class falls within range for this rank, update players rank 
        
if ( g_RankXP][ ] <= g_XPid ][ XP ][ NewClass ] <= g_RankXP][ ] ) 
        { 
            
g_XPid ][ Rank ] = i
            break;  
        } 
    } 
 
    
client_printid print_chat "* New class: %s - New Rank: %s" g_ClassDescNewClass ] , g_RankDescg_XPid ][ Rank ] ] ); 
 
    return 
PLUGIN_HANDLED;  
}
 
public 
SaveDataid )
{
    new 
iPos szSave[ ( XPDATA 10 ) + ];
 
    for ( new 
XPDATA i++ )
        
iPos += formatexszSaveiPos ] ,charsmaxszSave ) , "%10d" g_XPid ][ ] );
 
    
#if defined USE_SQL
    
nsqlx_setg_iSQL id g_szAuthIDid ] , szSave );
    
#else
    
nvault_setg_iVault g_szAuthIDid ] , szSave );
    
#endif
}
 
 
public 
LoadDataid )
{
 
    
#if defined USE_SQL
    
nsqlx_getg_iSQL id g_szAuthIDid ] );
    
#else
    
new iPos szNum11 ] , szLoad[ ( XPDATA 10 ) + ];
 
    
nvault_getg_iVault g_szAuthIDid ] , szLoad  charsmaxszLoad ) );
 
    for ( new 
XPDATA i++ )
    {    
        
iPos += copyszNum 10 szLoadiPos ] );
        
g_XPid ][ ] = str_to_numszNum ); 
    }
    
#endif
}
 
#if defined USE_SQL
public SQL_HandlerszData[] , iDataLen iRequestID iRequestType SQLHandle szError[] , iError )
{
    if ( ( 
iRequestType == QUERY_GET ) && iDataLen )
    {    
        new 
iPos szNum11 ];
 
        for ( new 
XPDATA i++ )
        {
            
iPos += copyszNum 10 szDataiPos ] );
            
g_XPiRequestID ][ ] = str_to_numszNum );     
        }
    }
}
#endif 
Bugsy,when you release the latest version?
Jacob is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-27-2010 , 10:09   Re: Saving Xp data.
Reply With Quote #9

Of what, nSQLx? I never officially released at all since I didn't think anyone had interest. Plus it was limited to basically what nvault does anyway.
__________________
Bugsy is offline
Jacob
Senior Member
Join Date: Oct 2010
Old 11-28-2010 , 00:25   Re: Saving Xp data.
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
Of what, nSQLx? I never officially released at all since I didn't think anyone had interest. Plus it was limited to basically what nvault does anyway.
for we beginners,nSQLx gave us a very good way to use sqlx.so hope you will release it officially.

Last edited by Jacob; 11-29-2010 at 19:29. Reason: grammar error
Jacob 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 16:16.


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