/* plugin_dotw by Sloth         
 
This plugin will change the active mapcycle file depending on the day-of-the-week (DOTW).
 
Usage: 	admin_dotw						// get status of DOTW mapcycle processing.
		admin_dotw <on|1> | <off|0>		// enable/disable DOTW mapcycle processing.
		admin_dotw_days					// get which days to process
		admin_dotw_days <sun mon ...>	// set which days to process.
		admin_mapfile					// get the active mapcycle file
		admin_mapfile <file name>		// set the active mapcycle file
 
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 the DOTW On/Off toggle and the DOTW_DAYS through
   map changes and server restarts. You can set this with the following line: 
		   admin_vault_file "vault.ini"
 
3) To setup the "vault.ini", either add the DOTW and DOTW_DAYS values manually
   or by supplying values to the admin_dotw and admin_dotw_days functions.
 
4) You must create a mapcycle file for each specific day to be processed. Valid
   mapcycle files are text files containing map names (like mapcycle.txt), starting
   with "mapcycle" and ending in a 3-letter extension corresponding to the
   day-of-the-week. Ex. mapcycle.tue, mapcycle.sun ...
 
5) At every map change, if plugin_dotw sees that the current day-of-the-week is in
   the vault file then that mapcycle file is activated. If not, the default 
   "mapcycle.txt" is activated, if not already. At any time DOTW processing is disabled,
   the default "mapcycle.txt" becomes active immediately.
   Admin_dotw_days accepts a single line of text of days-of-the-week separated by a
   space(s). Days may either be 3-letter abbreviations or the day-of-the-week spelled
   out or a combination of both. Ex. "sun Mon tuesday wed Thursday friday Sat"
 
Acknowledgements:
-------------
BlueDemon for the idea.
 
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_DOTW 4096	// security level required to enable DOTW processing
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
new DOTW[MAX_DATA_LENGTH];
 
 
public admin_dotw(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_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) {
		if(check_param(Data) == 1) {
			strcpy(DOTW, "On", MAX_DATA_LENGTH);
			check_dotw();
		} else {
			strcpy(DOTW, "Off", MAX_DATA_LENGTH);
			get_serverinfo("mapcyclefile", Data, MAX_TEXT_LENGTH);
			if(strcasestr(Data, "txt") == -1)
				change_mapfile("mapcycle.txt");
		}
		set_vaultdata("DOTW", DOTW);
	} else {
		get_vaultdata("DOTW", DOTW, MAX_DATA_LENGTH);
	}
 
	snprintf(Data, MAX_DATA_LENGTH, "DOTW mapfile cycling: %s", DOTW);
	selfmessage(Data);
 
	log_command(User, Command, Data);
 
	return PLUGIN_HANDLED;
}
 
 
public admin_dotw_days(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	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);
 
	if (strlen(Data) > 0) {
		set_vaultdata("DOTW_DAYS", Data);
	} else {
		get_vaultdata("DOTW_DAYS", Data, MAX_TEXT_LENGTH);
	}
 
	snprintf(Text, MAX_TEXT_LENGTH, "DOTW Days: ^"%s^"", Data);
	selfmessage(Text);
 
	log_command(User, Command, Data);
 
	return PLUGIN_HANDLED;
}
 
 
public admin_mapfile(HLCommand, HLData, HLUserName, UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_TEXT_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	if (strlen(Data) > 0) {
		change_mapfile(Data);
	}
 
	get_serverinfo("mapcyclefile", Data, MAX_TEXT_LENGTH);
	snprintf(Text, MAX_TEXT_LENGTH, "Active mapcycle file is ^"%s^"", Data);
	selfmessage(Text);
 
	log_command(User, Command, Data);
 
	return PLUGIN_HANDLED;
}
 
 
check_dotw() {
	new VaultData[MAX_TEXT_LENGTH];	
	new Text[MAX_DATA_LENGTH];
	new LogMessage[MAX_TEXT_LENGTH];
	new Day[MAX_DATA_LENGTH];
	new WeekDays[22] = "sunmontuewedthufrisat";
	new t_day[MAX_DATA_LENGTH];
	new i_day;
 
	servertime(t_day, MAX_DATA_LENGTH, "%a");
 
	// find the current 3-letter day in the WeekDays string
	i_day = strcasestr(WeekDays, t_day);
 
		// extract it and Day is now lcase
	strncpy(Day, WeekDays[i_day], 3, MAX_DATA_LENGTH);
 
	get_serverinfo("mapcyclefile", Text, MAX_TEXT_LENGTH);
	get_vaultdata("DOTW_DAYS", VaultData, MAX_TEXT_LENGTH);
 
	// See if the current day is in the DOTW_DAYS vault string, ignore case
	if(strcasestr(VaultData, Day) > -1) {
		// is it already active
		if(strcasestr(Text, Day) == -1) {
			snprintf(Text, MAX_DATA_LENGTH, "mapcycle.%s", Day);
			change_mapfile(Text);
		}
	} else {
		// is mapcycle.txt already active
		if(strcasestr(Text, "txt") == -1) {
			strcpy(Text, "mapcycle.txt", MAX_DATA_LENGTH);
			change_mapfile(Text);
		}
	}
 
	get_serverinfo("mapcyclefile", Text, MAX_TEXT_LENGTH);
	snprintf(LogMessage, MAX_TEXT_LENGTH, "Active mapcycle file is ^"%s^"", Text);
	log(LogMessage);
}	
 
 
change_mapfile(mapfile[]) {
	new Command[MAX_COMMAND_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	if(fileexists(mapfile)) {
		set_serverinfo("mapcyclefile", mapfile);
		snprintf(Command, MAX_COMMAND_LENGTH, "mapcyclefile ^"%s^"", mapfile);
		exec(Command);
	} else {
		// the mapfile doesn't exist
		snprintf(Text, MAX_TEXT_LENGTH, "Could not find file: ^"%s^"", mapfile);
		log(Text);
 
		// so change to 'mapcycle.txt'
		strcpy(mapfile, "mapcycle.txt", MAX_DATA_LENGTH);
		if(fileexists(mapfile)) {
			set_serverinfo("mapcyclefile", mapfile);
			snprintf(Command, MAX_COMMAND_LENGTH, "mapcyclefile ^"%s^"", mapfile);
			exec(Command);
		}
	}
}
 
 
public admin_time(HLCommand, HLData, HLUserName, UserIndex) {
	new Text[MAX_TEXT_LENGTH];
	new t_time[MAX_DATA_LENGTH];
	new t_date[MAX_DATA_LENGTH];
 
	servertime(t_time, MAX_DATA_LENGTH, "%I:%M %p");
	servertime(t_date, MAX_DATA_LENGTH, "%A, %B %d %Y");
 
	snprintf(Text, MAX_TEXT_LENGTH, "%s %s", t_time, t_date);		
	selfmessage(Text);
 
	return PLUGIN_HANDLED;
}
 
 
public plugin_init() {
	plugin_registerinfo("Day-of-the-week specific mapcycle","Use a different mapcycle file depending on the DOTW", STRING_VERSION);
	plugin_registercmd("admin_dotw", "admin_dotw", ACCESS_DOTW,"admin_dotw <^"on^" | ^"off^">: Turn DOTW processing on or off");
	plugin_registercmd("admin_dotw_days", "admin_dotw_days", ACCESS_DOTW,"admin_dotw_days <^"sun mon tue wed thu fri sat^">: Days with specific mapfiles");
	plugin_registercmd("admin_mapfile", "admin_mapfile", ACCESS_DOTW,"admin_mapfile <file name>: Change active map file");
	plugin_registercmd("admin_time", "admin_time", ACCESS_DOTW,"admin_time: Display current date and time in console");
 
	new VaultData[MAX_TEXT_LENGTH];	
 
	get_vaultdata("DOTW", VaultData, MAX_TEXT_LENGTH);
 
	if (strlen(VaultData) > 0) {
		if(check_param(VaultData) == 1) {
			check_dotw();
		}
	}
 
	return PLUGIN_CONTINUE;
}