////////////////////////////////////////
// WCS - Warcraft:Source              //
// ---------------------------------- //
//  by kRypT0n!Te                     //
//      http://wcs.despo-clan.de      //
// ---------------------------------- //
// Content of this subscript          //
// - race handling                    //
////////////////////////////////////////
// say commands:
// raceinfo, changerace
//
// admin commands:
// wcs_race_menu <userid>
// wcs_showraceinfo <userid>
//
////////////////////////////////////////

block load
{
  es_xexists wcs_exists keygroup "WCSinfo"
  es es_xif (server_var(wcs_exists) = 0) then es_xkeygroupcreate "WCSinfo"
  es_xexists wcs_exists key "WCSinfo" "raceinfo"
  es es_xif (server_var(wcs_exists) = 0) do
  {
    es_xkeycreate "WCSinfo" "raceinfo"
  }
  es_xelse do
  {
    es_xkeydelete "WCSinfo" "raceinfo"
    es_xkeycreate "WCSinfo" "raceinfo"
  }
  es_xkeygroupload "WCSraces" "|wcs/WCSraces"
  // loadup races, shop items
  es_xsetinfo wcs_numberofraces 0
  alias wcs_checkracealias "es es_xif (racealias in server_var(wcs_val)) then es_xkeygetvalue wcs_tmp WCSraces server_var(wcs_key) server_var(wcs_val);es es_xif (racealias in server_var(wcs_val)) then alias server_var(wcs_val) server_var(wcs_tmp)"
  alias wcs_checkracelanguage "es es_xif (wcs_lng_r in server_var(wcs_name)) then wcs_getlanguage wcs_name server_var(wcs_name);es es_xkeysetvalue WCSraces server_var(wcs_key) name server_var(wcs_name);es es_xkeygetvalue wcs_descr WCSraces server_var(wcs_key) skilldescr;es es_xif (wcs_lng_r in server_var(wcs_descr)) then wcs_getlanguage wcs_descr server_var(wcs_descr);es es_xkeysetvalue WCSraces server_var(wcs_key) skilldescr server_var(wcs_descr);es es_xkeygetvalue wcs_tmp WCSraces server_var(wcs_key) skillnames;es es_xif (wcs_lng_r in server_var(wcs_tmp)) then wcs_getlanguage wcs_tmp server_var(wcs_tmp);es es_xkeysetvalue WCSraces server_var(wcs_key) skillnames server_var(wcs_tmp);wcs_checkracelanguage2"
  alias wcs_checkracelanguage2 "es es_xkeygetvalue wcs_descr WCSraces server_var(wcs_key) shortdescription;es es_xif (wcs_lng_r in server_var(wcs_descr)) then wcs_getlanguage wcs_descr server_var(wcs_descr);es es_xkeysetvalue WCSraces server_var(wcs_key) shortdescription server_var(wcs_descr)"
  es_xforeachkey wcs_key in "WCSraces" "es es_xkeygetvalue wcs_cmd WCSraces server_var(wcs_key) preloadcmd;es es_xif (server_var(wcs_cmd) != 0) then alias wcs_loadcmd server_var(wcs_cmd);es es_xif (server_var(wcs_cmd) != 0) then wcs_loadcmd;es es_xforeachval wcs_val in WCSraces server_var(wcs_key) wcs_checkracealias;es es_xkeygetvalue wcs_name WCSraces server_var(wcs_key) name;wcs_checkracelanguage;es echo Loaded race : server_var(wcs_name);es_xmath wcs_numberofraces + 1"
  es echo "Total : " server_var(wcs_numberofraces) " Races loaded"
  // create the raceinfo data
  es_xdoblock wcs/WCSraces/wcs_raceinfo
  es_xexists wcs_exists command wcs_race_menu
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_race_menu wcs/WCSraces/wcs_race_menu "wcs_race_menu - usage: wcs_race_menu <userid>"
  es_xexists wcs_exists command wcs_showraceinfo
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_showraceinfo wcs/WCSraces/wcs_showraceinfo "Shows detail info about races"
  es_xexists wcs_exists command changerace
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd changerace wcs/WCSraces/wcs_changerace "Change your race"
  es_xexists wcs_exists command raceinfo
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd raceinfo wcs/WCSraces/wcs_showraceinfo "Shows detail info about the races"
}

block unload
{
  es_xkeygroupdelete "WCSraces"
  es_xkeydelete "WCSinfo" "raceinfo"
}

event dod_round_start
{
  // checks the roundstartcmd for all races
  es_xforeachkey wcs_key in "WCSraces" "es es_xkeygetvalue wcs_cmd WCSraces server_var(wcs_key) round_start_cmd;es es_xstrlen wcs_strlen server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then alias wcs_runcmd server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then wcs_runcmd"
}

event round_start
{
  // checks the roundstartcmd for all races
  es_xforeachkey wcs_key in "WCSraces" "es es_xkeygetvalue wcs_cmd WCSraces server_var(wcs_key) round_start_cmd;es es_xstrlen wcs_strlen server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then alias wcs_runcmd server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then wcs_runcmd"
}
event round_end
{
  // checks the roundendcmd for all races, and reset race counter
  es_xforeachkey wcs_key in "WCSraces" "es es_xkeygetvalue wcs_cmd WCSraces server_var(wcs_key) round_end_cmd;es es_xstrlen wcs_strlen server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then alias wcs_runcmd server_var(wcs_cmd);es es_xif (server_var(wcs_strlen) > 1) then wcs_runcmd;es es_xkeysetvalue WCSraces server_var(wcs_key) 2 0;es es_xkeysetvalue WCSraces server_var(wcs_key) 3 0"
  es es_xif (server_var(wcs_cfg_enableraceteamlimit) = "1") do
  {
  	// check the player's races
    es_xforeachkey wcs_key in "WCSuserdata" "es es_xgetplayerteam wcs_team server_var(wcs_key);es es_xkeygetvalue wcs_race WCSuserdata server_var(wcs_key) race;es es_xkeygetvalue wcs_tmp WCSraces server_var(wcs_race) server_var(wcs_team);es_xmath wcs_tmp + 1;es es_xif (server_var(wcs_team) >= 2) then es_xif (server_var(wcs_team) <= 3) then es_xkeysetvalue WCSraces server_var(wcs_race) server_var(wcs_team) server_var(wcs_tmp)"
    // check again, if one player is over the race limit/team
    es_xforeachkey wcs_key in "WCSuserdata" "es es_xgetplayerteam wcs_team server_var(wcs_key);es es_xkeygetvalue wcs_race WCSuserdata server_var(wcs_key) race;es es_xkeygetvalue wcs_tmp WCSraces server_var(wcs_race) server_var(wcs_team);es es_xkeygetvalue wcs_limit WCSraces server_var(wcs_race) teamlimit;es es_xif (server_var(wcs_limit) != 0) then es_xif (server_var(wcs_limit) < server_var(wcs_tmp)) then es_xdoblock wcs/WCSraces/wcs_checkplayerrace"
  }
}

event player_team
{
	es es_xexists wcs_exists key WCSuserdata event_var(userid)
	es es_xif (server_var(wcs_cfg_enableraceteamlimit) != "1") then es_xsetinfo wcs_exists "0"
	es es_xif (server_var(wcs_exists) = "1") do
	{
	  es es_xkeygetvalue wcs_limit WCSuserdata event_var(userid) "limitedrace"
	  es es_xif (server_var(wcs_limit) = "1") do
	  {
  		es es_xif (server_var(wcs_cfg_limitedraceforcespec) = "1") then est_Team event_var(userid) "1" "1"
      es es_xtell event_var(userid) #multi #greenPlease change your race -> #lightgreen'changerace'
  	}
  }
}

block wcs_checkplayerrace
{
	// wcs_key=userid wcs_race=race wcs_tmp=status wcs_limit=limit
	es es_xkeysetvalue WCSuserdata server_var(wcs_key) "limitedrace" "1"
	es es_xif (server_var(wcs_cfg_limitedraceforcespec) = "1") then est_Team server_var(wcs_key) "1" "1"
	es es_xkeygetvalue wcs_racename "WCSraces" server_var(wcs_race) "name"
	wcs_getlanguage "wcs_lng" "wcs_lng_limitedrace"
	es es_xformat wcs_lng server_var(wcs_lng) server_var(wcs_race) server_var(wcs_racename) server_var(wcs_limit) server_var(wcs_tmp)
	es es_xmenu 10 server_var(wcs_key) server_var(wcs_lng)
}

// >>>>>>>>>>>>>>>>> RACE INFO >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_showraceinfo
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showraceinfo start
  // first, is it a custom command ?
  es_xgetargv wcs_userid 1
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es es_xif (server_var(wcs_exists) = 0) do
  {
    // or a say command
    es_xgetcmduserid wcs_userid
    es es_xexists wcs_exists userid server_var(wcs_userid)
    es wcs_menuzero server_var(wcs_userid)
    es es_xkeygetvalue wcs_page "WCSuserdata" server_var(wcs_userid) "race"
    es_xmath wcs_page - 1
  }
  es_xelse do
  {
    es es_xkeygetvalue wcs_page WCSuserdata server_var(wcs_userid) page
  }
  es es_xkeysetvalue WCSuserdata server_var(wcs_userid) menu "raceinfo"
  es_xmath wcs_page + 1
  es es_xkeysetvalue WCSuserdata server_var(wcs_userid) page server_var(wcs_page)
  es_xsetinfo wcs_infotext 0
  es es_xkeygetvalue wcs_infotext WCSinfo raceinfo server_var(wcs_page)
  es es_xif (server_var(wcs_infotext) != "0") do
  {
  	es es_xstrlen wcs_strlen server_var(wcs_infotext)
  	es es_xif (server_var(wcs_strlen) <= 1020) then es_xmenu 0 server_var(wcs_userid) server_var(wcs_infotext)
  	es es_xif (server_var(wcs_strlen) > 1020) then es_xmenu 0 server_var(wcs_userid) "WARNING:\n \n Text too long ! Report this to an admin."
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showraceinfo end
}

block wcs_raceinfo
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_raceinfo start
  es_xforeachkey wcs_key in WCSraces "es es_xkeygetvalue wcs_name WCSraces server_var(wcs_key) name;es es_xkeygetvalue wcs_numberofskills WCSraces server_var(wcs_key) numberofskills;es es_xkeygetvalue wcs_numberoflevels WCSraces server_var(wcs_key) numberoflevels;es_xdoblock wcs/WCSraces/wcs_raceinfo_skills1"
es es_xif (server_var(wcs_debug) = "1") then echo wcs_raceinfo end
}

block wcs_raceinfo_skills1
{
	wcs_getlanguage "wcs_lng" "wcs_lng_skills"
  es es_xformat wcs_text "\n->%1.      %2 (%3 lvls/%4 %5)\n" server_var(wcs_key) server_var(wcs_name) server_var(wcs_numberoflevels) server_var(wcs_numberofskills) server_var(wcs_lng)
  es es_xkeygetvalue wcs_requiredlevel WCSraces server_var(wcs_key) required_level
  wcs_getlanguage "wcs_lng" "wcs_lng_required"
  wcs_getlanguage "wcs_lng2" "wcs_lng_level"
  es es_xif (server_var(wcs_cfg_requiredlevel) = 1) then es_xif (server_var(wcs_requiredlevel) > 0) then es_xformat wcs_text "%1%2 %3 : %4\n" server_var(wcs_text) server_var(wcs_lng) server_var(wcs_lng2) server_var(wcs_requiredlevel)
  es es_xkeygetvalue wcs_maxlevel "WCSraces" server_var(wcs_key) "maximum_level"
  wcs_getlanguage "wcs_lng" "wcs_lng_maxlevel"
  es es_xif (server_var(wcs_cfg_maxracelevel) = 1) then es_xif (server_var(wcs_maxlevel) > 0) then es_xformat wcs_text server_var(wcs_lng) server_var(wcs_text) server_var(wcs_maxlevel)
  es es_xkeygetvalue wcs_allow_only WCSraces server_var(wcs_key) allow_only
  wcs_getlanguage "wcs_lng" "wcs_lng_privaterace"
  es es_xif (server_var(wcs_cfg_restrictedrace) = 1) then es_xif (server_var(wcs_allow_only) != "0") then es_xformat wcs_text "%1< %2 > \n" server_var(wcs_text) server_var(wcs_lng)
  es es_xkeygetvalue wcs_author WCSraces server_var(wcs_key) author
  es es_xif (server_var(wcs_author) != "0") then es_xformat wcs_text "%1 credits: %2\n \n" server_var(wcs_text) server_var(wcs_author)
  es es_xkeygetvalue wcs_skillname WCSraces server_var(wcs_key) skillnames
  es es_xkeygetvalue wcs_skilldescr WCSraces server_var(wcs_key) skilldescr
  es_xsetinfo wcs_counter 1
  while "server_var(wcs_numberofskills) >= server_var(wcs_counter)" "es_xdoblock wcs/WCSraces/wcs_raceinfo_skills2;es_xmath wcs_counter + 1"
  es es_xformat wcs_text "%1\n \n " server_var(wcs_text)
  es es_xif (server_var(wcs_key) > 1) then es_xformat wcs_text "%1\n->8. %2" server_var(wcs_text) server_var(wcs_lng_back)
  es es_xsetinfo wcs_keynext server_var(wcs_key)
  es_xmath wcs_keynext + 1
  es es_xexists wcs_exists key WCSraces server_var(wcs_keynext)
  es es_xif (server_var(wcs_exists) = 1) then es_xformat wcs_text "%1\n->9. %2" server_var(wcs_text) server_var(wcs_lng_next)
  es es_xkeysetvalue WCSinfo raceinfo server_var(wcs_key) server_var(wcs_text)
}

block wcs_raceinfo_skills2
{
  es es_xtoken wcs_skill server_var(wcs_skillname) server_var(wcs_counter) "|"
  es es_xtoken wcs_skilld server_var(wcs_skilldescr) server_var(wcs_counter) "|"
  es es_xformat wcs_text "%1\n->%2. %3\n%4" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_skill) server_var(wcs_skilld)
}
// <<<<<<<<<<<<<<<<< END RACE INFO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>>>> START RACE MENU >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_changerace
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_changerace start
  es_xsetinfo wcs_userid 0
  // if its a say command
  es_xgetcmduserid wcs_userid
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
  	es es_xexists wcs_exists key WCSuserdata server_var(wcs_userid)
  	es es_xif (server_var(wcs_exists) = 1) do
  	{
  		wcs_getlanguage "wcs_lng" "wcs_lng_changerace"
      es es_xtell server_var(wcs_userid) #multi server_var(wcs_lng)
      es wcs_menuzero server_var(wcs_userid)
      es wcs_race_menu server_var(wcs_userid)
    }
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_changerace end
}

block wcs_race_menu
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_menu start
  // a custom command
  es_xgetargv wcs_userid 1
  wcs_getlanguage "wcs_lng" "wcs_lng_chooserace"
  es es_xformat wcs_text "%1:\n " server_var(wcs_lng)
  es_xsetinfo wcs_counter 0
  // get ID
  es es_xkeygetvalue wcs_id WCSuserdata server_var(wcs_userid) id
  es es_xgetplayerteam wcs_team server_var(wcs_userid)
  // get race level
  es es_xkeygetvalue wcs_race "WCSuserdata" server_var(wcs_userid) "race"
  // variables
  es_xsetinfo wcs_racename 0
  es es_xkeygetvalue wcs_page WCSuserdata server_var(wcs_userid) page
  es_xmath wcs_page + 1
  es es_xkeysetvalue WCSuserdata server_var(wcs_userid) page server_var(wcs_page)
  es_xsetinfo wcs_racenumber 0
  es_xsetinfo wcs_raceid 0
  es_xsetinfo wcs_addnum 7
  es es_xmath wcs_addnum * server_var(wcs_page)
  es_xmath wcs_addnum - 7
  // how many races ?
  es_xsetinfo wcs_racecount 0
  es_xforeachkey wcs_key in WCSraces "es_xmath wcs_racecount + 1"
  // get id
  es es_xmath wcs_raceid + server_var(wcs_addnum)
  // start is at 1
  es_xsetinfo wcs_counter 1
  // buildin menu 1..7
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  es_xdoblock wcs/WCSraces/wcs_race_menu_init
  es_xmath wcs_raceid + 1
  // check if we need next page
  es es_xformat wcs_text "%1 %2\n %3\n \n " server_var(wcs_lng_page) server_var(wcs_page) server_var(wcs_text)
  es es_xif (server_var(wcs_raceid) > server_var(wcs_racecount)) then es_xif (server_var(wcs_page) = 1) then es_xformat wcs_text "%1 0. %2" server_var(wcs_text) server_var(wcs_lng_exit)
  es es_xif (server_var(wcs_raceid) <= server_var(wcs_racecount)) then es_xif (server_var(wcs_page) = 1) then es_xformat wcs_text "%1 9. %2\n 0. %3" server_var(wcs_text) server_var(wcs_lng_next) server_var(wcs_lng_exit)
  es es_xif (server_var(wcs_raceid) > server_var(wcs_racecount)) then es_xif (server_var(wcs_page) > 1) then es_xformat wcs_text "%1 8. %2\n 0. %3" server_var(wcs_text) server_var(wcs_lng_exit) server_var(wcs_lng_back) server_var(wcs_lng_exit)
  es es_xif (server_var(wcs_raceid) <= server_var(wcs_racecount)) then es_xif (server_var(wcs_page) > 1) then es_xformat wcs_text "%1 8. %2\n 9. %3\n 0. %4" server_var(wcs_text) server_var(wcs_lng_exit) server_var(wcs_lng_back) server_var(wcs_lng_next) server_var(wcs_lng_exit)
  es es_xmenu 0 server_var(wcs_userid) server_var(wcs_text)
  es es_xkeysetvalue WCSuserdata server_var(wcs_userid) menu race
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_menu end
}

block wcs_race_menu_init
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_menu_init start
  es es_xif (server_var(wcs_raceid) <= server_var(wcs_racecount)) do
  {
    es es_xexists wcs_exists key WCSraces server_var(wcs_raceid)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      es es_xkeygetvalue wcs_racename WCSraces server_var(wcs_raceid) name
      es es_xif (server_var(wcs_racename) != 0) do
      {
      	es es_xkeygetvalue wcs_limit "WCSraces" server_var(wcs_raceid) "teamlimit"
        es_xsetinfo wcs_limited "0"
        es es_xif (server_var(wcs_cfg_enableraceteamlimit) != "1") then es_xsetinfo wcs_limit "0"
        es es_xif (server_var(wcs_limit) != "0") do
        {
        	es es_xkeygetvalue wcs_tmp1 "WCSraces" server_var(wcs_raceid) "2"
          es es_xkeygetvalue wcs_tmp2 "WCSraces" server_var(wcs_raceid) "3"
          es es_xif (server_var(wcs_team) = "1") then es_xif (server_var(wcs_tmp1) > server_var(wcs_tmp2)) then es_xcopy wcs_tmp1 wcs_tmp2
          es es_xif (server_var(wcs_team) = "1") then es_xif (server_var(wcs_tmp1) >= server_var(wcs_limit)) then es_xsetinfo wcs_limited "1"
          es es_xif (server_var(wcs_team) = "2") then es_xif (server_var(wcs_tmp1) >= server_var(wcs_limit)) then es_xsetinfo wcs_limited "1"
          es es_xif (server_var(wcs_team) = "3") then es_xif (server_var(wcs_tmp2) >= server_var(wcs_limit)) then es_xsetinfo wcs_limited "1"
        }
        // get level stuff
        es es_xformat wcs_racef "race_%1" server_var(wcs_raceid)
        es es_xkeygetvalue wcs_level "WCSusers" server_var(wcs_id) server_var(wcs_racef)
        es es_xtoken wcs_level server_var(wcs_level) "1" "|"
        es es_xkeygetvalue wcs_requiredlevel WCSraces server_var(wcs_raceid) "required_level"
        es es_xkeygetvalue wcs_allow_only WCSraces server_var(wcs_raceid) "allow_only"
        es es_xif (server_var(wcs_cfg_restrictedrace) = 0) then es_xsetinfo wcs_allow_only 0
        es es_xkeygetvalue wcs_total_level "WCSuserdata" server_var(wcs_userid) "total_level"
        es es_xif (server_var(wcs_cfg_requiredlevel) = "0") then es_xsetinfo wcs_requiredlevel 0
        es es_xif (server_var(wcs_total_level) >= server_var(wcs_requiredlevel)) do
        {
        	es es_xkeygetvalue wcs_maxlevel "WCSraces" server_var(wcs_raceid) "maximum_level"
          es es_xif (server_var(wcs_cfg_maxracelevel) = "0") then es_xsetinfo wcs_maxlevel server_var(wcs_total_level)
          es es_xif (server_var(wcs_maxlevel) = "0") then es_xsetinfo wcs_maxlevel server_var(wcs_total_level)
          es es_xif (server_var(wcs_total_level) <= server_var(wcs_maxlevel)) do
	        {
	         	es_xsetinfo wcs_allow 0
	          es es_xif (server_var(wcs_allow_only) = 0) then es_xsetinfo wcs_allow 1
	          es es_xformat wcs_uid "<%1>" server_var(wcs_id)
	          es es_xif (server_var(wcs_uid) in server_var(wcs_allow_only)) then es_xsetinfo wcs_allow 1
	          es es_xif (server_var(wcs_allow_only) != "0") do
	          {
	            es es_xif (server_var(wcs_uid) notin server_var(wcs_allow_only)) do
	            {
	            	// check for admin
	              es es_xexists wcs_isadmin key "WCSadmin" server_var(wcs_id)
	              es es_xif (server_var(wcs_isadmin) = "1") then es_xif ("<admins>" in server_var(wcs_allow_only)) then es_xsetinfo wcs_allow "1"
	              es es_xif (server_var(wcs_allow) = 0) do
	              {
	                wcs_getlanguage "wcs_lng" "wcs_lng_privaterace"
	        	      es es_xformat wcs_text "%1 \n%2. %3 < %4 >" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_lng)
	        	    }
	            }
	          }
	          es es_xif (server_var(wcs_allow) = 1) do
	          {
	          	es es_xif (server_var(wcs_limited) = "0") do
	          	{
	              es es_xif (server_var(wcs_level) > 0) then es_xif (server_var(wcs_cfg_globallevel) = "0") then es_xformat wcs_text "%1 \n->%2. %3 (lvl %4)" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_level)
	              es es_xif (server_var(wcs_level) > 0) then es_xif (server_var(wcs_cfg_globallevel) = "1") then es_xformat wcs_text "%1 \n->%2. %3 (lvl %4)" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_total_level)
	        	    es es_xif (server_var(wcs_level) = 0) then es_xformat wcs_text "%1 \n->%2. %3" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename)
	        	  }
	        	  es_xelse do
	        	  {
	        	  	// level too high
        	      wcs_getlanguage "wcs_lng" "wcs_lng_limited"
        	      es es_xformat wcs_text "%1 \n%2. %3 (%4 : %5)" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_lng) server_var(wcs_limit)
	        	  }
	        	}
	        }
	        es_xelse do
	        {
	          // level too high
        	  wcs_getlanguage "wcs_lng" "wcs_lng_maximum"
        	  es es_xformat wcs_text "%1 \n%2. %3 (%4 lvl %5)" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_lng) server_var(wcs_maxlevel)
	        }
        }
        es_xelse do
        {
        	// level too low
        	wcs_getlanguage "wcs_lng" "wcs_lng_required"
        	es es_xformat wcs_text "%1 \n%2. %3 (%4 lvl %5)" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_racename) server_var(wcs_lng) server_var(wcs_requiredlevel)
        }
        es es_xif (server_var(wcs_cfg_changeracedetails) = "1") do
        {
          es es_xkeygetvalue wcs_descr "WCSraces" server_var(wcs_raceid) "shortdescription"
          es es_xif (server_var(wcs_descr) != "0") then es_xformat wcs_text "%1\n     %2" server_var(wcs_text) server_var(wcs_descr)
        }
        // IMPORTANT !!
        es_xmath wcs_counter + 1
      }
      es_xelse do
      {
      es es_xformat wcs_text "%1\n (-)" server_var(wcs_text)
      }
    }
    es_xelse do
    {
      es es_xformat wcs_text "%1\n (-)" server_var(wcs_text)
    }
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_menu_init end
}

block wcs_race_handle
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_handle start
  es_xsetinfo wcs_page 0
  es es_xkeygetvalue wcs_page WCSuserdata event_var(userid) page
  es es_xkeygetvalue wcs_id WCSuserdata event_var(userid) id
  es_xsetinfo wcs_racecount 0
  es_xforeachkey wcs_key in WCSraces "es_xmath wcs_racecount + 1"
  es_xsetinfo wcs_r8id 7
  es es_xmath wcs_r8id * server_var(wcs_page)
  es_xmath wcs_r8id + 1
  // handle back
  es es_xif (event_var(commandstring) = "8") do
  {
    es es_xif (server_var(wcs_page) > 1) do
    {
      es_xmath wcs_page - 2
      es es_xkeysetvalue WCSuserdata event_var(userid) page server_var(wcs_page)
      es wcs_race_menu event_var(userid)
    }
  }
  // handle next
  es es_xif (event_var(commandstring) = "9") then es_xif (server_var(wcs_r8id) <= server_var(wcs_racecount)) then wcs_race_menu event_var(userid)
  // race selected
  es es_xif (event_var(commandstring) < 8) do
  {
    es es_xif (event_var(commandstring) > 0) do
    {
      es_xsetinfo wcs_raceid 0
      es_xsetinfo wcs_racename 0
      es_xsetinfo wcs_addnum 7
      es es_xmath wcs_addnum * server_var(wcs_page)
      es_xmath wcs_addnum - 7
      es es_xmath wcs_raceid + server_var(wcs_addnum)
      es es_xmath wcs_raceid + event_var(commandstring)
      es es_xexists wcs_exists key WCSraces server_var(wcs_raceid)
      es es_xif (server_var(wcs_exists) = 1) do
      {
        es es_xkeygetvalue wcs_racename WCSraces server_var(wcs_raceid) name
        es es_xkeygetvalue wcs_requiredlevel WCSraces server_var(wcs_raceid) required_level
        es es_xkeygetvalue wcs_total_level WCSuserdata server_var(wcs_userid) total_level
        es es_xif (server_var(wcs_cfg_requiredlevel) = 0) then es_xsetinfo wcs_requiredlevel 0
        es es_xkeygetvalue wcs_allow_only WCSraces server_var(wcs_raceid) allow_only
        es es_xif (server_var(wcs_cfg_restrictedrace) = 0) then es_xsetinfo wcs_allow_only 0
        es es_xif (server_var(wcs_total_level) >= server_var(wcs_requiredlevel)) do
        {
        	es es_xkeygetvalue wcs_maxlevel "WCSraces" server_var(wcs_raceid) "maximum_level"
          es es_xif (server_var(wcs_cfg_maxracelevel) = "0") then es_xsetinfo wcs_maxlevel server_var(wcs_total_level)
          es es_xif (server_var(wcs_maxlevel) = "0") then es_xsetinfo wcs_maxlevel server_var(wcs_total_level)
          es es_xif (server_var(wcs_total_level) <= server_var(wcs_maxlevel)) do
          {
	        	es_xsetinfo wcs_allow 0
	          es es_xif (server_var(wcs_allow_only) = 0) then es_xsetinfo wcs_allow 1
	          es es_xformat wcs_uid "<%1>" server_var(wcs_id)
	          es es_xif (server_var(wcs_uid) in server_var(wcs_allow_only)) then es_xsetinfo wcs_allow 1
	          es es_xif (server_var(wcs_allow_only) != "0") do
	          {
	            es es_xif (server_var(wcs_uid) notin server_var(wcs_allow_only)) do
	            {
	            	// check for admin
	              es es_xexists wcs_isadmin key "WCSadmin" server_var(wcs_id)
	              es es_xif (server_var(wcs_isadmin) = "1") then es_xif ("<admins>" in server_var(wcs_allow_only)) then es_xsetinfo wcs_allow "1"
	              es es_xif (server_var(wcs_allow) = 0) do
	              {
	              	wcs_getlanguage "wcs_lng" "wcs_lng_notallowed"
	            	  es es_xformat wcs_text "#lightgreen%1 : #green%2" server_var(wcs_racename) server_var(wcs_lng)
	        	      es es_xtell event_var(userid) #multi server_var(wcs_text)
	        	    }
	            }
	          }
	        	// check team limit
	        	es es_xgetplayerteam wcs_team event_var(userid)
	        	es es_xkeygetvalue wcs_limit "WCSraces" server_var(wcs_raceid) "teamlimit"
            es_xsetinfo wcs_limited "0"
            es es_xif (server_var(wcs_cfg_enableraceteamlimit) != "1") then es_xsetinfo wcs_limit "0"
            es es_xif (server_var(wcs_limit) != "0") do
            {
        	    es es_xkeygetvalue wcs_tmp1 "WCSraces" server_var(wcs_raceid) "2"
              es es_xkeygetvalue wcs_tmp2 "WCSraces" server_var(wcs_raceid) "3"
              es es_xif (server_var(wcs_team) = "1") then es_xif (server_var(wcs_tmp1) > server_var(wcs_tmp2)) then es_xcopy wcs_tmp1 wcs_tmp2
              es es_xif (server_var(wcs_team) = "1") then es_xif (server_var(wcs_tmp1) >= server_var(wcs_limit)) then es_xsetinfo wcs_allow "0"
              es es_xif (server_var(wcs_team) = "2") then es_xif (server_var(wcs_tmp1) >= server_var(wcs_limit)) then es_xsetinfo wcs_allow "0"
              es es_xif (server_var(wcs_team) = "3") then es_xif (server_var(wcs_tmp2) >= server_var(wcs_limit)) then es_xsetinfo wcs_allow "0"
            }
	          es es_xif (server_var(wcs_allow) = 1) do
	          {
		          es es_xif (event_var(es_userdead) = 0) then est_Slay event_var(userid)
		          // save the old race !
		          es wcs_saveplayer event_var(userid)
		          es es_xkeysetvalue "WCSuserdata" event_var(userid) "race" server_var(wcs_raceid)
		          es es_xkeysetvalue "WCSusers" server_var(wcs_id) "race" server_var(wcs_raceid)
		          es_xdoblock wcs/WCSfunctions/wcs_playercheck
		          es wcs_getplayer event_var(userid)
		          es_xsetinfo wcs_text 0
		          wcs_getlanguage "wcs_lng" "wcs_lng_changedrace"
		          es es_xformat wcs_text "#lightgreen%1: #green%2" server_var(wcs_lng) server_var(wcs_racename)
		          es es_xtell event_var(userid) #multi server_var(wcs_text)
		          // refresh skills
		          es es_xkeysetvalue "WCSuserdata" event_var(userid) "skillcheck" "1"
		          // unrestrict all (required for predator)
			        es est_UnrestrictAll event_var(userid)
			        // reset the limited flag
			        es es_xkeysetvalue "WCSuserdata" event_var(userid) "limitedrace" "0"
	        	}
	        }
        	es_xelse do
	        {
	        	es es_xmath wcs_total_level - server_var(wcs_maxlevel)
        	  wcs_getlanguage "wcs_lng" "wcs_lng_overmaxlevel"
        	  es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_racename) server_var(wcs_total_level)
        	  es es_xtell event_var(userid) #multi server_var(wcs_text)
	        }
        }
        es_xelse do
        {
          es es_xmath wcs_requiredlevel - server_var(wcs_total_level)
          wcs_getlanguage "wcs_lng" "wcs_lng_notreqlevel"
          es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_racename) server_var(wcs_requiredlevel)
        	es es_xtell event_var(userid) #multi server_var(wcs_text)
        }
      }
    }
  }
  // set page to 0 if race selected
  es es_xif (event_var(commandstring) != "9") then es_xif (event_var(commandstring) != "8") then es_xkeysetvalue WCSuserdata event_var(userid) menu 0
es es_xif (server_var(wcs_debug) = "1") then echo wcs_race_handle end
}

// <<<<<<<<<<<<<<<<< END RACE MENU <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<