#include <sdktools>
#include <clientprefs>

public Plugin myinfo =
{
	name = "[CS:GO] KnifeAlert",
	author = "xstage",
	description = "Проигрывание звука при убийстве ножом",
	version = "2.1",
	url = "https://hlmod.ru/members/xstage.99505/"
};
	
Cookie		g_hCookie;
ArrayList	g_hList;
float		g_fSndVolume;
bool		g_bAlertChat, g_bAlertSound[MAXPLAYERS+1];

public void OnPluginStart()
{
	delete g_hList;

	g_hList = new ArrayList(ByteCountToCells(256));
	g_hCookie = new Cookie("g_iSoundAlert", "g_iSoundAlert", CookieAccess_Private); 

	RegConsoleCmd("sm_ka", AlertCmd);
	RegConsoleCmd("sm_knifealert", AlertCmd);
	RegConsoleCmd("sm_ksound", AlertCmd);
	
	RegAdminCmd("sm_ka_reload", ReloadConfig, ADMFLAG_ROOT);
	
	HookEvent("player_death", EventPlayerDeath);
	
	LoadConfig("configs/knife_alert.ini");
	
	for(int i = 1; i <= MaxClients; i++)
		if(IsClientInGame(i) && !IsFakeClient(i))
			OnClientCookiesCached(i);
}

public void OnMapStart()
{
	char sBuffer[PLATFORM_MAX_PATH], sSoundPath[PLATFORM_MAX_PATH / 2];	
	
	for (int i = 0; i < g_hList.Length; i++)
	{
		g_hList.GetString(i, sSoundPath, sizeof(sSoundPath));
		PrecacheSound(sSoundPath, true);
		
		FormatEx(sBuffer, sizeof(sBuffer), "sound/%s", sSoundPath);
		AddFileToDownloadsTable(sBuffer);
	}
}

public void OnClientCookiesCached(int client)
{
	char szValue[4];
	g_hCookie.Get(client, szValue, sizeof(szValue));
	
	g_bAlertSound[client] = szValue[0] ? view_as<bool>(StringToInt(szValue)):true;
}

Action AlertCmd(int client, int args)
{
	if(!IsClientInGame(client))
		return Plugin_Handled;
	
	if(g_bAlertSound[0])
	{
		g_bAlertSound[client] = !g_bAlertSound[client];
		PrintToChat(client, " [KnifeAlert] Вы (%s) звуковые оповещения", g_bAlertSound[client] ? "\x04Включили\x01":"\x02Выключили\x01");
	}
	else
	{
		PrintToChat(client, " [KnifeAlert] \x02Звуковые оповещения выключены сервером!");
	}
	
	return Plugin_Handled;
}

public Action EventPlayerDeath(Event hEvent, const char[] sEvent, bool dontBroadcast)
{
	char sWeapon[24];
	hEvent.GetString("weapon", sWeapon, sizeof(sWeapon));

	if((StrContains(sWeapon, "bayonet", false) != -1))
	{
		char sSound[PLATFORM_MAX_PATH];
		
		g_hList.GetString(GetRandomInt(0, g_hList.Length - 1), sSound, sizeof(sSound));
		
		int victim = GetClientOfUserId(hEvent.GetInt("userid"));
		int killer = GetClientOfUserId(hEvent.GetInt("attacker"));
		
		if(g_bAlertChat)
			{
				PrintToChatAll(" [刀战系统] %s%N\x01 撅了 %s%N", GetClientTeam(killer) == 2 ? "\x07" : "\x0B", killer, GetClientTeam(victim) == 2 ? "\x07" : "\x0B", victim);
				PrintCenterTextAll(" [刀战系统] %s%N\x01 撅了 %s%N", GetClientTeam(killer) == 2 ? "\x07" : "\x0B", killer, GetClientTeam(victim) == 2 ? "\x07" : "\x0B", victim);
			}
		for(int i = 1; i <= MaxClients; i++)
			if(IsClientInGame(i) && g_bAlertSound[i] && g_bAlertSound[0])
				EmitSoundToClient(i, sSound, _, SNDCHAN_STREAM, _, _, g_fSndVolume);
	}
	return Plugin_Continue;
}

public void OnClientDisconnect(int client)
{
	char szValue[4];
	IntToString(view_as<int>(g_bAlertSound[client]), szValue, sizeof(szValue));
	
	g_hCookie.Set(client, szValue);
}

public void OnPluginEnd()
{
	char szValue[4];

	for(int i = 1; i <= MaxClients; i++)
	{
		if(IsClientInGame(i) && !IsFakeClient(i))
		{
			IntToString(view_as<int>(g_bAlertSound[i]), szValue, sizeof(szValue));
			g_hCookie.Set(i, szValue);
		}
	}
}

Action ReloadConfig(int client, int args)
{
	LoadConfig("configs/knife_alert.ini");
	ReplyToCommand(client, "[KnifeAlert] Настройки были перезагружены!");
}

void LoadConfig(const char[] sDir)
{
	char sPath[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, sPath, sizeof(sPath), sDir);
	
	KeyValues hKeyValues = new KeyValues("KnifeAlert");
	
	if(!hKeyValues.ImportFromFile(sPath))
	{
		SetFailState("[KnifeAlert] :: Файл конфигураций не найден");
		return;
	}
	
	if(!hKeyValues.JumpToKey("soundpath"))
	{
		SetFailState("[KnifeAlert] :: Не найдены пути к звукам");
		return;
	}
	
	if(hKeyValues.GotoFirstSubKey(false))
	{
		do
		{
			hKeyValues.GetString(NULL_STRING, sPath, sizeof(sPath));
			g_hList.PushString(sPath);
		}
		while(hKeyValues.GotoNextKey(false));
	}
	
	hKeyValues.Rewind();
	
	g_bAlertChat = view_as<bool>(hKeyValues.GetNum("ka_alert_chat", 1));
	g_bAlertSound[0] = view_as<bool>(hKeyValues.GetNum("ka_alert_sound", 1));
	g_fSndVolume = (hKeyValues.GetNum("ka_volume", 50) * 1.0) / 100;

	delete hKeyValues;
}