#pragma semicolon 1
#include <sourcemod>
#include <sdktools_sound>
#include <sdktools_stringtables>

#define CONFIG 0
#define HOOK_CONVAR_CHANGE 1

new Handle:	g_hConVar_sSound;
new Handle:	g_hConVar_bOnlyForAdmin;
new	Handle:	g_hConVar_sRequiredFlags;
new Handle:	g_hConVar_fSoundVolume;

new String:	g_sSound[PLATFORM_MAX_PATH];
new Float:	g_fSoundVolume;
new bool:	g_bOnlyForAdmin;
new			g_iRequieredFlagBits;

public OnPluginStart()
{
	AddCommandListener(Listener_Say, 		"say");
	AddCommandListener(Listener_SayTeam, 	"say_team");
	
	g_hConVar_sSound 			= CreateConVar("sm_saysound_sound", 			"buttons/bell1.wav", 	"звук, который будет проигрываться при написании сообщения в чат");
	g_hConVar_bOnlyForAdmin 	= CreateConVar("sm_saysound_onlyforadmin", 		"0", 					"проигрывать звук при сообщении только от администраторов (1) или от всех (0)", _, true, 0.0, true, 1.0);
	g_hConVar_sRequiredFlags 	= CreateConVar("sm_saysound_requeiredflags", 	"b", 					"если только для администратора, то каким флагом(ами) он должен обладать");
	g_hConVar_fSoundVolume      = CreateConVar("sm_saysound_soundvolume",		"0.10",					"громкость проигрываемового звука", _, true, 0.0, true, 1.0);
	
#if CONFIG == 1
	AutoExecConfig(true, "saysound");
#endif
}

public OnConfigsExecuted()
{
	decl String:sReqFlag[16];
	GetConVarString(g_hConVar_sRequiredFlags, sReqFlag, sizeof(sReqFlag) - 1);
	g_iRequieredFlagBits = ReadFlagString(sReqFlag);

	GetConVarString(g_hConVar_sSound, g_sSound, sizeof(g_sSound) - 1);
	PrecacheSound(g_sSound);
	
	decl String:sSoundToTable[PLATFORM_MAX_PATH];
	Format(sSoundToTable, sizeof(sSoundToTable) - 1, "sound/%s", g_sSound);
	AddFileToDownloadsTable(sSoundToTable);
	
	g_bOnlyForAdmin = GetConVarBool	(g_hConVar_bOnlyForAdmin);
	g_fSoundVolume	= GetConVarFloat(g_hConVar_fSoundVolume);
	
#if HOOK_CONVAR_CHANGE == 1
	HookConVarChange(g_hConVar_sRequiredFlags, 	ConVar_Callback);
	HookConVarChange(g_hConVar_sSound, 			ConVar_Callback);
	HookConVarChange(g_hConVar_bOnlyForAdmin, 	ConVar_Callback);
	HookConVarChange(g_hConVar_fSoundVolume, 	ConVar_Callback);
#endif
}

#if HOOK_CONVAR_CHANGE == 1
public ConVar_Callback(Handle:hConVar, const String:sOldVal[], const String:sNewVal[])
{
	if ( hConVar == g_hConVar_sRequiredFlags )
	{
		decl String:sReqFlag[16];
		GetConVarString(g_hConVar_sRequiredFlags, sReqFlag, sizeof(sReqFlag) - 1);
		g_iRequieredFlagBits = ReadFlagString(sReqFlag);
	}
	else if ( hConVar == g_hConVar_sSound )
	{
		GetConVarString(g_hConVar_sSound, g_sSound, sizeof(g_sSound) - 1);
		PrecacheSound(g_sSound);
		
		decl String:sSoundToTable[PLATFORM_MAX_PATH];
		Format(sSoundToTable, sizeof(sSoundToTable) - 1, "sound/%s", g_sSound);
		AddFileToDownloadsTable(sSoundToTable);
	}
	else if ( hConVar == g_hConVar_bOnlyForAdmin )
	{
		g_bOnlyForAdmin = GetConVarBool	(g_hConVar_bOnlyForAdmin);
	}
	else if ( hConVar == g_hConVar_fSoundVolume )
	{
		g_fSoundVolume	= GetConVarFloat(g_hConVar_fSoundVolume);
	}
}
#endif

public Action:Listener_Say(client, const String:command[], argc)
{
	if ( g_bOnlyForAdmin )
	{
		if ( !CheckCommandAccess(client, "saysound_override", g_iRequieredFlagBits) )
		{
			return Plugin_Continue;
		}
	}
	
	EmitSoundToAll(g_sSound, SOUND_FROM_PLAYER, SNDCHAN_AUTO, SNDLEVEL_NORMAL, SND_NOFLAGS, g_fSoundVolume);
	return Plugin_Continue;
}

public Action:Listener_SayTeam(client, const String:command[], argc)
{
	if ( g_bOnlyForAdmin )
	{
		if ( !CheckCommandAccess(client, "saysound_override", g_iRequieredFlagBits) )
		{
			return Plugin_Continue;
		}
	}
	
	new team = GetClientTeam(client),
		array[MaxClients], count = 0;
	
	for ( new i = 1; i <= MaxClients; i++ )
		if ( IsClientInGame(i) && GetClientTeam(i) == team )
			array[count++] = i;
	
	EmitSound(array, count, g_sSound, SOUND_FROM_PLAYER, SNDCHAN_AUTO, SNDLEVEL_NORMAL, SND_NOFLAGS, g_fSoundVolume);
	return Plugin_Continue;
}

public Plugin:myinfo = 
{
	name 		= "[ Say Sound ]",
	author 		= "Regent",
	description = "allow you to add custom sound when chat message recieved",
	version 	= "1.0",
	url 		= ""
};