/*
plugin_simple_time.sma
Simple Server Time Plugin
 
Original Code by [geist.method]
Modifed by Monolyth <|L|>
 
 
Purpose for Recode:
I have been using this plugin for about 2 years, and it just started annoying me having all
these extra features that I never used, not to mention after looking at all the "if" statements
that was used originally I almost puked. No offense to geist.method, gotta start somewhere's mate.
And without his original coding I could not have done all the changes I did. Basically I wanted to
tweak this bad boy down to something that "most" admin's may find handy without all the extra baggage.
So without further ado here's the skimpy.
 
Additions:
-admin_timeregion <region>: Easy time region specification, and changes without re-compiling.
 
Improvements:
-Replaced extreme amounts of "if-then" statements with switch statements to improve efficiency.
-Fixed stupid EOL warnings that have so many people whining.
 
Changes:
-Removed stopwatch and alarm functionality (is it really that useful?)
 
 
Notes:
Be sure to change the access level for the timeregion function to what u you want.
 
If you wish to change the default voice delay scroll to bottom and change:
iDelay += xx;
Where xx is the amount of time (in seconds) you wish the script to wait till it uses the voice again.
 
Any bugs, problems, issues - E-Mail: dummy@monolyth.org
 
Peace!
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_TIMEREGION 131071      // Defines access level to set the time region. See Admin-Mod Documentation for Levels.
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new THour[MAX_DATA_LENGTH];
new TMinute[MAX_DATA_LENGTH];
new TValue[MAX_DATA_LENGTH];
new TWDay[MAX_DATA_LENGTH];
new TMonth[MAX_DATA_LENGTH];
new TDay[MAX_DATA_LENGTH];
new TYear[MAX_DATA_LENGTH];
new iDelay = 0;
 
 
public strnsplit(a[],b[],n,m)
{
	new i;
 
	for(i=n;i<=m;i++)
	{ 
		a[i-n]=b[i];
	}
 
	return 1;
}
 
 
public timeregion(HLCommand, HLData, HLUserName, UserIndex)
{
 	new time_region[MAX_DATA_LENGTH];
 
 	convert_string(HLData, time_region, MAX_DATA_LENGTH);
 
	if(set_vaultdata("time_region",time_region)!=0)
	{
		selfmessage("The time region has now been set.");
	}
 
 	return PLUGIN_HANDLED;
}
 
 
public SpeakTime(WithSpeech)
{
	new OUTSTRING[300];
	new words[5][30];
	new i;
	new maxplayers = maxplayercount();
	new Team;
	new time_region[MAX_DATA_LENGTH];
	new PlaceHolder[MAX_DATA_LENGTH];
	new iPlaceHolder[MAX_DATA_LENGTH];
	new WONID;
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Text[MAX_DATA_LENGTH];
	servertime(THour, MAX_DATA_LENGTH, "%I");
	new iTHour = strtonum(THour);
	servertime(TMinute, MAX_DATA_LENGTH, "%M");
	servertime(TValue, MAX_DATA_LENGTH, "%p");
	servertime(TWDay, MAX_DATA_LENGTH, "%A");
	servertime(TMonth, MAX_DATA_LENGTH, "%B");
	servertime(TDay, MAX_DATA_LENGTH, "%d");
	servertime(TYear, MAX_DATA_LENGTH, "%Y");
 
	strnsplit(PlaceHolder, TMinute, 0, 1);
	iPlaceHolder[0] = strtonum(PlaceHolder[0]);
	strnsplit(PlaceHolder, TMinute, 1, 2);
	iPlaceHolder[1] = strtonum(PlaceHolder[0]);
 
	switch(iTHour)
	{
		case 12:{strncpy(words[1], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);}
		case 11:{strncpy(words[1], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);}
		case 10:{strncpy(words[1], "ten ", strlen("ten "),MAX_DATA_LENGTH);}
		case 09:{strncpy(words[1], "nine ", strlen("nine "),MAX_DATA_LENGTH);}
		case 08:{strncpy(words[1], "eight ", strlen("eight "),MAX_DATA_LENGTH);}
		case 07:{strncpy(words[1], "seven ", strlen("seven "),MAX_DATA_LENGTH);}
		case 06:{strncpy(words[1], "six ", strlen("six "),MAX_DATA_LENGTH);}
		case 05:{strncpy(words[1], "five ", strlen("five "),MAX_DATA_LENGTH);}
		case 04:{strncpy(words[1], "four ", strlen("four "),MAX_DATA_LENGTH);}
		case 03:{strncpy(words[1], "three ", strlen("three "),MAX_DATA_LENGTH);}
		case 02:{strncpy(words[1], "two ", strlen("two "),MAX_DATA_LENGTH);}
		case 01:{strncpy(words[1], "one ", strlen("one "),MAX_DATA_LENGTH);}
	}
 
	if((iPlaceHolder[0] >= 10) && (iPlaceHolder[0] <= 20))
	{
		switch(iPlaceHolder[1])
		{
			case 0:{strncpy(words[2], "ten ", strlen("ten "),MAX_DATA_LENGTH);}
			case 1:{strncpy(words[2], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);}
			case 2:{strncpy(words[2], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);}
			case 3:{strncpy(words[2], "thirteen ", strlen("thirteen "),MAX_DATA_LENGTH);}
			case 4:{strncpy(words[2], "fourteen ", strlen("fourteen "),MAX_DATA_LENGTH);}
			case 5:{strncpy(words[2], "fifteen ", strlen("fifteen "),MAX_DATA_LENGTH);}
			case 6:{strncpy(words[2], "sixteen ", strlen("sixteen "),MAX_DATA_LENGTH);}
			case 7:{strncpy(words[2], "seventeen ", strlen("seventeen "),MAX_DATA_LENGTH);}
			case 8:{strncpy(words[2], "eighteen ", strlen("eighteen "),MAX_DATA_LENGTH);}
			case 9:{strncpy(words[2], "nineteen ", strlen("nineteen "),MAX_DATA_LENGTH);}
		}
 
	}
 
	if( iPlaceHolder[0] >= 20 )
	{	
		switch(iPlaceHolder[0])
		{
			case 20,21,22,23,24,25,26,27,28,29:{strncpy(words[2], "twenty ", strlen("twenty "),MAX_DATA_LENGTH);}
			case 30,31,32,33,34,35,36,37,38,39:{strncpy(words[2], "thirty ", strlen("thirty "),MAX_DATA_LENGTH);}
			case 40,41,42,43,44,45,46,47,48,49:{strncpy(words[2], "fourty ", strlen("fourty "),MAX_DATA_LENGTH);}
			case 50,51,52,53,54,55,56,57,58,59:{strncpy(words[2], "fifty ", strlen("fifty "),MAX_DATA_LENGTH);}
		}
	}
	else if ((iPlaceHolder[0] < 10) && (iPlaceHolder[0] > 0))
	{
               	strncpy(words[2], "boop ", strlen("boop "),MAX_DATA_LENGTH);
	}
 
 
	if( ((iPlaceHolder[0] < 10) || (iPlaceHolder[0] > 20)) && (iPlaceHolder[1] > 0))
	{
		switch(iPlaceHolder[1])
		{
			case 1:{strncpy(words[3], "one ", strlen("one "),MAX_DATA_LENGTH);}
			case 2:{strncpy(words[3], "two ", strlen("two "),MAX_DATA_LENGTH);}
			case 3:{strncpy(words[3], "three ", strlen("three "),MAX_DATA_LENGTH);}
			case 4:{strncpy(words[3], "four ", strlen("four "),MAX_DATA_LENGTH);}
			case 5:{strncpy(words[3], "five ", strlen("five "),MAX_DATA_LENGTH);}
			case 6:{strncpy(words[3], "six ", strlen("six "),MAX_DATA_LENGTH);}
			case 7:{strncpy(words[3], "seven ", strlen("seven "),MAX_DATA_LENGTH);}
			case 8:{strncpy(words[3], "eight ", strlen("eight "),MAX_DATA_LENGTH);}
			case 9:{strncpy(words[3], "nine ", strlen("nine "),MAX_DATA_LENGTH);}
		}
 
	}
 
	if(streq(TValue, "AM")==1)
	{
		strncpy(words[4], "am", strlen("am"),MAX_DATA_LENGTH);
	}
 
	if(streq(TValue, "PM")==1)
	{
		strncpy(words[4], "pm", strlen("pm"),MAX_DATA_LENGTH);
	}
 
	if (WithSpeech == 2)
	{	
		snprintf(OUTSTRING, MAX_DATA_LENGTH, "speak ^"fvox/buzz buzz buzz alert time_is_now %s%s%s%s safe_day^"", words[1], words[2], words[3], words[4]);
		for(i=1; i<=maxplayers; i++)
		{
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1)
			{
				execclient(Target,OUTSTRING);
			}
		}
	}
 
	if (WithSpeech == 1)
	{
		snprintf(OUTSTRING, MAX_DATA_LENGTH, "speak ^"fvox/bell time_is_now %s%s%s%s^"", words[1], words[2], words[3], words[4]);
		for(i=1; i<=maxplayers; i++)
		{
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1)
			{
				execclient(Target,OUTSTRING);
			}
		}
	}
 
	if(get_vaultdata("time_region",time_region,MAX_TEXT_LENGTH) == 0)
	{
		strncpy(time_region[0],"UNSET", 6,MAX_DATA_LENGTH);
	}
 
	snprintf(Text, MAX_TEXT_LENGTH, "[ Current Server Time: %s:%s %s %s]", THour, TMinute, TValue, time_region[0]);
	say(Text);
	snprintf(Text, MAX_TEXT_LENGTH, "[ %s, %s %s %s ]", TWDay, TMonth, TDay, TYear);
	say(Text);
}
 
 
public HandleSay(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);
 
	strstripquotes(Data);
        if ( (strcasestr(Data, "the") != -1) && (strcasestr(Data, "time") != -1) )
	{ 
                if (systemtime() < iDelay)
		{
			SpeakTime(0);
			return PLUGIN_CONTINUE;
		}
		else
		{
			SpeakTime(1);
			iDelay = systemtime();
			iDelay += 30;
		}
	}
	return PLUGIN_CONTINUE;
}
 
 
public plugin_init()
{
	plugin_registerinfo("Server Time/Date Plugin", "Speaks the current server time.", STRING_VERSION);
 	plugin_registercmd("admin_timeregion", "timeregion", ACCESS_TIMEREGION, "admin_timeregion <region>: Add Custom time region to time plugin.");
 	plugin_registerhelp("admin_timeregion",ACCESS_TIMEREGION,"admin_timeregion <region>: Add Custom time region to time plugin.");
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say thetime: Speaks/displays current server time.");
 
 
	return PLUGIN_CONTINUE;
}