#include <cstrike>
#include <hlstatsx_loghelper>
#include <sourcemod>
#include <zombiereloaded>

#pragma semicolon 1
#pragma newdecls required

/* CONVARS */
ConVar g_cvarStreakInterval = null;
ConVar g_cvarMinimumStreak = null;
ConVar g_cvarMaximumStreak = null;

ConVar g_cvarDifficultyHuman = null;
ConVar g_cvarDifficultyZombie = null;
ConVar g_cvarDifficultyDuration = null;

/* HANDLES */
Handle g_tMinimalRoundDuration = INVALID_HANDLE;

/* INTERGERS */
int g_iKillCount[MAXPLAYERS+1] = 0;

/* BOOLEANS */
bool g_bIsHuman[MAXPLAYERS+1] = false;
bool g_bIsZombie[MAXPLAYERS+1] = false;
bool g_bMotherZM[MAXPLAYERS+1] = false;

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name        = "HLstatsX CE - Zombie Escape",
	author      = "zaCade",
	description = "Create additional actions for the default HLstatsX",
	version     = "1.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	g_cvarStreakInterval     = CreateConVar("hlx_ze_killstreaks_interval", "1",  "amount of kills required to progress to the next killstreak", 0, true, 1.0);
	g_cvarMinimumStreak      = CreateConVar("hlx_ze_killstreaks_minimal",  "2",  "amount of kills required for the lowest killstreak",          0, true, 0.0);
	g_cvarMaximumStreak      = CreateConVar("hlx_ze_killstreaks_maximal",  "12", "amount of kills required for the highest killstreak",         0, true, 0.0);

	g_cvarDifficultyHuman    = CreateConVar("hlx_ze_difficulty_human",     "0",  "the difficulty level of the current map for humans",          0, true, 0.0);
	g_cvarDifficultyZombie   = CreateConVar("hlx_ze_difficulty_zombie",    "0",  "the difficulty level of the current map for zombies",         0, true, 0.0);
	g_cvarDifficultyDuration = CreateConVar("hlx_ze_difficulty_duration",  "60", "the minumum amount of time a round has to last in seconds",   0, true, 0.0, true, 180.0);

	HookEvent("round_start",  OnRoundStarting, EventHookMode_Pre);
	HookEvent("round_end",    OnRoundEnding,   EventHookMode_Pre);
	HookEvent("player_spawn", OnClientSpawn,   EventHookMode_Pre);
	HookEvent("player_death", OnClientDeath,   EventHookMode_Pre);

	for (int client = 1; client <= MaxClients; client++)
	{
		if (IsValidClient(client))
		{
			g_bIsHuman[client]  = ZR_IsClientHuman(client);
			g_bIsZombie[client] = ZR_IsClientZombie(client);
		}
	}

	AutoExecConfig();
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapEnd()
{
	if (g_tMinimalRoundDuration != INVALID_HANDLE && CloseHandle(g_tMinimalRoundDuration))
		g_tMinimalRoundDuration = INVALID_HANDLE;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRoundStarting(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	if (g_tMinimalRoundDuration != INVALID_HANDLE && CloseHandle(g_tMinimalRoundDuration))
		g_tMinimalRoundDuration = INVALID_HANDLE;

	g_tMinimalRoundDuration = CreateTimer(g_cvarDifficultyDuration.FloatValue, OnRoundMinimalDuration);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnRoundMinimalDuration(Handle timer)
{
	g_tMinimalRoundDuration = INVALID_HANDLE;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ZR_OnClientHumanPost(int client, bool respawn, bool protect)
{
	g_bIsHuman[client]  = true;
	g_bIsZombie[client] = false;
	g_bMotherZM[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ZR_OnClientInfected(int client, int attacker, bool motherInfect, bool respawnOverride, bool respawn)
{
	g_bIsHuman[client]  = false;
	g_bIsZombie[client] = true;
	g_bMotherZM[client] = motherInfect;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRoundEnding(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	bool bAwardHumans  = ((g_tMinimalRoundDuration == INVALID_HANDLE) && (hEvent.GetInt("winner") == CS_TEAM_CT));
	bool bAwardZombies = ((g_tMinimalRoundDuration == INVALID_HANDLE) && (hEvent.GetInt("winner") == CS_TEAM_T));

	int iAliveHumans;
	int iAliveZombies;

	for (int client = 1; client <= MaxClients; client++)
	{
		if (IsValidClient(client) && GetClientTeam(client) == CS_TEAM_CT)
		{
			iAliveHumans++;
		}
		else if(IsValidClient(client) && GetClientTeam(client) == CS_TEAM_T)
		{
			iAliveZombies++;
		}
	}

	for (int client = 1; client <= MaxClients; client++)
	{
		if (IsValidClient(client))
		{
			if (bAwardHumans && g_bIsHuman[client])
			{
				if (iAliveHumans == 1)
				{
					LH_LogPlayerEvent(client, "triggered", "ze_h_win_solo", true);
				}
				else
				{
					char sPlayerEvent[32];
					Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_h_win_%d", g_cvarDifficultyHuman.IntValue);

					LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
				}
			}
			else if (bAwardZombies && g_bIsZombie[client])
			{

				if (iAliveZombies == 1)
				{
					LH_LogPlayerEvent(client, "triggered", "ze_z_win_solo", true);
				}
				else if (g_bMotherZM[client])
				{
					char sPlayerEvent[32];
					Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_m_win_%d", g_cvarDifficultyZombie.IntValue);

					LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
				}
				else
				{
					char sPlayerEvent[32];
					Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_z_win_%d", g_cvarDifficultyZombie.IntValue);

					LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
				}
			}

			EndKillStreak(client);
		}
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientSpawn(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	int client = GetClientOfUserId(hEvent.GetInt("userid"));

	ResetClient(client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDeath(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	int client = GetClientOfUserId(hEvent.GetInt("userid"));
	int killer = GetClientOfUserId(hEvent.GetInt("attacker"));

	EndKillStreak(client);

	if (IsValidClient(killer))
	{
		g_iKillCount[killer] += 1;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void EndKillStreak(int client)
{
	if (g_iKillCount[client] >= g_cvarMinimumStreak.IntValue)
	{
		if (g_iKillCount[client] > g_cvarMaximumStreak.IntValue)
			g_iKillCount[client] = g_cvarMaximumStreak.IntValue;

		if (g_bIsHuman[client])
		{
			char sPlayerEvent[32];
			Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_h_kill_streak_%d", RoundToFloor(float(g_iKillCount[client]) / float(g_cvarStreakInterval.IntValue)) * g_cvarStreakInterval.IntValue);

			LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
		}
		else if (g_bIsZombie[client])
		{
			if (g_bMotherZM[client])
			{
				char sPlayerEvent[32];
				Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_m_kill_streak_%d", RoundToFloor(float(g_iKillCount[client]) / float(g_cvarStreakInterval.IntValue)) * g_cvarStreakInterval.IntValue);

				LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
			}
			else
			{
				char sPlayerEvent[32];
				Format(sPlayerEvent, sizeof(sPlayerEvent), "ze_z_kill_streak_%d", RoundToFloor(float(g_iKillCount[client]) / float(g_cvarStreakInterval.IntValue)) * g_cvarStreakInterval.IntValue);

				LH_LogPlayerEvent(client, "triggered", sPlayerEvent, true);
			}
		}
	}

	ResetClient(client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock void ResetClient(int client)
{
 	g_bIsHuman[client]   = true;
	g_bIsZombie[client]  = false;
	g_bMotherZM[client]  = false;
	g_iKillCount[client] = 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock bool IsValidClient(int client)
{
	return (client > 0 && client <= MaxClients && IsClientInGame(client) && IsPlayerAlive(client));
}