Author Topic: findClients( mask ) [RESOURCE]  (Read 2563 times)

When scripting, I often find myself in the situation that findClientByName or findClientByBL_ID aren't good enough, and I need to expose a better way of selecting targets to clients. So this time, I decided to make my own method.

findClients( mask )
    return: string


The return value will always be a string. If no targets are found, this is simply an empty string. If targets are found, it will be a space-seperated list of object ID's (of gameConnection objects).

mask is a string containing a "search pattern" which is highly inspired by SourceMod. Normally, it will simply return all clients whose names mask occurs in, but these "special rules" occur:
  • If mask starts with an exclamation mark (!), it will return all clients who do not match the expression. Example: !pie will return all clients that do not have pie in their name.
  • If mask starts with the & character, it will return the client with the specified user ID. Example: &2 will return the third player on the server (because user ID's start at 0).
  • If mask starts with a sharp (#) which is followed by a number, it will return all clients using the specified BL_ID. Example: #12297 will return all clients with BL_ID 12297. (Note: On LAN servers, there are no BL_IDs, all clients play under BL_ID 999999)
  • If mask starts with a sharp (#) which is followed by a string, it will return all clients with the exact name specified. Example: #Port will return all clients with the name "Port", but not clients with the name "Portable".
  • If mask starts with the at character (@), it will return a special list of clients matching the "tag" following it. A list of all tags can be seen below.

List of available "tags" for @:
  • all: All clients.
  • alive: All clients with a player object that is not dead.
  • admin: All admins.
  • admins: All admins.
  • sa: All super admins.
  • sas: All super admins.
  • superadmin: All super admins.
  • superadmins: All super admins.
  • loading: All clients who are currently loading.

Please note:
  • You cannot combine ! and a diffirent operator in a incorrect way. Example: @!loading will return all clients. The correct use would be !@loading, to return all clients who are not loading. Another example: #!Port will return all clients named exactly "!Port". To return all clients who are not named "Port", you need to use !#Port.
  • Specifying none or an invalid "tag" to @ will just result in retrieving all clients.


Code: (Example of usage) [Select]
exec( "./Support_ClientFind.cs" );

function serverCmdAdvKick( %cl, %a, %b, %c, %d, %e )
{
if ( !%cl.isAdmin )
return;

%result = findClients( trim( %a SPC %b SPC %c SPC %d SPC %e ) );

if ( !strLen( %result ) || !getWordCount( %result ) )
return;

%cnt = getWordCount( %result );
for ( %i = 0 ; %i < %cnt ; %i++ )
{
%target = getWord( %result, %i );
messageAll( '', "\c3" @ %cl.name SPC "\c6has kicked \c3" @ %target.getPlayerName() );
%target.delete();
}
}

With the above code, typing /advkick !@loading would kick all players who are not currently loading.

Download: http://pastebin.com/raw.php?i=KtE3pRLQ
(included functions: findClients, startsWith, clientFind_isInt)

If you find any bugs or have suggestions for features, feel free to tell me.
« Last Edit: November 15, 2011, 10:27:22 AM by Port »



Here are some client sided methods

Code: [Select]
function findplayerbyname(%name)
{
if(!isobject(serverconnection))
{
echo("You are not connected to a server");
return 0;
}

if(!isobject(serverconnection.getcontrolobject()))
{
echo("You cannot see other players at this time.");
return 0;
}
%group = serverconnection.getcontrolobject().getgroup();

for(%a=0; %a<%group.getcount(); %a++)
{
if((%b = %group.getobject(%a)).getclassname() $= "Player")
{
if(strPos(%b.getshapename(), %name) > -1) //case sensitive
return %b;

}
}
return -1;
}

Code: [Select]
function fetchall()
{
for(%a=0; %a<npl_list.rowcount(); %a++)
commandtoserver('fetch', getfield(npl_list.getrowtext(%a), 1));
}

mmm yes

What do those have to do with this topic?

Also, small "snippet" script: Probably the smallest system for saving data, although this could probably be optimized a lot and saved files can be modified to do evil things upon loaded.

Code: [Select]
function dbsLoad( %file )
{
if ( !strLen( %file ) || !isFile( %file ) )
{
error( "dbsLoad() - no/unexistant file specified" );
return false;
}

exec( %file );

if ( !isObject( %obj = nameToID( "tempDBS" ) ) )
{
error( "dbsLoad() - invalid file specified" );
return false;
}

%obj.setName( "" );
return %obj;
}

function dbsSave( %obj, %file )
{
%name = %obj.getName();

%obj.setName( "tempDBS" );
%obj.save( %file );
%obj.setName( %name );
}

What do those have to do with this topic?

They find clients.
At which point do those three words differ from this topic in any way?

It is a resource, and he never specified that it needed to be server sided, nor were there any previous examples of client sided methods.