core/smn_player.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Jan 05 00:42:22 2007 +0000 (2007-01-05)
changeset 268 d6fed4f05e07
parent 260 110d1d014b0c
child 280 f2a82da2facc
permissions -rw-r--r--
Fixed CPlayer init, it was called too early.
nothing important on the logger
     1 #include "CPlayerManager.h"
     2 
     3 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
     4 {
     5 	if (params[1])
     6 	{
     7 		return g_PlayerManager.GetPlayerCount();
     8 	}
     9 
    10 	int maxplayers = g_PlayerManager.GetMaxClients();
    11 	int count = 0;
    12 	for (int i=1; i<=maxplayers; ++i)
    13 	{
    14 		CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(i);
    15 		if ((pPlayer->IsPlayerConnected()) && !(pPlayer->IsPlayerInGame()))
    16 		{
    17 			count++;
    18 		}
    19 	}
    20 
    21 	return (g_PlayerManager.GetPlayerCount() + count);
    22 }
    23 
    24 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    25 {
    26 	return g_PlayerManager.GetMaxClients();
    27 }
    28 
    29 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    30 {
    31 	int index = params[1];
    32 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    33 	{
    34 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    35 	}
    36 
    37 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    38 	if (!pPlayer->IsPlayerConnected())
    39 	{
    40 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    41 	}
    42 
    43 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->PlayerName(), NULL);
    44 	return 1;
    45 }
    46 
    47 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    48 {
    49 	int index = params[1];
    50 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    51 	{
    52 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    53 	}
    54 
    55 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    56 	if (!pPlayer->IsPlayerConnected())
    57 	{
    58 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    59 	}
    60 
    61 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->PlayerIP());
    62 	return 1;
    63 }
    64 
    65 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    66 {
    67 	int index = params[1];
    68 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    69 	{
    70 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    71 	}
    72 
    73 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
    74 	if (!pPlayer->IsPlayerConnected())
    75 	{
    76 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    77 	}
    78 
    79 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->PlayerAuthString());
    80 	return 1;
    81 }
    82 
    83 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
    84 {
    85 	int index = params[1];
    86 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    87 	{
    88 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    89 	}
    90 
    91 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerConnected()) ? 1 : 0;
    92 }
    93 
    94 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
    95 {
    96 	int index = params[1];
    97 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
    98 	{
    99 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   100 	}
   101 
   102 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerInGame()) ? 1 : 0;
   103 }
   104 
   105 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   106 {
   107 	int index = params[1];
   108 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   109 	{
   110 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   111 	}
   112 
   113 	return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerAuthorized()) ? 1 : 0;
   114 }
   115 
   116 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   117 {
   118 	int index = params[1];
   119 	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
   120 	{
   121 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   122 	}
   123 
   124 	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
   125 	if (!pPlayer->IsPlayerConnected())
   126 	{
   127 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
   128 	}
   129 
   130 	return (pPlayer->IsPlayerFakeClient()) ? 1 : 0;
   131 }
   132 
   133 REGISTER_NATIVES(playernatives)
   134 {
   135 	{"GetMaxClients",			sm_GetMaxClients},
   136 	{"GetClientCount",			sm_GetClientCount},
   137 	{"GetClientName",			sm_GetClientName},
   138 	{"GetClientIP",				sm_GetClientIP},
   139 	{"GetClientAuthString",		sm_GetClientAuthStr},
   140 	{"IsPlayerConnected",		sm_IsPlayerConnected},
   141 	{"IsPlayerInGame",			sm_IsPlayerIngame},
   142 	{"IsPlayerAuthorized",		sm_IsPlayerAuthorized},
   143 	{"IsPlayerFakeClient",		sm_IsPlayerFakeClient},
   144 	{NULL,						NULL}
   145 };