Author Topic: [RESOURCE] Safe way to check if a player is an admin  (Read 1310 times)

Code: [Select]
function isBL_IDAdmin(%id) {
    // check for manual admin
    if(isobject(%client = findclientbybl_id(%id)))
        if(%client.isadmin || %client.issuperadmin)
            return true;

    // parse admin list
    %count = getWordCount($Pref::Server::AutoAdminList);
    for(%i=0; %i < %count; %i++) {
        %guy = getWord($Pref::Server::AutoAdminList, %i);
        if(%guy == %id)
            return true;
    }
    
    // parse super-admin list
    %count = getWordCount($Pref::Server::AutoSuperAdminList);
    for(%i=0; %i < %count; %i++) {
        %guy = getWord($Pref::Server::AutoSuperAdminList, %i);
        if(%guy == %id)
            return true;
    }
    
    // ...is it the host?
    if(%id == getNumKeyID()) {
        return true;
    }
    
    return false;
}

Usually when you check a player is admin you do it through %client.isAdmin and/or %client.isSuperAdmin. This code is for checking the admin status of people who may or may not be on the server. As far as I can tell, it works in all situations. If you come up with a better or more optimized version feel free to post it under this one.

EDIT: Added an amendment by Zeblote.
« Last Edit: March 06, 2014, 04:48:06 PM by chrisbot6 »

What if they're on the server and have manual admin?

If you come up with a better or more optimized version feel free to post it under this one.
Code: [Select]
function isbl_idadmin(%id)
{
    if(isobject(%client = findclientbybl_id(%id)))
        if(%client.isadmin || %client.issuperadmin)
            return 1;

    if(%id == getnumkeyid() || strstr(" " @ $pref::server::autoadminlist SPC $pref::server::autosuperadminlist @ " ", " " @ %id @ " ") >= 0)
        return 1;

    return 0;
}
?

That's pretty much unreadable for anybody who doesn't know what they're doing whom want to learn. Why didn't you just post this instead if that's what you intended?

function isBL_IDAdmin(%a){return %a==getnumkeyid()||strpos("  "@$pref::server::autoadminlist@" "@$pref::server::autosuperadminlist@" "," "@%a@" ")||%b=findclientbybl_id(%a)&&(%b.isadmin||%b.issuperadmin);}

That's pretty much unreadable for anybody who doesn't know what they're doing whom want to learn. Why didn't you just post this instead if that's what you intended?

function isBL_IDAdmin(%a){return %a==getnumkeyid()||strpos("  "@$pref::server::autoadminlist@" "@$pref::server::autosuperadminlist@" "," "@%a@" ")||%b=findclientbybl_id(%a)&&(%b.isadmin||%b.issuperadmin);}
He didn't say more readable

I disagree, based on the OP's code sample he clearly understands multioperative if statements, spacing the code out more wouldn't have really helped anything. The only thing Zeblote used that the OP didn't demonstrate knowledge of in his one post is the string append operator.

I disagree, based on the OP's code sample he clearly understands multioperative if statements, spacing the code out more wouldn't have really helped anything. The only thing Zeblote used that the OP didn't demonstrate knowledge of in his one post is the string append operator.

I wasn't referring to the OP.

I wasn't referring to the OP.
The only thing Zeblote used that the OP didn't demonstrate knowledge of in his one post is the string append operator.

The point was that it's more optimized, uses c++ loops instead of torquescript loops.

Returning 1 in some cases and true in others is bad practice at the least. Might wanna fix that.

You could shorten it by simple combining both admin lists, then looping though all of them instead of each one individually.

You could shorten it by simple combining both admin lists, then looping though all of them instead of each one individually.
Or how about not using a torkscript loop at all

Or how about not using a torkscript loop at all
Or how about a banana?