// TK/TD control. To use with StatsMe MetaMod plugin.
// (also displays msg when T drops the bomb)
// http://www.olo.counter-strike.pl
// http://forums.wwgaming.com/forumdisplay.php?s=&forumid=63

#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>

#define ACCESS_CONSOLE 131072
new STRING_VERSION[MAX_DATA_LENGTH] = "2.1.8";

new tknumber[MAX_PLAYERS];
new tdnumber[MAX_PLAYERS];

public set_him_tk(UserIndex,UserName[]) {
	new wmsg[MAX_DATA_LENGTH];
	tknumber[UserIndex] += 1; //increase number of tk (tk is also 5 td)

	// when he gets third warning then kick him
	if (tknumber[UserIndex] > 2) {
		snprintf(wmsg,MAX_DATA_LENGTH,"TK/TD WARNING (%i/3) for %s^nKicking player...",tknumber[UserIndex],UserName); //set proper message as warning
		kick(UserName);
	}
	else {
		snprintf(wmsg,MAX_DATA_LENGTH,"TK/TD WARNING (%i/3) for %s^nOn third warning you will be kicked",tknumber[UserIndex],UserName); //set proper message as warning
	}

	typesay(wmsg, 6, 0, 100, 200); //send text to left hud msg
}

// Format of msg: "global target" "msg type" "msg" "additional parm"
public event_sm_td(HLCommand,HLData,HLUserName,UserIndex) {
	// get parameters from this event
	new eng_msg[MAX_DATA_LENGTH];
	convert_string(HLData,eng_msg,MAX_DATA_LENGTH);
	new temp0[4], temp1[4], temp2[MAX_DATA_LENGTH], temp3[MAX_DATA_LENGTH];
	strgsplit(eng_msg, " ","^"", temp0, 4, temp1, 4, temp2, MAX_DATA_LENGTH,temp3, MAX_DATA_LENGTH);
	//get index of player to which msg was send (0 = to all or to world)
	new tarIndex = strtonum(temp0); 
	if (tarIndex) { // seperate msg send to specified player
		if (!strncmp(temp2[1],"Game_teammate_a",15)) { //check if it's message about injuring a teammmate (not whole see titles.txt in cstrike)
			new tdIndex;
			get_userindex(temp3,tdIndex) //get user index from second parameter of text which was nick of player who td
			if (tarIndex == tdIndex) { //this msg is send 2 times so catch only when it's send to tding player
				tdnumber[tdIndex] += 1; //increase him td
				if (tdnumber[tdIndex] > 5) {//if more then 5 td then treat it as tk
					set_him_tk(tdIndex,temp3); //temp3 is a nick
					tdnumber[tdIndex] = 0; //reset td to next time
				}
			}
		}
		else if (!strncmp(temp2[1],"Killed_T",8)) { //msg about team killing (not whole)
			playerinfo(tarIndex, temp3, MAX_NAME_LENGTH);
			set_him_tk(tarIndex ,temp3);
		}
	}
	else { // and msg send to all
		if (!strncmp(temp2[1],"Game_b",6)) { //msg about bomb droping (not whole)
			exec("sm_say_bset");
			snprintf( eng_msg, MAX_TEXT_LENGTH, "sm_say ^"%s dropped the bomb!!!^"", temp3);
			exec(eng_msg);
		}
	}

	return PLUGIN_CONTINUE;
}

public plugin_connect(HLUserName, HLIP, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS){
		tknumber[UserIndex] = 0; // new user connected - reset his tk/td
		tdnumber[UserIndex] = 0;
	}
	return PLUGIN_CONTINUE;
}

public plugin_init() {
	plugin_registerinfo("TK/TD control","TK/TD control.",STRING_VERSION);
	plugin_registercmd("event_sm_td","event_sm_td",ACCESS_CONSOLE,"");
	exec("sm_register TextMsg ^"admin_command event_sm_td^"");
	return PLUGIN_CONTINUE;
}