core/smn_player.cpp
author Borja Ferrer <faluco@alliedmods.net>
Wed Jan 10 23:49:22 2007 +0000 (2007-01-10)
changeset 280 f2a82da2facc
parent 260 110d1d014b0c
child 351 7c219cde57e7
permissions -rw-r--r--
added some console natives
more consts here and there in the logger
     1 #include "CPlayerManager.h"
     2 #include "sm_stringutil.h"
     3 
     4 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
     5 {
     6 	if (params[1])
     7 	{
     8 		return g_PlayerManager.GetPlayerCount();
     9 	}
    10 
    11 	int maxplayers = g_PlayerManager.GetMaxClients();
    12 	int count = 0;
    13 	for (int i=1; i<=maxplayers; ++i)
    14 	{
    15 		CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(i);
    16 		if ((pPlayer->IsPlayerConnected()) && !(pPlayer->IsPlayerInGame()))
    17 		{
    18 			count++;
    19 		}
    20 	}
    21 
    22 	return (g_PlayerManager.GetPlayerCount() + count);
    23 }
    24 
    25 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    26 {
    27 	return g_PlayerManager.GetMaxClients();
    28 }
    29 
    30 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    31 {
    32 	int index = params[1];
    33 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    34 	{
    35 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    36 	}
    37 
    38 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    39 	if (!pPlayer->IsPlayerConnected())
    40 	{
    41 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    42 	}
    43 
    44 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->PlayerName(), NULL);
    45 	return 1;
    46 }
    47 
    48 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    49 {
    50 	int index = params[1];
    51 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    52 	{
    53 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    54 	}
    55 
    56 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    57 	if (!pPlayer->IsPlayerConnected())
    58 	{
    59 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    60 	}
    61 
    62 	char buf[64], *ptr;
    63 	strcpy(buf, pPlayer->PlayerIP());
    64 
    65 	if (params[4] && (ptr = strchr(buf, ':')))
    66 	{
    67 		*ptr = '\0';
    68 	}
    69 
    70 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
    71 	return 1;
    72 }
    73 
    74 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    75 {
    76 	int index = params[1];
    77 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    78 	{
    79 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    80 	}
    81 
    82 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    83 	if (!pPlayer->IsPlayerConnected())
    84 	{
    85 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    86 	}
    87 
    88 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->PlayerAuthString());
    89 	return 1;
    90 }
    91 
    92 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
    93 {
    94 	int index = params[1];
    95 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    96 	{
    97 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    98 	}
    99 
   100 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerConnected()) ? 1 : 0;
   101 }
   102 
   103 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
   104 {
   105 	int index = params[1];
   106 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   107 	{
   108 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   109 	}
   110 
   111 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerInGame()) ? 1 : 0;
   112 }
   113 
   114 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   115 {
   116 	int index = params[1];
   117 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   118 	{
   119 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   120 	}
   121 
   122 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerAuthorized()) ? 1 : 0;
   123 }
   124 
   125 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   126 {
   127 	int index = params[1];
   128 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   129 	{
   130 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   131 	}
   132 
   133 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
   134 	if (!pPlayer->IsPlayerConnected())
   135 	{
   136 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
   137 	}
   138 
   139 	return (pPlayer->IsPlayerFakeClient()) ? 1 : 0;
   140 }
   141 
   142 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
   143 {
   144 	char buffer[1024];
   145 	char *fmt;
   146 	int arg = 2;
   147 
   148 	pCtx->LocalToString(params[1], &fmt);
   149 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   150 
   151 	buffer[res++] = '\n';
   152 	buffer[res] = '\0';
   153 
   154 	META_CONPRINT(buffer);
   155 
   156 	return 1;
   157 }
   158 
   159 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
   160 {
   161 	int index = params[1];
   162 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   163 	{
   164 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   165 	}
   166 
   167 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
   168 	if (!pPlayer->IsPlayerInGame())
   169 	{
   170 		return pCtx->ThrowNativeError("Client %d is not in game.", index);
   171 	}
   172 
   173 	char buffer[1024];
   174 	char *fmt;
   175 	int arg = 3;
   176 
   177 	pCtx->LocalToString(params[2], &fmt);
   178 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   179 
   180 	buffer[res++] = '\n';
   181 	buffer[res] = '\0';
   182 
   183 	engine->ClientPrintf(pPlayer->GetPlayerEdict(), buffer);
   184 
   185 	return 1;
   186 }
   187 
   188 REGISTER_NATIVES(playernatives)
   189 {
   190 	{"GetMaxClients",			sm_GetMaxClients},
   191 	{"GetClientCount",			sm_GetClientCount},
   192 	{"GetClientName",			sm_GetClientName},
   193 	{"GetClientIP",				sm_GetClientIP},
   194 	{"GetClientAuthString",		sm_GetClientAuthStr},
   195 	{"IsPlayerConnected",		sm_IsPlayerConnected},
   196 	{"IsPlayerInGame",			sm_IsPlayerIngame},
   197 	{"IsPlayerAuthorized",		sm_IsPlayerAuthorized},
   198 	{"IsPlayerFakeClient",		sm_IsPlayerFakeClient},
   199 	{"PrintToServer",			sm_PrintToServer},
   200 	{"PrintToConsole",			sm_PrintToConsole},
   201 	{NULL,						NULL}
   202 };