/*******************************************************************
        * 							  *********
       **  Sir Drink a lots Timerem                      v 2.0    ******** 
      ***                                                         ******* 
     ****  INFO:  Announces the time remaining before mapchange.  ****** 
    *****  It uses Game_Commencing for starting timer and	  ***** 
   ******  checks every second the cvar mp_timelimit,		  **** 
  *******  to display the correct time. 			  *** 
 ********  Thanks to Warhead,Black Knight,Schnuffer and the       **
*********  www.admin-mod.de forumusers for help	and the idea	  *
******************************************************************/ 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
#define ACCESS_CONSOLE 131072
#define ACCESS_BAN 256
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.0";
new checktime;
new itime;
new GameCommencing=0;
new origtimelimit;
new z;
 
public execute_all(a[]) {
	new userid = 0;
	new wonid = 0;
	new teamid = 0;
	new username[200];
	new i = 0;
	new x = 0;
	x = maxplayercount();
	for (i = 1; i <= x; i = i + 1) {
		strinit(username);
		if (playerinfo(i, username, 200, userid, wonid, teamid) == 1) {
			execclient(username, a);
		}
    }
	return PLUGIN_CONTINUE;
}	
 
public lt_welt(HLCommand,HLData,HLUserName,UserIndex) {
	new Data [MAX_DATA_LENGTH];
	new x [MAX_DATA_LENGTH];
	new dummy[MAX_DATA_LENGTH];
 
	convert_string( HLData, Data, MAX_DATA_LENGTH );
	strsplit(Data,"_",x,MAX_DATA_LENGTH,dummy,MAX_DATA_LENGTH);
 
	if(strcmp(x,"Game")==0 && GameCommencing == 0) {
		GameCommencing = 1;
		origtimelimit =getvar("mp_timelimit")*60+2;
		checktime = systemtime();
		kill_timer(z);
		z=set_timer("lt_timecheck",1,99999);
	}
	else if(strcmp(x,"Restart")==0) {
		origtimelimit =getvar("mp_timelimit")*60+2;
		checktime = systemtime();
		kill_timer(z);
		z=set_timer("lt_timecheck",1,99999);
	}
	return PLUGIN_CONTINUE;
}
 
public lt_timecheck(Timer,Repeat,HLUser,HLParam) {	
	new Text[MAX_TEXT_LENGTH]; 
	new NextMap[MAX_NAME_LENGTH];
 
  	nextmap(NextMap,MAX_NAME_LENGTH); 
 
	if (getvar("mp_timelimit")*60+2 != origtimelimit) {
		checktime = systemtime()-(systemtime()- checktime);
		origtimelimit= getvar("mp_timelimit")*60+2;	
	}
 
	itime = (origtimelimit-(systemtime()-checktime));
 
	if (itime - 1800 == 0) {
		execute_all("speak ^"fvox/thirty minutes remaining^"");
		centersay("30 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 1500 == 0) {
		execute_all("speak ^"fvox/twenty five minutes remaining^"");
		centersay("25 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 1200 == 0) {
		execute_all("speak ^"fvox/twenty minutes remaining^"");
		centersay("20 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 900 == 0) {
		execute_all("speak ^"fvox/fifteen minutes remaining^"");
		centersay("15 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 600 == 0) {
		execute_all("speak ^"fvox/ten minutes remaining^"");
		centersay("10 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 300 == 0) {
		execute_all("speak ^"fvox/five minutes remaining^"");
		centersay("5 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 120 == 0) {
		execute_all("speak ^"fvox/two minutes remaining^"");
		centersay("2 minutes until mapchange!",6,255,0,0);
	}
	if (itime - 60 == 0) {
		execute_all("speak ^"fvox/sixty seconds remaining^"");
		centersay("60 seconds until mapchange!",6,255,0,0);
	}
	if (itime - 30 == 0) {
		execute_all("speak ^"fvox/thirty seconds remaining^"");
		centersay("30 seconds until mapchange!",6,255,0,0);
	}
	if (itime - 10 == 0) {
		execute_all("speak ^"fvox/ten^"");
		centersay("TEN",2,255,255,255);
	}
	if (itime - 9 == 0) {
		execute_all("speak ^"fvox/nine^"");
		centersay("NINE",2,255,255,255);
	}
	if (itime - 8 == 0) {
		execute_all("speak ^"fvox/eight^"");
		centersay("EIGHT",2,255,255,255);
	}
	if (itime - 7 == 0) {
		execute_all("speak ^"fvox/seven^"");
		centersay("SEVEN",2,255,255,255);
	}
	if (itime - 6 == 0) {
		execute_all("speak ^"fvox/six^"");
		centersay("SIX",2,255,255,255);
	}
	if (itime - 5 == 0) {
		execute_all("speak ^"fvox/five^"");
		centersay("FIVE",2,255,255,255);
	}
	if (itime - 4 == 0) {
		execute_all("speak ^"fvox/four^"");
		centersay("FOUR",2,255,255,255);
	}
	if (itime - 3 == 0) {
		execute_all("speak ^"fvox/three^"");
		snprintf(Text, MAX_TEXT_LENGTH, "THREE ^n Nextmap: %s", NextMap);
		centersay(Text,2,255,255,255);
	}
	if (itime - 2 == 0) {
		execute_all("speak ^"fvox/two^"");
		snprintf(Text, MAX_TEXT_LENGTH, "TWO ^n Nextmap: %s", NextMap);
		centersay(Text,2,255,255,255);
	}
	if (itime - 1 == 0) {
		execute_all("speak ^"fvox/one^"");
		snprintf(Text, MAX_TEXT_LENGTH, "ONE ^n Nextmap: %s", NextMap);
		centersay(Text,6,255,255,255);
	}
	if (itime == 0) {
		execute_all("speak ^"you are authorized for a advanced level^"");
		snprintf(Text, MAX_TEXT_LENGTH, "GAME OVER^n Nextmap: %s", NextMap);
		centersay(Text,6,255,255,255);
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
	plugin_registerinfo("Timeremember on basis of logd","",STRING_VERSION);
	plugin_registercmd("lt_welt", "lt_welt", ACCESS_CONSOLE);
	origtimelimit = getvar("mp_timelimit")*60+2;
	checktime = systemtime();
	z=set_timer("lt_timecheck",1,99999);	
	exec("logd_reg 62 admin_command lt_welt");
	return PLUGIN_CONTINUE;
}