#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define WMCHAT_SPEAK 1
#define WMCHAT_TL 1
#define WMCHAT_TTELL 1
#define WMCHAT_BASE 0
 
//<----------Dont Edit Below this Line------------->//
 
new TV[2];
#define MAX_TIME_LENGTH 6
#define MAX_NUMBERSTR_LENGTH 11
new CLOCKNAME[20][MAX_NAME_LENGTH];
new STRING_VERSION[] = "1.9";
public plugin_init() {
	plugin_registerinfo("WMs Chat Plugin", "Nextmap, Currentmap, Time, TimeRemaning, and TimeInto", STRING_VERSION);
	//
	plugin_registercmd("say", "HandleSay", ACCESS_ALL);
	plugin_registercmd("admin_currentmap", "admin_currentmap", ACCESS_ALL, "admin_currentmap : Displays the next/currentmap");
	plugin_registercmd("admin_timeinto", "admin_timeinto", ACCESS_ALL, "admin_timeinto : Displays the time already passed on map");
	plugin_registercmd("admin_time", "admin_time", ACCESS_ALL, "admin_time : Displays the current server time");
	// replacement for plugin_base code, optional(see WMCHAT_BASE)
	#if WMCHAT_BASE
	plugin_registercmd("admin_timeleft", "admin_timeleft", ACCESS_ALL, "admin_timeleft : Displays the timeleft on map");
	plugin_registercmd("admin_nextmap", "admin_nextmap", ACCESS_ALL, "admin_nextmap : Displays the next/currentmap");
	#endif
	// all numbers that may appear in the ones place or teens
	strcpy(CLOCKNAME[1], "one ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[2], "two ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[3], "three ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[4], "four ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[5], "five ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[6], "six ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[7], "seven ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[8], "eight ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[9], "nine ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[10], "ten ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[11], "eleven ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[12], "twelve ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[13], "thirteen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[14], "fourteen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[15], "fifteen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[16], "sixteen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[17], "seventeen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[18], "eighteen ", MAX_NUMBERSTR_LENGTH);
	strcpy(CLOCKNAME[19], "nineteen ", MAX_NUMBERSTR_LENGTH);
	// timer set up for 1/2 hour time and timeleft announce
	#if WMCHAT_TL
	TV[0] = set_timer("wmtl_check", 1, 99999);
	#endif
	#if WMCHAT_TTELL
	TV[1] = set_timer("wmtime_check", 60, 99999);
	#endif
	//
	return PLUGIN_CONTINUE;
}
 
// time console call
public admin_time() {
	wm_time(0);
	return PLUGIN_HANDLED;
}
 
// replacement for plugin_base code, optional(see WMCHAT_BASE)
#if WMCHAT_BASE
// timeleft console call
public admin_timeleft() {
	wm_timeleft(0);
	return PLUGIN_HANDLED;
}
 
// nextmap console call
public admin_nextmap() {
	wm_nextmap(0);
	return PLUGIN_HANDLED;
}
#endif
 
// timeinto console call
public admin_timeinto() {
	wm_timeinto(0);
	return PLUGIN_HANDLED;
}
 
// currentmap console call
public admin_currentmap() {
	wm_nextmap(0);
	return PLUGIN_HANDLED;
}
 
//
// if activated, tells the timeremaing at 20, 10, 5, and 1 min remaining.  5 sec countdown to end
//
#if WMCHAT_TL
public wmtl_check() {
	new iTmp = maptime(1, 0);
	switch(iTmp) {
		case 1200: {wm_timeleft(1);}
		case 600: {wm_timeleft(1);}
		case 300: {wm_timeleft(1);}
		case 150: {wm_timeleft(1);}
		case 60: {wm_timeleft(1);}
		case 5: {execall("speak five");}
		case 4: {execall("speak four");}
		case 3: {execall("speak three");}
		case 2: {execall("speak two");}
		case 1: {
			execall("speak one");
			kill_timer(TV[0]);
			kill_timer(TV[1]);
		}
	}
}
#endif
 
//
// if activated, system time check (if its on or 1/2 passed the hour, tell time)
//
#if WMCHAT_TTELL
public wmtime_check() {
	new sTime[MAX_NAME_LENGTH];
	new sMin[MAX_NAME_LENGTH];
	//
	servertime(sTime, MAX_NAME_LENGTH, "%H:%M");
	strsplit(sTime, ":", sTime, MAX_NAME_LENGTH, sMin, MAX_NAME_LENGTH);
	//
	if(streq(sMin, "00")) wm_time(1);
	else if(streq(sMin, "30")) wm_time(1);
}
#endif
 
// what the users say in text (mm1 only)
public HandleSay(HLCom, HLData, HLUser, UserIndex) {
	new data[MAX_DATA_LENGTH];
	//
	convert_string(HLData, data, MAX_DATA_LENGTH);
	strstripquotes(data);
	//
	if( strcasecmp(data, "time")==0) wm_time(1);
	else if( strcasecmp(data, "timeleft")==0 ) wm_timeleft(1);
	else if( strcasecmp(data, "timeinto")==0 ) wm_timeinto(1);
	else if( (strcasecmp(data, "nextmap")==0) || (strcasecmp(data, "currentmap")==0) ) wm_nextmap(1);
	//
	return PLUGIN_CONTINUE;
}
 
// handle nextmap call (0 - just to user, all else - tsay to all)
wm_nextmap(iType) {
	new sCur[MAX_NAME_LENGTH];
	new sNex[MAX_NAME_LENGTH];
	new sMsg[MAX_TEXT_LENGTH];
	nextmap(sNex, MAX_NAME_LENGTH);
	currentmap(sCur, MAX_NAME_LENGTH);
	if(!iType) {
		snprintf(sMsg, MAX_TEXT_LENGTH, "[ADMIN] CurrentMap: %s^n[ADMIN] NextMap: %s", sCur, sNex);
		selfmessage(sMsg);
	}
	else {
		snprintf(sMsg, MAX_TEXT_LENGTH, "CurrentMap: %s^nNextMap: %s", sCur, sNex);
		typesay(sMsg, 5, 10, 250, 10);
	}
}
 
// handle time call (0 - just to user, all else - tsay to all)
wm_time(iType) {
	new sTime[MAX_TIME_LENGTH];
	new sHr[MAX_NUMBER_LENGTH];
	new sMin[MAX_NUMBER_LENGTH];
	new sMsg[MAX_TEXT_LENGTH];
	new iTime, pm = FALSE;
	//
	servertime(sTime, MAX_TIME_LENGTH, "%H:%M");
	strsplit(sTime, ":", sHr, MAX_NUMBER_LENGTH, sMin, MAX_NUMBER_LENGTH);
	if(streq(sHr, "00")) strcpy(sHr, "12", 3);
	iTime = strtonum(sHr);
	if(iTime > 11) pm = TRUE;
	if(iTime > 12) {
		iTime-=12;
		numtostr(iTime, sHr);
	}
	//
	if(!iType) {
		if(pm) snprintf(sMsg, MAX_TEXT_LENGTH, "[ADMIN] CurrentTime[%s:%s] PM", sHr, sMin);
		else snprintf(sMsg, MAX_TEXT_LENGTH, "[ADMIN] CurrentTime[%s:%s] AM", sHr, sMin);
		selfmessage(sMsg);
	}
	else {
		if(pm) snprintf(sMsg, MAX_TEXT_LENGTH, "CurrentTime[%s:%s] PM", sHr, sMin);
		else snprintf(sMsg, MAX_TEXT_LENGTH, "CurrentTime[%s:%s] AM", sHr, sMin);
		typesay(sMsg, 5, 10, 10, 250);
		#if WMCHAT_SPEAK
			wm_speaknum(1, sTime);
		#endif
	}
}
 
// handle timeleft call (0 - just to user, all else - tsay to all)
wm_timeleft(iType) {
	if(!iType) {
		new sMsg[MAX_TEXT_LENGTH];
		new iSec = maptime(1, 0);
		new iTime;
		//
		iTime=iSec / 60;
		iSec = iSec - (iTime * 60);
		//
		numtostr(iSec, sMsg);
		if(iSec < 10) snprintf(sMsg, MAX_TEXT_LENGTH, "0%s", sMsg);
		//
		snprintf(sMsg, MAX_TEXT_LENGTH, "[ADMIN] TimeLeft[%i:%s]", iTime, sMsg);
		selfmessage(sMsg);
	} else {
		new sTl[MAX_NUMBER_LENGTH];
		new sMsg[MAX_TEXT_LENGTH];
		new iTl;
		//
		iTl = maptime(1, 0);
		numtostr(iTl, sTl);
		iTl/=60;
		if( (iTl>0) && (iTl<200) ) {
			snprintf(sMsg, MAX_TEXT_LENGTH, "%i minutes remaining", iTl);
			typesay(sMsg, 5, 250, 250, 250);
			#if WMCHAT_SPEAK
				wm_speaknum(2, sTl);
			#endif
		} else typesay("*No Time Limit*", 5, 250, 250, 250);
	}
}
 
// handle timeinto call (0 - just to user, all else - tsay to all)
wm_timeinto(iType) {
	if(!iType) {
		new sMsg[MAX_TEXT_LENGTH];
		new iSec = maptime(0, 0);
		new iTime;
		//
		iTime=iSec / 60;
		iSec = iSec - (iTime * 60);
		//
		numtostr(iSec, sMsg);
		if(iSec < 10) snprintf(sMsg, MAX_TEXT_LENGTH, "0%s", sMsg);
		//
		snprintf(sMsg, MAX_TEXT_LENGTH, "[ADMIN] TimeInto[%i:%s]", iTime, sMsg);
		selfmessage(sMsg);
	} else {
		new sTl[MAX_NUMBER_LENGTH];
		new sMsg[MAX_TEXT_LENGTH];
		new iTl;
		//
		iTl = maptime(0, 0);
		numtostr(iTl, sTl);
		iTl/=60;
		snprintf(sMsg, MAX_TEXT_LENGTH, "%i minutes into level", iTl);
		typesay(sMsg, 5, 250, 250, 250);
		//
		#if WMCHAT_SPEAK
			wm_speaknum(3, sTl);
		#endif
	}
 
}
 
// handle numbers when they need to be spoken
//(1 - speak servertime, 2 - speak timeleft, all else - speak timeinto)
wm_speaknum(iType, sNum[]) {
	new msg[MAX_TEXT_LENGTH];
	new sMin[MAX_NUMBER_LENGTH];
	new sOnes[MAX_NUMBERSTR_LENGTH];
	new sTens[MAX_NUMBERSTR_LENGTH];
	new iTmp, pm = FALSE;
	if(iType == 1) {
		strsplit(sNum, ":", sNum, MAX_NUMBER_LENGTH, sMin, MAX_NUMBER_LENGTH);
		//
		if(streq(sNum, "00")) strcpy(sNum, "12", 3);
		iTmp = strtonum(sNum);
		if(iTmp > 11) pm = TRUE;
		if(iTmp > 12) {
			iTmp-=12;
			numtostr(iTmp, sNum);
		}
		//
		iTmp = strtonum(sMin);
	} else {
		iTmp = strtonum(sNum);
		iTmp/=60;
		numtostr(iTmp, sMin);
	}
	if( (iTmp / 50) > 0 ) {
		strcpy(sTens, "fifty ", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	else if( (iTmp / 40) > 0 ) {
		strcpy(sTens, "fourty ", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	else if( (iTmp / 30) > 0 ) {
		strcpy(sTens, "thirty ", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	else if( (iTmp / 20) > 0 ) {
		strcpy(sTens, "twenty ", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	else if( (iTmp / 10) > 0 ) {
		strcpy(sTens, "", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		iTmp += 10;
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	else {
		strcpy(sTens, "", MAX_NUMBERSTR_LENGTH);
		iTmp = strtonum(sMin[1]);
		if(iTmp==0) iTmp = strtonum(sMin[0]);
		strcpy(sOnes, CLOCKNAME[iTmp], MAX_NUMBERSTR_LENGTH);
	}
	if(iType == 1) {
		iTmp = strtonum(sNum);
		if(pm == TRUE) snprintf(msg, MAX_TEXT_LENGTH, "speak ^"fvox/bell time_is_now %s%s%spm^"", CLOCKNAME[iTmp], sTens, sOnes);
		else snprintf(msg, MAX_TEXT_LENGTH, "speak ^"fvox/bell time_is_now %s%s%sam^"", CLOCKNAME[iTmp], sTens, sOnes);
		execall(msg);
	} else if(iType == 2) {
		snprintf(msg, MAX_TEXT_LENGTH, "speak ^"fvox/%s%sminutes remaining^"", sTens, sOnes);
		execall(msg);
	} else {
		snprintf(msg, MAX_TEXT_LENGTH, "speak ^"%s%sminutes in two level^"", sTens, sOnes);
		execall(msg);
	}
}
 
// run commands on all users, used for speak commands in this plugin
execall(cmd[]) {
	new user[MAX_NAME_LENGTH];
	new i;
	for(i = 1; i<=maxplayercount(); i++) {
		if(playerinfo(i, user, MAX_NAME_LENGTH)) {
			execclient(user, cmd);
		}
	}
}