#include <sourcemod>
#include <SteamWorks>
#include <NoSteamDetection>

#pragma semicolon 1
#pragma newdecls required

bool g_bHasFakeClient[MAXPLAYERS + 1] = {false,...};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name        = "NoSteamPlayerCount",
	author      = "Neon",
	description = "",
	version     = "1.0",
	url         = "https://steamcommunity.com/id/n3ontm"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnGameFrame()
{
	SteamWorks_SetMaxPlayers(65);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegAdminCmd("sm_addfake", Command_AddFake, ADMFLAG_ROOT, "");
	RegAdminCmd("sm_removefake", Command_RemoveFake, ADMFLAG_ROOT, "");
	RegAdminCmd("sm_countfakes", Command_CountFakes, ADMFLAG_BAN, "");

	for(int client = 1; client <= MaxClients; client++)
	{
		if(IsValidClient(client) && IsClientProfileValidated(client))
			OnClientProfileValidated(client, false);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginEnd()
{
	for(int client = 1; client <= MaxClients; client++)
	{
		if(IsValidClient(client))
			OnClientDisconnect(client);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_AddFake(int client, int argc)
{
	SteamWorks_CreateFake("Kaitou Sinbad");
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_RemoveFake(int client, int argc)
{
	SteamWorks_KickFake();
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_CountFakes(int client, int argc)
{
	int iFakes = SteamWorks_CountFakes();
	ReplyToCommand(client, "There are currently %d Fake Clients active.", iFakes);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientProfileValidated(int client, bool valid)
{
	char sName[128];
	GetClientName(client, sName, sizeof(sName));

	if(!IsClientProfileValid(client))
	{
		int iFakeID = SteamWorks_CreateFake(sName);
		g_bHasFakeClient[client] = true;
		LogMessage("\"%L\" connected as NoSteam. Fake Client with ID: \"%d\" got created.", client, iFakeID);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	if (!g_bHasFakeClient[client])
		return;

	SteamWorks_KickFake();
	g_bHasFakeClient[client] = false;
	LogMessage("\"%L\" left as NoSteam. Fake Client got removed.", client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public bool IsValidClient(int client)
{
	if (client <= 0)
		return false;

	if (client > GetMaxClients())
		return false;

	return true;
}