block load
{
  es_xdoblock corelib/playervar/playervar_register
}

block playervar_register
{
  // variables used by playervar
  es_xsetinfo _playervar_argc 0
  es_xsetinfo _playervar_arg1 0
  es_xsetinfo _playervar_player 0
  es_xsetinfo _playervar_id 0
  es_xsetinfo _playervar_userid 0
  es_xsetinfo _playervar_infoname 0
  es_xsetinfo _playervar_value 0
  es_xsetinfo _playervar_outputvar 0
  // check to see if playervar exists
  es_xexists _exists command playervar
  if (server_var(_exists) = 0) do
  {
    es_xregcmd playervar corelib/playervar/playervar
  }
}

block playervar
{
  es_xgetargc _playervar_argc
  es es_xif (server_var(_playervar_argc) >= 5) do
  {
    es_xgetargv _playervar_arg1 1
    es es_xif (server_var(_playervar_arg1) = set) do
    {
      es_xdoblock corelib/playervar/set
    }
    else do
    {
      es es_xif (server_var(_playervar_arg1) = get) do
      {
        es_xdoblock corelib/playervar/get
      }
      else do
      {
        es_xdbgmsg 0 playervar: Incorrect sub-command to playervar. Syntax: playervar <set/get> [var] <player> <var-name> [value]
      }
    }
  }
  else do
  {
    es_xdbgmsg 0 playervar: Not enough arguments. Syntax: playervar <set/get> [var] <player> <var-name> [value]
  }
}

block set
{
  es_xgetargv _playervar_player 2
  es es_xgetuserid _playervar_userid server_var(_playervar_player)
  es es_xexists _tempcore userid server_var(_playervar_userid)
  es es_xif (server_var(_tempcore) = 1) do
  {
    es_xgetargv _playervar_infoname 3
    es_xgetargv _playervar_value 4
    es uniqueid _playervar_id server_var(_playervar_userid) 1
    es es_xformat _playervar_id "PLAYERVAR_%1_%2" server_var(_playervar_id) server_var(_playervar_infoname)
    es_xstring _playervar_id replace STEAM_
    es_xstring _playervar_id replace ":" "-"
    es es_xsetinfo server_var(_playervar_id) server_var(_playervar_value)
  }
  else do
  {
    es_xdbgmsg 0 playervar: The specified player does not exist. Syntax: playervar <set/get> [var] <player> <var-name> [value]
  }
}

block get
{
  es_xgetargv _playervar_player 3
  es es_xgetuserid _playervar_userid server_var(_playervar_player)
  es es_xexists _tempcore userid server_var(_playervar_userid)
  es es_xif (server_var(_tempcore) = 1) do
  {
    es_xgetargv _playervar_outputvar 2
    es_xgetargv _playervar_infoname 4
    es uniqueid _playervar_id server_var(_playervar_userid) 1
    es es_xformat _playervar_id "PLAYERVAR_%1_%2" server_var(_playervar_id) server_var(_playervar_infoname)
    es_xstring _playervar_id replace STEAM_
    es_xstring _playervar_id replace ":" "-"
    es es_exists _tempcore variable server_var(_playervar_id)
    es es_xif (server_var(_tempcore) = 1) do
    {
      es es_xsetinfo server_var(_playervar_outputvar) server_var(server_var(_playervar_id))
    }
    else do
    {
      es_xdbgmsg 0 playervar: The specified info-name does not exist. Syntax: playervar <set/get> [var] <player> <var-name> [value]
    }
  }
  else do
  {
    es_xdbgmsg 0 playervar: The specified player does not exist. Syntax: playervar <set/get> [var] <player> <var-name> [value]
  }
}

block test_playervar
{
  es_xsetinfo _playercount 0
  es_xsetinfo _botquota 0
  es_xcopy _botquota bot_quota
  es_xgetplayercount _playercount
  es es_xif (server_var(_playercount) = 0) do
  {
    bot_quota 2
  }
  else do
  {
    es es_xif (server_var(_playercount) = 1) do
    {
      bot_add
    }
  }
  es_xdelayed 1 es_xdoblock corelib/playervar/playervar_test
  es es_xdelayed 1 es_xsoon es bot_quota server_var(_botquota)
}

block playervar_test
{
  es_xsetinfo _player1 0
  es_xsetinfo _player2 0
  es_xsetinfo _result1 0
  es_xsetinfo _result2 0
  getrandplayer _player1 #all
  es_xdoblock corelib/playervar/playervar_getuserid
  es echo Setting the var 'stupidmeter' to 10 for userid: server_var(_player1)
  es echo Setting the var 'stupidmeter' to 500 for userid: server_var(_player2)
  es playervar set server_var(_player1) stupidmeter 10
  es playervar set server_var(_player2) stupidmeter 500
  es echo Getting stupidmeter level for userid: server_var(_player1) ...
  es echo Getting stupidmeter level for userid: server_var(_player2) ...
  es playervar get _result1 server_var(_player1) stupidmeter
  es playervar get _result2 server_var(_player2) stupidmeter
  es es_xif (server_var(_result1) = 10) do
  {
    es es_xif (server_var(_result2) = 500) do
    {
      echo "Test is completed, results: command is working perfectly"
      echo "Test is completed, results: command is working perfectly"
      echo "Test is completed, results: command is working perfectly"
      echo "Test is completed, results: command is working perfectly"
      echo "Test is completed, results: command is working perfectly"
    }
    else do
    {
      echo "Test is completed, results: command has been busted"
      echo "Test is completed, results: command has been busted"
      echo "Test is completed, results: command has been busted"
      echo "Test is completed, results: command has been busted"
      echo "Test is completed, results: command has been busted"
    }
  }
  else do
  {
    echo "Test is completed, results: command has been busted"
    echo "Test is completed, results: command has been busted"
    echo "Test is completed, results: command has been busted"
    echo "Test is completed, results: command has been busted"
    echo "Test is completed, results: command has been busted"
  }
}

block playervar_getuserid
{
  getrandplayer _player2 #all
  es es_xif (server_var(_player2) = server_var(_player1)) do
  {
    es_xdoblock corelib/playervar/playervar_getuserid
  }
}
