/* Dieses Plugin kündigt einen HLTV-Server beim Connect
und auf Befehl an. Plugin von [WING]Black Knight 13.09.2004 
 
This Plugin announces a hltv server on connect and on
execution of a command. Plugin by [WING]Black Knight 13.09.2004*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CONSOLE 131072
#define MAX_IP_LENGTH 22
 
new STRING_VERSION[MAX_DATA_LENGTH] = "1.2";
 
/********************** Change your settings here ********************/
/* Set 0 for WON and 1 for STEAM */
#define STEAM 1
 
/* Set 0 for English and 1 for German */
#define LANG 0
/********************* Don't change anything below *******************/
 
new hltvname[MAX_NAME_LENGTH];
new hltvid=0;
new hltvteam=0;
 
#if STEAM==1
new hspecs[MAX_NAME_LENGTH]="hspecs";
new hslots[MAX_NAME_LENGTH]="hslots";
new hdelay[MAX_NAME_LENGTH]="hdelay";
#endif
 
#if STEAM==0
new hspecs[MAX_NAME_LENGTH]="hltv_specs";
new hslots[MAX_NAME_LENGTH]="hltv_slots";
new hdelay[MAX_NAME_LENGTH]="hltv_delay";
#endif
 
public admin_hltv(HLCommand,HLData,HLUserName,UserIndex){
	a_hltv();
	return PLUGIN_CONTINUE;
}
 
public a_hltv() {
	if(hltvteam==TEAM_PROXY) {
		new text[MAX_TEXT_LENGTH];
		new hltvspecs[MAX_NAME_LENGTH];
		new hltvslots[MAX_NAME_LENGTH];
		new hltvdelay[MAX_NAME_LENGTH];
		new iPort;
		new hltvip[MAX_IP_LENGTH];
 
		get_userinfo(hltvname,hspecs,hltvspecs,MAX_NAME_LENGTH);
		get_userinfo(hltvname,hslots,hltvslots,MAX_NAME_LENGTH);
		get_userinfo(hltvname,hdelay,hltvdelay,MAX_NAME_LENGTH);
		get_userIP(hltvname,hltvip,MAX_IP_LENGTH,iPort);
#if LANG==1
		snprintf(text, MAX_TEXT_LENGTH, "%s online:^n%s:%d^nSlots %s/%s, Delay %s s",hltvname,hltvip,iPort,hltvspecs,hltvslots,hltvdelay);
#else
		snprintf(text, MAX_TEXT_LENGTH, "%s online:^n%s:%d^nSlots %s/%s, Delay %s s",hltvname,hltvip,iPort,hltvspecs,hltvslots,hltvdelay);
#endif
		typesay(text,10,68,153,125);
#if LANG==1
		say("Schreib ^"hltvconnect^" im Chat, um auf den HLTV geleitet zu werden!");
#else
		say("Type ^"hltvconnect^" in chat to get redirected to the HLTV!");
#endif
	} else {
#if LANG==1
		typesay("Kein HLTV-Server online!",3,181,40,44);
#else
		typesay("No HLTV-Server online!",3,181,40,44);
#endif
	}
	return PLUGIN_HANDLED;
}
 
public plugin_disconnect(HLUserName, UserIndex){
	if (hltvid==UserIndex){
		hltvid=0;
		hltvteam=0;
#if LANG==1
		typesay("Der HLTV ist jetzt offline!",3,181,40,44);
#else
		typesay("The HLTV is offline!",3,181,40,44);
#endif
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_info(HLOldName, HLNewName, UserIndex){
	new Name[MAX_NAME_LENGTH];
	new UserID,WONID,Team,Dead;
	new sAuthID[MAX_AUTHID_LENGTH];
 
	new sHLOldName[MAX_NAME_LENGTH];
	new sHLNewName[MAX_NAME_LENGTH];
 
	convert_string(HLOldName,sHLOldName,MAX_NAME_LENGTH);
	convert_string(HLNewName,sHLNewName,MAX_NAME_LENGTH);
 
	if(UserIndex>0){
		playerinfo(UserIndex,Name,MAX_NAME_LENGTH,UserID,WONID,Team,Dead,sAuthID);
	}
	else{
		return PLUGIN_CONTINUE;
	}
 
	if(Team==TEAM_PROXY){
		if(hltvteam!=TEAM_PROXY){
			new text[MAX_TEXT_LENGTH];
			new delay;
			get_userinfo(Name,hdelay,text,MAX_TEXT_LENGTH);
			log(text);
			delay=strtonum(text);
			if(delay!=0){
				hltvteam=TEAM_PROXY;
				hltvid=UserIndex;
				strcpy(hltvname,Name,MAX_NAME_LENGTH);
				a_hltv();
			}
			return PLUGIN_CONTINUE;
		}
		if(!streq(sHLOldName,sHLNewName)){
			strcpy(hltvname,Name,MAX_NAME_LENGTH);
		}
	}
 
	return PLUGIN_CONTINUE;
}
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	strstripquotes(Data);
	if(streq(Data, "hltvconnect")==1) {
		hltvconnect(User);
	}
 
	return PLUGIN_CONTINUE;
}
 
public admin_hltv_connect(HLCommand,HLData,HLUserName,UserIndex) {
	if(hltvid==0){
#if LANG==1
		selfmessage("Kein HLTV online!");
#else
		selfmessage("No HLTV online!");
#endif
		return PLUGIN_HANDLED;
	}
 
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strstripquotes(Data);
	if(strlen(Data)==0) {
		hltvconnect(User);
		return PLUGIN_HANDLED;
	}
	new UIndex;
	get_userindex(Data,UIndex);
	new Name[MAX_NAME_LENGTH];
	new UserID,WONID,Team,Dead;
	new sAuthID[MAX_AUTHID_LENGTH];
	playerinfo(UIndex,Name,MAX_NAME_LENGTH,UserID,WONID,Team,Dead,sAuthID);
	if(Team==TEAM_PROXY){
#if LANG==1
		selfmessage("Na klar, wir lassen den HLTV auf sich selbst connecten...");
#else
		selfmessage("Of course, we let the hltv connect to itself...");
#endif
		return PLUGIN_HANDLED;
	}
	else {
		if(auth(User)){
			hltvconnect(Data);
		}
		else{
#if LANG==1
			selfmessage("Sorry, dir fehlen die Rechte dafür!");
#else
			selfmessage("Sorry, but you do not have the needed rights!");
#endif
		}			
	}
	return PLUGIN_HANDLED;
}
 
hltvconnect(Data[]){
	new Text[MAX_TEXT_LENGTH];
	new hltvip[MAX_IP_LENGTH];
	new iPort;
	new hltvinfo[MAX_NAME_LENGTH];
	new ihltv;
	get_userIP(hltvname,hltvip,MAX_IP_LENGTH,iPort);
	get_userinfo(hltvname,hslots,hltvinfo,MAX_NAME_LENGTH);
	ihltv=strtonum(hltvinfo);
	get_userinfo(hltvname,hspecs,hltvinfo,MAX_NAME_LENGTH);
	ihltv-=strtonum(hltvinfo);
	if(ihltv>0){
#if LANG==1
		snprintf(Text,MAX_TEXT_LENGTH,"%s connected zum HLTV auf %s:%d. Schreib ^"hltvconnect^"^n um ihm/ihr zu folgen.",Data,hltvip,iPort);
#else
		snprintf(Text,MAX_TEXT_LENGTH,"%s connected to the HLTV on %s:%d. Type ^"hltvconnect^"^n to follow him/her.",Data,hltvip,iPort);
#endif
		say(Text);
		snprintf(Text,MAX_TEXT_LENGTH,"connect %s:%d",hltvip,iPort);
#if LANG==1
		selfmessage("Auf HLTV umgeleitet!");
#else
		selfmessage("Redirected to HLTV!");
#endif
		execclient(Data,Text);
	}
	else{
#if LANG==1
		selfmessage("Sorry, kein Transfer moeglich, HLTV voll!");
#else
		selfmessage("Sorry, no redirection possible, HLTV full!");
#endif
	}			
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
#if LANG==1
	plugin_registerinfo("HLTV Announcer plugin","Kuendigt HLTV an.",STRING_VERSION);
	plugin_registercmd("admin_hltv","admin_hltv",ACCESS_ALL,"admin_hltv: Zeigt an, wo der HLTV laeuft.");
	plugin_registercmd("admin_hltv_connect","admin_hltv_connect",ACCESS_ALL,"admin_hltv_connect: Leitet einen selbst oder jemanden auf den HLTV.");
	plugin_registerhelp("say",ACCESS_ALL,"say hltvconnect: Leitet den Spieler auf den HLTV um.");
#else
	plugin_registerinfo("HLTV Announcer plugin","Announces HLTV.",STRING_VERSION);
	plugin_registercmd("admin_hltv","admin_hltv",ACCESS_ALL,"admin_hltv: Shows information about connected hltv.");
	plugin_registercmd("admin_hltv_connect","admin_hltv_connect",ACCESS_ALL,"admin_hltv_connect: Redirects yourself or someone else to the HLTV.");
	plugin_registerhelp("say",ACCESS_ALL,"say hltvconnect: Redirects yourself to the HLTV.");
#endif
 
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
 
	return PLUGIN_CONTINUE;
}