////////////////////////////////////////
// WCS - Warcraft:Source              //
// ---------------------------------- //
//  by kRypT0n!Te                     //
//      http://wcs.despo-clan.de      //
// ---------------------------------- //
// Content of this subscript          //
// - redirects if server is full      //
////////////////////////////////////////
// admin commands:
// wcs_redirect <userid>
//
////////////////////////////////////////
//
// THIS REQUIRES cl_restrict_server_commands 0
// ON THE CLIENT ! YOU CAN USE SOME PLUGINS LIKE
// "Force cl_restrict_server_commands" by LukeD
// TO FORCE THAT SETTING, OTHERWISE THIS SCRIPT
// IS USELESS !

block load
{
  es es_xexists wcs_exists command wcs_redirect
  es es_xif (server_var(wcs_exists) = "0") then es_xregcmd wcs_redirect wcs/WCSredirect/wcs_redirect "redirects *g*"
  // define maxplayers, a good value is maxplayers/or 1 less than maxplayers, at this playercount new players are redirected
  es_xsetinfo wcs_maxplayers "20"
  // SET YOUR SERVER !!!!!!!!!!!!
  es_xsetinfo wcs_thisserver "2"
  // no need to change this
  es_xsetinfo wcs_numberofservers "2"
  es_xsetinfo wcs_serverip "85.131.219.139:27015|80.190.75.122:27015"
}

event player_activate
{
  es_xsetinfo wcs_players 0
  es_xgetplayercount wcs_players
  es es_xif (server_var(wcs_players) >= server_var(wcs_maxplayers)) do
	{
  	es es_xkeygetvalue wcs_id "WCSuserdata" event_var(userid) "id"
  	es_xsetinfo wcs_isadmin "0"
  	es es_xexists wcs_isadmin key "WCSadmin" server_var(wcs_id)
  	// check for no admins to be ban...redirected !
  	es es_xif (server_var(wcs_isadmin) = "0") do
  	{
  		es es_xdelayed 10 wcs_redirect event_var(userid)
  		es es_xtell event_var(userid) #lightgreen Sorry, this server has reserved slots.
  		es es_xdelayed 2 es_xtell event_var(userid) #multi #lightgreenYou are going to be #greenredirected !!
  	}
	}
}

block wcs_redirect
{
	es_xgetargv wcs_userid 1
	es es_xexists wcs_exists userid server_var(wcs_userid)
	es es_xif (server_var(wcs_exists) = "1") do
	{
		es es_xsetinfo wcs_rand server_var(wcs_thisserver)
		es_xsetinfo wcs_counter 0
		// dont connect on this server !
		while "server_var(wcs_thisserver) = server_var(wcs_rand)" "es es_xrand wcs_rand 1 server_var(wcs_numberofservers);es_xmath wcs_counter + 1;es es_xif (server_var(wcs_counter) = 10) then es_xsetinfo wcs_rand 0"
		es_xsetinfo wcs_ip 0
		es es_xtoken wcs_ip server_var(wcs_serverip) server_var(wcs_rand) "|"
		es es_xtell server_var(wcsr_userid) #lightgreen Sorry, this server has reserved slots.
  	es es_xdelayed 2 es_xtell server_var(wcsr_userid) #multi #lightgreenYou are going to be #greenredirected !! (server # server_var(wcs_rand))
		esnq es_xdelayed 5 es_xcexec server_var(wcs_userid) connect server_var(wcs_ip)
  }
}
