/* plugin_runtime by Sloth         For all of you 24/7 de_dust servers  :)
 
This plugin will display the start date/time or elapsed runtime of the current map.
 
Usage: 	say 'runtime'					// display date/time of map start
		admin_runtime_time				// get message frequency
		admin_runtime_time <minutes>	// set message frequency
		admin_runtime_style				// get time display style
		admin_runtime_style [1|2|3]		// set time display style
										// 1 - Map start time
										// 2 - Elapsed run time
										// 3 - Map start time 
										//   & Elapsed run time
 
Setup:
------
1) Install the file as per the normal procedure defined in the AdminMod manual.
 
2) Make sure you have defined the admin_vault_file in your server.cfg as it is 
   used to retain the value of RUNTIME frequency and RUNTIME style through map 
   changes and server restarts. You can set this with the following line: 
		   admin_vault_file "vault.ini"
 
3) Test your installation. Type the following commands in the server console:
 
   	admin_runtime_time				
   	------------------
 	admin_runtime_time <minutes>
 	----------------------------
   	admin_runtime_style
   	------------------
 	admin_runtime_style [1|2|3]
 
	The very first time the plugin is run or if the vault_file is unaccessable:
		RUNTIME_TIME will default to 45 mins.
		RUNTIME_STYLE will default to 1 - Map start time
 
4) Every RUNTIME_TIME minutes the date/time or elapsed time or both will display
   to the screen or when a player says 'runtime'.
 
Acknowledgements:
-------------
a) [fah-q] Dio for the vault stuff.
b) [geist.method] for the system time.
c) CiZiN for the idea.
 
*/
 
#include <core>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_RUNTIME 4096	// security level required to set run timer
#define min_in_day 	1440
#define min_in_hr 	60
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new RUNTIME_TIME;
new RUNTIME_STYLE;
new t_index;		// timer index
new t_time[MAX_DATA_LENGTH];
new t_date[MAX_DATA_LENGTH];
 
new RT_STYLE[3][] = {"Map start time", "Elapsed run time", "Map start time & Elapsed run time"};
 
public admin_runtime_time(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	if (strlen(Data) > 0) {
		RUNTIME_TIME = strtonum(Data);
		if(RUNTIME_TIME < 0) {
			RUNTIME_TIME = 0;
		}
		kill_timer(t_index);
		t_index = set_timer("run_time",RUNTIME_TIME * 60,99999);
 
		numtostr(RUNTIME_TIME, Data);
		set_vaultdata("RUNTIME_TIME", Data);
	}
 
	snprintf(Text, MAX_TEXT_LENGTH, "[ADMIN] RUNTIME will display every %d minutes", RUNTIME_TIME);
	selfmessage(Text);	
 
	log_command(User, Command, Data);
 
	return PLUGIN_HANDLED;
}
 
 
public admin_runtime_style(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	if (strlen(Data) > 0) {
		RUNTIME_STYLE = strtonum(Data);
		if(RUNTIME_STYLE != 1 && RUNTIME_STYLE != 2 && RUNTIME_STYLE != 3) {
			RUNTIME_STYLE = 1;
		}
 
		numtostr(RUNTIME_STYLE, Data);
		set_vaultdata("RUNTIME_STYLE", Data);
	}
 
	snprintf(Text, MAX_TEXT_LENGTH, "[ADMIN] RUNTIME STYLE is %d: %s", RUNTIME_STYLE, RT_STYLE[RUNTIME_STYLE - 1]);
	selfmessage(Text);	
 
	log_command(User, Command, Data);
 
	return PLUGIN_HANDLED;
}
 
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
 
	strstripquotes(Data);
	if (streq(Data, "runtime") == 1) {
		run_time();
	}
	return PLUGIN_CONTINUE;
}
 
 
public run_time() {
	new Text[MAX_DATA_LENGTH];
	new i_elap_systime;
	new i_days;
	new i_hours;
	new i_mins;
 
	if((RUNTIME_STYLE == 1) || (RUNTIME_STYLE == 3)) {
		snprintf(Text, MAX_TEXT_LENGTH, "This map running since: %s", t_time);		
		say(Text);
		snprintf(Text, MAX_TEXT_LENGTH, "%s", t_date);
		say(Text);
	}
	if((RUNTIME_STYLE == 2) || (RUNTIME_STYLE == 3)) {
 
		i_elap_systime = maptime(0)/60;					// minutes
 
		i_days = i_elap_systime / min_in_day;
		i_hours = (i_elap_systime % min_in_day) / min_in_hr;
		i_mins = (i_elap_systime % min_in_day) % min_in_hr;
 
		if(RUNTIME_STYLE == 2) {
			snprintf(Text, MAX_TEXT_LENGTH, "This map running for: %d days : %d hours : %d minutes", i_days, i_hours, i_mins);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "%d days : %d hours : %d minutes", i_days, i_hours, i_mins);
		}
		say(Text);
	}
	return PLUGIN_HANDLED;
}
 
 
public plugin_init() {
	plugin_registerinfo("Server Run Time Plugin", "Says total map run time.", STRING_VERSION);
	plugin_registercmd("admin_runtime_time", "admin_runtime_time", ACCESS_RUNTIME,"admin_runtime_time <minutes>: Set the time between RUNTIME messages.");
	plugin_registercmd("admin_runtime_style", "admin_runtime_style", ACCESS_RUNTIME,"admin_runtime_style [1|2|3]: Set the time display style.");
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
 
	new VaultData[MAX_DATA_LENGTH];	
 
	if(get_vaultdata("RUNTIME_TIME", VaultData, MAX_DATA_LENGTH) != 0) {
		RUNTIME_TIME = strtonum(VaultData);
		if(RUNTIME_TIME < 0) {
			RUNTIME_TIME = 0;
		}
	} else {
		RUNTIME_TIME = 45;
		set_vaultdata("RUNTIME_TIME", "45");
	}
 
	if(get_vaultdata("RUNTIME_STYLE", VaultData, MAX_DATA_LENGTH) != 0) {
		RUNTIME_STYLE = strtonum(VaultData);
		if(RUNTIME_STYLE != 1 && RUNTIME_STYLE != 2 && RUNTIME_STYLE != 3) {
			RUNTIME_STYLE = 1;
		}
	} else {
		RUNTIME_STYLE = 1;
		set_vaultdata("RUNTIME_STYLE", "1");
	}
 
	// for formatted text
	servertime(t_time, MAX_DATA_LENGTH, "%I:%M %p");
	servertime(t_date, MAX_DATA_LENGTH, "%A, %B %d %Y");
 
	// use seconds for timer
	t_index = set_timer("run_time",RUNTIME_TIME * 60,99999);
 
	return PLUGIN_CONTINUE;
}