////////////////////////////////////////
// WCS - Warcraft:Source              //
// ---------------------------------- //
//  by kRypT0n!Te                     //
//      http://wcs.despo-clan.de      //
// ---------------------------------- //
// Content of this subscript          //
// - top10 handling                   //
////////////////////////////////////////
// say commands:
// wcstop, showtop10 (both the same)
//
// admin commands:
// wcs_showtop10 <userid>
// wcs_checktop10 <userid>
// wcs_cleartop10
//
// wcs_top10switch (internal use)
////////////////////////////////////////

block load
{
	// register the commands
	es_xexists wcs_exists command "wcstop"
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd wcstop "wcs/WCStop10/wcs_showtop10" "Shows top10 players"
  es_xexists wcs_exists command "showtop10"
  es es_xif (server_var(wcs_exists) = 0) then es_xregsaycmd showtop10 "wcs/WCStop10/wcs_showtop10" "Shows top10 players"
  es_xexists wcs_exists command "wcs_showtop10"
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_showtop10 "wcs/WCStop10/wcs_showtop10" "Shows top10 players"
  es_xexists wcs_exists command "wcs_top10switch"
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_top10switch "wcs/WCStop10/wcs_top10switch" "Switches two places in top10"
  es_xexists wcs_exists command "wcs_checktop10"
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_checktop10 "wcs/WCStop10/wcs_checktop10" "Checks if a player goes in top10"
  es_xexists wcs_exists command "wcs_cleartop10"
  es es_xif (server_var(wcs_exists) = 0) then es_xregcmd wcs_cleartop10 "wcs/WCStop10/wcs_cleartop10" "Clears the top10 (not the player levels)"
  es_xsetinfo wcs_a 0
  es_xsetinfo wcs_b 0
  // init the keygroup, create place 1-10
  es_xkeygroupcreate "WCStop10"
  es_xkeygroupload "WCStop10" "|wcs/WCStop10"
  es_xsetinfo wcs_counter "1"
  while "server_var(wcs_counter) <= 10" "es es_xkeycreate WCStop10 server_var(wcs_counter);es_xmath wcs_counter + 1"
}

block unload
{
	es es_xif (server_var(wcs_turbomode) != "1") then es_xkeygroupsave WCStop10 "|wcs/WCStop10"
  es_xkeygroupdelete "WCStop10"
}

event server
{
	es es_xif (server_var(wcs_turbomode) != "1") then es_xkeygroupsave WCStop10 "|wcs/WCStop10"
}

// >>>>>>>>>>>>>>>>> TOP10 RANKING >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
block wcs_checktop10
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_checktop10 start
  // "WCStop10"
  // {
  // "1"
  //   {
  //     "name"
  //     "steamid"
  //     "level"
  //     "curlevel"
  //     "currace"
  //   }
  //  ....
  // }
  es_xsetinfo wcs_sid 0
  es_xsetinfo wcs_uid 0
  es_xgetargv wcs_uid 1
  es es_xkeygetvalue wcs_sid "WCSuserdata" server_var(wcs_uid) "id"
  es_xexists wcs_exists keygroup "WCStop10"
  es es_xif (server_var(wcs_sid) = "0") then es_xsetinfo wcs_exists "0"
  es es_xif (server_var(wcs_uid) = "0") then es_xsetinfo wcs_exists "0"
  es es_xif (server_var(wcs_exists) = 1) do
  {
    // get minimum level
    es_xsetinfo wcs_lvl_min "0"
    // last place in top10 = min level
    es_xkeygetvalue wcs_lvl_min "WCStop10" "10" "level"
    // get the user level
    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_total_level "WCSuserdata" server_var(wcs_uid) "total_level"
    // check if player can reach top10
    es es_xif (server_var(wcs_total_level) >= server_var(wcs_lvl_min)) do
    {
      // check if player is in top10
      es_xsetinfo wcs_exists 0
      es_xsetinfo wcs_pos_old 0
      es_xforeachkey wcs_key in "WCStop10" "es es_xkeygetvalue wcs_tmp WCStop10 server_var(wcs_key) steamid;es es_xif (server_var(wcs_tmp) = server_var(wcs_sid)) then es_xsetinfo wcs_exists 1;es es_xif (server_var(wcs_tmp) = server_var(wcs_sid)) then es_xsetinfo wcs_pos_old server_var(wcs_key)"
      es es_xif (server_var(wcs_exists) = 1) do
      {
        // in top10 ! so find the place..
        es_xsetinfo wcs_pos_entry 11
        es_xsetinfo wcs_tmplevel 0
        es_xsetinfo wcs_counter 10
        while "server_var(wcs_counter) > 0" "es es_xkeygetvalue wcs_tmplevel WCStop10 server_var(wcs_counter) level;es es_xif (server_var(wcs_tmplevel) < server_var(wcs_total_level)) then es_xsetinfo wcs_pos_entry server_var(wcs_counter);es_xmath wcs_counter - 1"
        es es_xif (server_var(wcs_pos_entry) <= 10) do
        {
          es es_xif (server_var(wcs_pos_old) > server_var(wcs_pos_entry)) do
          {
          	es es_xif (server_var(wcs_cfg_top10announce) = "0") do
            {
              wcs_getlanguage "wcs_lng" "wcs_lng_top10_sw"
              es es_xformat wcs_text "#lightgreen%1 : %2 -> #green%3" server_var(wcs_lng) server_var(wcs_pos_old) server_var(wcs_pos_entry)
              es es_xtell server_var(wcs_uid) #multi server_var(wcs_text)
            }
            es_xelse do
            {
              wcs_getlanguage "wcs_lng" "wcs_lng_top10_sw_all"
              es es_xgetplayername wcs_name server_var(wcs_uid)
              es es_xformat wcs_text "#green%1 #lightgreen%2 : %3 -> #green%4" server_var(wcs_name) server_var(wcs_lng) server_var(wcs_pos_old) server_var(wcs_pos_entry)
              es es_xmsg #multi server_var(wcs_text)
            }
            // higher position
            while "server_var(wcs_pos_old) > server_var(wcs_pos_entry)" "es wcs_top10switch server_var(wcs_pos_old);es_xmath wcs_pos_old - 1"
            es es_xgetplayername wcs_name server_var(wcs_uid)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) name server_var(wcs_name)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) steamid server_var(wcs_sid)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) level server_var(wcs_total_level)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) curlevel server_var(wcs_level)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) currace server_var(wcs_race)
          }
          es_xelse do
          {
            // refresh the entry
            es es_xgetplayername wcs_name server_var(wcs_uid)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_old) name server_var(wcs_name)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_old) steamid server_var(wcs_sid)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_old) level server_var(wcs_total_level)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_old) curlevel server_var(wcs_level)
            es es_xkeysetvalue WCStop10 server_var(wcs_pos_old) currace server_var(wcs_race)
          }
        }
      }
      es_xelse do
      {
        // not in top10, find position to insert
        es_xsetinfo wcs_pos_entry 11
        es_xsetinfo wcs_tmplevel 0
        es_xsetinfo wcs_counter 10
        while "server_var(wcs_counter) > 0" "es es_xkeygetvalue wcs_tmplevel WCStop10 server_var(wcs_counter) level;es es_xif (server_var(wcs_tmplevel) < server_var(wcs_total_level)) then es_xsetinfo wcs_pos_entry server_var(wcs_counter);es_xmath wcs_counter - 1"
        es es_xif (server_var(wcs_pos_entry) <= 10) do
        {
          // shift all others
          es_xsetinfo wcs_counter 10
          while "server_var(wcs_counter) >= server_var(wcs_pos_entry)" "es es_xsetinfo wcs_counter2 server_var(wcs_counter);es_xmath wcs_counter2 + 1;es es_xkeyrename WCStop10 server_var(wcs_counter) server_var(wcs_counter2);es_xmath wcs_counter - 1"
          // insert new
          es es_xkeycreate WCStop10 server_var(wcs_pos_entry)
          es_xkeydelete WCStop10 11
          es es_xgetplayername wcs_name server_var(wcs_uid)
          es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) name server_var(wcs_name)
          es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) steamid server_var(wcs_sid)
          es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) level server_var(wcs_total_level)
          es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) curlevel server_var(wcs_level)
          es es_xkeysetvalue WCStop10 server_var(wcs_pos_entry) currace server_var(wcs_race)
          wcs_getlanguage "wcs_lng" "wcs_lng_top10_entry"
          es es_xformat wcs_text "#lightgreen%1 : #green%2" server_var(wcs_lng) server_var(wcs_pos_entry)
          es es_xtell server_var(wcs_uid) #multi server_var(wcs_text)
        }
      }
    }
    es_xelse do
    {
      es es_xmath wcs_total_level - server_var(wcs_lvl_min)
      es es_xif (server_var(wcs_total_level) > 0) do
      {
      	wcs_getlanguage "wcs_lng" "wcs_lng_notop10"
        es es_xformat wcs_text server_var(wcs_lng) server_var(wcs_total_level)
        es es_xtell server_var(wcs_uid) #multi server_var(wcs_text)
      }
    }
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_checktop10 end
}

block wcs_top10switch
{
  // get pos a and switch with the one above
  es_xgetargv wcs_a 1
  es_xcopy wcs_b wcs_a
  es_xmath wcs_b - 1
  es es_xkeyrename "WCStop10" server_var(wcs_a) "tmp"
  es es_xkeyrename "WCStop10" server_var(wcs_b) server_var(wcs_a)
  es es_xkeyrename "WCStop10" "tmp" server_var(wcs_b)
  // cleanup
  es_xexists wcs_exists key "WCStop10" "tmp"
  es es_xif (server_var(wcs_exists) = "1") then es_xkeydelete "WCStop10" "tmp"
  es_xexists wcs_exists key "WCStop10" "0"
  es es_xif (server_var(wcs_exists) = "1") then es_xkeydelete "WCStop10" "0"
  es_xexists wcs_exists key "WCStop10" "11"
  es es_xif (server_var(wcs_exists) = "1") then es_xkeydelete "WCStop10" "11"
}

block wcs_showtop10
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showtop10 start
  // first, is it a custom command ?
  es_xsetinfo wcs_userid 0
  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 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) = "0") then es_xkeycreate "WCSuserdata" server_var(wcs_userid)
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_userid) "menu" "top10"
    es es_xkeysetvalue "WCSuserdata" server_var(wcs_userid) "page" "1"
    es_xdoblock wcs/WCStop10/wcs_showtop10_popup
  }
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showtop10 end
}

block wcs_showtop10_popup
{
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showtop10_popup start
  // get the page and create the content
  es es_xkeygetvalue wcs_page WCSuserdata server_var(wcs_userid) page
  wcs_getlanguage "wcs_lng" "wcs_lng_top10"
  es es_xformat wcs_text "%1\n " server_var(wcs_lng)
  es es_xif (server_var(wcs_page) = 1) do
  {
    es_xsetinfo wcs_counter "1"
    while "server_var(wcs_counter) <= 5" "es_xdoblock wcs/WCStop10/wcs_showtop10_init;es_xmath wcs_counter + 1"
    es es_xformat wcs_text "%1\n \n->9. %2\n0. %3" server_var(wcs_text) server_var(wcs_lng_next) server_var(wcs_lng_exit)
  }
  es es_xif (server_var(wcs_page) = 2) do
  {
    es_xsetinfo wcs_counter "6"
    while "server_var(wcs_counter) <= 10" "es_xdoblock wcs/WCStop10/wcs_showtop10_init;es_xmath wcs_counter + 1"
    es es_xformat wcs_text "%1\n \n->8. %2\n0. %3" server_var(wcs_text) server_var(wcs_lng_back) server_var(wcs_lng_exit)
  }
  es es_xstrlen wcs_strlen server_var(wcs_text)
  es es_xif (server_var(wcs_strlen) > 1) then es_xmenu 0 server_var(wcs_userid) server_var(wcs_text)
es es_xif (server_var(wcs_debug) = "1") then echo wcs_showtop10_popup end
}

block wcs_showtop10_handle
{
  es es_xsetinfo wcs_userid event_var(userid)
  es es_xkeygetvalue wcs_page WCSuserdata server_var(wcs_userid) page
  es es_xif (event_var(commandstring) = "8") do
  {
  	// 8=back, only on page 2
    es es_xif (server_var(wcs_page) = "2") do
    {
    	es es_xkeysetvalue "WCSuserdata" event_var(userid) "page" "1"
    	es_xdoblock wcs/WCStop10/wcs_showtop10_popup
    }
  }
  es es_xif (event_var(commandstring) = "9") do
  {
  	// 9=next, only on page 1
    es es_xif (server_var(wcs_page) = "1") do
    {
    	es es_xkeysetvalue "WCSuserdata" event_var(userid) "page" "2"
    	es_xdoblock wcs/WCStop10/wcs_showtop10_popup
    }
  }  
  es es_xif (event_var(commandstring) != "8") then es_xif (event_var(commandstring) != "9") then es_xkeysetvalue "WCSuserdata" event_var(userid) "menu" "0"
}

block wcs_showtop10_init
{
  es es_xexists wcs_exists key WCStop10 server_var(wcs_counter)
  es es_xif (server_var(wcs_exists) = 1) do
  {
    es es_xkeygetvalue wcs_name WCStop10 server_var(wcs_counter) name
    es es_xif (server_var(wcs_name) != 0) do
    {
    	// get all the values and create the text
      es es_xkeygetvalue wcs_level WCStop10 server_var(wcs_counter) level
      es es_xkeygetvalue wcs_curlevel WCStop10 server_var(wcs_counter) curlevel
      es es_xkeygetvalue wcs_race WCStop10 server_var(wcs_counter) currace
      es es_xif (server_var(wcs_race) != "0") then es_xkeygetvalue wcs_racename WCSraces server_var(wcs_race) name
      es es_xif (server_var(wcs_race) = "0") then es_xsetinfo wcs_racename "-"
      wcs_getlanguage "wcs_lng" "wcs_lng_total"
      wcs_getlanguage "wcs_lng2" "wcs_lng_level"
      es es_xformat wcs_text "%1\n->%2. %3\n %4 %5" server_var(wcs_text) server_var(wcs_counter) server_var(wcs_name) server_var(wcs_lng) server_var(wcs_lng2)
      wcs_getlanguage "wcs_lng" "wcs_lng_playing"
      es es_xformat wcs_text "%1 %2, %3 %4 %5 %6" server_var(wcs_text) server_var(wcs_level) server_var(wcs_lng) server_var(wcs_racename) server_var(wcs_lng2) server_var(wcs_curlevel)
    }
    es_xelse do
    {
      es es_xformat wcs_text "%1\n->%2. -" server_var(wcs_text) server_var(wcs_counter)
    }
  }
  es_xelse do
  {
    es es_xformat wcs_text "%1\n->%2. -" server_var(wcs_text) server_var(wcs_counter)
  }
}

block wcs_cleartop10
{
	// clear the top10, and create empty place 1-10
	es_xkeygroupdelete "WCStop10"
	es_xkeygroupcreate "WCStop10"
  es_xsetinfo wcs_counter "1"
  while "server_var(wcs_counter) <= 10" "es es_xkeycreate WCStop10 server_var(wcs_counter);es_xmath wcs_counter + 1"
  es_xkeygroupsave "WCStop10" "|wcs/WCStop10"
  es_xmsg #lightgreen "WCS : Top10 cleared"
}
