// uniqueid command v0.2
// Submitted by [NATO]Hunter
// Syntax: uniqueid <var> <player> [add botname]
block load
{
  es_xdoblock corelib/uniqueid/uniqueid_register
}

block uniqueid_register
{
  // variables used by uniqueid
  es_xsetinfo _uniqueid_var 0
  es_xsetinfo _uniqueid_forcebot 0
  es_xsetinfo _uniqueid_userid 0
  es_xsetinfo _uniqueid_steamid 0
  es_xsetinfo _uniqueid_ip 0
  es_xsetinfo _uniqueid_string 0
  es_xsetinfo _uniqueid_isbot 0
  es_xsetinfo _uniqueid_name 0
  // check to see if uniqueid exists
  es_xexists _exists command uniqueid
  if (server_var(_exists) = 0) do
  {
    es_xregcmd uniqueid corelib/uniqueid/uniqueid "Generates a unique id based on the ip for LAN servers or returns the SteamID for Internet servers"
  }
}

block uniqueid
{
  es_xgetargv _uniqueid_var 1
  es es_xif (server_var(_uniqueid_var) != 0) do
  {
    es_xgetargv _uniqueid_userid 2
    es es_xif (server_var(_uniqueid_userid) != 0) do
    {
      es es_xgetuserid _uniqueid_userid server_var(_uniqueid_userid)
      es es_xif (server_var(_uniqueid_userid) > 0) do
      {
        es es_xisbot _uniqueid_isbot server_var(_uniqueid_userid)
        es es_xif (server_var(_uniqueid_isbot) != 1) do
        {
          es es_xif (server_var(sv_lan) != 1) do
          {
            es es_xgetplayersteamid _uniqueid_steamid server_var(_uniqueid_userid)
            es es_xsetinfo server_var(_uniqueid_var) server_var(_uniqueid_steamid)
          }
          else do
          {
            es getplayerinfo _uniqueid_ip server_var(_uniqueid_userid) "address"
            es es_xtoken _uniqueid_ip server_var(_uniqueid_ip) 1 ":"
            es es_xstrlen _uniqueid_strlen server_var(_uniqueid_ip)
            es es_xstring _uniqueid_ip replace "."
            es es_xformat _uniqueid_string "LAN_%1:%2" server_var(_uniqueid_strlen) server_var(_uniqueid_ip)
            es es_xsetinfo server_var(_uniqueid_var) server_var(_uniqueid_string)
          }
        }
        else do
        {
          es_xgetargv _uniqueid_forcebot 3
          es es_xif (server_var(_uniqueid_forcebot) = 1) do
          {
            es getbotname _uniqueid_name server_var(_uniqueid_userid)
            es forcecase _uniqueid_name "upper"
            es es_xformat _uniqueid_string "BOT_%1" server_var(_uniqueid_name)
            es es_xsetinfo server_var(_uniqueid_var) server_var(_uniqueid_string)
          }
          else do
          {
            es es_xsetinfo server_var(_uniqueid_var) "BOT"
          }
        }
      }
      else do
      {
        es_xdbgmsg 0 uniqueid: Invalid player for uniqueid. Syntax: uniqueid <var> <player> [add botname]
      }
    }
    else do
    {
      es_xdbgmsg 0 uniqueid: Invalid arguments for uniqueid. Syntax: uniqueid <var> <player> [add botname]
    }
  }
  else do
  {
    es_xdbgmsg 0 uniqueid: Invalid arguments for uniqueid. Syntax: uniqueid <var> <player> [add botname]
  }
}