core/smn_player.cpp
author David Anderson <dvander@alliedmods.net>
Thu Feb 08 01:11:45 2007 +0000 (2007-02-08)
changeset 453 7611078f11cd
parent 449 e24772d5d9eb
child 456 b181fc6f59c7
permissions -rw-r--r--
added gameevents to the publicly available interfaces
     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 "sm_stringutil.h"
    16 
    17 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
    18 {
    19 	if (params[1])
    20 	{
    21 		return g_Players.NumPlayers();
    22 	}
    23 
    24 	int maxplayers = g_Players.MaxClients();
    25 	int count = 0;
    26 	for (int i=1; i<=maxplayers; ++i)
    27 	{
    28 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
    29 		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
    30 		{
    31 			count++;
    32 		}
    33 	}
    34 
    35 	return (g_Players.NumPlayers() + count);
    36 }
    37 
    38 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    39 {
    40 	return g_Players.MaxClients();
    41 }
    42 
    43 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    44 {
    45 	int index = params[1];
    46 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    47 	{
    48 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    49 	}
    50 
    51 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    52 	if (!pPlayer->IsConnected())
    53 	{
    54 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    55 	}
    56 
    57 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
    58 	return 1;
    59 }
    60 
    61 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    62 {
    63 	int index = params[1];
    64 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    65 	{
    66 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    67 	}
    68 
    69 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    70 	if (!pPlayer->IsConnected())
    71 	{
    72 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    73 	}
    74 
    75 	char buf[64], *ptr;
    76 	strcpy(buf, pPlayer->GetIPAddress());
    77 
    78 	if (params[4] && (ptr = strchr(buf, ':')))
    79 	{
    80 		*ptr = '\0';
    81 	}
    82 
    83 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
    84 	return 1;
    85 }
    86 
    87 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    88 {
    89 	int index = params[1];
    90 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    91 	{
    92 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    93 	}
    94 
    95 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    96 	if (!pPlayer->IsConnected())
    97 	{
    98 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    99 	}
   100 
   101 	if (!pPlayer->IsAuthorized())
   102 	{
   103 		return 0;
   104 	}
   105 
   106 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
   107 
   108 	return 1;
   109 }
   110 
   111 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
   112 {
   113 	int index = params[1];
   114 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   115 	{
   116 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   117 	}
   118 
   119 	return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
   120 }
   121 
   122 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
   123 {
   124 	int index = params[1];
   125 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   126 	{
   127 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   128 	}
   129 
   130 	return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
   131 }
   132 
   133 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   134 {
   135 	int index = params[1];
   136 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   137 	{
   138 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   139 	}
   140 
   141 	return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
   142 }
   143 
   144 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   145 {
   146 	int index = params[1];
   147 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   148 	{
   149 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   150 	}
   151 
   152 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   153 	if (!pPlayer->IsConnected())
   154 	{
   155 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
   156 	}
   157 
   158 	return (pPlayer->IsFakeClient()) ? 1 : 0;
   159 }
   160 
   161 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
   162 {
   163 	char buffer[1024];
   164 	char *fmt;
   165 	int arg = 2;
   166 
   167 	pCtx->LocalToString(params[1], &fmt);
   168 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   169 
   170 	buffer[res++] = '\n';
   171 	buffer[res] = '\0';
   172 
   173 	META_CONPRINT(buffer);
   174 
   175 	return 1;
   176 }
   177 
   178 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
   179 {
   180 	int index = params[1];
   181 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   182 	{
   183 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   184 	}
   185 
   186 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   187 	if (!pPlayer->IsInGame())
   188 	{
   189 		return pCtx->ThrowNativeError("Client %d is not in game.", index);
   190 	}
   191 
   192 	char buffer[1024];
   193 	char *fmt;
   194 	int arg = 3;
   195 
   196 	pCtx->LocalToString(params[2], &fmt);
   197 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   198 
   199 	buffer[res++] = '\n';
   200 	buffer[res] = '\0';
   201 
   202 	engine->ClientPrintf(pPlayer->GetEdict(), buffer);
   203 
   204 	return 1;
   205 }
   206 
   207 REGISTER_NATIVES(playernatives)
   208 {
   209 	{"GetMaxClients",			sm_GetMaxClients},
   210 	{"GetClientCount",			sm_GetClientCount},
   211 	{"GetClientName",			sm_GetClientName},
   212 	{"GetClientIP",				sm_GetClientIP},
   213 	{"GetClientAuthString",		sm_GetClientAuthStr},
   214 	{"IsPlayerConnected",		sm_IsPlayerConnected},
   215 	{"IsPlayerInGame",			sm_IsPlayerIngame},
   216 	{"IsPlayerAuthorized",		sm_IsPlayerAuthorized},
   217 	{"IsPlayerFakeClient",		sm_IsPlayerFakeClient},
   218 	{"PrintToServer",			sm_PrintToServer},
   219 	{"PrintToConsole",			sm_PrintToConsole},
   220 	{NULL,						NULL}
   221 };
   222