////////////////////////////////////////
// WCS - Warcraft:Source              //
// ---------------------------------- //
//  by kRypT0n!Te                     //
//      http://wcs.despo-clan.de      //
// ---------------------------------- //
// Content of this subscript          //
// - XP handling like giveXP, levelup //
////////////////////////////////////////
// say commands:
// showxp
//
// admin commands:
// wcs_givexp <userid> <amount> <reason> <forced>
// wcs_givelevel <userid> <amount> <reason> <forced>
// wcs_showxp <userid>
// wcs_deathhandle <userid> <attackerid> <weapon> <headshot>
//
////////////////////////////////////////

block load
{
  es_xexists wcs_exists command wcs_givexp
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_givexp wcs/WCSfunctions/WCSxp/wcs_givexp "wcs_givexp - usage: wcs_givexp <userid> <amount> <reason> <forced>"
  es_xexists wcs_exists command wcs_givelevel
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_givelevel wcs/WCSfunctions/WCSxp/wcs_givelevel "wcs_givelevel - usage: wcs_givelevel <userid> <amount> <reason> <forced>"
  es_xexists wcs_exists command wcs_showxp
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_showxp wcs/WCSfunctions/WCSxp/wcs_showxp "wcs_showxp - usage: wcs_showxp <userid>"
  es_xexists wcs_exists command showxp
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd showxp wcs/WCSfunctions/WCSxp/wcs_showxp "Shows your current XP and level"
  es_xexists wcs_exists command wcs_deathhandle
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_deathhandle wcs/WCSfunctions/WCSxp/wcs_deathhandle "Checks for death events"
}

block wcs_showxp
{
  // 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)
  }
  // check if its valid
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xkeygetvalue wcs_race "WCSuserdata" server_var(wcs_userid) "race"
    es es_xexists wcs_exists key "WCSraces" server_var(wcs_race)
    es es_xif (server_var(wcs_exists) = 1) do
    {
      es es_xkeygetvalue wcs_racename "WCSraces" server_var(wcs_race) "name"
    }
    es_xelse do
    {
      es_xsetinfo wcs_race 1
      es_xsetinfo wcs_racename "error"
    }
    es es_xkeygetvalue wcs_level WCSuserdata server_var(wcs_userid) "level"
    es es_xkeygetvalue wcs_xp WCSuserdata server_var(wcs_userid) "xp"
    es es_xsetinfo wcs_levelup server_var(wcs_level)
    es_xmath wcs_levelup + 1
    es es_xmath wcs_levelup * server_var(wcs_levelxp)
    wcs_getlanguage "wcs_lng" "wcs_lng_level"
    es es_xformat wcs_text "#green%1 #lightgreen- %2: #green%3 #lightgreen- XP: #green%4/%5" server_var(wcs_racename) server_var(wcs_lng) server_var(wcs_level) server_var(wcs_xp) server_var(wcs_levelup)
    es es_xtell server_var(wcs_userid) #multi server_var(wcs_text)
  }
}

block wcs_givexp
{
es es_xif (server_var(wcs_debug) = 1) then profile begin givexp
  // usage:  wcs_givexp <userid> <amount> <reason> <forced>
  // userid: give a VALID userid
  // amount: how many XP to add
  // reason: text (optional)
  // forced: 0/1 (optional)
  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_exists) = 1) then es_xexists wcs_exists key "WCSuserdata" server_var(wcs_giveuserid)
  // check for bots
  es es_xif (server_var(wcs_cfg_allowbotsgetxp) = "0") do
  {
  	es es_xkeygetvalue wcs_tmp WCSuserdata server_var(wcs_giveuserid) id
  	es es_xif ("BOT" in server_var(wcs_tmp)) then es_xsetinfo wcs_exists 0
  }
  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_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_unused WCSuserdata server_var(wcs_giveuserid) "unused"
      es es_xkeygetvalue wcs_level WCSuserdata server_var(wcs_giveuserid) "level"
      es es_xkeygetvalue wcs_xp WCSuserdata server_var(wcs_giveuserid) "xp"
      // add the XP (with turbomode support)
      es es_xif (server_var(wcs_turbomode) = "1") then es_xif (server_var(wcs_turbomultiplier) > 1) then es_xmath wcs_amount * server_var(wcs_turbomultiplier)
      es es_xmath wcs_xp + server_var(wcs_amount)
      // support for negative amount
      es es_xif (server_var(wcs_xp) <= 0) then es_xsetinfo wcs_xp 0
      es es_xsetinfo wcs_levelup server_var(wcs_level)
      es_xmath wcs_levelup + 1
      es es_xmath wcs_levelup * server_var(wcs_levelxp)
      es_xsetinfo wcs_levelupcount 0
      es es_xif (server_var(wcs_levelxp) > 0) then while "server_var(wcs_xp) >= server_var(wcs_levelup)" "es es_xmath wcs_xp - server_var(wcs_levelup);es_xmath wcs_levelupcount + 1;es es_xmath wcs_levelup + server_var(wcs_levelxp)"
      es es_xif (server_var(wcs_levelupcount) > 0) do
      {
        // levelup the player
        es es_xmath wcs_level + server_var(wcs_levelupcount)
        es es_xmath wcs_unused + server_var(wcs_levelupcount)
        es es_xkeygetvalue wcs_total_level "WCSuserdata" server_var(wcs_giveuserid) "total_level"
        es es_xmath wcs_total_level + server_var(wcs_levelupcount)
        es es_xkeysetvalue "WCSuserdata" server_var(wcs_giveuserid) "total_level" server_var(wcs_total_level)
        es es_xkeysetvalue "WCSuserdata" server_var(wcs_giveuserid) "level" server_var(wcs_level)
        es es_xkeysetvalue "WCSuserdata" server_var(wcs_giveuserid) "xp" server_var(wcs_xp)
        es es_xkeysetvalue "WCSuserdata" server_var(wcs_giveuserid) "unused" server_var(wcs_unused)
        // check for top10
        es_xsetinfo wcs_allow "1"
        es es_xif ("BOT" in event_var(es_steamid)) then es_xif (server_var(wcs_cfg_allowbotstop10) = "0") then es_xsetinfo wcs_allow "0"
        es es_xif (server_var(wcs_enabletop10) = "1") then es_xif (server_var(wcs_allow) = "1") then wcs_checktop10 server_var(wcs_giveuserid)
        // nice fx (only if alive)
        es es_xgetplayerprop wcs_dead server_var(wcs_giveuserid) "CCSPlayer.baseclass.pl.deadflag"
        es es_xif (server_var(wcs_graphicfx) = "1") do
        {
        	es es_xif (server_var(wcs_dead) = "0") do
        	{
            es es_xgetplayerlocation wcs_x wcs_y wcs_z server_var(wcs_giveuserid)
            es_xmath wcs_z + 50
            es est_Effect 10 #a 0 "sprites/steam1.vmt" server_var(wcs_x) server_var(wcs_y) server_var(wcs_z) 20 50 3 20 5 2 255 200 0 200 1
          }
        }
        wcs_getlanguage "wcs_lng" "wcs_lng_levelup"
        es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_level) server_var(wcs_xp) server_var(wcs_levelup)
        wcs_getlanguage "wcs_lng" "wcs_lng_turbomode"
        es es_xif (server_var(wcs_turbomode) = "1") then es_xformat wcs_text "%1 %2" server_var(wcs_text) server_var(wcs_lng)
        es es_xif (server_var(wcs_soundfx) = "1") then es_xif (server_var(wcs_dead) = "0") then est_PlayPlayer server_var(wcs_giveuserid) "ambient/machines/teleport1.wav" 0.7
        es es_xtell server_var(wcs_giveuserid) #multi server_var(wcs_text)
        // save the player
        es wcs_saveplayer server_var(wcs_giveuserid)
        // call levelup menu
        es es_xdelayed 2.0 wcs_spendskills server_var(wcs_giveuserid)
      }
      es_xelse do
      {
        // no levelup, so just save the XP
        es es_xkeysetvalue WCSuserdata server_var(wcs_giveuserid) "xp" server_var(wcs_xp)
        es es_xif (server_var(wcs_reason) != "0") do
        {
        	es es_xif (server_var(wcs_amount) >= 0) do
        	{
        	  wcs_getlanguage "wcs_lng" "wcs_lng_gained"
            es es_xformat wcs_text "#lightgreen%1 #green%2 XP #lightgreen: %3" server_var(wcs_lng) server_var(wcs_amount) server_var(wcs_reason)
            wcs_getlanguage "wcs_lng" "wcs_lng_turbomode"
            es es_xif (server_var(wcs_turbomode) = 1) then es_xformat wcs_text "%1 #green%2" server_var(wcs_text) server_var(wcs_lng)
            es es_xtell server_var(wcs_giveuserid) #multi server_var(wcs_text)
          }
          es_xelse do
          {
        	  wcs_getlanguage "wcs_lng" "wcs_lng_lostxp"
        	  es_xmath wcs_amount * -1
        	  es es_xformat wcs_text "#lightgreen%1 #green%2 XP#lightgreen: %3" server_var(wcs_lng) server_var(wcs_amount) 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 givexp
}

block wcs_givelevel
{
es es_xif (server_var(wcs_debug) = 1) then profile begin givelevel
  // usage:  wcs_givelevel <userid> <amount> <reason> <forced>
  // userid: give a VALID userid
  // amount: how many level to add
  // reason: text (optional)
  // forced: 0/1 (optional)
  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_exists) = 1) then es_xexists wcs_exists key "WCSuserdata" server_var(wcs_giveuserid)
  // check for bots
  es es_xif (server_var(wcs_cfg_allowbotsgetxp) = "0") do
  {
  	es es_xkeygetvalue wcs_tmp WCSuserdata server_var(wcs_giveuserid) id
  	es es_xif ("BOT" in server_var(wcs_tmp)) then es_xsetinfo wcs_exists 0
  }
  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_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_unused WCSuserdata server_var(wcs_giveuserid) "unused"
      es es_xkeygetvalue wcs_level WCSuserdata server_var(wcs_giveuserid) "level"
      es es_xkeygetvalue wcs_xp WCSuserdata server_var(wcs_giveuserid) "xp"
      es es_xkeygetvalue wcs_total_level "WCSuserdata" server_var(wcs_giveuserid) "total_level"
      // add the level
      es es_xif (server_var(wcs_amount) > 0) do
      {
      	es es_xmath wcs_level + server_var(wcs_amount)
        es es_xmath wcs_unused + server_var(wcs_amount)
        es es_xmath wcs_total_level + server_var(wcs_amount)
      }
      es es_xkeysetvalue WCSuserdata server_var(wcs_giveuserid) "level" server_var(wcs_level)
      es es_xkeysetvalue WCSuserdata server_var(wcs_giveuserid) "unused" server_var(wcs_unused)
      es es_xkeysetvalue "WCSuserdata" server_var(wcs_giveuserid) "total_level" server_var(wcs_total_level)
      // check for top10
      es_xsetinfo wcs_allow "1"
      es es_xif ("BOT" in event_var(es_steamid)) then es_xif (server_var(wcs_cfg_allowbotstop10) = "0") then es_xsetinfo wcs_allow "0"
      es es_xif (server_var(wcs_enabletop10) = "1") then es_xif (server_var(wcs_allow) = "1") then wcs_checktop10 server_var(wcs_giveuserid)
      // nice fx
      es es_xgetplayerprop wcs_dead server_var(wcs_giveuserid) "CCSPlayer.baseclass.pl.deadflag"
      es es_xif (server_var(wcs_graphicfx) = "1") do
      {
        es es_xif (server_var(wcs_dead) = "0") do
        {
        	es es_xgetplayerlocation wcs_x wcs_y wcs_z server_var(wcs_giveuserid)
          es_xmath wcs_z + 50
          es est_Effect 10 #a 0 "sprites/steam1.vmt" server_var(wcs_x) server_var(wcs_y) server_var(wcs_z) 20 50 3 20 5 2 255 200 0 200 1
        }
      }
      es es_xsetinfo wcs_levelup server_var(wcs_level)
      es_xmath wcs_levelup + 1
      es es_xmath wcs_levelup * server_var(wcs_levelxp)
      wcs_getlanguage "wcs_lng" "wcs_lng_levelup"
      es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_level) server_var(wcs_xp) server_var(wcs_levelup)
      es es_xif (server_var(wcs_soundfx) = "1") then es_xif (server_var(wcs_dead) = "0") then est_PlayPlayer server_var(wcs_giveuserid) "ambient/machines/teleport1.wav" 0.7
      es es_xtell server_var(wcs_giveuserid) #multi server_var(wcs_text)
      es wcs_saveplayer server_var(wcs_giveuserid)
      // call levelup menu
      es es_xdelayed 2.0 wcs_spendskills server_var(wcs_giveuserid)
    }
    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 givelevel
}

block wcs_deathhandle
{
es es_xif (server_var(wcs_debug) = 1) then profile begin deathhandle
  // parameter is <userid> <attacker> <weapon> <headshot>
  es_xsetinfo wcs_userid 0
  es_xgetargv wcs_userid 1
  es_xsetinfo wcs_attackerid 0
  es_xgetargv wcs_attackerid 2
  es_xsetinfo wcs_weapon 0
  es_xgetargv wcs_weapon 3
  es_xsetinfo wcs_headshot 0
  es_xgetargv wcs_headshot 4
  es es_xexists wcs_exists userid server_var(wcs_userid)
  es es_xexists wcs_tmp userid server_var(wcs_attackerid)
  es es_xmath wcs_exists + server_var(wcs_tmp)
  es es_xif (server_var(wcs_exists) = "2") do
  {
    es_xsetinfo wcs_bonusxp 0
    es es_xif (server_var(wcs_xpboost) > 0) do
    {
      es_xsetinfo wcs_level_v 0
      es_xsetinfo wcs_level_a 0
      es es_xexists wcs_exists key WCSuserdata server_var(wcs_userid)
      es es_xif (server_var(wcs_exists) = 1) do
      {
        es es_xkeygetvalue wcs_race WCSuserdata server_var(wcs_userid) "race"
        es es_xkeygetvalue wcs_level_v WCSuserdata server_var(wcs_userid) "level"
      }
      es es_xexists wcs_exists key WCSuserdata server_var(wcs_attackerid)
      es es_xif (server_var(wcs_exists) = 1) do
      {
        es es_xkeygetvalue wcs_race WCSuserdata server_var(wcs_attackerid) "race"
        es es_xkeygetvalue wcs_level_a WCSuserdata server_var(wcs_attackerid) "level"
      }
      es es_xif (server_var(wcs_level_a) < server_var(wcs_level_v)) do
      {
        es es_xmath wcs_level_v - server_var(wcs_level_a)
        es es_xsetinfo wcs_bonusxp server_var(wcs_level_v)
        es es_xmath wcs_bonusxp * server_var(wcs_xpboost)
      }
    }
    es es_xif (server_var(wcs_bonusxp) > 0) do
    {
    	wcs_getlanguage "wcs_lng" "wcs_lng_highlevel"
      es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_level_v)
      es es_xmath wcs_bonusxp + server_var(wcs_killxp)
      es wcs_givexp server_var(wcs_attackerid) server_var(wcs_bonusxp) server_var(wcs_text) 0
    }
    es_xelse do
    {
    	wcs_getlanguage "wcs_lng" "wcs_lng_killedenemy"
      es wcs_givexp server_var(wcs_attackerid) server_var(wcs_killxp) server_var(wcs_lng) 0
    }
    // additional reward
    es es_xif (server_var(wcs_weapon) in server_var(wcs_wpn_knives)) do
    {
      es wcs_givexp server_var(wcs_attackerid) server_var(wcs_knifexp)
      wcs_getlanguage "wcs_lng" "wcs_lng_gained"
      wcs_getlanguage "wcs_lng2" "wcs_lng_knifekill"
      es es_xif (server_var(wcs_weapon) in "knife amerknife") then es_xformat wcs_text "#lightgreen%1 #green%2 #lightgreen%3" server_var(wcs_lng) server_var(wcs_knifexp) server_var(wcs_lng2)
      wcs_getlanguage "wcs_lng2" "wcs_lng_spadekill"
      es es_xif (server_var(wcs_weapon) = "spade") then es_xformat wcs_text "#lightgreen%1 #green%2 #lightgreen%3" server_var(wcs_lng) server_var(wcs_knifexp) server_var(wcs_lng2)
      wcs_getlanguage "wcs_lng2" "wcs_lng_punchkill"
      es es_xif (server_var(wcs_weapon) = "punch") then es_xformat wcs_text "#lightgreen%1 #green%2 #lightgreen%3" server_var(wcs_lng) server_var(wcs_knifexp) server_var(wcs_lng2)
      es es_xtell server_var(wcs_attackerid) #multi server_var(wcs_text)
    }
    es es_xif (server_var(wcs_headshot) = "1") do
    {
      es wcs_givexp server_var(wcs_attackerid) server_var(wcs_headshotxp)
      wcs_getlanguage "wcs_lng" "wcs_lng_gained"
      wcs_getlanguage "wcs_lng2" "wcs_lng_headshot"
      es es_xformat wcs_text "#lightgreen%1 #green%2 #lightgreen%3" server_var(wcs_lng) server_var(wcs_headshotxp) server_var(wcs_lng2)
      es es_xtell server_var(wcs_attackerid) #multi server_var(wcs_text)
    }
    // check for phoenix team event
    es es_xgetplayerteam wcs_team server_var(wcs_userid)
    es es_xif (server_var(wcs_team) >= 2) do
    {
      es es_xkeygetvalue wcs_phoenix WCStmp "phoenix" server_var(wcs_team)
      es es_xif (server_var(wcs_phoenix) > 0) do
      {
    		es_xmath wcs_phoenix  - 1
    		es es_xkeysetvalue WCStmp "phoenix" server_var(wcs_team) server_var(wcs_phoenix)
    		wcs_getlanguage "wcs_lng" "wcs_lng_phoenixspawn"
    		es es_xdelayed 2.5 es_xtell server_var(wcs_userid) #multi server_var(wcs_lng)
    		es es_xdelayed 3 est_Spawn server_var(wcs_userid)
es es_xif (server_var(wcs_debug) = "1") then echo RESPAWNED PHOENIX server_var(wcs_userid)
    	}
    }
  }
es es_xif (server_var(wcs_debug) = 1) then profile end deathhandle
}
