/* Sdal-Consgreet
 
Substitutes keywords with their correct value in consgreet.txt
*/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#pragma dynamic 4096
 
new STRING_VERSION[MAX_DATA_LENGTH] = "15.03.02";
 
#define ACCESS_CONSOLE 131072
#define MAX_COMMANDS 19
#define ADR_IP		0
#define ADR_MINUTE	1
#define ADR_SECOND	2
#define ADR_USER	3
#define ADR_TEAM1	4
#define ADR_TEAM2	5
#define ADR_TEAM3	6
#define ADR_TEAM4	7
#define ADR_BESTPLAYER	8
#define ADR_MOSTFRAGS	9
#define ADR_MAXPLAYER	10
#define ADR_CURPLAYER	11
#define ADR_NEXTMAP	12
#define ADR_CURMAP	13
#define ADR_WONID	14
#define ADR_DATE	15
#define ADR_ROUND	16
#define ADR_TEAM1SCORE	17
#define ADR_TEAM2SCORE	18  
 
new Keywords[MAX_COMMANDS][]=	{{"&ip&"},{"&timeleftmin&"},{"&timeleftsec&"},{"&user&"},{"&team1&"},{"&team2&"},{"&team3&"},{"&team4&"},{"&bestplayer&"},
				{"&mostfrags&"},{"&maxplayer&"},{"&curplayer&"},{"&nextmap&"},{"&currentmap&"},{"&wonid&"},{"&date&"},
				{"&round&"},{"&tscore&"},{"&ctscore&"}};
 
new Value[MAX_COMMANDS][MAX_TEXT_LENGTH];
 
new ConsText[MAX_TEXT_LENGTH]="addons/adminmod/config/consgreet/consgreet.txt";
 
new GameCommencing=0;
new checktime;
new origtimelimit;
 
new round=1;
new mostfrags=0;
 
public plugin_connect(HLUserName, HLIP ,UserIndex) {
 
	new time;
	new Minutes[MAX_NUMBER_LENGTH];
	new Seconds[MAX_NUMBER_LENGTH];
	new seconds;
 
	/*Alle Keyword-Variablen ermitteln*/
	convert_string(HLIP,Value[ADR_IP][0],30);
	/*Zeitermittlung*/
	if (getvar("mp_timelimit")*60+2 != origtimelimit){
        	checktime = systemtime()-(systemtime()- checktime);
        }
        time= origtimelimit-(systemtime()-checktime);
        snprintf(Minutes,MAX_NUMBER_LENGTH,"%i",time/60);
        seconds=time%60;
        if(seconds<10){
        	snprintf(Seconds,MAX_NUMBER_LENGTH,"0%i",seconds);
        }else{
        	snprintf(Seconds,MAX_NUMBER_LENGTH,"%i",seconds);
        }
        snprintf(Value[ADR_MINUTE],MAX_NUMBER_LENGTH,"%s:%s",Minutes,Seconds);
        numtostr(time,Value[ADR_SECOND][0]);
	convert_string(HLUserName,Value[ADR_USER][0],MAX_NAME_LENGTH);
        numtostr(getteamcount(1),Value[ADR_TEAM1][0]);
        numtostr(getteamcount(2),Value[ADR_TEAM2][0]);
        numtostr(getteamcount(3),Value[ADR_TEAM3][0]);
        numtostr(getteamcount(4),Value[ADR_TEAM4][0]);
        get_playerstats();
        numtostr(maxplayercount(),Value[ADR_MAXPLAYER][0]);
        numtostr(playercount(),Value[ADR_CURPLAYER][0]);
        nextmap(Value[ADR_NEXTMAP][0],MAX_NAME_LENGTH);
        currentmap(Value[ADR_CURMAP][0],MAX_NAME_LENGTH);
        servertime(Value[ADR_DATE][0], MAX_DATA_LENGTH, "%d.%m.%y %H:%M:%S");
 
        if(round==1){
		numtostr(1,Value[ADR_ROUND][0]);
		numtostr(0,Value[ADR_TEAM1SCORE][0]);
		numtostr(0,Value[ADR_TEAM2SCORE][0]);
	}
 
	new wonid;
        get_userWONID(Value[ADR_USER][0],wonid);
        numtostr(wonid,Value[ADR_WONID][0]);
 
        /*Funktionsvariablen*/
        new Data[MAX_DATA_LENGTH];
        new Text[MAX_TEXT_LENGTH];
        new iLine;
	new i;
	new j;
 
 
	/*consgreet.txt einlesen und Keywords vergleichen*/
	iLine=filesize(ConsText);
 
	for(i=1;i<=iLine;i++){
		readfile(ConsText,Data,i,MAX_DATA_LENGTH);
		if(strstr(Data,"&cvar=")!=-1){
			subst_cvar(Data,Text);
			strcpy(Data,Text,MAX_TEXT_LENGTH);
		}else{
			for(j=0;j<=MAX_COMMANDS;j++){
				strsubst(Data,Keywords[j],Value[j],MAX_DATA_LENGTH);
			}
		}
		consgreet(Data);
	}
 
	return PLUGIN_CONTINUE;
}
 
subst_cvar(String[],Output[]){
	new substText[MAX_TEXT_LENGTH];
	new substText2[MAX_TEXT_LENGTH];
	new strVar[MAX_TEXT_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new strPos;
	new iCount;
	new i;
	new iNum;
 
	iCount=strchr(String,38);
 
	for(i=0;i<=iCount;i++){
		strPos=strstr(String,"&cvar=");
		if(strPos==-1){
			break;
		}
		strsep(String,"&",substText,MAX_TEXT_LENGTH,strVar,MAX_TEXT_LENGTH);
		strcpy(strVar, strVar[strchr(strVar, '=')+1], MAX_TEXT_LENGTH);
		strsep(strVar,"&",strVar,MAX_TEXT_LENGTH,substText2,MAX_TEXT_LENGTH);
		getstrvar(strVar, Text, MAX_TEXT_LENGTH);
		iNum=strtonum(Text);
		if(iNum!=0){
			snprintf(Text,MAX_TEXT_LENGTH,"%i",iNum);
		}
		snprintf(String,MAX_TEXT_LENGTH,"%s%s%s",substText,Text,substText2);
	}
	strcpy(Output,String,MAX_TEXT_LENGTH);
}
 
get_playerstats(){
	new Player[MAX_NAME_LENGTH];
	new iFrags;
	new i;
	new maxplayers=maxplayercount();
 
	for(i=1;i<=maxplayers;i++){
		if(playerinfo(i,Player,MAX_NAME_LENGTH)){
			get_userFrags(Player,iFrags);
			if(iFrags>mostfrags){
				strcpy(Value[ADR_BESTPLAYER][0],Player,MAX_NAME_LENGTH);
				numtostr(iFrags,Value[ADR_MOSTFRAGS][0]);
				mostfrags=iFrags;
			}
		}
	}
	if(strlen(Value[ADR_BESTPLAYER][0])==0){
		strcpy(Value[ADR_BESTPLAYER][0],"Niemand",MAX_NAME_LENGTH);
		numtostr(0,Value[ADR_MOSTFRAGS][0]);
	}
 
	return PLUGIN_CONTINUE;
}
 
/*CS only: Team Scoring*/
public sc_team_action(HLCommand,HLData,HLUserName,UserIndex){
	new Params[MAX_DATA_LENGTH];
	new Data1[MAX_DATA_LENGTH];                                
        new Data2[MAX_DATA_LENGTH];  
	new dummy[MAX_DATA_LENGTH];
 
	convert_string(HLData,Params,MAX_DATA_LENGTH);
	strbreak(Params,Data1,Data2,MAX_DATA_LENGTH);
	strsplit(Data2, "# " ,dummy,MAX_DATA_LENGTH,dummy,MAX_DATA_LENGTH,Value[ADR_TEAM2SCORE][0],MAX_NUMBER_LENGTH,dummy,MAX_DATA_LENGTH,Value[ADR_TEAM1SCORE][0],MAX_NUMBER_LENGTH);
 
	round++;
	numtostr(round,Value[ADR_ROUND][0]);
 
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName,UserIndex){
	new Player[MAX_NAME_LENGTH];
	convert_string(HLUserName,Player,MAX_NAME_LENGTH);
	if(strcmp(Player,Value[ADR_BESTPLAYER][0])==0){
		mostfrags=0;
		numtostr(mostfrags,Value[ADR_MOSTFRAGS][0]);
		strinit(Value[ADR_BESTPLAYER][0]);
	}
	return PLUGIN_CONTINUE;
}
 
/*Korrekte Zeitermittlung bei RestartRound und GameCommencing.*/
public sc_world(HLCommand,HLData,HLUserName,UserIndex){
        new Data [MAX_DATA_LENGTH];                                         
        convert_string( HLData, Data, MAX_DATA_LENGTH );
 
        if(Data[0]=='G' && GameCommencing == 0){
                GameCommencing = 1;
                checktime =systemtime();
        }else if(Data[8]=='R'){
                checktime = systemtime();
        }
        return PLUGIN_CONTINUE;
}
 
public plugin_init(){
        plugin_registerinfo("Sdal-Consgreet","Make your own consgreet with keywords!",STRING_VERSION);
        plugin_registercmd("sc_team_action", "sc_team_action", ACCESS_CONSOLE);
        plugin_registercmd("sc_world", "sc_world", ACCESS_CONSOLE);
        exec("logd_reg 61 admin_command sc_team_action");
        exec("logd_reg 62 admin_command sc_world");
        checktime =systemtime();
        origtimelimit = getvar("mp_timelimit")*60+2;
        return PLUGIN_CONTINUE;
}