core/smn_player.cpp
author David Anderson <dvander@alliedmods.net>
Mon Feb 05 20:32:34 2007 +0000 (2007-02-05)
changeset 435 3097c4ab9f8f
parent 370 d3a484f98f6d
child 448 25984ddd0ce0
permissions -rw-r--r--
heavily cleaned up the player API
exposed player API as an interface
added client event listeners to API
dvander@370
     1
/**
dvander@370
     2
 * ===============================================================
dvander@370
     3
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     4
 * ===============================================================
dvander@370
     5
 *
dvander@370
     6
 * This file is not open source and may not be copied without explicit
dvander@370
     7
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     8
 * in whole or significant part.
dvander@370
     9
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    10
 *
dvander@370
    11
 * Version: $Id$
dvander@370
    12
 */
dvander@370
    13
faluco@242
    14
#include "CPlayerManager.h"
faluco@280
    15
#include "sm_stringutil.h"
faluco@242
    16
faluco@260
    17
static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
faluco@242
    18
{
faluco@242
    19
	if (params[1])
faluco@242
    20
	{
dvander@435
    21
		return g_PlayerManager.NumPlayers();
faluco@242
    22
	}
faluco@242
    23
dvander@435
    24
	int maxplayers = g_PlayerManager.MaxClients();
faluco@242
    25
	int count = 0;
faluco@242
    26
	for (int i=1; i<=maxplayers; ++i)
faluco@242
    27
	{
faluco@242
    28
		CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(i);
dvander@435
    29
		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
faluco@242
    30
		{
faluco@242
    31
			count++;
faluco@242
    32
		}
faluco@242
    33
	}
faluco@242
    34
dvander@435
    35
	return (g_PlayerManager.NumPlayers() + count);
faluco@242
    36
}
faluco@242
    37
faluco@260
    38
static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
faluco@242
    39
{
dvander@435
    40
	return g_PlayerManager.MaxClients();
faluco@242
    41
}
faluco@242
    42
faluco@260
    43
static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
faluco@242
    44
{
faluco@242
    45
	int index = params[1];
faluco@242
    46
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
    47
	{
faluco@260
    48
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    49
	}
faluco@242
    50
faluco@260
    51
	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
dvander@435
    52
	if (!pPlayer->IsConnected())
faluco@260
    53
	{
faluco@260
    54
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    55
	}
faluco@260
    56
dvander@435
    57
	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
faluco@242
    58
	return 1;
faluco@242
    59
}
faluco@242
    60
faluco@260
    61
static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
faluco@242
    62
{
faluco@242
    63
	int index = params[1];
faluco@242
    64
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
    65
	{
faluco@260
    66
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    67
	}
faluco@242
    68
faluco@260
    69
	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
dvander@435
    70
	if (!pPlayer->IsConnected())
faluco@260
    71
	{
faluco@260
    72
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    73
	}
faluco@260
    74
faluco@280
    75
	char buf[64], *ptr;
dvander@435
    76
	strcpy(buf, pPlayer->GetIPAddress());
faluco@280
    77
faluco@280
    78
	if (params[4] && (ptr = strchr(buf, ':')))
faluco@280
    79
	{
faluco@280
    80
		*ptr = '\0';
faluco@280
    81
	}
faluco@280
    82
faluco@280
    83
	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
faluco@242
    84
	return 1;
faluco@242
    85
}
faluco@242
    86
faluco@260
    87
static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
faluco@242
    88
{
faluco@242
    89
	int index = params[1];
faluco@242
    90
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
    91
	{
faluco@260
    92
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    93
	}
faluco@242
    94
faluco@260
    95
	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
dvander@435
    96
	if (!pPlayer->IsConnected())
faluco@260
    97
	{
faluco@260
    98
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    99
	}
faluco@260
   100
dvander@435
   101
	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
faluco@242
   102
	return 1;
faluco@242
   103
}
faluco@242
   104
faluco@260
   105
static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
faluco@242
   106
{
faluco@242
   107
	int index = params[1];
faluco@242
   108
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
   109
	{
faluco@260
   110
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   111
	}
faluco@242
   112
dvander@435
   113
	return (g_PlayerManager.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
faluco@242
   114
}
faluco@242
   115
faluco@260
   116
static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
faluco@242
   117
{
faluco@242
   118
	int index = params[1];
faluco@242
   119
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
   120
	{
faluco@260
   121
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   122
	}
faluco@242
   123
dvander@435
   124
	return (g_PlayerManager.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
faluco@242
   125
}
faluco@242
   126
faluco@260
   127
static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
faluco@242
   128
{
faluco@242
   129
	int index = params[1];
faluco@242
   130
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
   131
	{
faluco@260
   132
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   133
	}
faluco@242
   134
dvander@435
   135
	return (g_PlayerManager.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
faluco@242
   136
}
faluco@242
   137
faluco@260
   138
static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
faluco@242
   139
{
faluco@242
   140
	int index = params[1];
faluco@242
   141
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@242
   142
	{
faluco@260
   143
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@260
   144
	}
faluco@260
   145
faluco@260
   146
	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
dvander@435
   147
	if (!pPlayer->IsConnected())
faluco@260
   148
	{
faluco@260
   149
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@242
   150
	}
faluco@242
   151
dvander@435
   152
	return (pPlayer->IsFakeClient()) ? 1 : 0;
faluco@260
   153
}
faluco@260
   154
faluco@280
   155
static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
faluco@280
   156
{
faluco@280
   157
	char buffer[1024];
faluco@280
   158
	char *fmt;
faluco@280
   159
	int arg = 2;
faluco@280
   160
faluco@280
   161
	pCtx->LocalToString(params[1], &fmt);
faluco@280
   162
	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
faluco@280
   163
faluco@280
   164
	buffer[res++] = '\n';
faluco@280
   165
	buffer[res] = '\0';
faluco@280
   166
faluco@280
   167
	META_CONPRINT(buffer);
faluco@280
   168
faluco@280
   169
	return 1;
faluco@280
   170
}
faluco@280
   171
faluco@280
   172
static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
faluco@280
   173
{
faluco@280
   174
	int index = params[1];
faluco@280
   175
	if ((index < 1) || (index > g_PlayerManager.GetMaxClients()))
faluco@280
   176
	{
faluco@280
   177
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@280
   178
	}
faluco@280
   179
faluco@280
   180
	CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index);
dvander@435
   181
	if (!pPlayer->IsInGame())
faluco@280
   182
	{
faluco@280
   183
		return pCtx->ThrowNativeError("Client %d is not in game.", index);
faluco@280
   184
	}
faluco@280
   185
faluco@280
   186
	char buffer[1024];
faluco@280
   187
	char *fmt;
faluco@280
   188
	int arg = 3;
faluco@280
   189
faluco@280
   190
	pCtx->LocalToString(params[2], &fmt);
faluco@280
   191
	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
faluco@280
   192
faluco@280
   193
	buffer[res++] = '\n';
faluco@280
   194
	buffer[res] = '\0';
faluco@280
   195
dvander@435
   196
	engine->ClientPrintf(pPlayer->GetEdict(), buffer);
faluco@280
   197
faluco@280
   198
	return 1;
faluco@280
   199
}
faluco@280
   200
faluco@260
   201
REGISTER_NATIVES(playernatives)
faluco@260
   202
{
faluco@260
   203
	{"GetMaxClients",			sm_GetMaxClients},
faluco@260
   204
	{"GetClientCount",			sm_GetClientCount},
faluco@260
   205
	{"GetClientName",			sm_GetClientName},
faluco@260
   206
	{"GetClientIP",				sm_GetClientIP},
faluco@260
   207
	{"GetClientAuthString",		sm_GetClientAuthStr},
faluco@260
   208
	{"IsPlayerConnected",		sm_IsPlayerConnected},
faluco@260
   209
	{"IsPlayerInGame",			sm_IsPlayerIngame},
faluco@260
   210
	{"IsPlayerAuthorized",		sm_IsPlayerAuthorized},
faluco@260
   211
	{"IsPlayerFakeClient",		sm_IsPlayerFakeClient},
faluco@280
   212
	{"PrintToServer",			sm_PrintToServer},
faluco@280
   213
	{"PrintToConsole",			sm_PrintToConsole},
faluco@260
   214
	{NULL,						NULL}
dvander@351
   215
};
dvander@351
   216