////////////////////////////////////////
// WCS - Warcraft:Source              //
// ---------------------------------- //
//  by kRypT0n!Te                     //
//      http://wcs.despo-clan.de      //
// ---------------------------------- //
// Content of this subscript          //
// - many functions...                //
////////////////////////////////////////
// say commands:
// - wcshelp (or warcraft)
// - savexp
// - feedback text...
// - bindmouse3
// - wcsmenu (or wcs)
// - levi (DOD:S only)
//
// admin/internal commands:
// - wcs_say <user|team|world> <text> <userid> (obsolete)
// - wcs_givecredits <userid> <amount> <reason> <forced>
// - wcs_menuzero <userid>
// - wcs_flash <userid> <targetid>
// - wcs_showcredits <userid>
// - wcs_removefx <userid> <effectype>
// - wcs_giveitem <userid> <item>
// - wcs_explode <userid> <targetid> <magnitude> <radius>
// - wcs_dealdamage <userid> <targetid> <magnitude> <radius>
// - wcs_saveplayer <userid>
// - wcs_getplayer <userid>
// - wcs_checkevents <userid> <type>
// - wcs_showinfohandle <type>
//
////////////////////////////////////////

block load
{
	es_xload wcs/WCSfunctions/WCSxp
	es_xload wcs/WCSfunctions/WCSskills
	
	es_xkeygroupcreate "WCSfeedback"
	es_xkeygroupload "WCSfeedback" "|wcs"
	
  es_xexists wcs_exists command wcs_checkevents
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_checkevents wcs/WCSfunctions/wcs_checkevents "wcs_checkevents - usage: wcs_checkevents <userid> <type>"
  es_xexists wcs_exists command wcs_say
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_say wcs/WCSfunctions/wcs_say "wcs_say - usage: wcs_say <user|team|world> <text> <userid>"
  es_xexists wcs_exists command wcs_givecredits
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_givecredits wcs/WCSfunctions/wcs_givecredits "wcs_givecredits - usage: wcs_givecredits <userid> <amount> <reason> <forced>"
  es_xexists wcs_exists command wcshelp
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd wcshelp wcs/WCSfunctions/wcs_help "Shows help message"
  es_xexists wcs_exists command warcrafthelp
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd warcraft wcs/WCSfunctions/wcs_help "Shows help message"
  es_xexists wcs_exists command wcs_menuzero
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_menuzero wcs/WCSfunctions/wcs_menuzero "Resets the menu"
  es_xexists wcs_exists command savexp
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd savexp wcs/WCSfunctions/wcs_savexp "savexp"
  es_xexists wcs_exists command feedback
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd feedback wcs/WCSfunctions/wcs_feedback "Enables users to send feedback"
  es_xexists wcs_exists command wcs_flash
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_flash wcs/WCSfunctions/wcs_flash "wcs_flash <userid> <targetid>"
  es_xexists wcs_exists command wcs_showcredits
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_showcredits wcs/WCSfunctions/wcs_showcredits "wcs_showcredits - usage: wcs_showcredits <userid> (DOD:S only)"
  es_xexists wcs_exists command wcs_showinfohandle
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_showinfohandle wcs/WCSfunctions/wcs_showinfohandle "a handler..."
  es_xexists wcs_exists command bindmouse3
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd bindmouse3 wcs/WCSfunctions/wcs_bindmouse3 "Binds the ultimate on the mouse3 button"
  es_xexists wcs_exists command wcsmenu
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd wcsmenu wcs/WCSfunctions/wcs_wcsmenu "Shows a menu where you can reach all other menus"
  es_xexists wcs_exists command wcs
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd wcs wcs/WCSfunctions/wcs_wcsmenu "Shows a menu where you can reach all other menus"
  es_xexists wcs_exists command wcs_removefx
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_removefx wcs/WCSfunctions/wcs_removefx "wcs_removefx <userid> <type> - Takes an effect from a player (type: freeze, speed, god)"
  es_xexists wcs_exists command wcs_giveitem
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_giveitem wcs/WCSfunctions/wcs_giveitem "wcs_giveitem <id> <item name> - Gives a custom item, checks for death and stacks grenades"
  es_xexists wcs_exists command wcs_explode
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_explode wcs/WCSfunctions/wcs_explode "wcs_explode - usage: wcs_explode <userid> <targetid> <magnitude> <radius>"
  es_xexists wcs_exists command wcs_dealdamage
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_dealdamage wcs/WCSfunctions/wcs_dealdamage "wcs_dealdamage - usage: wcs_dealdamage <userid> <targetid> <damage>"
  es_xexists wcs_exists command wcs_saveplayer
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_saveplayer wcs/WCSfunctions/wcs_saveplayer "save a single player to the DB"
  es_xexists wcs_exists command wcs_getplayer
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_getplayer wcs/WCSfunctions/wcs_getplayer "get the player record from the DB"
  // one special command for DOD:S bugfix (theres no player_jump event)
  es es_xif (server_var(wcs_game) != server_var(wcs_game_css)) do
  {
    es_xexists wcs_exists command levi
    es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd levi wcs/WCSfunctions/wcs_levi_reactivate "Re-activate your levitation (used after climbing ladders)"
  }
}

block unload
{
	es_xunload wcs/WCSfunctions/WCSxp
	es_xunload wcs/WCSfunctions/WCSskills
	es_xkeygroupsave "WCSfeedback" "|wcs"
	es_xkeygroupdelete "WCSfeedback"
}

block wcs_playercheck
{
es es_xif (server_var(wcs_debug) = 1) then profile begin playercheck
  // checks the record in WCSusers, if new player create them
  es es_xif (server_var(wcs_id) != 0) do
  {
    es es_xkeygetvalue wcs_race "WCSusers" server_var(wcs_id) "race"
    es es_xif (server_var(wcs_race) = "0") do
    {
      // random start race, but not a restricted one (NEW PLAYER !)
      es es_xrand wcs_race 1 server_var(wcs_numberofraces)
      es es_xkeygetvalue wcs_tmp "WCSraces" server_var(wcs_race) "required_level"
      es es_xif (server_var(wcs_tmp) != "0") then es_xsetinfo wcs_race "1"
      es es_xkeygetvalue wcs_tmp "WCSraces" server_var(wcs_race) "allow_only"
      es es_xif (server_var(wcs_tmp) != "0") then es_xsetinfo wcs_race "1"
      es es_xkeysetvalue "WCSusers" server_var(wcs_id) "race" server_var(wcs_race)
      es wcs_menuzero event_var(userid)
      es es_xif ("BOT" notin server_var(wcs_id)) then es_xdelayed 11 wcs_race_menu event_var(userid)
      es es_xif (server_var(wcs_showtotalplayers) = "1") then es_xmath wcs_totalplayers + 1
    }
    es es_xkeygetvalue wcs_lastplayed "WCSusers" server_var(wcs_id) "lastplayed"
    es es_xif (server_var(wcs_lastplayed) = 0) then es_xkeysetvalue "WCSusers" server_var(wcs_id) "lastplayed" "0"
    es es_xkeygetvalue wcs_numberofskills "WCSraces" server_var(wcs_race) "numberofskills"
    es_xsetinfo wcs_counter 1
    while "server_var(wcs_counter) <= server_var(wcs_numberofskills)" "es es_xformat wcs_skillnumber skill_%1 server_var(wcs_counter);es es_xkeysetvalue WCSuserdata event_var(userid) server_var(wcs_skillnumber) 0;es_xmath wcs_counter + 1"
  }
es es_xif (server_var(wcs_debug) = 1) then profile end playercheck
}

// >>>>>>>>>>>>>>>>> BEGIN CUSTOM COMMANDS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_say
{
  // usage:    wcs_say <location> <text> <userid>
  // location: world, team, user
  // text:     string for output, plain text in quotes
  // userid:   give a VALID userid (not required with location: world)
  es_xsetinfo wcs_location 0
  es_xsetinfo wcs_text 0
  es_xsetinfo wcs_sayuserid 0
  es_xgetargv wcs_location 1
  es_xgetargv wcs_text 2
  es_xgetargv wcs_sayuserid 3
  es es_xexists wcs_exists userid server_var(wcs_sayuserid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xif (server_var(wcs_location) = "user") do
    {
      es es_xif (server_var(wcs_game) = server_var(wcs_game_css)) do
      {
        es es_xtell server_var(wcs_sayuserid) #green server_var(wcs_text)
      }
      es_xelse do
      {
        es es_xtell server_var(wcs_sayuserid) server_var(wcs_text)
      }
    }
    es es_xif (server_var(wcs_location) = "world") do
    {
      es es_xif (server_var(wcs_game) = server_var(wcs_game_css)) do
      {
        es es_xmsg #green server_var(wcs_sayuserid) server_var(wcs_text)
      }
      es_xelse do
      {
        es es_xmsg server_var(wcs_text)
      }
    }
    es es_xif (server_var(wcs_location) = "team") do
    {
      es es_xgetplayerteam wcs_team server_var(wcs_sayuserid)
      es es_xif (server_var(wcs_team) = 3) then est_msg #c server_var(wcs_text)
      es es_xif (server_var(wcs_team) = 2) then est_msg #t server_var(wcs_text)
    }
  }
  es_xelse do
  {
    es es_xif (server_var(wcs_location) = "world") do
    {
      es es_xif (server_var(wcs_game) = server_var(wcs_game_css)) do
      {
        es es_xmsg #green server_var(wcs_text)
      }
      es_xelse do
      {
        es es_xmsg server_var(wcs_text)
      }
    }
  }
}

block wcs_givecredits
{
es es_xif (server_var(wcs_debug) = 1) then profile begin givecredits
  // usage:  wcs_givecredits <userid> <amount> <reason> <forced>
  // userid: give a VALID userid
  // amount: how many credits to add
  // reason: text (optional)
  // forced: 0/1 (optional)

  es_xsetinfo wcs_giveuserid 0
  es_xsetinfo wcs_amount 0
  es_xsetinfo wcs_reason 0
  es_xsetinfo wcs_forced 0

  es_xgetargv wcs_giveuserid 1
  es_xgetargv wcs_amount 2
  es_xgetargv wcs_reason 3
  es_xgetargv wcs_forced 4

  es es_xexists wcs_exists userid server_var(wcs_giveuserid)
  es es_xif (server_var(wcs_cfg_dods_noshopmenu) != 0) do
  {
    es_xmath wcs_amount / 10
    es est_rounddecimal wcs_amount server_var(wcs_amount) 0
    es wcs_givexp server_var(wcs_giveuserid) server_var(wcs_amount) server_var(wcs_reason) server_var(wcs_forced)
    es_xsetinfo wcs_exists 0
  }
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es_xsetinfo wcs_credits 0
    es_xsetinfo wcs_giveid 0
    es es_xkeygetvalue wcs_giveid "WCSuserdata" server_var(wcs_giveuserid) "id"
    es es_xexists wcs_exists key WCSuserdata server_var(wcs_giveuserid)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      // check playercount if not forced
      es es_xif (server_var(wcs_forced) = 0) do
      {
        es_xsetinfo wcs_playercount 0
        es es_xgetplayercount wcs_playercount
      }
      es_xelse do
      {
        es es_xsetinfo wcs_playercount server_var(wcs_minplayers)
      }

      es es_xif (server_var(wcs_playercount) >= server_var(wcs_minplayers)) do
      {
        es es_xkeygetvalue wcs_credits WCSuserdata server_var(wcs_giveuserid) credits
        // add the credits
        es es_xmath wcs_credits + server_var(wcs_amount)
        // 16000 credits is the limit !
        es es_xif (server_var(wcs_credits) > 16000) then es_xsetinfo wcs_credits 16000
        es es_xkeysetvalue WCSuserdata server_var(wcs_giveuserid) credits server_var(wcs_credits)
        es es_xif (server_var(wcs_reason) != "0") do
        {
        	wcs_getlanguage "wcs_lng" "wcs_lng_gained"
        	wcs_getlanguage "wcs_lng2" "wcs_lng_credits"
          es es_xformat wcs_text "#lightgreen%1 #green%2 #lightgreen%3 : %4" server_var(wcs_lng) server_var(wcs_amount) server_var(wcs_lng2) server_var(wcs_reason)
          es es_xtell server_var(wcs_giveuserid) #multi server_var(wcs_text)
        }
      }
      es_xelse do
      {
      	wcs_getlanguage "wcs_lng" "wcs_lng_minplayers"
        es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_minplayers)
        es es_xtell server_var(wcs_giveuserid) #multi server_var(wcs_text)
      }
    }
  }
es es_xif (server_var(wcs_debug) = 1) then profile end givecredits
}



block wcs_help
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_help start
  es_xgetcmduserid wcs_userid
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es wcs_menuzero server_var(wcs_userid)
    // prints the wchelp menu out
    es es_xif (server_var(wcs_game) != server_var(wcs_game_css)) then wcs_getlanguage "wcs_text" "wcs_lng_help_dods"
    es es_xif (server_var(wcs_game) = server_var(wcs_game_css)) then wcs_getlanguage "wcs_text" "wcs_lng_help_css"
    es es_xformat wcs_text "%1 \n [ %2 by kRypT0n!Te - http://wcs.despo-clan.de ]" server_var(wcs_text) server_var(wcs_ver)
    es es_xstrlen wcs_strlen server_var(wcs_text)
    es es_xif (server_var(wcs_strlen) > 1) then es_xif (server_var(wcs_strlen) < 900) then es_xmenu 30 server_var(wcs_userid) server_var(wcs_text)
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_help end
}

block wcs_checkevents
{
es es_xif (server_var(wcs_debug) = 1) then profile begin checkevents
  // custom command
  es_xgetargv wcs_userid 1
  es_xgetargv wcs_type 2
  es es_xexists wcs_exists userid server_var(wcs_userid)
  // check for item activation
  es es_xkeygetvalue wcs_itemactivate WCSuserdata server_var(wcs_userid) "itemactivate"
  es es_xif (server_var(wcs_itemactivate) = "0") then es_xsetinfo wcs_exists 0
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xexists wcs_exists keygroup server_var(wcs_type)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      es es_xexists wcs_exists key server_var(wcs_type) server_var(wcs_userid)
      es es_xif (server_var(wcs_exists) = 1) do
      {
        es es_xkeygetvalue wcs_sid server_var(wcs_type) server_var(wcs_userid) id
        es es_xkeygetvalue wcs_id "WCSuserdata" server_var(wcs_userid) "id"
        // Check if user id belongs to the steamid/ip, if not remove
        es es_xif (server_var(wcs_sid) = server_var(wcs_id)) do
        {
          es_xsetinfo wcs_cmd 0
          es_xsetinfo wcs_descr 0
          es_xsetinfo wcs_number 0
          //es_foreachval wcs_val in server_var(wcs_type) event_var(userid) "es es_xif (icmd in server_var(wcs_val)) then es_xdoblock wcs/WCSfunctions/wcs_checkevents2"
          // check only cmd1..3
          es_xsetinfo wcs_val "icmd1"
          es_xdoblock wcs/WCSfunctions/wcs_checkevents2
          es_xsetinfo wcs_val "icmd2"
          es_xdoblock wcs/WCSfunctions/wcs_checkevents2
          es_xsetinfo wcs_val "icmd3"
          es_xdoblock wcs/WCSfunctions/wcs_checkevents2
        }
        es_xelse do
        {
          es es_xkeydelete server_var(wcs_type) server_var(wcs_userid)
        }
      }
    }
  }
es es_xif (server_var(wcs_debug) = 1) then profile end checkevents
}

block wcs_checkevents2
{
  es es_xkeygetvalue wcs_cmd server_var(wcs_type) server_var(wcs_userid) server_var(wcs_val)
  es es_xif (server_var(wcs_cmd) != "0") do
  {

    es es_xtoken wcs_number server_var(wcs_val) "1" "icmd"
    es es_xformat wcs_descr "idescr%1" server_var(wcs_number)
    es es_xsetinfo wcs_itemdescrslot server_var(wcs_descr)
    es es_xkeygetvalue wcs_descr server_var(wcs_type) server_var(wcs_userid) server_var(wcs_descr)
    es es_xif (server_var(wcs_descr) != "0") do
    {
      es alias wcs_runcmd server_var(wcs_descr)
      wcs_runcmd
    }
    es es_xsetinfo wcs_itemslot server_var(wcs_val)
    es alias wcs_runcmd server_var(wcs_cmd)
    wcs_runcmd
  }
}

block wcs_giveweapons
{
  es_xsetinfo wcs_wa 0
  es_xsetinfo wcs_wb 0
  es es_xkeygetvalue wcs_wa WCSuserdata event_var(userid) "wa"
  es es_xkeygetvalue wcs_wb WCSuserdata event_var(userid) "wb"
  es es_xif (server_var(wcs_wa) != "0") then es_xdelayed 1 est_Give event_var(userid) server_var(wcs_wa)
  es es_xif (server_var(wcs_wb) != "0") then es_xdelayed 1.2 est_Give event_var(userid) server_var(wcs_wb)
  es es_xif (server_var(wcs_wa) != "0") then es_xdelayed 1.4 est_SetAmmo event_var(userid) 1 50
  es es_xif (server_var(wcs_wb) != "0") then es_xdelayed 1.6 est_SetAmmo event_var(userid) 2 50
}

block wcs_menuzero
{
  es_xsetinfo wcs_uid 0
  es_xgetargv wcs_uid 1
  es es_xexists wcs_exists userid server_var(wcs_uid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xkeycreate "WCSuserdata" server_var(wcs_uid)
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_uid) "menu" "0"
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_uid) "page" "0"
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_uid) "confirm" "0"
  }
}

block wcs_checkff
{
  es es_xif (server_var(mp_friendlyfire) != server_var(wcs_friendlyfire)) do
  {
    es mp_friendlyfire server_var(wcs_friendlyfire)
    wcs_getlanguage "wcs_lng" "wcs_lng_checkff"
    es es_xmsg #green server_var(wcs_lng)
  }
}

block wcs_bindmouse3
{
  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
  {
    // doesnt work all time !!
    es es_xcexec server_var(wcs_userid) "bind mouse3 ultimate"
    wcs_getlanguage "wcs_lng" "wcs_lng_bindmouse3a"
    es es_xtell server_var(wcs_userid) #multi server_var(wcs_lng)
    wcs_getlanguage "wcs_lng" "wcs_lng_bindmouse3b"
    es es_xdelayed 1 es_xtell server_var(wcs_userid) #multi server_var(wcs_lng)
  }
}

block wcs_removefx
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_removefx start
	// type: freeze, speed, god
  es_xsetinfo wcs_removeid 0
  es_xsetinfo wcs_type 0
  es_xgetargv wcs_type 1
  es_xgetargv wcs_removeid 2
  es es_xexists wcs_exists userid server_var(wcs_removeid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es_xsetinfo wcs_dead "1"
    es es_xgetplayerprop wcs_dead server_var(wcs_removeid) "CCSPlayer.baseclass.pl.deadflag"
    es es_xif (server_var(wcs_dead) = "0") do
    {
    	es es_xif (server_var(wcs_type) = "freeze") do
    	{
    	  es est_Freeze server_var(wcs_removeid) 0
    	  es es_xsetplayerprop server_var(wcs_removeid) CBasePlayer.m_fFlags 1
    	}
    	es es_xif (server_var(wcs_type) = "speed") do
    	{
    		es_xsetinfo wcs_speed 1.0
    		es es_xkeygetvalue wcs_speed WCSuserdata server_var(wcs_removeid) "speed"
    		es es_xif (server_var(wcs_speed) != "0") then est_Speed server_var(wcs_removeid) server_var(wcs_speed)
    	}
    	es es_xif (server_var(wcs_type) = "god") then est_God server_var(wcs_removeid) 0
    }
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_removefx server_var(wcs_type) end
}

block wcs_giveitem
{
	es_xsetinfo wcs_uid 0
	es_xsetinfo wcs_item 0
	es_xgetargv wcs_uid 1
	es_xgetargv wcs_item 2
	es es_xexists wcs_exists userid server_var(wcs_uid)
	es es_xgetplayerprop wcs_dead server_var(wcs_uid) "CCSPlayer.baseclass.pl.deadflag"
	es es_xif (server_var(wcs_dead) = "1") then es_xsetinfo wcs_exists "0"
	es es_xif (server_var(wcs_item) notin server_var(wcs_wpn_all)) then es_xsetinfo wcs_exists "0"
	es es_xif (server_var(wcs_exists) = 1) do
	{
		// special HE/Flash/Smoke handling
		es_xsetinfo wcs_stack 0
		es es_xif ("grenade" in server_var(wcs_item)) then es_xsetinfo wcs_stack 1
		es es_xif ("weapon_flashbang" = server_var(wcs_item)) then es_xsetinfo wcs_stack 1
		es es_xif ("frag_" in server_var(wcs_item)) then es_xsetinfo wcs_stack 1
		es es_xif (server_var(wcs_stack) = 1) do
		{
			es_xsetinfo wcs_ammo 0
			es est_GetAmmo wcs_ammo server_var(wcs_uid) server_var(wcs_item)
			es_xmath wcs_ammo + 1
			es es_xgive server_var(wcs_uid) server_var(wcs_item)
			es es_xif (server_var(wcs_ammo) > 1) then est_SetAmmo server_var(wcs_uid) server_var(wcs_item) server_var(wcs_ammo)
		}
		es_xelse do
	  {
			es es_xgive server_var(wcs_uid) server_var(wcs_item)
		}
		es_xstring wcs_item replace "weapon_" " "
		wcs_getlanguage "wcs_lng" "wcs_lng_gained"
		es es_xformat wcs_text "#lightgreen%1 : #green%2" server_var(wcs_lng) server_var(wcs_item)
		es es_xtell server_var(wcs_uid) #multi server_var(wcs_text)
	}
}
// <<<<<<<<<<<<<<<<< END CUSTOM COMMANDS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>>>> BEGIN DAMAGE EFFECTS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_dealdamage
{
es es_xif (server_var(wcs_debug) = 1) then profile begin dealdamage
  // usage:    wcs_dealdamage <userid> <targetid> <damage>
  // userid:   give a VALID userid
  // targetid: id of the target
  // damage:   damage to do

  es_xsetinfo wcs_dduserid 0
  es_xsetinfo wcs_ddtargetid 0
  es_xsetinfo wcs_dddamage 0

  es_xgetargv wcs_dduserid 1
  es_xgetargv wcs_ddtargetid 2
  es_xgetargv wcs_dddamage 3

  es es_xexists wcs_exists userid server_var(wcs_dduserid)
  es es_xif (server_var(wcs_dddamage) < 1) then es_xsetinfo wcs_exists 0
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xexists wcs_exists userid server_var(wcs_ddtargetid)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      es est_GetHealth wcs_health server_var(wcs_ddtargetid)
      es es_xif (server_var(wcs_health) > server_var(wcs_dddamage)) do
      {
        // doesnt work yet (EST bug) !!!
        //es est_damage server_var(wcs_dduserid) server_var(wcs_ddtargetid) server_var(wcs_dddamage)
        es es_xmath wcs_health - server_var(wcs_dddamage)
        es est_sethealth server_var(wcs_ddtargetid) server_var(wcs_health)
      }
      es_xelse do
      {
        // new method, player will die !
        es es_xgive server_var(wcs_ddtargetid) point_hurt
        es es_xfire server_var(wcs_ddtargetid) !self addoutput "targetname badboy"
        es es_xfire server_var(wcs_ddtargetid) point_hurt addoutput "targetname rps_damage"
        es es_xformat wcs_hurt_damage "Damage %1" server_var(wcs_dddamage)
        es es_xfire server_var(wcs_ddtargetid) rps_damage addoutput server_var(wcs_hurt_damage)
        es es_xfire server_var(wcs_ddtargetid) rps_damage addoutput "DamageType 1"
        es es_xfire server_var(wcs_dduserid) rps_damage addoutput "DamageTarget badboy"
        es es_xfire server_var(wcs_ddtargetid) rps_damage TurnOn
        es es_xfire server_var(wcs_dduserid) rps_damage Hurt
        es es_xfire server_var(wcs_ddtargetid) rps_damage kill
        es es_xfire server_var(wcs_ddtargetid) !self addoutput "targetname taco"
      }
    }
  }
es es_xif (server_var(wcs_debug) = 1) then profile end dealdamage
}

block wcs_explode
{
  // usage:     wcs_explode <userid> <targetid> <magnitude> <radius>
  // userid:    give a VALID userid
  // targetid:  id of the target
  // magnitude: strenght of explosion
  // radius:    size of the effect
  es_xsetinfo wcs_userid 0
  es_xsetinfo wcs_targetid 0
  es_xsetinfo wcs_explodemagnitude 0
  es_xsetinfo wcs_exploderadius 0
  es_xgetargv wcs_userid 1
  es_xgetargv wcs_targetid 2
  es_xgetargv wcs_explodemagnitude 3
  es_xgetargv wcs_exploderadius 4
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es_xsetinfo wcs_tmp "0"
  es es_xkeygetvalue wcs_tmp "WCSuserdata" server_var(wcs_userid) "ismole"
  es es_xif (server_var(wcs_tmp) = "1") then es_xsetinfo wcs_exists "0"
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xexists wcs_exists userid server_var(wcs_targetid)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      // set to default if missing parameters
      es es_xif (server_var(wcs_explodemagnitude) = 0) then es_xsetinfo wcs_explodemagnitude 10
      es es_xif (server_var(wcs_exploderadius) = 0) then es_xsetinfo wcs_exploderadius 10
      es_xsetinfo wcs_x 0
      es_xsetinfo wcs_y 0
      es_xsetinfo wcs_z 0
      es es_xgetplayerlocation wcs_x wcs_y wcs_z server_var(wcs_targetid)
      // random explosion sound
      es_xsetinfo wcs_exrand 0
      es_xrand wcs_exrand 1 6
      es es_xif (server_var(wcs_exrand) = 1) then es_xsetinfo wcs_sound "weapons\explode3.wav"
      es es_xif (server_var(wcs_exrand) = 2) then es_xsetinfo wcs_sound "weapons\explode4.wav"
      es es_xif (server_var(wcs_exrand) = 3) then es_xsetinfo wcs_sound "weapons\explode5.wav"
      es es_xif (server_var(wcs_exrand) = 4) then es_xsetinfo wcs_sound "weapons\mortar\mortar_explode1.wav"
      es es_xif (server_var(wcs_exrand) = 5) then es_xsetinfo wcs_sound "weapons\mortar\mortar_explode2.wav"
      es es_xif (server_var(wcs_exrand) = 6) then es_xsetinfo wcs_sound "weapons\mortar\mortar_explode3.wav"
      es es_xif (server_var(wcs_cfg_friendlyexplosion) = "1") do
      {
	      //set explosion variables
	      es es_xgive server_var(wcs_userid) env_explosion
	      //Origin
	      es es_xformat wcs_format "origin %1 %2 %3" server_var(wcs_x) server_var(wcs_y) server_var(wcs_z)
	      es es_xfire server_var(wcs_userid) env_explosion addoutput server_var(wcs_format)
	      es es_xformat wcs_format "%1,%2,%3" server_var(wcs_x) server_var(wcs_y) server_var(wcs_z)
	      es es_xsetindexprop server_var(eventscripts_lastgive) CBaseEntity.m_vecOrigin server_var(wcs_format)
	      //Power
	      es es_xformat wcs_format "imagnitude %1" server_var(wcs_explodemagnitude)
	      es es_xfire server_var(wcs_userid) env_explosion addoutput server_var(wcs_format)
	      //Range
	      es es_xformat wcs_format "iradiusoverride %1" server_var(wcs_exploderadius)
	      es es_xfire server_var(wcs_userid) env_explosion addoutput server_var(wcs_format)
	      // Set the owner of the explosion.
	      es_xsetinfo owner 0
	      es es_xgetplayerhandle owner server_var(wcs_userid)
	      es_xexists wcs_exists keygroup wcs_explosions
	      es es_xif (server_var(wcs_exists) = 1) then es_xkeygroupdelete wcs_explosions
	      es_xcreateentitylist wcs_explosions env_explosion
	      es_xsetinfo wcs_expindex 0
	      es_xforeachkey wcs_expindex in wcs_explosions "esnq es_xsetindexprop server_var(wcs_expindex) CBaseEntity.m_hOwnerEntity server_var(owner)"
	      // This is it... time to explode!
	      es es_xif (server_var(wcs_soundfx) = 1) then es_xemitsound entity server_var(eventscripts_lastgive) server_var(wcs_sound) 1 0.60
	      es es_xfire server_var(wcs_userid) env_explosion explode
	      es es_xfire server_var(wcs_userid) env_explosion kill
      }
      es_xelse do
      {
        // no area damage effect
        es_xsetinfo wcs_exdmg 0
        es_xcopy wcs_exdmg wcs_explodemagnitude
        es es_xmath wcs_exdmg * server_var(wcs_exploderadius)
        es_xmath wcs_exdmg / 150
        es est_RoundDecimal wcs_exdmg server_var(wcs_exdmg) 0
        es wcs_dealdamage server_var(wcs_userid) server_var(wcs_targetid) server_var(wcs_exdmg)
        es es_xif (server_var(wcs_soundfx) = 1) then est_PlayPlayer server_var(wcs_targetid) server_var(wcs_sound)
      }
    }
    es_xelse do
    {
      echo "[WCS] WARNING: called wcs_explode with non-existing targetid !"
    }
  }
}
// <<<<<<<<<<<<<<<<< END DAMAGE EFFECTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>>>> CLEANUP >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_cleanup
{
  // a try to implement a cleanup script, but Eventscripts cannot handle large files ...
  es_xsetinfo wcs_lastplayed 0
  es_xsetinfo wcs_time 0
  es_xgettime wcs_time
  es_xsetinfo wcs_removetime 0
  es es_xsetinfo wcs_deletetime server_var(wcs_deletedays)
  es_xmath wcs_deletetime * 86400
  es es_xif (server_var(wcs_deletetime) > 0) do
  {
    es es_xif (server_var(wcs_debug) = 1) then profile begin cleanup_duration
    es_xforeachkey wcs_key in WCSusers "es es_xif (server_var(wcs_key) != bot) then es_xif (server_var(wcs_key) != 0) then es_xdoblock wcs/WCSfunctions/wcs_cleanupcheck"
    // delete every key with deleteflag = 1
    es es_xkeygroupfilter WCSusers not "deleteflag" 1
    // save xp to file
    es es_xkeygroupsave WCSusers "|wcs/WCSusers"
    es es_xif (server_var(wcs_debug) = 1) then profile end cleanup_duration
    es es_xmsg Cleanup successfull, duration: server_var(cleanup_duration)
  }
  es_xelse do
  {
    echo "[WCS] WARNING: cleanup executed, but deletetime is 0 !! All players would have beed deleted ..."
  }
}

block wcs_cleanupcheck
{
  es es_xsetinfo wcs_removetime server_var(wcs_time)
  es es_xkeygetvalue wcs_lastplayed WCSusers server_var(wcs_key) lastplayed
  es es_xmath wcs_removetime - server_var(wcs_lastplayed)
  es es_xif (server_var(wcs_removetime) >= server_var(wcs_deletetime)) then es_xkeysetvalue WCSusers server_var(wcs_key) "deleteflag" "1"
}
// <<<<<<<<<<<<<<<<< END CLEANUP <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


// >>>>>>>>>>>>>>>>> START COMMON INFO >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_showinfohandle
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showinfohandle start
  // custom command - argument is type of showinfo
  es_xsetinfo wcs_type 0
  es_xgetargv wcs_type 1
  es es_xexists wcs_exists key "WCSuserdata" event_var(userid)
  es es_xif (server_var(wcs_type) notin "raceinfo|shopinfo|playerinfo") then es_xsetinfo wcs_exists 0
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xkeygetvalue wcs_page "WCSuserdata" event_var(userid) "page"
    // 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 es_xif (server_var(wcs_type) = "raceinfo") then wcs_showraceinfo event_var(userid)
        es es_xif (server_var(wcs_type) = "shopinfo") then wcs_showshopinfo event_var(userid)
        es es_xif (server_var(wcs_type) = "playerinfo") then wcs_showplayerinfo event_var(userid)
      }
      es_xelse do
      {
        es es_xkeysetvalue "WCSuserdata" event_var(userid) "page" "0"
        es es_xkeysetvalue "WCSuserdata" event_var(userid) "menu" "0"
      }
    }
    // handle next
    es es_xif (event_var(commandstring) = "9") do
    {
      es_xsetinfo wcs_next 0
      es es_xif (server_var(wcs_type) != "playerinfo") then es_xkeygetvalue wcs_next "WCSinfo" server_var(wcs_type) server_var(wcs_page)
      es es_xif (server_var(wcs_type) = "playerinfo") do
      {
      	es_xgetplayercount wcs_playercount
      	es_xcopy wcs_tmp wcs_page
      	es_xmath wcs_tmp * 7
      	es es_xif (server_var(wcs_playercount) > server_var(wcs_tmp)) then es_xsetinfo wcs_next "ok"
      }
      es es_xif (server_var(wcs_next) != "0") do
      {
      	es_xmath wcs_page + 1
        es es_xif (server_var(wcs_type) = "raceinfo") then wcs_showraceinfo event_var(userid)
        es es_xif (server_var(wcs_type) = "shopinfo") then wcs_showshopinfo event_var(userid)
        es es_xif (server_var(wcs_type) = "playerinfo") then wcs_showplayerinfo event_var(userid)
      }
      es_xelse do
      {
        es es_xkeysetvalue WCSuserdata event_var(userid) "page" "0"
        es es_xkeysetvalue WCSuserdata event_var(userid) "menu" "0"
      }
    }
    es es_xif (event_var(commandstring) != "9") do
    {
      es es_xif (event_var(commandstring) != "8") do
      {
      	es es_xif (server_var(wcs_type) = "shopinfo") do
      	{
      		es es_xif (event_var(commandstring) = "7") do
      		{
      			es es_xkeygetvalue wcs_category WCSshop server_var(wcs_page) category
      			es wcs_shop_menu2 event_var(userid) server_var(wcs_category)
      		}
      		es_xelse do
      		{
      			es es_xkeysetvalue "WCSuserdata" event_var(userid) "page" "0"
            es es_xkeysetvalue "WCSuserdata" event_var(userid) "menu" "0"
      		}
      	}
      	es es_xif (server_var(wcs_type) = "playerinfo") do
      	{
      		es es_xif (event_var(commandstring) != "10") do
      		{
      		  es_xcopy wcs_target wcs_page
      		  es_xmath wcs_target * 7
      		  es_xmath wcs_target - 7
      		  es es_xmath wcs_target + event_var(commandstring)
      		  es_xsetinfo wcs_scounter 1
      		  es_xforeachkey wcs_key in "WCSuserdata" "es es_xif (server_var(wcs_target) = server_var(wcs_scounter)) then es_xdoblock wcs/WCSusers/wcs_playerinfo_details;es_xmath wcs_scounter + 1"
      	  }
      	}
      	es es_xif (server_var(wcs_type) != "shopinfo") do
      	{
      		es es_xif (server_var(wcs_type) != "playerinfo") do
      		{
            es es_xkeysetvalue "WCSuserdata" event_var(userid) "page" "0"
            es es_xkeysetvalue "WCSuserdata" event_var(userid) "menu" "0"
          }
        }
      }
    }
  }
  es_xelse do
  {
    es es_xkeycreate "WCSuserdata" event_var(userid)
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showinfohandle end
}
// <<<<<<<<<<<<<<<<< END COMMON INFO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

block wcs_levi_reactivate
{
  // fix for losing gravity when climbing ladders
  es es_xexists wcs_exists key "WCSuserdata" event_var(userid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xkeygetvalue wcs_gravity "WCSuserdata" event_var(userid) "gravity"
    es es_xif (server_var(wcs_gravity) != "0") do
    {
      es es_xgetplayerprop wcs_dead event_var(userid) "CCSPlayer.baseclass.pl.deadflag"
      es es_xif (server_var(wcs_dead) = "0") then est_SetGravity event_var(userid) server_var(wcs_gravity)
    }
  }
}

block wcs_flash
{
  es_xgetargv wcs_uid 1
  es_xgetargv wcs_targetid 2
  // "superflash" effect, attacker has the flag
  es es_xexists wcs_exists key WCSuserdata server_var(wcs_uid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
  	es es_xkeygetvalue wcs_target WCSuserdata server_var(wcs_uid) flash_target
   	es es_xif (server_var(wcs_target) = "attacker") do
	  {
	  	es es_xkeygetvalue wcs_duration WCSuserdata server_var(wcs_uid) flash_duration
  	  es es_xkeygetvalue wcs_alpha WCSuserdata server_var(wcs_uid) flash_alpha
  		es es_xif (server_var(wcs_duration) != "0") do
		  {
  			es es_xgetplayerprop wcs_tmp server_var(wcs_targetid) CCSPlayer.m_flFlashDuration
			  es es_xmath wcs_duration * server_var(wcs_tmp)
			  es es_xsetplayerprop server_var(wcs_targetid) CCSPlayer.m_flFlashDuration server_var(wcs_duration)
		  }
      es es_xif (server_var(wcs_alpha) != "0") then es_xsetplayerprop server_var(wcs_targetid) CCSPlayer.m_flFlashMaxAlpha server_var(wcs_alpha)
	  }
	}
  // "weak flash" effect, victim has the flag
  es es_xexists wcs_exists key WCSuserdata server_var(wcs_targetid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
  	es es_xkeygetvalue wcs_target WCSuserdata server_var(wcs_targetid) flash_target
   	es es_xif (server_var(wcs_target) = "victim") do
	  {
	  	es es_xkeygetvalue wcs_duration WCSuserdata server_var(wcs_targetid) flash_duration
  	  es es_xkeygetvalue wcs_alpha WCSuserdata server_var(wcs_targetid) flash_alpha
  		es es_xif (server_var(wcs_duration) != "0") do
		  {
  			es es_xgetplayerprop wcs_tmp server_var(wcs_targetid) CCSPlayer.m_flFlashDuration
			  es es_xmath wcs_duration * server_var(wcs_tmp)
			  es es_xsetplayerprop server_var(wcs_targetid) CCSPlayer.m_flFlashDuration server_var(wcs_duration)
		  }
      es es_xif (server_var(wcs_alpha) != "0") then es_xsetplayerprop server_var(wcs_targetid) CCSPlayer.m_flFlashMaxAlpha server_var(wcs_alpha)
	  }
	}
}

block wcs_saveplayer
{
es es_xif (server_var(wcs_debug) = 1) then profile begin saveplayer
	es_xgetargv wcs_uid 1
	es es_xexists wcs_exists key WCSuserdata server_var(wcs_uid)
	es es_xif (server_var(wcs_exists) = 1) do
	{
		// save the record
		es es_xkeygetvalue wcs_id "WCSuserdata" server_var(wcs_uid) "id"
		es es_xkeygetvalue wcs_race "WCSuserdata" server_var(wcs_uid) "race"
		es es_xkeygetvalue wcs_level "WCSuserdata" server_var(wcs_uid) "level"
		es es_xkeygetvalue wcs_xp "WCSuserdata" server_var(wcs_uid) "xp"
		es es_xkeygetvalue wcs_unused "WCSuserdata" server_var(wcs_uid) "unused"

		es es_xkeysetvalue "WCSusers" server_var(wcs_id) "race" server_var(wcs_race)
		// save all skill levels
		es_xsetinfo wcs_counter 1
		es es_xkeygetvalue wcs_numberofskills "WCSraces" server_var(wcs_race) "numberofskills"
		es es_xformat wcs_tmp2 "%1|%2" server_var(wcs_level) server_var(wcs_xp)
    while "server_var(wcs_counter) <= server_var(wcs_numberofskills)" "es es_xformat wcs_skillnumber skill_%1 server_var(wcs_counter);es es_xkeygetvalue wcs_skill WCSuserdata server_var(wcs_uid) server_var(wcs_skillnumber);es es_xif (server_var(wcs_skill) = 0) then es_xsetinfo wcs_skill 0;es es_xformat wcs_tmp2 %1|%2 server_var(wcs_tmp2) server_var(wcs_skill);es_xmath wcs_counter + 1"
    es es_xformat wcs_racef "race_%1" server_var(wcs_race)
    es es_xkeysetvalue "WCSusers" server_var(wcs_id) server_var(wcs_racef) server_var(wcs_tmp2)
	}
es es_xif (server_var(wcs_debug) = 1) then profile end saveplayer
}

block wcs_getplayer
{
es es_xif (server_var(wcs_debug) = 1) then profile begin getplayer
	es_xgetargv wcs_uid 1
	es es_xexists wcs_exists key WCSuserdata server_var(wcs_uid)
	es es_xif (server_var(wcs_exists) = 1) do
	{
		// get the WCSusers -> WCSuserdata stuff
		es es_xkeygetvalue wcs_id "WCSuserdata" server_var(wcs_uid) "id"
    es es_xkeygetvalue wcs_race "WCSusers" server_var(wcs_id) "race"
    es es_xformat wcs_racef "race_%1" server_var(wcs_race)
    es es_xkeygetvalue wcs_tmp1 "WCSusers" server_var(wcs_id) server_var(wcs_racef)
    es es_xkeygetvalue wcs_numberofskills "WCSraces" server_var(wcs_race) "numberofskills"
    es es_xif (server_var(wcs_tmp1) = "0") do
    {
    	// convert the old version
    	es_xsetinfo wcs_convtmp 0
    	// step 1: rename to a temporary key, and save the old values in a clean new
    	es es_xkeyrename WCSusers server_var(wcs_id) "convtmp"
    	es es_xkeycreate WCSusers server_var(wcs_id)
    	es es_xkeygetvalue wcs_convtmp WCSusers "convtmp" "race"
    	es es_xkeysetvalue WCSusers server_var(wcs_id) "race" server_var(wcs_convtmp)
    	es es_xkeygetvalue wcs_convtmp WCSusers "convtmp" "lastplayed"
    	es es_xkeysetvalue WCSusers server_var(wcs_id) "lastplayed" server_var(wcs_convtmp)
    	// step 2: check each race, and convert the level/xp/skills (unused is dropped)
    	es_xsetinfo wcs_counter 1
    	es_xsetinfo wcs_convskill 0
    	es_xsetinfo wcs_convlevel 0
    	es_xsetinfo wcs_convxp 0
    	es_xsetinfo wcs_convnumber 0
    	es_xsetinfo wcs_convtmp1 0
    	es_xsetinfo wcs_convtmp2 0
      while "server_var(wcs_counter) <= server_var(wcs_numberofraces)" "es_xdoblock wcs/WCSfunctions/wcs_convertdb;es_xmath wcs_counter + 1"
      // step 3: get the current race (now in new format)
      es es_xformat wcs_racef "race_%1" server_var(wcs_race)
      es es_xkeygetvalue wcs_tmp1 "WCSusers" server_var(wcs_id) server_var(wcs_racef)
      // step 4: clean up the temporary key
      es_xkeydelete WCSusers "convtmp"
    }
    // if the record is empty (new race) init with 0
    es es_xif (server_var(wcs_tmp1) = "0") do
    {
      es_xsetinfo wcs_counter 1
		  es es_xkeygetvalue wcs_numberofskills "WCSraces" server_var(wcs_race) "numberofskills"
		  es_xformat wcs_tmp1 "0|0"
      while "server_var(wcs_counter) <= server_var(wcs_numberofskills)" "es es_xformat wcs_tmp1 %1|0 server_var(wcs_tmp1);es_xmath wcs_counter + 1"
    }
    // new version of the playerrecord
    es es_xtoken wcs_level server_var(wcs_tmp1) 1 "|"
    es es_xtoken wcs_xp server_var(wcs_tmp1) 2 "|"
    es_xsetinfo wcs_counter 1
    es_xsetinfo wcs_skilllevel 0
    while "server_var(wcs_counter) <= server_var(wcs_numberofskills)" "es_xsetinfo wcs_skill 0;es es_xformat wcs_skillnumber skill_%1 server_var(wcs_counter);es_xcopy wcs_scounter wcs_counter;es_xmath wcs_scounter + 2;es es_xtoken wcs_skill server_var(wcs_tmp1) server_var(wcs_scounter) |;es es_xif(server_var(wcs_skill) = 0) then es_xsetinfo wcs_skill 0;es es_xkeysetvalue WCSuserdata server_var(wcs_uid) server_var(wcs_skillnumber) server_var(wcs_skill);es_xmath wcs_counter + 1;es es_xmath wcs_skilllevel + server_var(wcs_skill)"
    es es_xkeysetvalue WCSuserdata server_var(wcs_uid) race server_var(wcs_race)
    es es_xkeysetvalue WCSuserdata server_var(wcs_uid) level server_var(wcs_level)
    es es_xkeysetvalue WCSuserdata server_var(wcs_uid) xp server_var(wcs_xp)
    // CALCULATE UNUSED !!!!!!!!!!!!!
    es_xsetinfo wcs_unused 0
    es es_xif (server_var(wcs_level) > server_var(wcs_skilllevel)) do
    {
    	es_xcopy wcs_unused wcs_level
    	es es_xmath wcs_unused - server_var(wcs_skilllevel)
    }
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_uid) "unused" server_var(wcs_unused)
    es_xsetinfo wcs_total_level 0
    es_xsetinfo wcs_max_level 0
    alias wcs_calctotallevel "es es_xkeygetvalue wcs_tmp WCSusers server_var(wcs_id) server_var(wcs_val);es es_xtoken wcs_tmp server_var(wcs_tmp) 1 |;es es_xmath wcs_total_level + server_var(wcs_tmp);es es_xif (server_var(wcs_tmp) > server_var(wcs_max_level)) then es_xsetinfo wcs_max_level server_var(wcs_tmp)"
    es es_xforeachval wcs_val in WCSusers server_var(wcs_id) "es es_xif (race_ in server_var(wcs_val)) then wcs_calctotallevel"
    es es_xif (server_var(wcs_cfg_globallevel) = "0") then es_xkeysetvalue WCSuserdata server_var(wcs_uid) total_level server_var(wcs_total_level)
    es es_xif (server_var(wcs_cfg_globallevel) = "1") do
    {
    	es es_xkeysetvalue WCSuserdata server_var(wcs_uid) level server_var(wcs_max_level)
  	  es es_xkeysetvalue WCSuserdata server_var(wcs_uid) total_level server_var(wcs_max_level)
    }
	}
es es_xif (server_var(wcs_debug) = 1) then profile end getplayer
}

block wcs_convertdb
{
//	"STEAM_0:1:9632745"
//	{
//		"race"		"2"
//		"lastplayed"		"1174434179"
//		"level_1"		"0"
//		"xp_1"		"0"
//		"unused_1"		"0"
//		"race1_skill1"		"0"
//		"race1_skill2"		"0"
//		"race1_skill3"		"0"
//		"race1_skill4"		"0"
//		"level_4"		"11"
//		"xp_4"		"680"
//		"unused_4"		"0"
//		"race4_skill1"		"8"
//		"race4_skill2"		"1"
//		"race4_skill3"		"0"
//		"race4_skill4"		"2"
//		"race_4"		"16|1037|8|0|0|8"
//		"race_2"		"3|21|2|0|0|0"
//	}
	// race = wcs_counter, key is convtmp, steamid is wcs_id, wcs_convskill, wcs_convlevel, wcs_convxp
	// level
	es es_xformat wcs_levelf "level_%1" server_var(wcs_counter)
  es es_xkeygetvalue wcs_convlevel "WCSusers" "convtmp" server_var(wcs_levelf)
  // check if new level already exists
  es es_xformat wcs_racef "race_%1" server_var(wcs_counter)
  es es_xkeygetvalue wcs_convtmp1 "WCSusers" "convtmp" server_var(wcs_racef)
  es es_xif (server_var(wcs_convtmp1) != "0") do
  {
  	es es_xtoken wcs_convtmp2 server_var(wcs_convtmp1) 1 "|"
    es es_xif (server_var(wcs_convtmp2) > server_var(wcs_convlevel)) do
    {
    	// insert current value, do not convert
    	es_xsetinfo wcs_convlevel "0"
    	es es_xkeysetvalue "WCSusers" server_var(wcs_id) server_var(wcs_racef) server_var(wcs_convtmp1)
    }
  }
  // xp
  es es_xformat wcs_xpf "xp_%1" server_var(wcs_counter)
  es es_xkeygetvalue wcs_convxp "WCSusers" "convtmp" server_var(wcs_xpf)
  es es_xif (server_var(wcs_convlevel) != "0") do
  {
    es es_xformat wcs_tmp2 "%1|%2" server_var(wcs_convlevel) server_var(wcs_convxp)
    // skills
    es es_xkeygetvalue wcs_convnumber "WCSraces" server_var(wcs_counter) "numberofskills"
    es_xsetinfo wcs_convcounter "1"
    while "server_var(wcs_convcounter) <= server_var(wcs_convnumber)" "es es_xformat wcs_convskill race%1_skill%2 server_var(wcs_counter) server_var(wcs_convcounter);es es_xkeygetvalue wcs_convskill WCSusers convtmp server_var(wcs_convskill);es es_xif (server_var(wcs_convskill) = 0) then es_xsetinfo wcs_convskill 0;es es_xformat wcs_tmp2 %1|%2 server_var(wcs_tmp2) server_var(wcs_convskill);es_xmath wcs_convcounter + 1"
    es es_xkeysetvalue "WCSusers" server_var(wcs_id) server_var(wcs_racef) server_var(wcs_tmp2)
  }
}

block wcs_feedback
{
	es_xgetcmduserid wcs_uid
	es_xgetargs wcs_text
	es es_xif (server_var(wcs_text) != "0") do
	{
		es es_xgetplayername wcs_name server_var(wcs_uid)
		es es_xexists wcs_exists key "WCSfeedback" server_var(wcs_name)
		es es_xif (server_var(wcs_exists) = "0") then es_xkeycreate "WCSfeedback" server_var(wcs_name)
		es es_xkeygetvalue wcs_tmp1 "WCSfeedback" server_var(wcs_name) "number"
		es es_xif (server_var(wcs_tmp1) = "0") then es_xsetinfo wcs_tmp1 "0"
		es_xmath wcs_tmp1 + 1
		es es_xkeysetvalue "WCSfeedback" server_var(wcs_name) "number" server_var(wcs_tmp1)
		es es_xkeysetvalue "WCSfeedback" server_var(wcs_name) server_var(wcs_tmp1) server_var(wcs_text)
		wcs_getlanguage "wcs_lng" "wcs_lng_feedback"
		es es_xtell server_var(wcs_uid) #multi server_var(wcs_lng)
		es_xkeygroupsave "WCSfeedback" "|wcs"
	}
	es_xelse do
	{
		wcs_getlanguage "wcs_lng" "wcs_lng_feedbackfailed"
		es es_xtell server_var(wcs_uid) #multi server_var(wcs_lng)
	}
}

block wcs_savexp
{
  es_xgetcmduserid wcs_userid
  wcs_getlanguage "wcs_lng" "wcs_lng_savexp"
  es es_xtell server_var(wcs_userid) #multi server_var(wcs_lng)
  es wcs_saveplayer server_var(wcs_userid)
}

block wcs_wcsmenu
{
  es_xgetcmduserid wcs_userid
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es es_xif (server_var(wcs_exists) = 1) do
  {
  	es wcs_menuzero server_var(wcs_userid)
  	es es_xkeysetvalue "WCSuserdata" server_var(wcs_userid) "menu" "wcsmenu"
  	wcs_getlanguage "wcs_lng" "wcs_lng_wcsmenu"
  	es es_xmenu 0 server_var(wcs_userid) server_var(wcs_lng)
  }
}

block wcs_wcsmenu_handle
{
  es es_xif (event_var(commandstring) = "1") do
  {
    es_xsetinfo wcs_exists 1
    es es_xif (server_var(wcs_cfg_noshopmenu) = "1") then es_xsetinfo wcs_exists 0
    es es_xif (server_var(wcs_game) = server_var(wcs_game_dods)) then es_xif (server_var(wcs_cfg_dods_noshopmenu) = "1") then es_xsetinfo wcs_exists 0
    es es_xif (server_var(wcs_exists) = "0") do
    {
      wcs_getlanguage "wcs_lng" "wcs_lng_noshopmenu"
      es es_xtell event_var(userid) #multi server_var(wcs_lng)
      es_xsetinfo wcs_exists 0
    }
    es es_xif (server_var(wcs_exists) = "1") do
    {
      es es_xif (server_var(wcs_game) = server_var(wcs_game_dods)) then wcs_showcredits event_var(userid)
      es wcs_shop_menu event_var(userid)
    }
  }
  es es_xif (event_var(commandstring) = "2") then wcs_showshopinfo event_var(userid)
  es es_xif (event_var(commandstring) = "3") then wcs_showskills event_var(userid)
  es es_xif (event_var(commandstring) = "4") then wcs_resetskills event_var(userid)
  es es_xif (event_var(commandstring) = "5") then wcs_spendskills event_var(userid)
  es es_xif (event_var(commandstring) = "6") then wcs_race_menu event_var(userid)
  es es_xif (event_var(commandstring) = "7") then wcs_showraceinfo event_var(userid)
  es es_xif (event_var(commandstring) = "8") then wcs_showplayerinfo event_var(userid)
  es es_xif (event_var(commandstring) = "9") then wcs_showtop10 event_var(userid)
}
