#include <sourcemod>

#pragma newdecls required
#pragma	semicolon 1

char g_sLogFile[PLATFORM_MAX_PATH];
char g_sLogFileRecording[PLATFORM_MAX_PATH];
bool g_bEvil[MAXPLAYERS + 1];

bool g_bRecording;

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	BuildPath(Path_SM, g_sLogFile, sizeof(g_sLogFile), "logs/exploiter.txt");
	BuildPath(Path_SM, g_sLogFileRecording, sizeof(g_sLogFileRecording), "logs/lag_recording.txt");

	for(int client = 1; client <= MaxClients; client++)
	{
		if(IsValidClient(client) && IsClientAuthorized(client))
		{
			char sAuthID[32];
			GetClientAuthId(client, AuthId_Steam2, sAuthID, sizeof(sAuthID));

			OnClientAuthorized(client, sAuthID);
		}

	}
	AddCommandListener(Command_CommandListener);

	RegAdminCmd("sm_lag", Command_Record, ADMFLAG_KICK);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_CommandListener(int client, const char[] command,int argc)
{
	if (g_bRecording)
	{
		char sBuffer[1024];
		GetCmdArgString(sBuffer, sizeof(sBuffer));
		LogToFile(g_sLogFileRecording, "%L: [%s] [%s]", client, command, sBuffer);
	}
	else
	{
		if (!IsValidClient(client))
			return Plugin_Continue;

		if (!IsClientAuthorized(client))
			return Plugin_Continue;

		if (!g_bEvil[client])
			return Plugin_Continue;

		char sBuffer[1024];
		GetCmdArgString(sBuffer, sizeof(sBuffer));

		LogToFile(g_sLogFile, "%L: [%s] [%s]", client, command, sBuffer);
	}
	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_Record(int client, int args)
{
	if (g_bRecording)
	{
		ReplyToCommand(client, "[SM] Lag is already being recorded.");
		return Plugin_Handled;
	}

	g_bRecording = true;
	CreateTimer(30.0, StopRecording);

	LogAction(client, -1, "\"%L\" initiated a Lag Recording", client);
	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsClientInGame(i) && !IsFakeClient(i) && CheckCommandAccess(i, "sm_lag", ADMFLAG_GENERIC))
			PrintToChat(i, "[SM] Lag Recording has been started. Recording all client commands for the next 30s.");
	}

	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action StopRecording(Handle timer)
{
	g_bRecording = false;

	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsClientInGame(i) && !IsFakeClient(i) && CheckCommandAccess(i, "sm_lag", ADMFLAG_GENERIC))
			PrintToChat(i, "[SM] Lag Recording has been stopped.");
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientAuthorized(int client, const char[] sAuthID)
{
	if (IsValidClient(client) && StrEqual(sAuthID, "STEAM_0:1:439160990"))
		g_bEvil[client] = true;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bEvil[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int IsValidClient(int client, bool nobots = true)
{
	if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
		return false;

	return IsClientInGame(client);
}
