AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Move 3 almost same functions to one (https://forums.alliedmods.net/showthread.php?t=136225)

lazarev 08-24-2010 07:12

Move 3 almost same functions to one
 
Hi, I need you to move those functions to client_command.
PHP Code:

// =========== the commands are ============
register_clcmd"__SETKILLS""CMD_SETKILLS"ADMIN_LEVEL );
register_clcmd"__SETHEADSHOTS""CMD_SETHEADSHOTS"ADMIN_LEVEL );
register_clcmd"__SETSURVIVES""CMD_SETSURVIVES"ADMIN_LEVEL );
// ======================================

public CMD_SETKILLSidlevelcid )
{
    if ( !
cmd_accessidlevelcid) )
        return 
PLUGIN_HANDLED;
        
    new 
szArg33 ];
    
read_argv1szArg32 );
    
    if( !
equalszArg"" ) )
    
g_GlobalScoreKILLS ] = str_to_numszArg );
    
ShowEventMenuid );
    
    return 
PLUGIN_HANDLED;
}

public 
CMD_SETHEADSHOTSidlevelcid )
{
    if ( !
cmd_accessidlevelcid) )
        return 
PLUGIN_HANDLED;
    
    new 
szArg33 ];
    
read_argv1szArg32 );
    if( !
equalszArg"" ) )
    
g_GlobalScoreHEADSHOTS ] = str_to_numszArg );
    
ShowEventMenuid );
    
    return 
PLUGIN_HANDLED;
}

public 
CMD_SETSURVIVESidlevelcid )
{
    if ( !
cmd_accessidlevelcid) )
        return 
PLUGIN_HANDLED;
        
    new 
szArg33 ];
    
read_argv1szArg32 );
    
    if( !
equalszArg"" ) )
    
g_GlobalScoreSURVIVES ] = str_to_numszArg );
    
ShowEventMenuid );
    
    return 
PLUGIN_HANDLED;



Arkshine 08-24-2010 07:16

Re: Move 3 almost same functions to one
 
why ?

joaquimandrade 08-24-2010 07:47

Re: Move 3 almost same functions to one
 
You want to avoid code duplication?

You have to find what is different and take it out of the code:

PHP Code:

set_stuff(id,level,cid,type)
{
    if ( !
cmd_accessidlevelcid) )
        return 
PLUGIN_HANDLED;
        
    new 
szArg33 ];
    
read_argv1szArg32 );
    
    if( !
equalszArg"" ) )
    
g_GlobalScoretype ] = str_to_numszArg );
    
ShowEventMenuid );
    
    return 
PLUGIN_HANDLED;
}

public 
CMD_SETKILLSidlevelcid )
{
    return 
set_stuff(id,level,cid,KILLS



Bugsy 08-24-2010 07:52

Re: Move 3 almost same functions to one
 
Read the command and do a switch based off of that?

__SETKILLS
__SETHEADSHOTS
__SETSURVIVE

PHP Code:

// code...

new iSet;
switch ( 
szCmd] )
{
     case 
'K'iSet KILLS;
     case 
'H'iSet HEADSHOTS;
     case 
'S'iSet SURVIVES;
}

g_GlobalScoreiSet ] = str_to_numszArg );

// code... 


joaquimandrade 08-24-2010 07:59

Re: Move 3 almost same functions to one
 
:twisted:

lazarev 08-24-2010 10:24

Re: Move 3 almost same functions to one
 
thanks, Bugsy & joaquimandrade. both works :mrgreen:


All times are GMT -4. The time now is 21:52.

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