Raised This Month: $32 Target: $400
 8% 

Admin Hierarchy


Post New Thread Reply   
 
Thread Tools Display Modes
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-08-2009 , 00:44   Re: Admin Hierarchy
Reply With Quote #11

I offer you a little code donation:

PHP Code:
enum Rank
{
    
ADMIN_SLOT 0,
    
ADMIN_OFFICER,
    
ADMIN_MODERATOR,
    
ADMIN_ADMINISTRATOR,
    
ADMIN_OWNER
}

new 
g_Flags[Rank];

new 
g_cvars_rank[Rank];

public 
plugin_init()
{
    
register_plugin"Admin Hierarchy" "0.1" "bugsy" );
    
register_cvar"admin_hierarchy""0.1"FCVAR_SERVER);
    
    
g_pMode register_cvar"ah_mode" "0" );
    
g_pUser register_cvar"ah_user" "z" );
    
    
g_cvars_rank[ADMIN_OWNER] = register_cvar"ah_owner" "abcdefghijklmnopqrstu" );
    
g_cvars_rank[ADMIN_ADMINISTRATOR] = register_cvar"ah_administrator" "bcdefijmnopqrstu" );
    
g_cvars_rank[ADMIN_MODERATOR] = register_cvar"ah_moderator" "bcdefiju" );
    
g_cvars_rank[ADMIN_OFFICER] = register_cvar"ah_officer""bcefij" );
    
g_cvars_rank[ADMIN_SLOT] = register_cvar"ah_slot""b" );
}

public 
plugin_cfg()
{
    new 
szFlags[23];

    for ( new 
Rank:ADMIN_SLOTRank i++ )
    {
        
get_pcvar_string (g_cvars_rank[i], szFlags 22);
        
g_Flags[i] = read_flagsszFlags );
    }
}

(...)

for ( new 
Rank:ADMIN_SLOT Rank i++ )
{
    if ( 
g_AdminFlags[id] == g_Flags[i] )
    {
        
g_AdminLevel[id] = _:i;
        
g_CurrentLevel[id] = _:i;
        break;
    }

Also:

I see that you are using get_players with a flag. As you can see here it is not recommend. I also don't recommend, at least cause of the code readability.

Also:

After taking a look to your algorithm i think that:


PHP Code:
public client_disconnect(id)
{
    
//If an admin is disconnecting we clear out his powers so they will not be recognized in the 
    //SetAdmins function.
    
if ( g_AdminFlags[id] )
    {
        
g_AdminFlags[id] = 0;
        
g_AdminLevel[id] = 0;
        
g_CurrentLevel[id] = 0;

        
SetAdmins(0);
    }

can be:

PHP Code:
public client_disconnect(id)
{
    
//If an admin is disconnecting we clear out his powers so they will not be recognized in the 
    //SetAdmins function.
    
if ( g_AdminFlags[id] )
    {
        
g_AdminFlags[id] = 0;
        
g_AdminLevel[id] = 0;
        
        if(
g_CurrentLevel[id])
        {
            
g_CurrentLevel[id] = 0;
            
SetAdmins(0);    
        }
    }

__________________

Last edited by joaquimandrade; 03-08-2009 at 03:04.
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-08-2009 , 12:05   Re: Admin Hierarchy
Reply With Quote #12

@joaquimandrade

I don't see the benefit of switching to an array of cvar pointers for a plugin with only a handful of cvars; it only reduces readability in my opinion.

As for get_players, it functions consistently fine in every plugin I've used it in. I do see that bailopan does not recommend using it with flags but being it functions fine consistently, I choose to use it. If\when it is done away with in future releases of AMX-X, I will then do away with using it.

This portion I agree with and I had already corrected.
PHP Code:
public client_disconnect(id)
{
    
//If an admin is disconnecting we clear out his powers so they will not be recognized in the 
    //SetAdmins function.
    
if ( g_AdminFlags[id] )
    {
        
g_AdminFlags[id] = 0;
        
g_AdminLevel[id] = 0;
        
        if(
g_CurrentLevel[id])
        {
            
g_CurrentLevel[id] = 0;
            
SetAdmins(0);    
        }
    }

__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-08-2009 , 12:15   Re: Admin Hierarchy
Reply With Quote #13

The array of cvar pointers is self documenting and improves readability and the code itself . Looking at the code you will see that they are cvars related to the ranks, when you iterate you see that you are iterating over a Rank, and if you want to add a rank it would be easier to do it.

By using it you avoid code like:

PHP Code:
    for ( new 0<= i++ )
    {
        switch ( 
)
        {
            case 
ADMIN_OWNERget_pcvar_string g_pOwner szFlags 22);
            case 
ADMIN_ADMINISTRATORget_pcvar_string g_pAdministrator szFlags 22 );
            case 
ADMIN_MODERATORget_pcvar_string g_pModerator szFlags 22 );
            case 
ADMIN_OFFICERget_pcvar_string g_pOfficer szFlags 22 );
            case 
ADMIN_SLOTget_pcvar_string g_pSlot szFlags 22 );
        }

        
g_Flags[i] = read_flagsszFlags );
    } 
The getplayers question:

When you look at "c", you don't what that means, or if you know, you can easily forget. That don't happens if you use the more readable !is_user_bot(i)
__________________

Last edited by joaquimandrade; 03-08-2009 at 12:31.
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-08-2009 , 12:46   Re: Admin Hierarchy
Reply With Quote #14

Quote:
Originally Posted by joaquimandrade View Post
The array of cvar pointers is self documenting and improves readability and the code itself . Looking at the code you will see that they are cvars related to the ranks, when you iterate you see that you are iterating over a Rank, and if you want to add a rank it would be easier to do it.
I know what you are saying and understand it. The cvars are only used in plugin_init and plugin_cfg so they are easily accessible to edit\add anything. If the plugin was large with more cvars that were scattered throughout the code, I would agree with you 100%.

Quote:
Originally Posted by joaquimandrade View Post
The getplayers question:

When you look at "c", you don't what that means, or if you know, you can easily forget. That don't happens if you use the more readable !is_user_bot(i)
Using get_players() with no flag requires calling is_user_bot() which is just an additional unneeded native. If anyone reading the code for reference purposes is unsure of the flags, they can be easily referenced in the functions page. A comment can also be added above the function call showing the flags purpose.
__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-08-2009 , 12:56   Re: Admin Hierarchy
Reply With Quote #15

Quote:
Originally Posted by Bugsy View Post
Using get_players() with no flag requires calling is_user_bot() which is just an additional unneeded native. If anyone reading the code for reference purposes is unsure of the flags, they can be easily referenced in the functions page. A comment can also be added above the function call showing the flags purpose.
But by using the flag you are making get_players more expensive (it has to make more checks) that without using the flag, so what you gain by not calling is_user_bot you spend it in get_players. Anyway, even being get_players("c") (a little) more faster than is_user_bot, "making the code less readable" its not worthy. Anyway, i told you my opinion and btw i will test with ammxprofiler to see the performance difference.
__________________
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-08-2009 , 13:03   Re: Admin Hierarchy
Reply With Quote #16

Quote:
Originally Posted by joaquimandrade View Post
But by using the flag you are making get_players more expensive (it has to make more checks) that without using the flag, so what you gain by not calling is_user_bot you spend it in get_players. Anyway, even being get_players("c") (a little) more faster than is_user_bot, "making the code less readable" its not worthy. Anyway, i told you my opinion and btw i will test with ammxprofiler to see the performance difference.
Every programmer has their own personal opinion\preference for the best way to do things.

I do not see much of an expense in passing a flag to get_players(). Let me know your findings.
PHP Code:
static cell AMX_NATIVE_CALL get_players(AMX *amxcell *params/* 4 param */
{
    
int iNum 0;
    
int ilen;
    
charsptemp get_amxstring(amxparams[3], 0ilen);
    
int flags UTIL_ReadFlags(sptemp);

    
cell *aPlayers get_amxaddr(amxparams[1]);
    
cell *iMax get_amxaddr(amxparams[2]);

    
int team 0;

    if (
flags 48)
    {
        
sptemp get_amxstring(amxparams[4], 0ilen);

        if (
flags 16)
        {
            if (
flags 64)
                
team g_teamsIds.findTeamId(sptemp);
            else
                
team g_teamsIds.findTeamIdCase(sptemp);
        }
    }

    for (
int i 1<= gpGlobals->maxClients; ++i)
    {
        
CPlayerpPlayer GET_PLAYER_POINTER_I(i);
        if (
pPlayer->ingame)
        {
            if (
pPlayer->IsAlive() ? (flags 2) : (flags 1))
                continue;
            if (
pPlayer->IsBot() ? (flags 4) : (flags 8))
                continue;
            if ((
flags 16) && (pPlayer->teamId != team))
                continue;
            if ((
flags 128) && (pPlayer->pEdict->v.flags FL_PROXY))
                continue;
            if (
flags 32)
            {
                if (
flags 64)
                {
                    if (
stristr(pPlayer->name.c_str(), sptemp) == NULL)
                        continue;
                }
                else if (
strstr(pPlayer->name.c_str(), sptemp) == NULL)
                    continue;
            }
            
aPlayers[iNum++] = i;
        }
    }

    *
iMax iNum;
    
    return 
1;

__________________
Bugsy is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-08-2009 , 13:28   Re: Admin Hierarchy
Reply With Quote #17

Here they go. As you will see, the flag improvement is negligible.

Edit: ups. I need to test again. I tested without bots.

Ok now. Here they go. The improvement of the flag use is 1 second in 30000 calls.

What i have to say?

I learned that calling is_user_bot is a little more expensive than i thought. So this is a trade-off. Expensiveness for readability but, no big deal whatever the choice.
Attached Files
File Type: txt teste.amxx.txt (2.4 KB, 146 views)
File Type: sma Get Plugin or Get Source (teste.sma - 697 views - 895 Bytes)
__________________

Last edited by joaquimandrade; 03-08-2009 at 14:15. Reason: change the sma. uploaded a older one.
joaquimandrade is offline
Glist3r
Senior Member
Join Date: Feb 2008
Old 03-08-2009 , 14:21   Re: Admin Hierarchy
Reply With Quote #18

Well, the plugin doesn't work as it should.

When I connect with owner access , the other admins have the same powers as they had before. They don't lose their powers.
Glist3r is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-08-2009 , 14:23   Re: Admin Hierarchy
Reply With Quote #19

Quote:
Originally Posted by Glist3r View Post
Well, the plugin doesn't work as it should.

When I connect with owner access , the other admins have the same powers as they had before. They don't lose their powers.
Tell your cvars configurations and your flags. And be sure that you are the most powerful admin.
__________________
joaquimandrade is offline
Glist3r
Senior Member
Join Date: Feb 2008
Old 03-08-2009 , 14:32   Re: Admin Hierarchy
Reply With Quote #20

ah_mode 0
ah_user "bi"


"Glist3r" "******" "abcdefghijklmnopqrstu" "a"
Glist3r is offline
Reply


Thread Tools
Display Modes

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 23:27.


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