/*SDAL_CONNECT v.3.0
 
Begrüßt oder verabschiedet alle Spieler, die auf dem Server joinen
oder ihn wieder verlassen mit einem Standard-Sound und einer Nachricht.
Für spezielle Spieler (z.B. Admins) kann man auch individuelle
Sounds in einer Datei anlegen, so dass dann diese abgespielt 
werden.
 
Einstellungen:
- adminmod.cfg:
	- admin_vault_file muss definiert sein.
	- file_access_read 1
	- allow_client_exec 1
- vault.ini:
	- CONNECT_FILE Pfad/Datei.xyz -> Pfad und Dateiangabe, in der die speziellen Spieler anhand der SteamID mit entsprechendem Sound (Pfad/Datei.wav) angelegt werden
	- CONNECT_SNDCON "Pfad/Datei.wav" -> für den Standard-Connect Sound
	- CONNECT_SNDDISC "Pfad/Datei.wav" -> für den Standard-Disconnect Sound
	- CONNECT_DELAY Zahl in Sekunden -> Verzögerung des Sounds und der Begrüßung nach dem joinen.
	- CONNECT_COLOR_RED 0-255 -> Rotanteil der Nachrichten
	- CONNECT_COLOR_GREEN 0-255 -> Grünanteil der Nachricht
	- CONNECT_COLOR_BLUE 0-255 -> Blauanteil der Nachricht.
- Zusatzplugins:
	- plugin_sdal_allowsound, damit der Client individuell entscheiden kann, ob er 
	diesen Customsound hören möchte.
	- Die Textnachrichten (Begrüßung und Verabschiedung ist in der Funktion plr_announce(UserIndex[], lConnect) und
	kann dort entsprechend editiert werden. Danach muss das Plugin neu kompiliert werden.
 
 
Hinweis:
Standardmäßig können maximal 30 Spieler mit individuellen Sounds
in der Datei angelegt werden. Sonst muss #define MAX_ENTRIES erhöht werden!
*/
 
 
#include <core> 
#include <console> 
#include <string> 
#include <admin> 
#include <adminlib> 
 
#define ACCESS_CONSOLE 131072
 
new STRING_VERSION[MAX_DATA_LENGTH] = "v3.0_21.06.04";
 
new Key[]="am_sound";
 
/*PLUGIN SETTINGS*/
new g_FileName[MAX_TEXT_LENGTH];
new g_SoundConnect[MAX_TEXT_LENGTH];
new g_SoundDisconnect[MAX_TEXT_LENGTH];
new g_Announce_Delay;
new g_iRed;
new g_iGreen;
new g_iBlue;
 
/*PLAYER DATABASE*/
#define MAX_ENTRIES	30
new g_WONID[MAX_ENTRIES][MAX_AUTHID_LENGTH];
new g_SOUND[MAX_ENTRIES][MAX_TEXT_LENGTH];
new g_real_entries;
 
public plugin_init() {
	check_values();
	return PLUGIN_CONTINUE; 
}
 
check_values(){
	new Text[MAX_TEXT_LENGTH];
	new Vault[MAX_TEXT_LENGTH];
	new error;
	new fileaccess;
	new clientexec;
 
	fileaccess=getvar("file_access_read");
	clientexec=getvar("allow_client_exec");
	getstrvar("admin_vault_file",Vault,MAX_TEXT_LENGTH);
	snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] -------- CHECKING VALUES --------");
	log(Text);
	if(!fileexists(Vault)){
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] vault.ini not defined in adminmod.cfg!!");
		error++;
		log(Text);
	}
	if(!fileaccess){
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] file_access_read must set to 1 in adminmod.cfg!!");
		error++;
		log(Text);
	}
	if(!clientexec){
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] allow_client_exec must set to 1 in adminmod.cfg!!");
		error++;
		log(Text);
	}
 
	if(error==0){
		get_vaultdata( "CONNECT_FILE", g_FileName, MAX_TEXT_LENGTH);
		if(fileexists(g_FileName)){
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] special connectsoundfile: '%s'",g_FileName);
			read_file();
		}else{
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] special connectsoundfile could not be found!!");
		}
		log(Text);
		get_vaultdata( "CONNECT_SNDCON", g_SoundConnect, MAX_TEXT_LENGTH);
		get_vaultdata( "CONNECT_SNDDISC", g_SoundDisconnect, MAX_TEXT_LENGTH);
		if(fileexists(g_SoundConnect)){
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] default connect sound: '%s'",g_SoundConnect);			
		}else{
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] default connect sound could not be found!!");
			error++;
		}
		log(Text);
		if(fileexists(g_SoundDisconnect)){
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] default disconnect sound: '%s'",g_SoundDisconnect);
		}else{
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] default disconnect sound could not be found!!");
			error++;
		}
		log(Text);
		if(error!=0){
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] WARNING: %i default sound(s) is/are not set in vault.ini!!",error);
			log(Text);
		}
		get_vaultnumdata("CONNECT_DELAY",g_Announce_Delay);
		if(g_Announce_Delay==0){
			g_Announce_Delay=4;
			set_vaultnumdata( "CONNECT_DELAY", g_Announce_Delay );
		}
		get_vaultnumdata("CONNECT_COLOR_RED",g_iRed);
		if(g_iRed==0){
			g_iRed=255;
			set_vaultnumdata( "CONNECT_COLOR_RED", g_iRed );
		}
		get_vaultnumdata("CONNECT_COLOR_GREEN",g_iGreen);
		if(g_iGreen==0){
			g_iGreen=255;
			set_vaultnumdata( "CONNECT_COLOR_GREEN", g_iGreen );
		}
		get_vaultnumdata("CONNECT_COLOR_BLUE",g_iBlue);
		if(g_iBlue==0){
			g_iBlue=255;
			set_vaultnumdata( "CONNECT_COLOR_BLUE", g_iBlue );
		}
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] delay connect: '%i'",g_Announce_Delay);
		log(Text);
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] RGB-Color-RED: '%i'",g_iRed);
		log(Text);
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] RGB-Color-GREEN: '%i'",g_iGreen);
		log(Text);
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] RGB-Color-BLUE: '%i'",g_iBlue);
		log(Text);
		plugin_registerinfo("Client Connect Greetings!","Announces and greets every player who connects to server!",STRING_VERSION);
		plugin_registercmd("sdal_enter", "sdal_enter", ACCESS_CONSOLE);
		exec( "logd_reg 51 admin_command sdal_enter" ); /*ENTER-EVENT*/			
	}else{
		plugin_registerinfo("Client Connect Greetings!","TOO MANY ERRORS! PLUGIN DISABLED!",STRING_VERSION);
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] FATAL ERROR! Please check your entries in adminmod.cfg");
		log(Text);
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] ------------------------------------------");
		log(Text);
	}
}
 
read_file(){
	new Data[MAX_TEXT_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new i;
	new j;
 
	g_real_entries=0;
	snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] * Reading '%s'",g_FileName);
	log(Text);
	j=filesize(g_FileName);
	if(j>=MAX_ENTRIES){
		snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] To many entries in %s! Max: %i  ",g_FileName,MAX_ENTRIES-1);
		log(Text);
		j=MAX_ENTRIES-1;
	}
	for(i=0;i<=j;i++){
		readfile(g_FileName,Data,i,MAX_TEXT_LENGTH);
		strsep(Data," ",g_WONID[g_real_entries],MAX_AUTHID_LENGTH,g_SOUND[g_real_entries],MAX_TEXT_LENGTH);
		if(!fileexists(g_SOUND[g_real_entries])){
			snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] Sound-File: '%s' not found. Registration invalid! ",g_SOUND[g_real_entries]);
			log(Text);
		}else{
			g_real_entries++;
		}
	}
	g_real_entries--;
	snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] %i SteamIDs and Sounds loaded!",g_real_entries);
	log(Text);
	snprintf(Text,MAX_TEXT_LENGTH,"[SDAL_CONNECT] ------------------------------------------");
	log(Text);
	return PLUGIN_CONTINUE;
}
 
/*CLIENT SET-INFO FOR ENABLING SOUNDS */
allowsound(User[]){
	new Info[MAX_NUMBER_LENGTH];
	new iInfo;
	new Authid[MAX_AUTHID_LENGTH];
	get_userAuthID(User,Authid,MAX_AUTHID_LENGTH);
	if(strcmp(Authid,"BOT")!=0){
		get_userinfo(User,Key,Info,MAX_NUMBER_LENGTH);
		iInfo=strtonum(Info);
		if(strlen(Info)==0 || iInfo >=1){/*if no setinfo exists, return 1*/
			return 1;
		}
	}
	return 0;
}
 
playFile(sFileName[]){
	new Name[MAX_NAME_LENGTH];
	new iPlayerCount = maxplayercount( );
	new i;
	new Authid[MAX_AUTHID_LENGTH];
	for( i=1; i <= iPlayerCount; i++ ){
		if( playerinfo( i, Name, MAX_NAME_LENGTH,_,_,_,_,Authid ) ){
			if(allowsound(Name)){
				playsound(Name,sFileName);
			}
		}
	}
	return PLUGIN_CONTINUE;
}
 
public sdal_enter(HLCommand,HLUserIndex){
	new strUserIndex[MAX_NUMBER_LENGTH];
	convert_string(HLUserIndex, strUserIndex, MAX_NUMBER_LENGTH);
	set_timer("ann_timer",g_Announce_Delay, 0, strUserIndex);
	return PLUGIN_CONTINUE; 
}
 
public ann_timer(Timer,Repeat,HLName,HLParam) {
	new strUserIndex[MAX_NUMBER_LENGTH]; 
	convert_string(HLParam,strUserIndex,MAX_NUMBER_LENGTH);
	plr_announce(strUserIndex, 1);
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex) {
	new strUserIndex[MAX_NUMBER_LENGTH];
	numtostr(UserIndex,strUserIndex);
	plr_announce(strUserIndex, 0);
	return PLUGIN_CONTINUE; 
} 
 
plr_announce(UserIndex[], lConnect) { 
	new Text[MAX_TEXT_LENGTH];
	new strName[MAX_NAME_LENGTH];
	new AuthID[MAX_AUTHID_LENGTH];
	new uindex;
	new i;
	new x;
 
	uindex=strtonum(UserIndex);
 
	if(playerinfo(uindex,strName,MAX_NAME_LENGTH,_,_,_,_,AuthID)){
		if(strlen(g_FileName)!=0){
			for(i=0;i<=g_real_entries;i++){
				if(strcmp(g_WONID[i],AuthID)==0){
					playFile(g_SOUND[i]);
					x=1;
					break;
				}
			}
		}
		if (lConnect == 1) {
			snprintf(Text,MAX_TEXT_LENGTH,"Willkommen^n%s^nViel Spass hier!!",strName);
			if(!x && strlen(g_SoundConnect)!=0){
				playFile(g_SoundConnect);
			}
		} else {
			snprintf(Text,MAX_TEXT_LENGTH,"%s^nhat das Spiel verlassen!^nKommt aber sicherlich zurueck!",strName);
			if(!x && strlen(g_SoundDisconnect)!=0){
				playFile(g_SoundDisconnect);
			}
		}
		typesay(Text,12,g_iRed,g_iGreen,g_iBlue);
	}
	return PLUGIN_CONTINUE; 
}