core/smn_player.cpp
author David Anderson <dvander@alliedmods.net>
Fri Mar 02 08:34:13 2007 +0000 (2007-03-02)
changeset 558 fc0eec032efb
parent 540 27a380082863
child 591 86e97f3eda1f
permissions -rw-r--r--
added state change function
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "CPlayerManager.h"
    15 #include "AdminCache.h"
    16 #include "sm_stringutil.h"
    17 
    18 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
    19 {
    20 	if (params[1])
    21 	{
    22 		return g_Players.NumPlayers();
    23 	}
    24 
    25 	int maxplayers = g_Players.MaxClients();
    26 	int count = 0;
    27 	for (int i=1; i<=maxplayers; ++i)
    28 	{
    29 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
    30 		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
    31 		{
    32 			count++;
    33 		}
    34 	}
    35 
    36 	return (g_Players.NumPlayers() + count);
    37 }
    38 
    39 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    40 {
    41 	return g_Players.MaxClients();
    42 }
    43 
    44 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    45 {
    46 	int index = params[1];
    47 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    48 	{
    49 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    50 	}
    51 
    52 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    53 	if (!pPlayer->IsConnected())
    54 	{
    55 		return pCtx->ThrowNativeError("Client %d is not connected", index);
    56 	}
    57 
    58 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
    59 	return 1;
    60 }
    61 
    62 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    63 {
    64 	int index = params[1];
    65 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    66 	{
    67 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    68 	}
    69 
    70 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    71 	if (!pPlayer->IsConnected())
    72 	{
    73 		return pCtx->ThrowNativeError("Client %d is not connected", index);
    74 	}
    75 
    76 	char buf[64], *ptr;
    77 	strcpy(buf, pPlayer->GetIPAddress());
    78 
    79 	if (params[4] && (ptr = strchr(buf, ':')))
    80 	{
    81 		*ptr = '\0';
    82 	}
    83 
    84 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
    85 	return 1;
    86 }
    87 
    88 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    89 {
    90 	int index = params[1];
    91 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    92 	{
    93 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    94 	}
    95 
    96 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    97 	if (!pPlayer->IsConnected())
    98 	{
    99 		return pCtx->ThrowNativeError("Client %d is not connected", index);
   100 	}
   101 
   102 	if (!pPlayer->IsAuthorized())
   103 	{
   104 		return 0;
   105 	}
   106 
   107 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
   108 
   109 	return 1;
   110 }
   111 
   112 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
   113 {
   114 	int index = params[1];
   115 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   116 	{
   117 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   118 	}
   119 
   120 	return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
   121 }
   122 
   123 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
   124 {
   125 	int index = params[1];
   126 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   127 	{
   128 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   129 	}
   130 
   131 	return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
   132 }
   133 
   134 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   135 {
   136 	int index = params[1];
   137 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   138 	{
   139 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   140 	}
   141 
   142 	return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
   143 }
   144 
   145 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   146 {
   147 	int index = params[1];
   148 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   149 	{
   150 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   151 	}
   152 
   153 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   154 	if (!pPlayer->IsConnected())
   155 	{
   156 		return pCtx->ThrowNativeError("Client %d is not connected", index);
   157 	}
   158 
   159 	return (pPlayer->IsFakeClient()) ? 1 : 0;
   160 }
   161 
   162 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
   163 {
   164 	int client = params[1];
   165 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   166 	if (!pPlayer)
   167 	{
   168 		return pContext->ThrowNativeError("Invalid client index %d", client);
   169 	}
   170 	if (!pPlayer->IsConnected())
   171 	{
   172 		return pContext->ThrowNativeError("Client %d is not connected", client);
   173 	}
   174 
   175 	char *key;
   176 	pContext->LocalToString(params[2], &key);
   177 
   178 	const char *val = engine->GetClientConVarValue(client, key);
   179 	if (!val)
   180 	{
   181 		return false;
   182 	}
   183 
   184 	pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
   185 	return 1;
   186 }
   187 
   188 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
   189 {
   190 	int client = params[1];
   191 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   192 	if (!pPlayer)
   193 	{
   194 		return pContext->ThrowNativeError("Invalid client index %d", client);
   195 	}
   196 	if (!pPlayer->IsConnected())
   197 	{
   198 		return pContext->ThrowNativeError("Client %d is not connected", client);
   199 	}
   200 	if (!g_Admins.IsValidAdmin(params[2]))
   201 	{
   202 		return pContext->ThrowNativeError("AdminId %x is not valid", params[2]);
   203 	}
   204 
   205 	pPlayer->SetAdminId(params[2], params[3] ? true : false);
   206 
   207 	return 1;
   208 }
   209 
   210 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
   211 {
   212 	int client = params[1];
   213 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   214 	if (!pPlayer)
   215 	{
   216 		return pContext->ThrowNativeError("Invalid client index %d", client);
   217 	}
   218 	if (!pPlayer->IsConnected())
   219 	{
   220 		return pContext->ThrowNativeError("Client %d is not connected", client);
   221 	}
   222 
   223 	return pPlayer->GetAdminId();
   224 }
   225 
   226 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
   227 {
   228 	int client = params[1];
   229 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   230 	if (!pPlayer)
   231 	{
   232 		return pContext->ThrowNativeError("Invalid client index %d", client);
   233 	}
   234 	if (!pPlayer->IsConnected())
   235 	{
   236 		return pContext->ThrowNativeError("Client %d is not connected", client);
   237 	}
   238 
   239 	AdminId id;
   240 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   241 	{
   242 		id = g_Admins.CreateAdmin(NULL);
   243 		pPlayer->SetAdminId(id, true);
   244 	}
   245 
   246 	cell_t *addr;
   247 	for (int i=2; i<=params[0]; i++)
   248 	{
   249 		pContext->LocalToPhysAddr(params[i], &addr);
   250 		g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
   251 	}
   252 
   253 	return 1;
   254 }
   255 
   256 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	int client = params[1];
   259 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   260 	if (!pPlayer)
   261 	{
   262 		return pContext->ThrowNativeError("Invalid client index %d", client);
   263 	}
   264 	if (!pPlayer->IsConnected())
   265 	{
   266 		return pContext->ThrowNativeError("Client %d is not connected", client);
   267 	}
   268 	
   269 	AdminId id;
   270 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   271 	{
   272 		return 0;
   273 	}
   274 
   275 	cell_t *addr;
   276 	for (int i=2; i<=params[0]; i++)
   277 	{
   278 		pContext->LocalToPhysAddr(params[i], &addr);
   279 		g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
   280 	}
   281 
   282 	return 1;
   283 }
   284 
   285 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
   286 {
   287 	int client = params[1];
   288 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   289 	if (!pPlayer)
   290 	{
   291 		return pContext->ThrowNativeError("Invalid client index %d", client);
   292 	}
   293 	if (!pPlayer->IsConnected())
   294 	{
   295 		return pContext->ThrowNativeError("Client %d is not connected", client);
   296 	}
   297 
   298 	AdminId id;
   299 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   300 	{
   301 		id = g_Admins.CreateAdmin(NULL);
   302 		pPlayer->SetAdminId(id, true);
   303 	}
   304 
   305 	g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
   306 
   307 	return 1;
   308 }
   309 
   310 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
   311 {
   312 	int client = params[1];
   313 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   314 	if (!pPlayer)
   315 	{
   316 		return pContext->ThrowNativeError("Invalid client index %d", client);
   317 	}
   318 	if (!pPlayer->IsConnected())
   319 	{
   320 		return pContext->ThrowNativeError("Client %d is not connected", client);
   321 	}
   322 
   323 	AdminId id;
   324 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   325 	{
   326 		return 0;
   327 	}
   328 
   329 	return g_Admins.GetAdminFlags(id, Access_Effective);
   330 }
   331 
   332 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
   333 {
   334 	int client = params[1];
   335 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   336 	if (!pPlayer)
   337 	{
   338 		return pContext->ThrowNativeError("Invalid client index %d", client);
   339 	}
   340 	if (!pPlayer->IsConnected())
   341 	{
   342 		return pContext->ThrowNativeError("Client %d is not connected", client);
   343 	}
   344 
   345 	return engine->GetPlayerUserId(pPlayer->GetEdict());
   346 }
   347 
   348 static cell_t CanUserTarget(IPluginContext *pContext, const cell_t *params)
   349 {
   350 	int client = params[1];
   351 	int target = params[2];
   352 
   353 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   354 	if (!pPlayer)
   355 	{
   356 		return pContext->ThrowNativeError("Player %d is not a valid client", client);
   357 	} else if (!pPlayer->IsConnected()) {
   358 		return pContext->ThrowNativeError("Player %d is not connected", client);
   359 	}
   360 
   361 	CPlayer *pTarget = g_Players.GetPlayerByIndex(target);
   362 	if (!pTarget)
   363 	{
   364 		return pContext->ThrowNativeError("Player %d is not a valid client", target);
   365 	} else if (!pTarget->IsConnected()) {
   366 		return pContext->ThrowNativeError("Player %d is not connected", target);
   367 	}
   368 
   369 	return g_Admins.CanAdminTarget(pPlayer->GetAdminId(), pTarget->GetAdminId()) ? 1 : 0;
   370 }
   371 
   372 REGISTER_NATIVES(playernatives)
   373 {
   374 	{"GetMaxClients",			sm_GetMaxClients},
   375 	{"GetClientCount",			sm_GetClientCount},
   376 	{"GetClientName",			sm_GetClientName},
   377 	{"GetClientIP",				sm_GetClientIP},
   378 	{"GetClientAuthString",		sm_GetClientAuthStr},
   379 	{"IsClientConnected",		sm_IsPlayerConnected},
   380 	{"IsPlayerInGame",			sm_IsPlayerIngame},
   381 	{"IsClientAuthorized",		sm_IsPlayerAuthorized},
   382 	{"IsFakeClient",			sm_IsPlayerFakeClient},
   383 	{"GetClientInfo",			sm_GetClientInfo},
   384 	{"SetUserAdmin",			SetUserAdmin},
   385 	{"GetUserAdmin",			GetUserAdmin},
   386 	{"AddUserFlags",			AddUserFlags},
   387 	{"RemoveUserFlags",			RemoveUserFlags},
   388 	{"SetUserFlagBits",			SetUserFlagBits},
   389 	{"GetUserFlagBits",			GetUserFlagBits},
   390 	{"GetClientUserId",			GetClientUserId},
   391 	{"CanUserTarget",			CanUserTarget},
   392 	{NULL,						NULL}
   393 };
   394