plugins/include/helpers.inc
author David Anderson <dvander@alliedmods.net>
Fri Jun 08 01:09:17 2007 +0000 (2007-06-08)
changeset 922 980941c525fa
parent 903 a210892076b6
child 926 b42be6b5f221
permissions -rw-r--r--
sm_kick now uses targeting properly
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _helpers_included
    17  #endinput
    18 #endif
    19 #define _helpers_included
    20 
    21 /**
    22  * Formats a user's info as log text.
    23  *
    24  * @param client		Client index.
    25  * @param buffer		Buffer for text.
    26  * @param maxlength		Maximum length of text.
    27  */
    28 stock FormatUserLogText(client, String:buffer[], maxlength)
    29 {
    30 	decl String:auth[32];
    31 	decl String:name[40];
    32 	
    33 	new userid = GetClientUserId(client);
    34 	if (!GetClientAuthString(client, auth, sizeof(auth)))
    35 	{
    36 		StrCopy(auth, sizeof(auth), "UNKNOWN");
    37 	}
    38 	if (!GetClientName(client, name, sizeof(name)))
    39 	{
    40 		StrCopy(name, sizeof(name), "UNKNOWN");
    41 	}
    42 	
    43 	/** Currently, no team stuff ... */
    44 	
    45 	Format(buffer, maxlength, "\"%s<%d><%s><>\"", name, userid, auth);
    46 }
    47 
    48 /**
    49  * Returns plugin handle from plugin filename.
    50  *
    51  * @param filename		Filename of the plugin to search for.
    52  * @return				Handle to plugin if found, INVALID_HANDLE otherwise.
    53  */
    54 stock Handle:FindPluginByFile(const String:filename[])
    55 {
    56 	decl String:buffer[256];
    57 	
    58 	new Handle:iter = GetPluginIterator();
    59 	new Handle:pl;
    60 	
    61 	while (MorePlugins(iter))
    62 	{
    63 		pl = ReadPlugin(iter);
    64 		
    65 		GetPluginFilename(pl, buffer, sizeof(buffer));
    66 		if (StrCompare(buffer, filename) == 0)
    67 		{
    68 			return pl;
    69 		}
    70 	}
    71 	
    72 	CloseHandle(iter);
    73 	
    74 	return INVALID_HANDLE;
    75 }
    76 
    77 /**
    78  * Searches for clients that match an input string.
    79  *
    80  * Allowed patterns: 
    81  *  1) #<userid> or #<exact name>
    82  *  2) <partial or full name>
    83  *
    84  * @param pattern		Pattern to search for.
    85  * @param clients		Array to store matching clients in.
    86  * @param maxClients	Maximum clients in the array.
    87  * @return				Number of clients found.
    88  */
    89 stock SearchForClients(const String:pattern[], clients[], maxClients)
    90 {
    91 	new maxclients = GetMaxClients();
    92 	new total = 0;
    93 	
    94 	if (maxClients == 0)
    95 	{
    96 		return 0;
    97 	}
    98 	
    99 	if (pattern[0] == '#')
   100 	{
   101 		new input = StringToInt(pattern[1]);
   102 		if (!input)
   103 		{
   104 			decl String:name[65]
   105 			for (new i=1; i<=maxclients; i++)
   106 			{
   107 				if (!IsClientInGame(i))
   108 				{
   109 					continue;
   110 				}
   111 				GetClientName(i, name, sizeof(name));
   112 				if (StrEqual(name, pattern, false))
   113 				{
   114 					clients[0] = i;
   115 					return 1;
   116 				}
   117 			}
   118 		} else {
   119 			new client = GetClientOfUserId(input);
   120 			if (client)
   121 			{
   122 				clients[0] = client;
   123 				return 1;
   124 			}
   125 		}
   126 	}
   127 	
   128 	decl String:name[65]
   129 	for (new i=1; i<=maxclients; i++)
   130 	{
   131 		if (!IsClientInGame(i))
   132 		{
   133 			continue;
   134 		}
   135 		GetClientName(i, name, sizeof(name));
   136 		if (StrContains(name, pattern, false) != -1)
   137 		{
   138 			clients[total++] = i;
   139 			if (total >= maxClients)
   140 			{
   141 				break;
   142 			}
   143 		}
   144 	}
   145 	
   146 	return total;
   147 }