#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CONSOLE 131072
 
new STRING_VERSION[MAX_DATA_LENGTH] = "20.06.03";
 
/* Aktualisierung im Sekundentakt*/
#define AKTUALISIERUNG	1
 
new Hostname[MAX_DATA_LENGTH];
 
new time;
 
new GameCommencing;
new checktime;
new origtimelimit;
 
new iTimer;
 
public mp_timeleft_welt(HLCommand,HLData,HLUserName,UserIndex){
        new Data [MAX_DATA_LENGTH];
 
        convert_string( HLData, Data, MAX_DATA_LENGTH );
 
        if(Data[0]=='G' && GameCommencing == 0){
                GameCommencing = 1;
                checktime =systemtime();
                time_change();
        }else if(Data[8]=='R'){
                checktime = systemtime();
                time_change();
        }else if(Data[6]=='S'){
                if (getvar("mp_timelimit")*60+2 != origtimelimit){
                        checktime = systemtime()-(systemtime()- checktime);
                        time_change();
              	}
        }
        return PLUGIN_CONTINUE;
}
 
time_change(){
       	origtimelimit= getvar("mp_timelimit")*60+2;
       	time = (origtimelimit-(systemtime()-checktime));
       	if(iTimer!=0){
       		kill_timer(iTimer);
       		iTimer=0;
       	}
      	iTimer=set_timer("tm_change",AKTUALISIERUNG,time);
      	return PLUGIN_CONTINUE;
}
 
public tm_change(Timer,Repeat,HLUser,HLParam){
	new Text[MAX_TEXT_LENGTH];
	new sSeconds[MAX_NUMBER_LENGTH];
	new minutes;
	new seconds;
 
	time = (origtimelimit-(systemtime()-checktime));
 
	minutes=time/60; 
        seconds=time%60; 
 
        if(seconds<10){
        	snprintf(sSeconds,MAX_NUMBER_LENGTH,"0%i",seconds);
        }else{
        	snprintf(sSeconds,MAX_NUMBER_LENGTH,"%i",seconds);
        }
 
	snprintf(Text,MAX_TEXT_LENGTH,"%s (%i:%s verbleibend)",Hostname,minutes,sSeconds);
	setstrvar( "hostname", Text);
	setstrvar( "mp_timeleft", Text);
	return PLUGIN_CONTINUE;
}
 
public plugin_init(){
        plugin_registerinfo("Show mp_timeleft","Displays Time-Remaining in HLSW",STRING_VERSION);
        plugin_registercmd("mp_timeleft_welt", "mp_timeleft_welt", ACCESS_CONSOLE);
        exec("logd_reg 62 admin_command mp_timeleft_welt");
 
        checktime =systemtime();
        origtimelimit = getvar("mp_timelimit")*60+2;
        time_change();
        getstrvar("hostname",Hostname,MAX_DATA_LENGTH);
        return PLUGIN_CONTINUE;
}