// foreach command v0.5
// Submitted by [NATO]Hunter
//
// foreach loops are supported through corelib
// syntax:
// foreach player <var> <identifier> "<command>"
// foreach entity <keyvar> <classvar> <part-of-entity-name> "<command>"
// foreach weapon <var> <identifier> "<command>"
// foreach token <var> "<string>" "<separator>" "<command>"
// foreach part <var> "<string>" "<character-count>" "<command>"
// Originally submitted by [NATO]Hunter

block load
{
  es_xdoblock corelib/foreach/foreach_register
}

block foreach_register
{
  // variables used by foreach
  es_xsetinfo _foreachblock 0
  es_xsetinfo _foreachplayer 0
  es_xsetinfo _foreachkeycount 0
  es_xsetinfo _foreachkeyname 0
  es_xsetinfo _foreachcommand 0
  es_xsetinfo _foreachvar 0
  es_xsetinfo _foreachclassvar 0
  es_xsetinfo _foreachtarget 0
  es_xsetinfo _foreachseparator 0
  es_xsetinfo _foreachindex 0
  es_xsetinfo _foreachendex 0
  es_xsetinfo _foreachstring 0
  es_xsetinfo _foreachchars 0
  es_xsetinfo _foreachcmd 0
  es_xsetinfo _foreachweaponins 0
  es_xsetinfo _foreachweapontag 0
  es_xsetinfo _foreachweaponkey 0
  es_xsetinfo _foreachweaponkeyname 0
  es_xsetinfo _foreachweapontoken 0
  es_xsetinfo _foreachgame 0
  es_xsetinfo _foreachdoit 0
  es_xsetinfo _foreachexists 0
  es_xgetgame _foreachgame
  stack create _foreach_player_stack
  stack create _foreach_entity_stack
  stack create _foreach_weapon_stack
  stack create _foreach_token_stack
  stack create _foreach_part_stack
  es_xdoblock corelib/foreach/foreach_keygroupload
  es_xexists _exists command foreach
  if (server_var(_exists) = 0) do
  {
    es_xregcmd foreach corelib/foreach/foreach "Allows you to loop a command over different things. Currently supports 'foreach player', 'foreach token' and 'foreach part'"
  }
}

block unload
{
  es es_xkeygroupdelete server_var(_foreachweaponkeyname)
  stack delete _foreach_player_stack
  stack delete _foreach_entity_stack
  stack delete _foreach_weapon_stack
  stack delete _foreach_token_stack
  stack delete _foreach_part_stack
}

block foreach_keygroupload
{
  es es_xif ("Counter-Strike" in server_var(_foreachgame)) do
  {
    es_xsetinfo _foreachweaponkeyname "_foreach_weaponlist_cstrike"
  }
  es es_xif ("Day of Defeat" in server_var(_foreachgame)) do
  {
    es_xsetinfo _foreachweaponkeyname "_foreach_weaponlist_dods"
  }
  es es_xif ("Deathmatch" in server_var(_foreachgame)) do
  {
    es_xsetinfo _foreachweaponkeyname "_foreach_weaponlist_hl2dm"
  }
  es es_xkeygroupload server_var(_foreachweaponkeyname) |corelib/foreach
}

block foreach
{
  es_xgetargv _foreachcmd 1
  es es_xformat _foreachblock "corelib/foreach/foreach_cmd_%1" server_var(_foreachcmd)
  es es_xexists _foreachexists block server_var(_foreachblock)
  es es_xif (server_var(_foreachexists) == 1) do
  {
    es es_xdoblock server_var(_foreachblock)
  }
  else do
  {
    es_xdbgmsg 0 foreach: Invalid subcommand for foreach
  }
}

// -----------------------------------------------------------------------------
// Sub commmand part:
// ------------------
//

block foreach_cmd_player
{
  // sub command "player"
  es stack push _foreach_player_stack server_var(_foreachcommand)
  es stack push _foreach_player_stack server_var(_foreachvar)
  es stack push _foreach_player_stack server_var(_foreachtarget)
  es stack push _foreach_player_stack server_var(_foreachkeyname)
  es_xgetargv _foreachvar 2
  es es_xif (server_var(_foreachvar) != 0) do
  {
    es_xgetargv _foreachtarget 3
    // check for group/sort/team identifier
    es es_xif ("#" in server_var(_foreachtarget)) do
    {
      es_xgetargv _foreachcommand 4
      es es_xif (server_var(_foreachcommand) != 0) do
      {
        // we need to preserve the stack so foreach can be called from within itself.
        es_xmath _foreachkeycount + 1
        es es_xformat _foreachkeyname "_foreach_playerlist%1" server_var(_foreachkeycount)
        // create the keygroup with the player information
        es es_xcreateplayerlist server_var(_foreachkeyname)
        // check for things like #human#bot and remove them
        es es_xif ("#human" in server_var(_foreachtarget)) do
        {
          es es_xif ("#bot" in server_var(_foreachtarget)) do
          {
            es_xstring _foreachtarget replace "#human"
            es_xstring _foreachtarget replace "#bot"
          }
        }
        es es_xif ("#alive" in server_var(_foreachtarget)) do
        {
          es es_xif ("#dead" in server_var(_foreachtarget)) do
          {
            es_xstring _foreachtarget replace "#alive"
            es_xstring _foreachtarget replace "#dead"
          }
        }
        // filter the keygroup by identifiers
        es es_xif (server_var(_foreachtarget) != 0) do
        {
          es es_xif ("#all" in server_var(_foreachtarget)) do
          {
            es_xstring _foreachtarget replace "#all"
            es es_xif (server_var(_foreachtarget) != 0) do
            {
              es_xdoblock corelib/foreach/foreach_player_filter
            }
          }
          else do
          {
            es_xdoblock corelib/foreach/foreach_player_filter     
          }
        }
        es es_xforeachkey _foreachplayer in server_var(_foreachkeyname) "es_xdoblock corelib/foreach/foreach_player_cmd"
        es es_xkeygroupdelete server_var(_foreachkeyname)
      }
        else do
        {
          es_xdbgmsg 0 foreach player: Invalid arguments for foreach player. Syntax: foreach player <var> <identifier> "<command>"
        }
      }
      else do
      {
        // check for single player
        // expand a single player
        es es_xgetuserid _foreachplayer server_var(_foreachtarget)
        // check for a valid user
        es es_xif (server_var(_foreachplayer) > 0) do
        {
          es_xgetargv _foreachcommand 4
    	    // command block
          es_xdoblock corelib/foreach/foreach_player_cmd
        }
        else do
        {
          es es_xdbgmsg 0 foreach player: The player or identifier server_var(_foreachplayer) does not exists
          es_xdbgmsg 0 foreach player: Invalid player or identifier for foreach player. Syntax: foreach player <var> <identifier> "<command>"
          es_xdbgmsg 0 foreach player: Identifiers: userid, steamid, playername, #all, #spec, #un, #t, #ct, #human, #bot, #alive, #dead
        }
      }
    }
    else do
    {
      es_xdbgmsg 0 foreach player: Invalid arguments for foreach player. Syntax: foreach player <var> <identifier> "<command>"
    }
    stack pop _foreachkeyname _foreach_player_stack
    stack pop _foreachtarget _foreach_player_stack
    stack pop _foreachvar _foreach_player_stack
    stack pop _foreachcommand _foreach_player_stack
    // end of sub command "player"
}

block foreach_cmd_entity
{
  // sub command "entity"
  es stack push _foreach_entity_stack server_var(_foreachcommand)
  es stack push _foreach_entity_stack server_var(_foreachvar)
  es stack push _foreach_entity_stack server_var(_foreachclassvar)
  es stack push _foreach_entity_stack server_var(_foreachtarget)
  es stack push _foreach_entity_stack server_var(_foreachkeyname)
  es_xgetargv _foreachvar 2
  es es_xif (server_var(_foreachvar) != 0) do
  {
    es_xgetargv _foreachclassvar 3
    es es_xif (server_var(_foreachclassvar) != 0) do
    {
      es_xgetargv _foreachtarget 4
      es_xgetargv _foreachcommand 5
      es es_xif (server_var(_foreachcommand) != 0) do
      {
        es es_xsetinfo server_var(_foreachclassvar) 0
        // we need to preserve the stack so foreach can be called from within itself
        es_xmath _foreachkeycount + 1
        es es_xformat _foreachkeyname "_foreach_entitylist%1" server_var(_foreachkeycount)
        // create the keygroup with the entity information
        es es_xcreateentitylist server_var(_foreachkeyname)
        es es_xif (server_var(_foreachtarget) != 0) do
        {
          es es_xkeygroupfilter server_var(_foreachkeyname) only classname server_var(_foreachtarget)
        }
        es es_xforeachkey _foreachentity in server_var(_foreachkeyname) "es_xdoblock corelib/foreach/foreach_entity_cmd"
        es es_xkeygroupdelete server_var(_foreachkeyname)
      }
      else do
      {
        es_xdbgmsg 0 foreach entity: Invalid arguments for foreach entity. Syntax: foreach entity <keyvar> <classvar> <part-of-entity-name> "<command>"
      }
    }
    else do
    {
      es_xdbgmsg 0 foreach entity: Invalid arguments for foreach entity. Syntax: foreach entity <keyvar> <classvar> <part-of-entity-name> "<command>"
    }
  }
  else do
  {
    es_xdbgmsg 0 foreach entity: Invalid arguments for foreach entity. Syntax: foreach entity <keyvar> <classvar> <part-of-entity-name> "<command>"
  }
  stack pop _foreachkeyname _foreach_entity_stack
  stack pop _foreachtarget _foreach_entity_stack
  stack pop _foreachclassvar _foreach_entity_stack
  stack pop _foreachvar _foreach_entity_stack
  stack pop _foreachcommand _foreach_entity_stack
  // end of sub command "entity"
}

block foreach_cmd_weapon
{
  // sub command "weapon"
  es stack push _foreach_weapon_stack server_var(_foreachtarget)
  es stack push _foreach_weapon_stack server_var(_foreachcommand)
  es stack push _foreach_weapon_stack server_var(_foreachvar)
  es stack push _foreach_weapon_stack server_var(_foreachkeyname)
  // check arguments
  es_xgetargv _foreachvar 2
  es es_xif (server_var(_foreachvar) != "0") do
  {
    es_xgetargv _foreachtarget 3
    es es_xif (server_var(_foreachtarget) != "0") do
    {
      es_xgetargv _foreachcommand 4
      es es_xif (server_var(_foreachcommand) != "0") do
      {
        // we need to preserve the stack so foreach can be called from within itself
        es_xmath _foreachkeycount + 1
        es es_xformat _foreachkeyname "_foreach_weaponlist%1" server_var(_foreachkeycount)
        // create the keygroup for the weapon information
        es es_xkeygroupcreate server_var(_foreachkeyname)
        es foreach token _foreachweapontoken server_var(_foreachtarget) "#" "es_xdoblock corelib/foreach/foreach_weapon_tags"
        es es_xforeachkey _foreachweaponkey in server_var(_foreachkeyname) "es_xdoblock corelib/foreach/foreach_weapon_cmd"
        es es_xkeygroupdelete server_var(_foreachkeyname)
      }
      else do
      {
        es_xdbgmsg 0 foreach weapon: Invalid arguments for foreach weapon. Syntax: foreach weapon <var> <identifier> "<command>"
      }
    }
    else do
    {
      es_xdbgmsg 0 foreach weapon: Invalid arguments for foreach weapon. Syntax: foreach weapon <var> <identifier> "<command>"
    }
  }
  else do
  {
    es_xdbgmsg 0 foreach weapon: Invalid arguments for foreach weapon. Syntax: foreach weapon <var> <identifier> "<command>"
  }
  stack pop _foreachkeyname _foreach_weapon_stack
  stack pop _foreachvar _foreach_weapon_stack
  stack pop _foreachcommand _foreach_weapon_stack
  stack pop _foreachtarget _foreach_weapon_stack
  // end of sub command "weapon"
}

block foreach_cmd_token
{
  // sub command "token"
  es stack push _foreach_token_stack server_var(_foreachtarget)    
  es stack push _foreach_token_stack server_var(_foreachcommand)
  es stack push _foreach_token_stack server_var(_foreachseparator)
  es stack push _foreach_token_stack server_var(_foreachstring)
  es stack push _foreach_token_stack server_var(_foreachvar)
  es stack push _foreach_token_stack server_var(_foreachindex)
  es stack push _foreach_token_stack server_var(_foreachendex)
  // check arguments
  es_xgetargv _foreachvar 2
  es es_xif (server_var(_foreachvar) != "0") do
  {
    es_xgetargv _foreachstring 3
    es es_xif (server_var(_foreachstring) != "0") do
    {
      es_xgetargv _foreachseparator 4
      es es_xif (server_var(_foreachseparator) != "0") do
      {
        es_xgetargv _foreachcommand 5
        es es_xif (server_var(_foreachcommand) != "0") do
        {
          // loop through the string
          es_xdoblock corelib/foreach/foreach_token_loop
        }
        else do
        {
          es_xdbgmsg 0 foreach: Invalid arguments for foreach token. Syntax: foreach token <var> "<string>" "<separator>" "<command>"
        }
      }
      else do
      {
        es_xdbgmsg 0 foreach: Invalid arguments for foreach token. Syntax: foreach token <var> "<string>" "<separator>" "<command>"
      }
    }
    else do
    {
      es_xdbgmsg 0 foreach: Invalid arguments for foreach token. Syntax: foreach token <var> "<string>" "<separator>" "<command>"
    }
  }
  else do
  {
    es_xdbgmsg 0 foreach: Invalid arguments for foreach token. Syntax: foreach token <var> "<string>" "<separator>" "<command>"
  }
  stack pop _foreachendex _foreach_token_stack
  stack pop _foreachindex _foreach_token_stack
  stack pop _foreachvar _foreach_token_stack
  stack pop _foreachstring _foreach_token_stack
  stack pop _foreachseparator _foreach_token_stack
  stack pop _foreachcommand _foreach_token_stack
  stack pop _foreachtarget _foreach_token_stack
  // end of sub command "token"
}

block foreach_cmd_part
{
  // sub command "part"
  es stack push _foreach_part_stack server_var(_foreachindex)
  es stack push _foreach_part_stack server_var(_foreachendex)
  es stack push _foreach_part_stack server_var(_foreachseparator)
  es stack push _foreach_part_stack server_var(_foreachtarget)
  es stack push _foreach_part_stack server_var(_foreachcommand)
  es stack push _foreach_part_stack server_var(_foreachchars)
  es stack push _foreach_part_stack server_var(_foreachstring)
  es stack push _foreach_part_stack server_var(_foreachvar)
  // check arguments
  es_xgetargv _foreachvar 2
  es es_xif (server_var(_foreachvar) != "0") do
  {
    es_xgetargv _foreachstring 3
    es es_xif (server_var(_foreachstring) != "0") do
    {
      es_xgetargv _foreachchars 4
      es es_xif (server_var(_foreachchars) > "0") do
      {
        es_xgetargv _foreachcommand 5
        es es_xif (server_var(_foreachcommand) != "0") do
        {
          // loop through the string
          es_xsetinfo _foreachindex 0
          es_xcopy _foreachendex _foreachchars
          es_xdoblock corelib/foreach/foreach_part_loop
        }
        else do
        {
          es_xdbgmsg 0 foreach: Invalid arguments for foreach part. Syntax: foreach part <var> "<string>" "<character count>" "<command>"
        }
      }
      else do
      {
        es_xdbgmsg 0 foreach: Invalid arguments for foreach part. Syntax: foreach part <var> "<string>" "<character count>" "<command>"
      }
    }
    else do
    {
      es_xdbgmsg 0 foreach: Invalid arguments for foreach part. Syntax: foreach part <var> "<string>" "<character count>" "<command>"
    }
  }
  else do
  {
    es_xdbgmsg 0 foreach: Invalid arguments for foreach part. Syntax: foreach part <var> "<string>" "<character count>" "<command>"
  }
  stack pop _foreachvar _foreach_part_stack
  stack pop _foreachstring _foreach_part_stack
  stack pop _foreachchars _foreach_part_stack
  stack pop _foreachcommand _foreach_part_stack
  stack pop _foreachtarget _foreach_part_stack
  stack pop _foreachseparator _foreach_part_stack
  stack pop _foreachendex _foreach_part_stack
  stack pop _foreachindex _foreach_part_stack
  // end of sub command "part"
}

// -----------------------------------------------------------------------------
// Internal block part:
// --------------------
//

// filters the foreach player keygroup
block foreach_player_filter
{
  es es_xif ("#alive" in server_var(_foreachtarget)) do
  {
    es es_xkeygroupfilter server_var(_foreachkeyname) only isdead "0"
  }
  es es_xif ("#dead" in server_var(_foreachtarget)) do
  {
    es es_xkeygroupfilter server_var(_foreachkeyname) only isdead "1"
  }
  es es_xif ("#human" in server_var(_foreachtarget)) do
  {
    es es_xkeygroupfilter server_var(_foreachkeyname) only isbot "0"
  }
  es es_xif ("#bot" in server_var(_foreachtarget)) do
  {
    es es_xkeygroupfilter server_var(_foreachkeyname) only isbot "1"
  }
  _foreachdoit 1
  es es_xif ("#spec" notin server_var(_foreachtarget)) do
  {
    es es_xif ("#un" notin server_var(_foreachtarget)) do
    {
      es es_xif ("#t" notin server_var(_foreachtarget)) do
      {
        es es_xif ("#ct" notin server_var(_foreachtarget)) do
        {
          _foreachdoit 0
        }
      }
    }
  }
  es es_xif (server_var(_foreachdoit) == 1) do
  {
    es es_xif ("#spec" notin server_var(_foreachtarget)) do
    {
      es es_xkeygroupfilter server_var(_foreachkeyname) not teamid "0"
    }
    es es_xif ("#un" notin server_var(_foreachtarget)) do
    {
      es es_xkeygroupfilter server_var(_foreachkeyname) not teamid "1"
    }
    es es_xif ("#t" notin server_var(_foreachtarget)) do
    {
      es es_xkeygroupfilter server_var(_foreachkeyname) not teamid "2"
    }
    es es_xif ("#ct" notin server_var(_foreachtarget)) do
    {
      es es_xkeygroupfilter server_var(_foreachkeyname) not teamid "3"
    }
  }
}

// performe the command on a group/team/sort of players
block foreach_player_cmd
{
  // set up the var and run the command
  es es_xsetinfo server_var(_foreachvar) server_var(_foreachplayer)
  es alias _foreachcorecmd server_var(_foreachcommand)
  _foreachcorecmd
}

// performe the command on the entities
block foreach_entity_cmd
{
  // set up the var and run the command
  es es_xkeygetvalue server_var(_foreachclassvar) server_var(_foreachkeyname) server_var(_foreachentity) classname
  es es_xsetinfo server_var(_foreachvar) server_var(_foreachentity)
  es alias _foreachcorecmd server_var(_foreachcommand)
  _foreachcorecmd  
}

// copy the targetted weapons into the new loop keygroup
block foreach_weapon_prep
{
  es es_xkeygetvalue _foreachweapontag server_var(_foreachweaponkeyname) server_var(_foreachweaponkey) tags
  es es_xif (server_var(_foreachweapontoken) in server_var(_foreachweapontag)) do
  {
    es es_xkeycreate server_var(_foreachkeyname) server_var(_foreachweaponkey)
  }
}

// check for the tag
block foreach_weapon_tags
{
  es es_xformat _foreachweapontoken "#%1" server_var(_foreachweapontoken)
  es es_xforeachkey _foreachweaponkey in server_var(_foreachweaponkeyname) "es_xdoblock corelib/foreach/foreach_weapon_prep"
}

// performe the command on the weapons
block foreach_weapon_cmd
{
  // set up the var and run the command
  es es_xsetinfo server_var(_foreachvar) server_var(_foreachweaponkey)
  es alias _foreachcorecmd server_var(_foreachcommand)
  _foreachcorecmd
}

// set up the token parts
block foreach_token_cmd
{
  es es_xtoken _foreachtarget server_var(_foreachstring) 1 server_var(_foreachseparator)
  es es_xif (server_var(_foreachtarget) != "0") do
  {
    es es_xstrlen _foreachindex server_var(_foreachtarget)
    es es_xstrlen _foreachendex server_var(_foreachstring)
    es_xmath _foreachindex + 1
    es es_xstring _foreachstring section server_var(_foreachindex) server_var(_foreachendex)
    es es_xsetinfo server_var(_foreachvar) server_var(_foreachtarget)
    es alias _foreachcorecmd server_var(_foreachcommand)
    _foreachcorecmd
    es_xdoblock corelib/foreach/foreach_token_loop
  }
}

// call the cmd part
block foreach_token_loop
{
  es_xdoblock corelib/foreach/foreach_token_cmd
}

// set up the part parts
block foreach_part_cmd
{
  es es_xmath _foreachindex + server_var(_foreachchars)
  es es_xmath _foreachendex + server_var(_foreachchars)
  es es_xsetinfo server_var(_foreachvar) server_var(_foreachtarget)
  es alias _foreachcorecmd server_var(_foreachcommand)
  _foreachcorecmd
  es_xdoblock corelib/foreach/foreach_part_loop
}

// get the next part
block foreach_part_loop
{
  es_xcopy _foreachtarget _foreachstring
  es es_xstring _foreachtarget section server_var(_foreachindex) server_var(_foreachendex)
  es es_xstrlen _foreachseparator server_var(_foreachtarget)
  es es_xif (server_var(_foreachseparator) < server_var(_foreachchars)) do
  {
    es_xcopy _foreachendex _foreachindex
    es es_xmath _foreachendex + server_var(_foreachseparator)
    es_xcopy _foreachtarget _foreachstring
    es es_xstring _foreachtarget section server_var(_foreachindex) server_var(_foreachendex)
    es es_xstrlen _foreachseparator server_var(_foreachtarget)
  }
  es es_xif (server_var(_foreachseparator) > "0") do
  {
    es es_xif (server_var(_foreachtarget) != "0") then es_xdoblock corelib/foreach/foreach_part_cmd
  }
}

// -----------------------------------------------------------------------------
// Test block part:
// ----------------
//

block foreach_test1
{
  log on
  foreach player _tempuserid #all "es_sexec server_var(_tempuserid) kill"
  foreach player _tempuserid #ct "es_tell server_var(_tempuserid) #lightgreen You died on CT team!"
  foreach player _tempuserid #t "es_tell server_var(_tempuserid) #lightgreen You died on T team!"
  foreach player _tempuserid #spec "es_tell server_var(_tempuserid) #lightgreen You can't die on Spec team!"
}

block foreach_test2
{
  log on
  foreach player _tempuserid #bot "es_sexec server_var(_tempuserid) say I am a silly bot!"
  foreach player _tempuserid #human "es_sexec server_var(_tempuserid) say I am a real player!"
}

block foreach_test3
{
  log on
  foreach player _tempuserid #all "es_xsetinfo _tempusername 0;es_getplayername _tempusername server_var(_tempuserid);es_cexec server_var(_tempuserid) say Hello, my name is server_var(_tempusername)"
}

block foreach_test4
{
  log on
  profile begin foreach_token
  foreach token _temptoken "a-s-d-f-g-h-j-k-l-k-j-h-g-f-d-s-a" "-" "es_msg server_var(_temptoken) is a part of the token"
  profile end foreach_token
}

block foreach_test5
{
  log on
  profile begin foreach_player
  foreach player _tempuserid #bot "es_xdoblock corelib/foreach/foreach_test5b"
  foreach player _tempuserid #human "es_xdoblock corelib/foreach/foreach_test5c"
  profile end foreach_player
}

block foreach_test5b
{
  es_xsetinfo _tempname 0
  es_getplayername _tempname server_var(_tempuserid)
  es_msg Looping bot: server_var(_tempuserid)
  foreach player _tempuserid2 #ct "es_msg server_var(_tempname) hates #ct server_var(_tempuserid2)"
}

block foreach_test5c
{
  es_setinfo _tempname 0
  es_getplayername _tempname server_var(_tempuserid)
  foreach player _tempuserid2 #all "es_msg server_var(_tempname) hates #all server_var(_tempuserid2)"
  foreach player _tempuserid2 #ct "es_msg server_var(_tempname) loves #ct server_var(_tempuserid2)"
}

block foreach_test6
{
  log on
  es_xdoblock corelib/foreach/foreach_test6a
  es_xdoblock corelib/foreach/foreach_test6b
  es_xdoblock corelib/foreach/foreach_test6c
  es_xdoblock corelib/foreach/foreach_test6d
}

block foreach_test6a
{
  profile begin foreach_part1
  foreach part _temppart "a-b-c-d-e-f-g-h-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z" "1" "es_msg server_var(_temppart) is a part of the string"
  profile end foreach_part1
}

block foreach_test6b
{
  profile begin foreach_part2
  foreach part _temppart "a-b-c-d-e-f-g-h-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z" "2" "es_msg server_var(_temppart) is a part of the string"
  profile end foreach_part2
}

block foreach_test6c
{
  profile begin foreach_part3
  foreach part _temppart "a-b-c-d-e-f-g-h-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z" "3" "es_msg server_var(_temppart) is a part of the string"
  profile end foreach_part3
}

block foreach_test6d
{
  profile begin foreach_part4
  foreach part _temppart "a-b-c-d-e-f-g-h-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z" "4" "es_msg server_var(_temppart) is a part of the string"
  profile end foreach_part4
}

block foreach_test7
{
  log on
  es_xdoblock corelib/foreach/foreach_test7a
  es_xdoblock corelib/foreach/foreach_test7b
  es_xdoblock corelib/foreach/foreach_test7c
  es_xdoblock corelib/foreach/foreach_test7d
}

block foreach_test7a
{
  profile begin foreach_weapon1
  foreach weapon testvar #all "es es_xmsg server_var(testvar)"
  profile end foreach_weapon1
}

block foreach_test7b
{
  profile begin foreach_weapon1
  foreach weapon testvar #pistol#smg#shotgun "es es_xmsg server_var(testvar)"
  profile end foreach_weapon1
}

block foreach_test7c
{
  profile begin foreach_weapon1
  foreach weapon testvar #nade#objective "es es_xmsg server_var(testvar)"
  profile end foreach_weapon1
}

block foreach_test7d
{
  es_xmsg This test MUST fail! There should not be any message!
  profile begin foreach_weapon1
  foreach weapon testvar #sadgfdh#asfihujwef "es es_xmsg server_var(testvar)"
  profile end foreach_weapon1
}
