/* AMXMOD script.
*
* (c) Copyright 2000-2002, F117bomb
* This file is provided as is (no warranties).
*
*/

#include <amxmod>
#include <csstats>

new match_inprogress 
new ct_score 
new terrorist_score 

public end_half(id) {
	new buffor[1024]
	new cts[32]
	new ts[32]
	new inum, temp[256], tempname[32], i, stats[8], body[8]
	//CT
	format(buffor,256,"Counter-Terrorist Rounds Won: %i^n",ct_score)
	get_players(cts,inum,"e","CT")
	for(i = 0 ;i < inum; ++i)
	{
		get_user_name(cts[i],tempname,32)
		format(temp,256,"  -%s^n",tempname)
		add(buffor,1024,temp)	
	}
	//T
	format(temp,256,"^nTerrorist Rounds Won: %i^n",terrorist_score)
	add(buffor,1024,temp)
	get_players(ts,inum,"e","TERRORIST")
	for(i = 0 ;i < inum; ++i)
	{
		get_user_name(ts[i],tempname,32)
		format(temp,256,"  -%s^n",tempname)
		add(buffor,1024,temp)	
	}
	//Your Stats
	add(buffor,1024,"^nYour Stats:^n")
	get_user_wstats(id,0,stats,body)
	format(temp,256,"  -Kills: %d^n  -Deaths: %d^n  -Damage: %d^n  -Hits: %d^n  -Shots: %d^n^n",stats[0],stats[1],stats[6],stats[5],stats[4])
	add(buffor,1024,temp)
	show_motd(id,buffor,"* [AMX MATCH] Results")
	return PLUGIN_CONTINUE
}

public admin_startmatch(id)
{
	if (match_inprogress == 0)
	{
		match_inprogress = 1
		client_print(0,print_chat,"* [AMX MATCH] Executing CAL.cfg...")
		server_cmd("exec cal.cfg")
		client_print(0,print_chat,"* [AMX MATCH] Live in 3 restarts...")
		restart_round("2")
		set_task(2.0, "restart_round",0,"2",2)
		set_task(4.0, "restart_round",0,"4",2)
		set_task(10.0, "last_restart",0)
	}
	else  
		client_print(id,print_console,"* Match already in progress. ")	
		
	return PLUGIN_CONTINUE		
}

public restart_round(time[])
{
	server_cmd("sv_restartround %s",time)
	return PLUGIN_CONTINUE
}

public last_restart()
{
	set_hudmessage(0, 100, 200, -1.0, 0.40, 0, 6.0, 6.0, 0.1, 0.1, 9)
	show_hudmessage(0,"* LIVE *")
	client_print(0,print_chat,"* [AMX MATCH] LIVE")
	return PLUGIN_CONTINUE
}

public new_round()
{
	if (match_inprogress == 1)
	{	
		//new roundtime = 180
		new roundtime = floatround(get_cvar_float("mp_roundtime") * 60.0)
		if (roundtime == read_data(1))
		{
			set_hudmessage(0, 100, 200, 0.05, 0.75, 0, 6.0, 6.0, 0.5, 0.15, 1)
			if (ct_score + terrorist_score < 12)
			{
				if (ct_score > terrorist_score)
					show_hudmessage(0,"* [AMX MATCH] CT's are winning %d to %d",ct_score,terrorist_score )
				else if (ct_score < terrorist_score)
					show_hudmessage(0,"* [AMX MATCH] T's are winning %d to %d",terrorist_score,ct_score)
				else if (ct_score == terrorist_score && terrorist_score != 0) 
					show_hudmessage(0,"* [AMX MATCH] Both teams have won %d rounds",ct_score)
			}
			else
			{
				new players[32], inum
				get_players(players,inum,"a")
				for(new i = 0 ;i < inum ;++i)
				{
					new iwpn, iwpns[32]
					get_user_weapons(players[i],iwpns,iwpn)
					for(new a = 0; a < iwpn;++a)
					{
						new nwpn[24]
						get_weaponname(iwpns[a],nwpn,24)
						engclient_cmd(players[i],"drop",nwpn)
					}
					new origin[3]
					get_user_origin(players[i], origin)
					origin[2] -= 30
					set_user_origin(players[i], origin)
					end_half(players[i])
				}
				match_inprogress = 0
			}
			
		}
	}
	return PLUGIN_CONTINUE
}

public team_score()
{
	new team[32]
	read_data(1,team,32)

	if (equal(team,"CT")){
		ct_score = read_data(2)
	}
	else if (equal(team,"TERRORIST")){
		terrorist_score = read_data(2)
	}

	return PLUGIN_CONTINUE
}

public plugin_init()
{
	register_plugin("AMX MATCH","0.61","f117bomb")
	register_clcmd("amx_startmatch","admin_startmatch",ADMIN_LEVEL_A,"amx_startmatch - Starts a Official CAL Match")
    	register_event("TeamScore", "team_score", "a")
    	register_event("RoundTime", "new_round", "bc")
	register_cvar("amx_winslimit","7")

	return PLUGIN_CONTINUE
}

