#include <core>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_RANDOMIZE 8192
#define ACCESS_VOTE_RAND 1
#define ACCESS_LOGD 131072
#define SERVER_MAX 34 // Do not change
 
new Active = 0;
new NoVote = 0;
 
public do_randomize(HLCommand, HLData, HLUserName, UserIndex)
{
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData, Data, MAX_DATA_LENGTH);
 
	if (streq(Data, "Round_End") && (Active))
	{	
		new maxplayers = maxplayercount();
		new i, j, null, Team, OldCVAR;
		new Target[MAX_NAME_LENGTH];
		new Text[MAX_TEXT_LENGTH];
		new NumTs = 0;
		new NumCTs = 0;
		new NewTeams[2][SERVER_MAX]; // NewTeams[Team][User#]
 
		OldCVAR = getvar("mp_limitteams");
		strinit(Text);
		snprintf(Text, MAX_TEXT_LENGTH, "%i", OldCVAR);
		setstrvar("mp_limitteams","0");
 
		for (i = 0; i < 2; i++)
			for (j = 0; j < SERVER_MAX; j++)
				NewTeams[i][j] = 0;
 
		for(i = 1; i <= maxplayers; i++)
		{
			strinit(Target);
			if (playerinfo(i, Target, MAX_NAME_LENGTH, null, null, Team))
			{
				if (Team == 1 || Team == 2)
				{
					new num = random(100000);
					if (num % 2 == 0)
					{
						NumTs++;
						NewTeams[0][i] = 9;		// using 9 as a marker
						NewTeams[1][i] = 0;		// just in case
					}
					else
					{
						NumCTs++;
						NewTeams[1][i] = 9;		// using 9 as a marker
						NewTeams[0][i] = 0;		// just in case
					}
				}
			}
		}
 
		// Balance the team matrix (random numbers == random balance)
		while (NumTs - NumCTs > 1) // You could change this to OldCVAR value if you like (mp_limitteams)
		{
			for(;;)
			{
				i = random(SERVER_MAX);
				if (NewTeams[0][i] == 9)
				{
					NewTeams[1][i] = 9;
					NewTeams[0][i] = 0;
					NumTs--;
					NumCTs++;
					break;
				}
			}
		}
		while (NumCTs - NumTs > 1) // You could change this to OldCVAR value if you like (mp_limitteams)
		{
			for(;;)
			{
				i = random(SERVER_MAX);
				if (NewTeams[1][i] == 9)
				{
					NewTeams[0][i] = 9;
					NewTeams[1][i] = 0;
					NumCTs--;
					NumTs++;
					break;
				}
			}
		}
 
		// Perform the switch
		for (i = 1; i <= maxplayers; i++)
		{
			strinit(Target);
			if (playerinfo(i, Target, MAX_NAME_LENGTH))
			{
				if (NewTeams[0][i] == 9)
				{
					execclient(Target, "chooseteam;menuselect 1;menuselect 5;wait;wait;wait;wait;wait");
					execclient(Target, "wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;clear");
					execclient(Target, "slot1;wait;slot1;wait;slot1;wait;slot1;wait;slot1;wait;clear");
					execclient(Target, "wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;clear");
					execclient(Target, "slot1;wait;slot1;wait;slot1;wait;slot1;wait;slot1;wait;clear");
					messageex(Target, "You have been randomly placed on the TERRORIST force", print_chat);
				}
				if (NewTeams[1][i] == 9)
				{
					execclient(Target, "chooseteam;menuselect 2;menuselect 5;wait;wait;wait;wait;wait");	
					execclient(Target, "wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;clear");
					execclient(Target, "slot1;wait;slot1;wait;slot1;wait;slot1;wait;slot1;wait;clear");
					execclient(Target, "wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;clear");
					execclient(Target, "slot1;wait;slot1;wait;slot1;wait;slot1;wait;slot1;wait;clear");
					messageex(Target, "You have been randomly placed on the COUNTER-TERRORIST force", print_chat);
				}
			}
		}
 
		setstrvar("mp_limitteams",Text); // Put mp_limitteams back to what it was
		typesay("Team randomization complete!", 10, 255, 255, 0);
		log("[ADMIN] Team randomization complete!");
		Active = 0;
 
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}	
 
public admin_randomize(HLCommand, HLData, HLUserName, UserIndex)
{
	new Command[MAX_COMMAND_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Data[MAX_DATA_LENGTH];
 
	convert_string(HLUserName, User, MAX_NAME_LENGTH);
	convert_string(HLData, Data, MAX_DATA_LENGTH);
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
 
	say_command(User, Command, Data);
	log_command(User, Command, Data);
 
	typesay("Teams will be randomized at the end of this round!", 10, 255, 255, 0);
	say("Teams will be randomized at the end of this round!");
	Active = 1;
 
	return PLUGIN_CONTINUE;
}
 
public admin_vote_rand(HLCommand, HLData, HLUserName, UserIndex)
{
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new null;
 
	convert_string(HLCommand, Command, MAX_COMMAND_LENGTH);
	convert_string(HLData, Data, MAX_DATA_LENGTH);
	convert_string(HLUserName, User, MAX_NAME_LENGTH);
	strstripquotes(Data);
 
	if (vote_allowed() != 1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
	log_command(User, Command, Data);
	vote("Randomize teams?", "Yes", "No", "HandleRandVote", null);
	return PLUGIN_HANDLED;
}
 
public HandleRandVote(WinningOption, HLData, VoteCount, UserCount)
{
	if ( WinningOption == 1 )
	{
		new ratio = getvar("map_ratio");
		if ( ratio == 0 )
		{
			say("Randomization vote is not enabled");
			return;
		}
		if ( VoteCount > (ratio/100)*UserCount )
		{
			if (NoVote)
			{
				say("Randomization vote failed!");
				return;
			}
			typesay("Teams will be randomized at the end of this round due to vote!", 10, 255, 255, 0);
			say("Teams will be randomized at the end of this round due to vote!");
			Active = 1;
		}
		else
		{
			say("Not enough votes for team randomization!");
		}
	}
	else
	{
		say("Randomization vote failed!");
	}
	return;
}
 
public admin_norand(HLCommand, HLData, HLUserName, UserIndex)
{
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	convert_string(HLCommand, Command, MAX_COMMAND_LENGTH);
	convert_string(HLData, Data, MAX_DATA_LENGTH);
	convert_string(HLUserName, User, MAX_NAME_LENGTH);
 
	strstripquotes(Data);
	strinit(Text);
 
	log_command(User, Command, Data);
 
	snprintf(Text, MAX_TEXT_LENGTH, "%s has overridden the randomization! Action has been cancelled.", User);
	typesay(Text, 10, 255, 255, 0);
	say(Text);
	Active = 0;
 
	return PLUGIN_HANDLED;
}
 
public admin_novote(HLCommand, HLData, HLUserName, UserIndex)
{
	new User[MAX_NAME_LENGTH];
	new Name[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	convert_string(HLUserName, User, MAX_NAME_LENGTH);
 
	strinit(Text);
	snprintf(Text, MAX_TEXT_LENGTH, "(Admin) %s: Vote has been cancelled!", User);
 
	for(new i = 1; i <= maxplayercount(); i++)
	{
		strinit(Name);
		if(playerinfo(i, Name, MAX_NAME_LENGTH))
		{
			if(access(ACCESS_CHAT, Name))
			{
				messageex(Name, Text, print_chat);
			}
		}
	}
	NoVote = 1;
 
	return PLUGIN_HANDLED;
}
 
public plugin_init()
{
	plugin_registerinfo("Counter-Strike team randomizer", "Will randomize and balance teams in CS)", "2.50.51");
	plugin_registercmd("admin_randomize", "admin_randomize", ACCESS_RANDOMIZE, "admin_randomize: Will randomize and balance the teams.");
	plugin_registercmd("admin_norand", "admin_norand", ACCESS_RANDOMIZE, "admin_norand: Will cancel the randomization action.");
	plugin_registercmd("admin_novote", "admin_novote", ACCESS_MAP, "admin_novote: Will force the current vote to fail.");
	plugin_registercmd("admin_vote_rand", "admin_vote_rand", ACCESS_VOTE_RAND, "admin_vote_rand: Starts a vote to randomize the teams.");
	plugin_registercmd("do_randomize", "do_randomize", ACCESS_LOGD, "");
 
	exec("logd_reg 62 admin_command do_randomize");
 
	return PLUGIN_CONTINUE;
}