Raised This Month: $32 Target: $400
 8% 

Help, players connected never updated


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JLmelenchon
Senior Member
Join Date: Mar 2019
Old 08-22-2022 , 12:27   Help, players connected never updated
Reply With Quote #1

I need the actual player count on server, but if someone disconnect it never update it like still considering we are 8 when in fact only 7 for example. Is the code wrong or missing something?

Code:
new Handle:cvar_HumansNeeded = INVALID_HANDLE;

public void OnPluginStart()
{
      cvar_HumansNeeded = CreateConVar("l4d_war_minhumans", "8", "Minimum humans players for war mode to be enabled", FCVAR_PLUGIN, true, 1.0, true, 4.0);
}

public OnClientDisconnect(client)
{
    if (IsClientBot(client))
		return;
	
    new maxplayers = GetMaxClients();

    for (new i = 1; i <= maxplayers; i++)
	{
		if (i != client && IsClientConnected(i) && IsClientInGame(i) && !IsClientBot(i))
			return;
	}
}

public bool BeforeDoAction(int client)
{
	if (CheckHumans())
	{
	    CPrintToChat(client, "%T", "fullteam_only", client);
	    return false;
	}
	return true;
}

public Action Cmd_StartSpecialEvent(int client, int args) 
{
    if (!BeforeDoAction(client)) return;
    
    // Check if a new vote is allowed to be called
    if (IsNewBuiltinVoteAllowed())
	{
		new iNumPlayers;
		decl iPlayers[MaxClients];
		for (new i=1; i<=MaxClients; i++)
		{
			if (!IsClientInGame(i) || IsFakeClient(i) || (GetClientTeam(i) == TEAM_SPECTATOR))
			{
				continue;
			}
			iPlayers[iNumPlayers++] = i;
		}
				
		char special_voteTitle[64];
		
		// Set vote title
		Format(special_voteTitle, 64, "Special mode on");
		
		// Start the vote!
		special_hVote = CreateBuiltinVote(VoteWarHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End);
		SetBuiltinVoteArgument(special_hVote, special_voteTitle);
		SetBuiltinVoteInitiator(special_hVote, client);
		SetBuiltinVoteResultCallback(special_hVote, SpecialVoteResultHandler);
		DisplayBuiltinVote(special_hVote, iPlayers, iNumPlayers, 20);
		FakeClientCommand(client, "Vote Yes");
		CPrintToChatAll("%t", "specialevent_alert", client);
		return;
	}
    
    CPrintToChat(client, "%T", "not_now", client);
}

Hbool:CheckHumans()
{
	new MinHumans = GetConVarInt(cvar_HumansNeeded);
	new Humans = 0;
	new maxplayers = GetMaxClients();

	for (new i = 1; i <= maxplayers; i++)
	{
		if (IsClientConnected(i) && IsClientInGame(i) && !IsClientBot(i))
		{
			Humans++;
		}
	}

	if (Humans < MinHumans)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool:IsClientBot(client)
{
	if (client == 0 || !IsClientConnected(client) || IsFakeClient(client))
	{
		return true;
	}

	decl String:steamId[64];
	GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
	
	if (StrEqual(steamId, "BOT", false))
	{
		return true;
	}

	return false;
}

Last edited by JLmelenchon; 08-22-2022 at 12:29.
JLmelenchon is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-22-2022 , 14:06   Re: Help, players connected never updated
Reply With Quote #2

where did you get that code? lot of old syntax, redundant checks, misleading variable names and method returns.
looks like missing code parts as well (how is Cmd_StartSpecialEvent called?)
__________________
Marttt is offline
JLmelenchon
Senior Member
Join Date: Mar 2019
Old 08-22-2022 , 15:56   Re: Help, players connected never updated
Reply With Quote #3

I just forgot to put here the regconsolecmd, this was just to show a part of plugin. Checkhuman / on clientdisconnect are where there is a mistake i suppose, i just copied it from an old stats plugins.

Last edited by JLmelenchon; 08-22-2022 at 15:56.
JLmelenchon is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-22-2022 , 17:12   Re: Help, players connected never updated
Reply With Quote #4

sorry, but you should at least share a "valid" plugin code so people are able to recompile.

think about what is necessary to do in the current status:

Code:
people have to copy all your code, make it compilable first, and then later try to help you.
how could be so hard to attach a .sp file, why hide things that the plugin needs to run?

the current code also misleads the solution.

I don't know if you need a command that triggers the war mode or if should fire automatically on player connect/disconnect.

the code doesn't make much sense, copy paste things won't work naturally

If you had make what I said probably someone would have already helped you (which looks like a simple issue)
__________________
Marttt is offline
JLmelenchon
Senior Member
Join Date: Mar 2019
Old 08-25-2022 , 19:57   Re: Help, players connected never updated
Reply With Quote #5

So i was able to make something, but sometimes and i don't know at which point the count can potentially glitch. it showed 7 players when we were 8, then next map it fixed itself.

Code:
#include <sourcemod>
#include <sdktools>
#include <colors>

new hCount;
new Handle:CheckPlayerCountTimer;

public void OnPluginStart()
{
	hCount = 0;
	
	RegConsoleCmd( "sm_counthumans", Cmd_HumansDebug, "Debug command to check real player count");
}

public OnMapStart()
{
	CreateTimer(5.0, TimerOpeningMap);
}

public OnMapEnd()
{
    if(CheckPlayerCountTimer != INVALID_HANDLE)
        {
            // Stop it and clear handle
            KillTimer(CheckPlayerCountTimer);
            CheckPlayerCountTimer = INVALID_HANDLE;
        }
}

public OnClientConnected(client) 
{
    if (IsValidPlayer(client)) 
    {
        if (!IsFakeClient(client)) 
        {
            if (0 <= hCount) 
	        {
                hCount++;
            }
        }
    }
}

public OnClientDisconnect(client)
{
    if (IsValidPlayer(client)) 
    {
        if (!IsFakeClient(client)) 
        {
            if(0 < hCount)
                hCount--;
            else hCount = 0;
            
        }
    }
}

public Action:CheckPlayerCount(Handle:timer, any:client)
{
    new real = 0;
    for (new i = 1; i <= MaxClients; i++)
    {
        if(IsClientInGame(i)) {
            if(!IsFakeClient(i) && GetClientTeam(i) != 1)
                real++;
        }
    }
    hCount = real;
}

public Action:Cmd_HumansDebug(client, args)
{
    if (args < 1)
	{
		CPrintToChat(client, "Player Count: %i", hCount, client);
		return Plugin_Handled;
	}
    return Plugin_Handled;
} 


public Action TimerOpeningMap(Handle timer)
{
        RankedSlotRefresh();
}

stock RankedSlotRefresh ( client = -1 )
{
    if(CheckPlayerCountTimer != INVALID_HANDLE)
    {
        KillTimer(CheckPlayerCountTimer);
        CheckPlayerCountTimer = INVALID_HANDLE;
    }

    // Create timer and store it in handle for later use.
    CheckPlayerCountTimer = CreateTimer(0.5, CheckPlayerCount, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}

static bool:IsValidPlayer(client) 
{
    if (0 < client <= MaxClients)
        return true;
    return false;
}
Maybe i should add that onclientdisconnect because when players join/leave they replace bots :
Code:
decl String:steamId[64];
GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
if (strcmp(steamId, "BOT") == 0) return;
!isfakeclient should do the job but you never know

Last edited by JLmelenchon; 08-25-2022 at 20:39.
JLmelenchon is offline
JLmelenchon
Senior Member
Join Date: Mar 2019
Old 08-25-2022 , 22:33   Re: Help, players connected never updated
Reply With Quote #6

I think it might start to bug when player is moved in spectator for a reason or another, because they are not counted as real human apparently. I don't know i played another game and it worked as intended. Should i reset the hcount on map end?

Last edited by JLmelenchon; 08-26-2022 at 05:12.
JLmelenchon is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 09-03-2022 , 17:23   Re: Help, players connected never updated
Reply With Quote #7

Don't overcomplicate your life!

PHP Code:
int g_iPlayerCount;

public 
void OnClientConnected(client
{
    
UpdatePlayerCount();
}

public 
void OnClientDisconnect_Post(client)
{
    
UpdatePlayerCount();
}

void UpdatePlayerCount()
{
    
g_iPlayerCount 0;
    for (
int i 1<= MaxClientsi++)
    {
        if (!
IsClientInGame(i) || IsFakeClient(i) || GetClientTeam(i) <= 1)
            continue;
        
        
g_iPlayerCount++;
    }

g_iPlayerCount holds the player count, it only includes fully connected players, humans (no bots), and players that are either in team 2 or 3 (terrorist, counterterrorist in csgo/css)
__________________
GitHub | Discord: @azalty | Steam
azalty is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:08.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode