Fix GetClientCount(false)

This commit is contained in:
BotoX 2019-09-29 18:27:19 +02:00
parent 64b3c8fbd8
commit 60f3268236
4 changed files with 47 additions and 15 deletions

View File

@ -212,6 +212,9 @@ void PlayerManager::OnSourceModAllInitialized()
SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CmdMaxplayersCallback), true); SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CmdMaxplayersCallback), true);
maxplayersCmd = pCmd; maxplayersCmd = pCmd;
} }
gameevents->AddListener(this, "player_connect", true);
gameevents->AddListener(this, "player_disconnect", true);
} }
void PlayerManager::OnSourceModShutdown() void PlayerManager::OnSourceModShutdown()
@ -259,6 +262,8 @@ void PlayerManager::OnSourceModShutdown()
{ {
SH_REMOVE_HOOK(ConCommand, Dispatch, maxplayersCmd, SH_STATIC(CmdMaxplayersCallback), true); SH_REMOVE_HOOK(ConCommand, Dispatch, maxplayersCmd, SH_STATIC(CmdMaxplayersCallback), true);
} }
gameevents->RemoveListener(this);
} }
ConfigResult PlayerManager::OnSourceModConfigChanged(const char *key, ConfigResult PlayerManager::OnSourceModConfigChanged(const char *key,
@ -1424,6 +1429,11 @@ int PlayerManager::GetNumPlayers()
return m_PlayerCount; return m_PlayerCount;
} }
int PlayerManager::GetNumClients()
{
return m_ClientCount;
}
int PlayerManager::GetClientOfUserId(int userid) int PlayerManager::GetClientOfUserId(int userid)
{ {
if (userid < 0 || userid > USHRT_MAX) if (userid < 0 || userid > USHRT_MAX)
@ -2019,6 +2029,27 @@ bool PlayerManager::HandleConVarQuery(QueryCvarCookie_t cookie, int client, EQue
} }
#endif #endif
/* IGameEventListener2::FireGameEvent */
void PlayerManager::FireGameEvent(IGameEvent *pEvent)
{
const char *name = pEvent->GetName();
if (strcmp(name, "player_connect") == 0)
{
const int client = pEvent->GetInt("index") + 1;
const int userid = pEvent->GetInt("userid");
m_ClientCount++;
}
else if (strcmp(name, "player_disconnect") == 0)
{
const int userid = pEvent->GetInt("userid");
const int client = m_UserIdLookUp[userid];
m_ClientCount--;
}
}
/******************* /*******************
*** PLAYER CODE *** *** PLAYER CODE ***

View File

@ -159,7 +159,8 @@ private:
class PlayerManager : class PlayerManager :
public SMGlobalClass, public SMGlobalClass,
public IPlayerManager public IPlayerManager,
public IGameEventListener2
{ {
friend class CPlayer; friend class CPlayer;
public: public:
@ -202,6 +203,7 @@ public: //IPlayerManager
IGamePlayer *GetGamePlayer(edict_t *pEdict); IGamePlayer *GetGamePlayer(edict_t *pEdict);
int GetMaxClients(); int GetMaxClients();
int GetNumPlayers(); int GetNumPlayers();
int GetNumClients();
int GetClientOfUserId(int userid); int GetClientOfUserId(int userid);
bool IsServerActivated(); bool IsServerActivated();
int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags); int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags);
@ -212,6 +214,8 @@ public: //IPlayerManager
int GetClientFromSerial(unsigned int serial); int GetClientFromSerial(unsigned int serial);
void ClearAdminId(AdminId id); void ClearAdminId(AdminId id);
void RecheckAnyAdmins(); void RecheckAnyAdmins();
public: // IGameEventListener2
void FireGameEvent(IGameEvent *pEvent);
public: public:
inline int MaxClients() inline int MaxClients()
{ {
@ -272,6 +276,7 @@ private:
int m_SourceTVUserId; int m_SourceTVUserId;
int m_ReplayUserId; int m_ReplayUserId;
bool m_bInCCKVHook; bool m_bInCCKVHook;
int m_ClientCount;
private: private:
static const int NETMSG_TYPE_BITS = 5; // SVC_Print overhead for netmsg type static const int NETMSG_TYPE_BITS = 5; // SVC_Print overhead for netmsg type
static const int SVC_Print_BufferSize = 2048 - 1; // -1 for terminating \0 static const int SVC_Print_BufferSize = 2048 - 1; // -1 for terminating \0

View File

@ -270,18 +270,7 @@ static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
return playerhelpers->GetNumPlayers(); return playerhelpers->GetNumPlayers();
} }
int maxplayers = playerhelpers->GetMaxClients(); return playerhelpers->GetNumClients();
int count = 0;
for (int i = 1; i <= maxplayers; ++i)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(i);
if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
{
count++;
}
}
return (playerhelpers->GetNumPlayers() + count);
} }
static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params) static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)

View File

@ -41,7 +41,7 @@
#include <IAdminSystem.h> #include <IAdminSystem.h>
#define SMINTERFACE_PLAYERMANAGER_NAME "IPlayerManager" #define SMINTERFACE_PLAYERMANAGER_NAME "IPlayerManager"
#define SMINTERFACE_PLAYERMANAGER_VERSION 21 #define SMINTERFACE_PLAYERMANAGER_VERSION 22
struct edict_t; struct edict_t;
class IPlayerInfo; class IPlayerInfo;
@ -543,10 +543,17 @@ namespace SourceMod
/** /**
* @brief Returns the number of players currently connected. * @brief Returns the number of players currently connected.
* *
* @return Current number of connected clients. * @return Current number of connected players.
*/ */
virtual int GetNumPlayers() =0; virtual int GetNumPlayers() =0;
/**
* @brief Returns the number of clients currently connected.
*
* @return Current number of connected clients.
*/
virtual int GetNumClients() =0;
/** /**
* @brief Returns the client index by its userid. * @brief Returns the client index by its userid.
* *