/* This plugin let's you declare two clan tags, checks that everyone has them,
 * and kicks anyone that doesn't.  Then it puts a password on the server, and 
 * restarts the round in 10 secs and sets all ogl match rule settings. It then 
 * starts counting down from the specifed time in the matchtround time length field.  
 * When the time has expired in the first round it, the plugin attempts to switch
 * the teams and asks the players if they are ready to begin the second round. 
 * When everyone is ready, it restarts the round and restarts the timer. At the 
 * end of the second round, the plugin stops all counters and declares the match 
 * as over. The admin_endmatch can be used to stop the match in the middle if 
 * necessary. Remember the clan tags ARE case sensetive. 
 *
 *
 * ------------------------------- 
 * New to version 1.2
 *
 *	- Added Admin_endmatch 
 *	- Added countdown timer 
 *	- Added Set server properties on clanmatch start
 *	Note: All settings set to OGL.org defined RULES. 
 *	      You CAN change any of the settings for your own needs. 
 * -------------------------------
 *
 * ------------------------------- 
 * New to version 2.1a
 *	
 *	- Added fourth clanmatch variable 
 *	- Changed overall function
 *	- Added auto team switching (They cde that switchs teams "sucks". Someone please tell me a better way!) 
 *	- Added New Timer
 *	- Removed admin_matchtimer 
 * -------------------------------
 * 
 * ------------------------------- 
 * New to version 2.2
 *	
 *	- Changed voting method 
 *	- All players are required to type 'ready' to begin the second match. 
 *	  (This insures that the teams are as desired and all players are ready to begin)
 *	- Minor code changes 
 *	- Code is still a bit sloppy. I'll fix it when I have some time.  
 * -------------------------------
 *
 * ------------------------------- 
 * New to version 2.2a
 *	
 *	- Added status to see how many players are ready to start match 2 when anything is typed between matchs. 
 * -------------------------------
 * 
 * ------------------------------- 
 * New to version 2.4
 *	
 *	- Added bury type command after switch teams.  
 * -------------------------------
 *
 * New to version 2.5
 *	
 *	- Added External config files
 *	- Added say commands "matchtime" "time" "match time" "match" to display the match time
 *	- Added command Admin_secondround
 *	- Removed voting
 *	- Removed admin_matchtime (replace by say commands) 
 *	- Fixed the loose indent errors
 *	- Added burry teams when match ends
 * -------------------------------
 * 
 *Commands:
 *	Admin_clanmatch (clan tag 1) (clan tag 2) (serverpassword) (match roundtime duration)
 *		-Activates timer, kicks non clan players, passwords the server, restarts the round.
 *		NOTE: match roundtime lenght is the lenght of EACH of the 2 rounds you will play. 
 *		IE:  "admin_clanmatch Killaz Murdaz oglmatch 25"
 *		  This will make each of the two matchrounds 25 minutes long.  
 *
 *	Admin_endmatch 
 *		-If you need to stop the match use this. Restores server to user defined settings and stops timer.
 *	
 *	Admin_secondround
 *		-Starts the second round (use when prompted).
 *
 * Changed exec("admin_command... to plugin_exec("... ([WING] Black Knight)
 */	
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CLANMATCH 4096
#define ACCESS_MATCHTIME 0
 
 
new Pluginstage = 0;
new Status;
new Timerend = 1;
new Elapsed;             
new ElapsedM;            
new ElapsedS;
new z = 0;
new Roundtime[MAX_DATA_LENGTH];
 
 
 
 
public admin_clanmatch(HLCommand,HLData,HLUserName,UserIndex) 
{
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new ClanTag1[MAX_NAME_LENGTH];
	new ClanTag2[MAX_NAME_LENGTH];
	new Password[MAX_DATA_LENGTH];
	new maxplayers = maxplayercount();
	new Target[MAX_NAME_LENGTH];
	new i = 0;
	new SessionID;
	new WONID;
	new Team;
	new Text[MAX_TEXT_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strbreak(Data,ClanTag1,Data, MAX_NAME_LENGTH);
	if (strlen(ClanTag1) == 0) {
		selfmessage("Clantag 1 missing");
		return PLUGIN_HANDLED;
	}
	strbreak(Data, ClanTag2, Data, MAX_NAME_LENGTH);
	if (strlen(ClanTag2) == 0) {
		selfmessage("Clantag 2 missing");
		return PLUGIN_HANDLED;
	}
 
	strbreak(Data, Password, Roundtime, MAX_NAME_LENGTH);
	if (strlen(Roundtime) == 0) {
		selfmessage("Password or Roundtime missing");
		return PLUGIN_HANDLED;
	}
 
	for(i=1; i<=maxplayers; i++) {
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			if (strstr(Target, ClanTag1) == -1 && strstr(Target, ClanTag2) == -1) {
				if(check_immunity(Target)==0 && i != UserIndex) {
					message(Target, "A match is about to start ... thanks for leaving ;p");
					kick(Target);
				}
			}
 		}
	}
 
	if (Pluginstage == 0) 
		{
		Pluginstage = 1;
		}
 
	else 
		{ 
		say("Match already started. Please use 'admin_endmatch' before starting a new one.");
		return PLUGIN_HANDLED;
		}
	exec("exec clanmatch.cfg");
	execute_command(User,"SV_Restartround","sv_restartround","9");
	execute_command(User,"SV_Restartround","sv_restartround","10");
	execute_command(User,"SV_Password","sv_password",Password);
	centersay("Match 1 of 2 Starts at reset in 10 seconds",10,255,0,0);
	snprintf(Text, MAX_TEXT_LENGTH, "Password is now ^"%s^".", Password);
  	say(Text);
	z=10; 
	set_timer("startcountdown", 1, 11);
	roundcountdown();
 
	return PLUGIN_CONTINUE;
}
 
public startcountdown() 
{
	new Text[MAX_TEXT_LENGTH];
	if (z == 0) {
		snprintf(Text, MAX_TEXT_LENGTH, "Match is now STARTED. Good Luck.  ");
		say(Text);
		return PLUGIN_CONTINUE;
		}
 
	snprintf(Text, MAX_TEXT_LENGTH, "Reset in %i ", z);
	say(Text);
	z--;
	return PLUGIN_CONTINUE;
}
 
public roundcountdown()
{                        
	new Text[MAX_TEXT_LENGTH];
	if (Status == 0) 
        	{
		Status = 1;
		Elapsed = 0;
		ElapsedM = 0;
		ElapsedS = 0;
		Timerend = systemtime() + (strtonum(Roundtime) * 60); 
		snprintf(Text, MAX_TEXT_LENGTH, "[ Match time set for %i minute(s) ]", strtonum(Roundtime));
		say(Text);
		set_timer("refreshtime", 1, 99999);
		} 
 
	else
		{
		Status = 1;
		Elapsed = 0;
		ElapsedM = 0;
		ElapsedS = 0;
		Timerend = systemtime() + (strtonum(Roundtime) * 60); 
		snprintf(Text, MAX_TEXT_LENGTH, "[ Match time set for %i minute(s) ]", strtonum(Roundtime));
		say(Text);
		}
 
}
 
public admin_endmatch()
{
	new User[MAX_NAME_LENGTH];
	User = "Admin";
 
	if (Pluginstage == 0 ) 
		{
		say("[ There is currently no clan match in play. ]");
		return PLUGIN_HANDLED;
		}
 
	if (Pluginstage != 0 ) 
        	{
		Pluginstage = 0;
		exec("exec normal.cfg");
       	        plugin_exec("admin_nopass","");
       	        plugin_exec("admin_nopass","");
	        say("[ Clan Match Ended. ]");
	        return PLUGIN_HANDLED;
		}
 
 
	return PLUGIN_CONTINUE;
}
 
 
public refreshtime()
{
	if (Pluginstage == 2)
		{
		centersay("To start round 2 the admin has to type 'admin_secondround' ",10,255,0,0);
		}
 
 	if (Pluginstage == 1 || Pluginstage == 3)
 	{
		new Text[MAX_DATA_LENGTH];
		new Elapsedtemp; 
		new ElapsedMtemp;
		new ElapsedStemp;
		new Systime;
 
		Elapsedtemp = 0;
		ElapsedMtemp = 0;
		ElapsedStemp = 0;
      		Systime = 0;
 
		Elapsedtemp = Elapsed;
		ElapsedMtemp = ElapsedM;
		ElapsedStemp = ElapsedS;
		Systime = systemtime();
		Elapsedtemp = systemtime();
		Elapsedtemp = (Timerend - Elapsedtemp);
		ElapsedMtemp = (Elapsedtemp / 60);
		ElapsedStemp = (Elapsedtemp - (ElapsedMtemp * 60) );
 
		if ((Elapsedtemp - (ElapsedMtemp * 60)) == 0)
		{
			snprintf(Text, MAX_TEXT_LENGTH, "[ Match Time Remaing: %i minute(s) %i second(s) ]", ElapsedMtemp, ElapsedStemp);
			say(Text);
		}
 
 
		if (Timerend == (Systime + 5))
		{
		say("[ 5 Secs left! ]");
		return PLUGIN_CONTINUE;
		}
 
		if (Timerend == (Systime + 4))
		{
		say("[ 4 Secs left! ]");
		return PLUGIN_CONTINUE;
		}
 
		if (Timerend == (Systime + 3))
		{
		say("[ 3 Secs left! ]");
		return PLUGIN_CONTINUE;
		}
 
		if (Timerend == (Systime + 2))
		{
		say("[ 2 Sec left! ]");
		return PLUGIN_CONTINUE;
		}
 
		if (Timerend == (Systime + 1))
		{
		say("[ 1 Sec left! ]");
		return PLUGIN_CONTINUE;
		}
 
 
 
		if (Timerend < Systime)
		{
			if(Pluginstage == 3)
				{
				Pluginstage = 0;
				say("[ Match 2 of 2 over! Losing team please report loss. Thanks!]");
				freeze();
				admin_endmatch();
				}
 
			if(Pluginstage == 1)
				{
				Pluginstage = 2;
				say("[ Match 1 of 2 over!]");
				admin_switchteams();
				set_timer("freeze", 10, 1);
				}
			return PLUGIN_CONTINUE;
		}
 
 
	}
	return PLUGIN_CONTINUE;
}
 
 
 
public startroundtwo()
{
	Pluginstage = 3; 
	execute_command("Admin","SV_Restartround","sv_restartround","9");
	execute_command("Admin","SV_Restartround","sv_restartround","10");
	centersay("Match 2 of 2 Starts at reset in 10 seconds",10,255,0,0);
	z=10;
	set_timer("startcountdown", 1, 11);
	roundcountdown();
 
}
 
public HandleSay(HLCommand,HLData,HLUserName,u) 
{
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new word[MAX_TEXT_LENGTH];
 
 
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strbreak(Data,word,Data, MAX_NAME_LENGTH);
	strstripquotes(word);
 
	if ((streq(word, "matchtime")==1) || (streq(word, "match")==1) || (streq(word, "time")==1) || (streq(word, "match time")==1)) 
		{
		if (Pluginstage == 0)
			{
 
			say("[ There is currently no clan match in play. ]");
			return PLUGIN_CONTINUE;
			} 
 
		else 
			{
			new Text[MAX_DATA_LENGTH];
			new Elapsedtemp; 
			new ElapsedMtemp;
			new ElapsedStemp;
 
			Elapsedtemp = 0;
			ElapsedMtemp = 0;
			ElapsedStemp = 0;
 
			Elapsedtemp = Elapsed;
			ElapsedMtemp = ElapsedM;
			ElapsedStemp = ElapsedS;
 
			Elapsedtemp = systemtime();
			Elapsedtemp = (Timerend - Elapsedtemp);
			ElapsedMtemp = (Elapsedtemp / 60);
			ElapsedStemp = (Elapsedtemp - (ElapsedMtemp * 60) );
 
			snprintf(Text, MAX_TEXT_LENGTH, "[ Match Time Remaing: %i minute(s) %i second(s) ]", ElapsedMtemp, ElapsedStemp);
			say(Text);
			return PLUGIN_CONTINUE;
			}
 
 
		}
	return PLUGIN_CONTINUE;
}
 
public admin_secondround()
{
	if(Pluginstage == 2)
		{
		say("Round 2 has been started by an Admin.");
		startroundtwo();
		}
	else
		{	
		say("Clan match not started or first or second round already in play.");
		}
 
	return PLUGIN_CONTINUE;
}
 
public admin_switchteams()
{
 
	new User[MAX_NAME_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
 
	setstrvar("admin_ignore_immunity","1");
	User = "Admin";
 
	for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			if(Team==1) {
				plugin_exec("admin_ct",Target);
			}
			else if(Team==2) {
				plugin_exec("admin_t",Target);
			}
		}
	}
	setstrvar("admin_ignore_immunity","0");
}
 
public freeze() 
{
	new x, y, f, i = 0;
	new maxplayers = maxplayercount();
	new Target[MAX_NAME_LENGTH];
	new SessionID;
	new WONID;
	new Team;
 
	for(i=1; i<=maxplayers; i++) {
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
 
            		get_userorigin(Target, x, y, f);
            		teleport(Target, x, y, (f-50));			
 
		}	
 
 	}
 
}
 
public plugin_init() {
	plugin_registercmd("admin_clanmatch","admin_clanmatch",ACCESS_CLANMATCH,"admin_clanmatch <ClanTag1> <ClanTag2> <Password> <1 match lengh > : Starts a clanmatch.");
	plugin_registercmd("admin_endmatch", "admin_endmatch", ACCESS_CLANMATCH, "admin_endmatch Restores server to no clan match settings and stops the timer.");
	plugin_registercmd("admin_secondround", "admin_secondround", ACCESS_CLANMATCH, "Starts the second round.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say matchtime/time/match time :  Public Command displays timer time elapsed");
	return PLUGIN_CONTINUE;
}