/* plugin_jack9_chime.sma
   Server Clock Chime Plugin
 
   Coded by Jack9
   "Powered by AdminMod"
 
   Almost entire program ripped from plugin_method_time.sma
   I asked dio if he knew of this plugin, he didnt respond, so I 
   found a suitable template and reworked it (method_time) in about
   2 hours...and made it linux friendly of course so that this could
   be made available to all. I also "cleaned up" as much of method's
   code as I had time.
 
   Notes: 
   All (server)time formats derived from strftime() C/Unix function.
   Check 'man strftime' since SMALL has no reference to it in its manual. 
   Extranneous variables have been left for completeness' sake.
   I have left all my test code (commented out) so that others may use
   it as need be.
 
   jack9@jack9.org
 
   Everyone knows me.
   I am Jack9
   Often wrong but never in doubt.
 
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
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];
 
/* Used to determine if a vocal message has been used in the last
   "arbitrary" amount of time, primarily to prevent overlaps.
*/
new iDelay = 0;
 
public strnsplit(a[],b[],n,m){
	new i;
	for(i=n;i<=m;i++) 
		a[i-n]=b[i];
	return 1;
}
 
/* The all important function */
public SpeakTime(WithSpeech){
 
/* Check for overlap */
	if (systemtime() > iDelay) {
		iDelay = systemtime() + 60;
	} else {
		return;
	}
 
	new OUTSTRING[300];
	new words[5][30];
	new PlaceHolder[MAX_DATA_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new Team;
	new WONID;
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Text[MAX_DATA_LENGTH];
 
/* Get all the digits of the time. This is a BAD way to do it.
   Since while you are getting all these values, others could change,
   like trying to get all these values at 11:59:59 would cause bad
   data...should have gotten the time once and split it. If this was for
   money, I would do it, but a plugin is really not that important.
*/
 
/* Method did all this. */
	servertime(THour, MAX_DATA_LENGTH, "%I");
	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");
 
	if(streq(THour, "12")==1) {
		strncpy(words[1], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "11")==1) {
		strncpy(words[1], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "10")==1) {
		strncpy(words[1], "ten ", strlen("ten "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "09")==1) {
		strncpy(words[1], "nine ", strlen("nine "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "08")==1) {
		strncpy(words[1], "eight ", strlen("eight "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "07")==1) {
		strncpy(words[1], "seven ", strlen("seven "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "06")==1) {
		strncpy(words[1], "six ", strlen("six "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "05")==1) {
		strncpy(words[1], "five ", strlen("five "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "04")==1) {
		strncpy(words[1], "four ", strlen("four "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "03")==1) {
		strncpy(words[1], "three ", strlen("three "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "02")==1) {
		strncpy(words[1], "two ", strlen("two "),MAX_DATA_LENGTH);
	}
	if(streq(THour, "01")==1) {
		strncpy(words[1], "one ", strlen("one "),MAX_DATA_LENGTH);
	}
	strnsplit(PlaceHolder, TMinute, 0, 1);
	if(PlaceHolder[0] == '1') {
		strnsplit(PlaceHolder, TMinute, 1, 2);
		if(PlaceHolder[0] == '0') {
			strncpy(words[2], "ten ", strlen("ten "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '1') {
			strncpy(words[2], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '2') {
			strncpy(words[2], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);
		}	
		if(PlaceHolder[0] == '3') {
			strncpy(words[2], "thirteen ", strlen("thirteen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '4') {
			strncpy(words[2], "fourteen ", strlen("fourteen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '5') {
			strncpy(words[2], "fifteen ", strlen("fifteen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '6') {
			strncpy(words[2], "sixteen ", strlen("sixteen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '7') {
			strncpy(words[2], "seventeen ", strlen("seventeen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '8') {
			strncpy(words[2], "eighteen ", strlen("eighteen "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '9') {
			strncpy(words[2], "nineteen ", strlen("nineteen "),MAX_DATA_LENGTH);
		}
	}else{	
		strnsplit(PlaceHolder, TMinute, 0, 2);
		if(PlaceHolder[0] == '2') {
			strncpy(words[2], "twenty ", strlen("twenty "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '3') {
			strncpy(words[2], "thirty ", strlen("thirty "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '4') {
			strncpy(words[2], "fourty ", strlen("fourty "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '5') {
			strncpy(words[2], "fifty ", strlen("fifty "),MAX_DATA_LENGTH);
		}else{
			if ( (PlaceHolder[0] == '0') && (PlaceHolder[1] != '0') ) {
		                strncpy(words[2], "boop ", strlen("boop "),MAX_DATA_LENGTH);
		        }                                                                    
		}
		strnsplit(PlaceHolder, TMinute, 1, 2);
		if(PlaceHolder[0] == '1') {
			strncpy(words[3], "one ", strlen("one "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '2') {
			strncpy(words[3], "two ", strlen("two "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '3') {
			strncpy(words[3], "three ", strlen("three "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '4') {
			strncpy(words[3], "four ", strlen("four "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '5') {
			strncpy(words[3], "five ", strlen("five "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '6') {
			strncpy(words[3], "six ", strlen("six "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '7') {
			strncpy(words[3], "seven ", strlen("seven "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '8') {
			strncpy(words[3], "eight ", strlen("eight "),MAX_DATA_LENGTH);
		}
		if(PlaceHolder[0] == '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);
	}
 
/* With all this uncertainty regarding systemtime() and the actual time, if
   2 was passed to SpeakTime(), then I WANT IT TO SAY IT'S 4:20, regardless.
*/	
	if (WithSpeech == 2) {
		snprintf(OUTSTRING, MAX_DATA_LENGTH, "speak ^"the time is now 4:20 up in the west side^"", words[1], words[2], words[3]);
		for(i=1; i<=maxplayers; i++) {
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
				execclient(Target,OUTSTRING);
			}
		}
	}
/* end of Method's code */
 
/* The adminmod forums are next to useless. Female voice is done using
   fvox/  - and the forum denizens said it couldnt be done. Morons.
*/
	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);
			}
		}
	}
 
	snprintf(Text, MAX_TEXT_LENGTH, "[ Current Server Time: %s:%s %s ]", THour, TMinute, TValue);
	say(Text);
	snprintf(Text, MAX_TEXT_LENGTH, "[ %s, %s %s %s ]", TWDay, TMonth, TDay, TYear);
	say(Text);
}
 
/* For recognizing partial matched strings in player messages in game  */
/*
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, "chime") != -1) && (strcasestr(Data, "test") != -1) ) { 
		if(systemtime() < iDelay){
			return PLUGIN_CONTINUE;
  		} else {
			SpeakTime(1);
			iDelay = systemtime();
			iDelay += 60;
		}
	}
        if ( (strcasestr(Data, "chim2") != -1) && (strcasestr(Data, "est") != -1) ) { 
                if (systemtime() < iDelay) {                                         
			SpeakTime(0);
			return PLUGIN_CONTINUE;
  		} else {
			SpeakTime(2);
			iDelay = systemtime();
			iDelay += 60;
		}
	}
	return PLUGIN_CONTINUE;
}
*/
 
public chime_check()
{
	new Hour_dec[MAX_DATA_LENGTH];
	new Minute_dec[MAX_DATA_LENGTH];
 
/* Hour in decimal 00-12 including 01-09 for singles */
	servertime(Hour_dec, MAX_DATA_LENGTH, "%I");	
/* Minute in decimal of course 00-59 */
	servertime(Minute_dec, MAX_DATA_LENGTH, "%M");
 
/*	Bah, old code that I used for testing. Bad way to do it anyway. */	
/*	if(streq(Minute_dec,"00")==1) {
		if(streq(Hour_dec, "01")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "02")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "03")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "04")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "05")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "06")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "07")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "08")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "09")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "10")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "11")==1) {
			SpeakTime(1);
		}
		if(streq(Hour_dec, "12")==1) {
			SpeakTime(1);
		}
	}
*/
 
/* 420 is the police code in California, USA (only?) used to indicate
   an arrest for marijuana possesion. Therefore, 4:20am/pm is a
   recognized "important" time for many ppl as a symbolic cultural thing.
   You can of course just edit line number 220 (or about there depending
   on how you edit this filefrom "west" to "east" for your server if
   it's more appropriate. If I wanted to be 'cool', I would have made it
   line 420 :p
*/
 
/* If the minute is 20, then test if hour is 4 */
	if(streq(Minute_dec, "20")==1){
		if(streq(Hour_dec, "04")==1){
			SpeakTime(2);
		}
	}
 
/* Announce time every 30 min */
	if(streq(Minute_dec, "30")==1){
		SpeakTime(1);
	}
	if(streq(Minute_dec, "00")==1){
		SpeakTime(1);
	}
} 
 
/* Since I've never coded a plugin before, I assume this is necessary as a 
   default starting function, like 'public main(){}' in C/C++
*/
public plugin_init(){
	plugin_registerinfo("Server Chime Plugin", "Speaks server time every hour or so.", STRING_VERSION);
 
/* Only needed for testing using "said" triggers */
/*
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
*/
 
/* Using 60 second intervals does not work with a large number of plugins
   on slower computers or computer with 64 megs or less of ram.
   iDelay prevents overlaps.
*/ 
	set_timer("chime_check",45,999999);
	return PLUGIN_CONTINUE;
}