// Idea was taken from Biohazard`s LastScout Plugin for LogD.
// Last Man announce. To use with StatsMe MetaMod plugin.
// 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";

#define NUM_TXT_MSG 3

new sLastMsg[NUM_TXT_MSG][] = {
"I hope you still have a healthpack.",
"All your teammates were killed. Good luck !",
"Now you are alone. Have fun !"};
		
new notsend;

public announce_me(iState,sNames[2])
{
	if (iState==2){
		new msg[MAX_DATA_LENGTH], sName1[MAX_NAME_LENGTH], sName2[MAX_NAME_LENGTH];
		playerinfo(sNames[0],sName1,MAX_NAME_LENGTH);
		playerinfo(sNames[1],sName2,MAX_NAME_LENGTH);
		snprintf(msg,MAX_DATA_LENGTH,"sm_say_kill;sm_say ^"%s vs. %s^"",sName1,sName2);
		exec(msg);
	}
	else if (notsend) { // send only once
		notsend = 0;
		new msg[MAX_DATA_LENGTH], sName[MAX_NAME_LENGTH];
		playerinfo(sNames[iState],sName,MAX_NAME_LENGTH);
		snprintf(msg,MAX_DATA_LENGTH,"sm_say_kill;sm_say ^"%s: %s^"",sName,sLastMsg[random(NUM_TXT_MSG-1)] );
		exec(msg);
	}
}

public event_sm_lastkill(HLCommand,HLData,HLUserName,UserIndex) {
	new Name[MAX_NAME_LENGTH], iID, iTeam, iWONID, iDeath, i;
	new maxPlayers = maxplayercount();
	new sNames[2];
	new iTeam1, iTeam2, iState = 3;
	for(i = 1; i <= maxPlayers; i++){
		if (playerinfo(i,Name,MAX_NAME_LENGTH,iID,iWONID,iTeam,iDeath)==1&&iDeath == 0){
			if (iTeam == 1){
				iTeam1++;
				sNames[0]=i;
			}
			else if (iTeam == 2 || iTeam == 4){
				iTeam2++;
				sNames[1]=i;			
			}
		}
	}

	if (iTeam1 == 1 && iTeam2 > 1)
		iState = 0;
	else if (iTeam2 == 1 && iTeam1 > 1)
		iState = 1;
	else if (iTeam2 == 1 && iTeam1 == 1)
		iState = 2;
	else
		notsend = 1;

	if (iState < 3){
		announce_me(iState,sNames);
	}

	return PLUGIN_CONTINUE;
}

public plugin_init() {
	plugin_registerinfo("LastMan announce","LastMan announce.",STRING_VERSION);
	plugin_registercmd("event_sm_lastkill","event_sm_lastkill",ACCESS_CONSOLE,"");
	exec("sm_register DeathMsg ^"admin_command event_sm_lastkill^"");
	return PLUGIN_CONTINUE;
}