/*

MessageSound 2 !

Version: 2.1.1

CyberGuerrier


http://amx.cyberguerrier.info/MessageSound/


Thanks to Arkshine for many tests and bugs report

*/


#define MS_AMXX 			//comment this line for AMX

//#define AMX99				//uncomment this line for AMX 0.99

#define MS_ADMIN_LEVEL ADMIN_SLAY 	//Admin level required to use 
					//the msound_play command

#define MS_CSMOD			//must be defined for MS_MOTD_LIST
					//under CStrike or other mod use HTML MOTD


//================= DO NOT MODIFY ANYTHING AFTER THIS LINE !! ========================


#if defined MS_AMXX
    	#include <amxmodx>
        #include <engine>	/* precache_generic */
	#include <cstrike>	/* money */
#else
	#include <translator> 	/* translations */
        #include <amxmod>
        #include <VexdUM>  	/* precache_generic */
	#include <fun>		/* money */
#endif

#include <amxmisc>
#include <string>
#include <file>

#pragma semicolon 1

/* Plugin Infos */
#define PNAME "MessageSound"
#define PVERS "2.1"
#define PAUTH "CyberGuerrier"

new msenabled, mslistenabled, msadminonly, msadminchat, msmaxsound, 
msusercontrol, msmasksay, mscredit, mscreditprice, mslistcols,
mstimerenabled, mstimerplay;

new pSound[32]; 	//how many sound played by a player ?
new pMSActive[32]; 	//MessageSound activate or not for this player ?
new pAdminSounds[500];	//reserved sounds for admin
new pASoundsNum=0;	//how many sounds as reserved ?
new pPlayedTime[32];	//time the player as played

#if defined MS_AMXX
new msadminfile[] = "addons/amxmodx/configs/msound.ini";
new msadminhelp[] = "addons/amxmodx/configs/msound_adminhelp.txt";
new mscvarhelp[]  = "addons/amxmodx/configs/msound_cvarhelp.txt";
new msuserhelp[]  = "addons/amxmodx/configs/msound_userhelp.txt";
#else
new msadminfile[] = "addons/amx/config/msound.ini";
new msadminhelp[] = "addons/amx/config/msound_adminhelp.txt";
new mscvarhelp[]  = "addons/amx/config/msound_cvarhelp.txt";
new msuserhelp[]  = "addons/amx/config/msound_userhelp.txt";
#endif

enum
{
	NOSOUND,
	SOUND
};


#if defined AMX99
#if !defined MS_AMXX
stock is_user_admin(id) {
  return (get_user_flags(id) > 0 && !(get_user_flags(id) & ADMIN_USER));
}
#endif
#endif


public isSound(sound[])
{
	new wsnd[500];
	new msnd[500];
	copy(wsnd, 500,"sound/msound/");
	add(wsnd, 500, sound);
	add(wsnd, 500, ".wav");
	copy(msnd, 500, "sound/msound/");
	add(msnd, 500, sound);
	add(msnd, 500, ".mp3");
	if(file_exists(wsnd) || file_exists(msnd))
		return SOUND;

	return NOSOUND;
}

public addsound(id,sound[])
{
	new exist=0;

	if(file_exists(msadminfile))
	{
		new line, fic[255], len;
		
		while((line=read_file(msadminfile,line,fic,255,len))!=0)
		{ 
			if(equal(fic, sound))
				exist = 1;
		}
	}

	if(exist != 1)
	{
		write_file(msadminfile,sound,-1);
		loadsound(); //reload sound
		#if defined MS_AMXX
		client_print(id, print_console, "[MSOUND] %L", LANG_PLAYER, "SOUND_SUCCESSFULY_ADDED");
		#else
		client_print(id, print_console, _T("[MSOUND] Sound successfuly added in the list of reserved sound"));
		#endif
	}
	else
	{
		#if defined MS_AMXX
		client_print(id, print_console, "[MSOUND] %L", LANG_PLAYER, "SOUND_ALREADY_RESERVED");
		#else
		client_print(id, print_console, _T("[MSOUND] This sound is already in reserved sound list"));
		#endif
	}
}

public loadsound()
{
	new i = 0;
	for(i = 0; i < pASoundsNum; i++)
	{
		copy(pAdminSounds[i], 500, "");
	}

	pASoundsNum = 0;

	if(file_exists(msadminfile))
	{
		new line, fic[255], len;
			
		while((line=read_file(msadminfile,line,fic,255,len))!=0)
		{ 
			copy(pAdminSounds[pASoundsNum], 500, fic);
			pASoundsNum++;
		}
	}
}

public msound_addsound(id)
{
	if(!(get_user_flags(id) & MS_ADMIN_LEVEL))
		return PLUGIN_HANDLED;

	new sound[500];
	read_argv(1,sound,500);
	addsound(id,sound);

	return PLUGIN_HANDLED;
}

public client_connect(id)
{
	pSound[id] = 0;
	pMSActive[id] = 1;
	pPlayedTime[id] = 0;
}

public PlaySound(id, file[], Type[])
{
	pSound[id]++;

    	new cmdline[500];

        if(equali(Type, "WAV"))
        {
        	copy(cmdline, 500, "spk ");
            	add(cmdline, 500, file);
        }
        else if(equali(Type, "MP3"))
        {
            	copy(cmdline, 500, "mp3 play ");
            	add(cmdline, 500, file);
        }

        new buffer[48];

	new i;
	for(i = 0; i < 32; i++)
	{
		if(pMSActive[i] == 1)
		        client_cmd(i, cmdline, buffer);
	}

}


public SearchSoundFile(id, sound[])
{
        new wavfile[500];
        new mp3file[500];
    
        copy(wavfile, 500, "sound/msound/");
        add(wavfile, 500, sound);
        add(wavfile, 500, ".wav");

        if(file_exists(wavfile))
        {
	        PlaySound(id, wavfile, "WAV");
        }
        else
        {
        	copy(mp3file, 500, "sound/msound/");
         	add(mp3file, 500, sound);
            	add(mp3file, 500, ".mp3");

            	if(file_exists(mp3file))
            	{
                    	PlaySound(id, mp3file, "MP3");
            	}
    	}
}

public msound_onoff(id)
{
	msusercontrol = get_cvar_num("msound_user_control");

	if(msusercontrol == 1)
	{
		if(pMSActive[id] == 1)
		{
			pMSActive[id] = 0;
			//client_cmd(id, "stopsound"); //stop wav
			client_cmd(id, "mp3 stop"); //stop mp3

			#if defined MS_AMXX
			client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER, "U_MSOUND_IS_NOW_OFF");
			#else
			client_print(id, print_chat, _T("[MSOUND] MessageSound is now off for you"));
			#endif
		}
		else
		{
			pMSActive[id] = 1;
			#if defined MS_AMXX
			client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER, "U_MSOUND_IS_NOW_ON");
			#else
			client_print(id, print_chat, _T("[MSOUND] MessageSound is now on for you"));
			#endif
		}
	}
	else
	{
		#if defined MS_AMXX
		client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER, "ADMIN_DENY_COMMAND");
		#else
		client_print(id, print_chat, _T("[MSOUND] Admin as deny this command"));
		#endif
	}
}
	

public msound_play(id)
{

	if(! (get_user_flags(id) & MS_ADMIN_LEVEL) )
		return PLUGIN_HANDLED;

	msenabled = get_cvar_num("msound_enabled");

	if(msenabled == 1)
	{
            	new sound[32];
        	read_argv(1, sound, 32);
            	SearchSoundFile(id,sound);
        }
        else
        {
		#if defined MS_AMXX
            	client_cmd(id, "echo ^"[MSOUND] %L^"",LANG_PLAYER, "MSOUND_IS_DISABLED");
        	#else
		client_cmd(id, "echo ^"%s^"", _T("[MSOUND] MessageSound is disabled"));
		#endif
	}

	return PLUGIN_HANDLED;
}

public MS_BUY_CREDIT(id)
{
	mscredit = get_cvar_num("msound_credit");
	msmaxsound = get_cvar_num("msound_user_maxsound");

	if(msmaxsound == -1)
	{
		#if defined MS_AMXX
		client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"SOUND_ILLIMITED_BUY_USELESS");
		#else
		client_print(id, print_chat, _T("[MSOUND] The number of sounds is ilimited, it's useless to buy a sound"));
		#endif

		return PLUGIN_HANDLED;
	}

	if(mscredit == 1)
	{
		mscreditprice = get_cvar_num("msound_credit_price");

		#if defined MS_AMXX
		new current_money = cs_get_user_money(id);
		#else
		new current_money = get_user_money(id);
		#endif		

		if(current_money >= mscreditprice)
		{
			if(pSound[id] > 0)
			{
				pSound[id]--;

				#if defined MS_AMXX
				cs_set_user_money(id, (current_money - mscreditprice));
				#else
				set_user_money(id, (current_money - mscreditprice));
				#endif

				#if defined MS_AMXX
				client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"U_BUYED_NEW_SOUND");
				#else
				client_print(id, print_chat, _T("[MSOUND] You have buyed a new sound"));
				#endif
			}
			else
			{
				msmaxsound = get_cvar_num("msound_user_maxsound");

				#if defined MS_AMXX
				client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"U_ALREADY_MAX_SOUND", msmaxsound);
				#else
				client_print(id, print_chat, _T("[MSOUND] You can't have more %d sounds at the same time"), msmaxsound);
				#endif
			}
		}			
		else
		{
			#if defined MS_AMXX
			client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"U_ENOUGH_MONEY");	
			#else
			client_print(id, print_chat, _T("[MSOUND] You have enough money to buy a sound"));
			#endif
		}
		
	}
	else
	{
		#if defined MS_AMXX
		client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"CREDIT_DISABLED");
		#else
		client_print(id, print_chat, _T("[MSOUND] The credits is disable"));
		#endif
	}

	return PLUGIN_HANDLED;
}

public handle_say(id)
{
	msenabled = get_cvar_num("msound_enabled");
	msadminonly = get_cvar_num("msound_admin_only");
	msadminchat = get_cvar_num("msound_admin_chat");

       	new sound[32];
       	read_argv(1, sound, 32);

	if(msenabled == 1)
	{
		if(equal(sound, "/msound switch"))
		{
			msound_onoff(id);
			return PLUGIN_HANDLED;
		}
		else if(equal(sound, "/msound list"))
		{
			MS_MOTD_LIST(id);
			return PLUGIN_HANDLED;
		}
		else if(equal(sound, "/msound help"))
		{
			MS_MOTD_HELP(id);
			return PLUGIN_HANDLED;
		}
		else if(equal(sound, "/msound ahelp"))
		{
			if(get_user_flags(id) & MS_ADMIN_LEVEL)
			{
				MS_MOTD_AHELP(id);
				return PLUGIN_HANDLED;
			}
		}
		else if(equal(sound, "/msound chelp"))
		{
			if(get_user_flags(id) & MS_ADMIN_LEVEL)
			{
				MS_MOTD_CHELP(id);
				return PLUGIN_HANDLED;
			}
		}
		else if(equal(sound, "/msound buy"))
		{
			MS_BUY_CREDIT(id);
			return PLUGIN_HANDLED;
		}

		if(msadminonly == 1)
		{
            		if(get_user_flags(id) & MS_ADMIN_LEVEL)
            		{
				if(msadminchat == 1)
				{
					if(isSound(sound) == SOUND)
                    				SearchSoundFile(id,sound);
                		}
                		else
                		{
					if(isSound(sound) == SOUND)
					{
						#if defined MS_AMXX
						client_print(id, print_chat, "[MSOUND(admin_only)] %L",LANG_PLAYER,"ADMIN_CHAT_DISABLED");
						#else
                   				client_print(id, print_chat, _T("[MSOUND(admin_only)] Admin chat disable use the console command msound_play"));
						#endif
					}
                		}
            		}
            		else
            		{
				if(isSound(sound) == SOUND)
				{
					#if defined MS_AMXX
        				client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"MSOUND_IS_DISABLED");
					#else
                			client_print(id, print_chat, _T("[MSOUND]MessageSound is disabled"));
					#endif
				}
	    		}
        	}
        	else
        	{        
			if(isSound(sound) == SOUND)
			{
				msmaxsound = get_cvar_num("msound_user_maxsound");
				if(pSound[id] < msmaxsound || msmaxsound == -1)
				{
					mstimerenabled = get_cvar_num("msound_timer_enabled");
				
					if(mstimerenabled == 1)
					{
						mstimerplay = get_cvar_num("msound_timer_playing");	

						if(pPlayedTime[id] == 0) //first sound played
						{
							pPlayedTime[id] = get_user_time(id);
						}
						else //not first sound played
						{ 
							if(get_user_time(id) < (pPlayedTime[id] + mstimerplay)) //wait
							{
								#if defined MS_AMXX
								client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"U_PLAYED_SOUND_RECENTLY", ((pPlayedTime[id]+mstimerplay) - get_user_time(id)));
								#else
								client_print(id, print_chat, _T("[MSOUND] You have recently played a sound, wait again %dsecond(s)"), ((pPlayedTime[id]+mstimerplay) - get_user_time(id)));
								#endif

								return PLUGIN_HANDLED;
							}
							else
							{
								pPlayedTime[id] = get_user_time(id); //update
							}
						}
					}			
	
					new i;
					new reserved = 0;
					for(i = 0; i < pASoundsNum; i++)
					{
						if(equal(pAdminSounds[i], sound))
						{
							reserved = 1;
						}
					} 
				
					if(reserved == 0)
						SearchSoundFile(id,sound);
					else
						if(is_user_admin(id))
							SearchSoundFile(id,sound); 
				}
				else
				{
					#if defined MS_AMXX
       		 			client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"U_MAXSOUND_LIMIT_REACHED");
					#else
					client_print(id, print_chat, _T("[MSOUND] User max sound limit reached"));		
					#endif
				}
			}
		}

		msmasksay = get_cvar_num("msound_mask_say");
		if(msmasksay == 1)
		{
			if(isSound(sound) == SOUND)
				return PLUGIN_HANDLED;
		}
			

    	}
    	else
    	{
		if(isSound(sound) == SOUND)
		{
			#if defined MS_AMXX
    			client_print(id, print_chat, "[MSOUND] %L",LANG_PLAYER,"MSOUND_IS_DISABLED");
			#else
        		client_print(id, print_chat, _T("[MSOUND]MessageSound is disabled"));
			#endif
		}
	}

    	return PLUGIN_CONTINUE;

}

public MS_MOTD_LIST(id)
{
	mslistenabled = get_cvar_num("msound_list_enabled");

	if(mslistenabled == 1)
	{

            	new szFilename[500];

		#if !defined MS_AMXX
       		new len;
       		new iDirectoryPosition;
		#endif

		new motd[2048];
		new mlen;
	
		#if defined MS_CSMOD
		mlen = format(motd, 2047, "<body bgcolor=#000000 text=#FFFFFF>");
		mlen += format(motd[mlen], 4607-mlen, "Sounds list:<br><br>");
		#else
		mlen = format(motd, 2047, "Sound lists:^n^n");
		#endif		

		new iPos = 0;

		#if defined MS_AMXX
		new dir = open_dir("sound/msound/", szFilename, 500);
		while(next_file(dir, szFilename, 500))
		#else
       	 	while ( read_dir("sound/msound/",iDirectoryPosition++ ,szFilename,500,len) ) 
		#endif  
		{ 
			if(contain(szFilename, ".ztmp") == -1  && contain(szFilename, ".") != 0 && contain(szFilename, "..") != 0)
			{

				replace(szFilename,500,".wav","");
				replace(szFilename,500,".mp3","");

				mslistcols = get_cvar_num("msound_list_cols");

				if(iPos < (mslistcols - 1))
				{
					mlen += format(motd[mlen], 2047-mlen, "%s | ", szFilename);
					iPos++;
				}
				else
				{
					#if defined MS_CSMOD
					mlen += format(motd[mlen], 2047-mlen, "%s<br>",szFilename);
					#else
					mlen += format(motd[mlen], 2047-mlen, "%s^n", szFilename);
					#endif
					iPos = 0;
				}
				
			}
            	}
		#if defined MS_AMXX
		close_dir(dir);
		#endif

		new motd_t[256];
		copy(motd_t, 30, PNAME);
		add(motd_t, 30, " ");
		add(motd_t, 30, PVERS);
		add(motd_t, 30, " ");
		add(motd_t, 30, PAUTH);

		show_motd(id, motd, motd_t);
	}

	return PLUGIN_HANDLED;
}

public MS_MOTD_AHELP(id)
{
	new motd[2048];
	new mlen;

	new ligne[255], len, line;
	
	#if defined MS_CSMOD
	mlen = format(motd, 2048, "<body bgcolor=#000000 text=#FFFFFF>");
	#else
	mlen = format(motd, 2048, "");
	#endif

	while((line=read_file(msadminhelp,line,ligne,255,len))!=0)
	{ 
		#if !defined MS_CSMOD
		#if defined MS_AMXX
		replace(ligne, "<BR>", "^n");
		replace(ligne, "<B>", "");
		replace(ligne, "</B>", "");
		replace(ligne, "<U>", "");
		replace(ligne, "</U>", "");
		#else
		replace(ligne,355,"<BR>", "^n");
		replace(ligne,355,"<B>", "");
		replace(ligne,355,"</B>", "");
		replace(ligne,355,"<U>", "");
		replace(ligne,355,"</U>", "");
		#endif 
		#endif

		mlen += format(motd[mlen], 2047-mlen, ligne);
		//add(motd, 2048, ligne)
	}


	new motd_t[256];
	copy(motd_t, 30, PNAME);
	add(motd_t, 30, " ");
	add(motd_t, 30, PVERS);
	add(motd_t, 30, " ");
	add(motd_t, 30, PAUTH);

	show_motd(id, motd, motd_t);

}

public MS_MOTD_CHELP(id)
{
	new motd[2048];
	new mlen;

	new ligne[255], len, line;
	
	#if defined MS_CSMOD
	mlen = format(motd, 2048, "<body bgcolor=#000000 text=#FFFFFF>");
	#else
	mlen = format(motd, 2048, "");
	#endif

	while((line=read_file(mscvarhelp,line,ligne,255,len))!=0)
	{ 
		#if !defined MS_CSMOD
		#if defined MS_AMXX
		replace(ligne, "<BR>", "^n");
		replace(ligne, "<B>", "");
		replace(ligne, "</B>", "");
		replace(ligne, "<U>", "");
		replace(ligne, "</U>", "");
		#else
		replace(ligne,355,"<BR>", "^n");
		replace(ligne,355,"<B>", "");
		replace(ligne,355,"</B>", "");
		replace(ligne,355,"<U>", "");
		replace(ligne,355,"</U>", "");
		#endif 
		#endif

		mlen += format(motd[mlen], 2047-mlen, ligne);
	}


	new motd_t[256];
	copy(motd_t, 30, PNAME);
	add(motd_t, 30, " ");
	add(motd_t, 30, PVERS);
	add(motd_t, 30, " ");
	add(motd_t, 30, PAUTH);

	show_motd(id, motd, motd_t);

}


public MS_MOTD_HELP(id)
{
	new motd[2048];
	new mlen;

	new ligne[255], len, line;
	
	#if defined MS_CSMOD
	mlen = format(motd, 2047, "<body bgcolor=#000000 text=#FFFFFF>");
	#else
	mlen = format(motd, 2047", "");
	#endif

	while((line=read_file(msuserhelp,line,ligne,255,len))!=0)
	{ 
		#if !defined MS_CSMOD
		#if defined MS_AMXX
		replace(ligne, "<BR>", "^n");
		replace(ligne, "<B>", "");
		replace(ligne, "</B>", "");
		replace(ligne, "<U>", "");
		replace(ligne, "</U>", "");
		#else
		replace(ligne,355,"<BR>", "^n");
		replace(ligne,355,"<B>", "");
		replace(ligne,355,"</B>", "");
		replace(ligne,355,"<U>", "");
		replace(ligne,355,"</U>", "");
		#endif 
		#endif

		mlen += format(motd[mlen], 4607-mlen, ligne);
	}

	new motd_t[256];
	copy(motd_t, 30, PNAME);
	add(motd_t, 30, " ");
	add(motd_t, 30, PVERS);
	add(motd_t, 30, " ");
	add(motd_t, 30, PAUTH);

	show_motd(id, motd, motd_t);

	 
}

public plugin_precache()
{
    	new sound[500];
        new szFilename[500];

	#if !defined MS_AMXX
       	new len;
       	new iDirectoryPosition;
	#endif

	#if defined MS_AMXX
	new dir = open_dir("sound/msound/", szFilename, 500);
	while(next_file(dir, szFilename, 500))
	#else
        while ( read_dir("sound/msound/",iDirectoryPosition++ ,szFilename,500,len) ) 
	#endif          
	{	
        	if(contain(szFilename, ".ztmp") == -1 && contain(szFilename, ".") != 0 && contain(szFilename, "..") != 0)
        	{
                	copy(sound, 500, "sound/msound/");
                	add(sound, 500, szFilename);
	                precache_generic(sound);
        	}
        }
	#if defined MS_AMXX
	close_dir(dir);
	#endif
}

public msound_addcredit(id)
{
	new arg[32];
	read_argv(1, arg, 31);
	new player = cmd_target(id, arg, 1);
	
	if (!player)
		return PLUGIN_HANDLED;

	new arg2[32];
	read_argv(2, arg2, 31);

	new amount = str_to_num(arg2);

	if(pSound[player] > 0)
	{
		if(pSound[player] - amount < 0)
		{
			pSound[player] = 0;

			#if defined MS_AMXX
			client_print(id, print_console, "[MSOUND] %L",LANG_PLAYER,"A_USER_NHAVE_MAXSOUND");
			#else
			client_print(id, print_console, _T("[MSOUND] User have not maximum of sounds"));
			#endif
		}
		else
		{
			pSound[player] -= amount;

			#if defined MS_AMXX
			client_print(id, print_console, "[MSOUND] %L",LANG_PLAYER,"A_SUCCESSFULY_GAVE_CREDIT", amount);
			#else
			client_print(id, print_console, _T("[MSOUND] You have successfully gave %d credit(s) to user"), amount);
			#endif
		} 
	}

	return PLUGIN_HANDLED;
}


public plugin_init()
{
    	register_plugin(PNAME, PVERS, PAUTH);

	#if defined MS_AMXX
	register_dictionary("msound.txt");
	#endif

        register_concmd("msound_play", "msound_play", MS_ADMIN_LEVEL, "msound_play <sound> Play the sound");
	register_concmd("msound_addsound", "msound_addsound", MS_ADMIN_LEVEL, "msound_addsound <sound> add reserved sound for admins");
	register_concmd("msound_addcredit", "msound_addcredit", MS_ADMIN_LEVEL, "msound_addcredit <name or #userid> <number of credit>");

        register_clcmd("say", "handle_say");

	register_cvar("msound_enabled", "1");

	register_cvar("msound_list_enabled", "1");
	register_cvar("msound_list_cols", "4");

	register_cvar("msound_mask_say", "0");

	register_cvar("msound_admin_only", "0");
	register_cvar("msound_admin_chat", "0");

	register_cvar("msound_user_maxsound", "10");
	register_cvar("msound_user_control", "0");

	register_cvar("msound_credit", "1");
	register_cvar("msound_credit_price", "1000");

	register_cvar("msound_timer_enabled", "1");
	register_cvar("msound_timer_playing", "120");

	loadsound();
}