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