Daniel Wilson randomly typed the following on 02/15/2006 11:36 PM:
Nice code,
Thank you.
If your list grows larger, you could use a hash table or, in such a case, I
would prefer one of the tree structures like AVL. You would have to populate
them at runtime, but that are much faster, especially with large amounts of
entries.
I will look into this if I get a long list of admin commands.
In this case a tree would be better because you probably also want to
iterate over all entrees alphabetically such as listing all the commands a
client has access too.
Checking client access to commands can also be done quickly. This is my
old cz code for checking commands against an allowed list. The function
for displaying the list of commands available (using admin_help) to a
given player is at the bottom.
struct client_t
{
bool (*function)(edict_t *pEntity);
char command[32];
bool showinhelp; // whether to show in admin_help (commands
like say should be ignored)
long perms; // bit flag which (dis)allows access to this command
char *help; // a brief help description
};
struct client_t client_commands[] = {
{cmdBan, "admin_ban", TRUE,
ADMIN_BAN, "Ban a player for a given time period"},
{cmdBury, "admin_bury", TRUE,
ADMIN_BURY, "Restrict a players movement by burying them"},
{cmdChat, "admin_chat", TRUE,
0, "Talk to an admin on the admin channel"},
....
{cmdHandleSay, "say", FALSE,
0, " "},
{cmdIrcSay, "say_irc", TRUE,
0, "Send a message to #axia on quakenet"},
{cmdHandleSay, "say_team", FALSE,
0, " "},
...
};
#define ACTOP ((sizeof(client_commands) / sizeof(struct client_t)) -1)
bool client_command(edict_t *pEntity, char *pcmd)
{
int top = ACTOP;
int mid,found,low=0;
player_t *pPlayer = GET_PLAYER_POINTER(pEntity);
while(low <= top) {
mid = (low+top)/2;
if((found = str_cmp(pcmd, client_commands[mid].command))
> 0)
{
low = mid +1 ;
}
else if(found < 0)
{
top = mid -1;
}
else if(found == 0)
{
if ( ! client_commands[mid].perms ) // no
permissions required?
{
return (
((*client_commands[mid].function)(pEntity)) );
}
else if ( (pPlayer->perms &
client_commands[mid].perms) ) // required bit set?
{
if ( pPlayer->confirmed ) // has the
admin used admin_login yet?
{
return (
((*client_commands[mid].function)(pEntity)) );
}
else
{
// Added to help protect from
the wandering STEAM ID bug
ClientPrint(pEntity,
HUD_PRINTCONSOLE, "You need to unlock admin commands using: admin_login
<pasword>\n");
return(TRUE);
}
}
return(FALSE);
}
}
return(FALSE);
}
void _show_help(edict_t *pEntity)
{
player_t *pPlayer = GET_PLAYER_POINTER(pEntity);
for (unsigned int c = 0;c <= ACTOP;c++)
{
if( client_commands[c].showinhelp && ( (pPlayer->perms &
client_commands[c].perms) || !client_commands[c].perms) )
{
sprintf(g_tmp, "%s - %s\n",
client_commands[c].command, client_commands[c].help);
ClientPrint(pEntity, HUD_PRINTCONSOLE, g_tmp);
}
}
}
_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders