//---------------------------------------------------------------------------------
// keygroupsort v1.01
// by Don
// 11/14/2006
//
// Description:
// This script creates a new command called "keygroupsort". 
// This command will sort a keygroup on the "field to sort"
// parameter.
//
// Installation:
// Create a directory called keygroupsort under cstrike/addons/eventscripts.
// Copy this file(es_keygroupsort.txt) to this directory.
// Put "es_load keygroupsort" into your autoexec.cfg.
// 
// Use:
// keygroupsort <keygroupname> <field to sort>
//
// Notes:
// This command is not fast when the keygroup gets large.  If the keygroup you
// want to sort is larger than about 20 keys then you should probably only run
// this at round_end or es_map_start.  With 10 keys it takes about 25 mS but
// with 100 keys it takes more than 1 second.
//---------------------------------------------------------------------------------
block load
{
	es_xdoblock corelib/keygroupsort/keygroupsort_register
}

block keygroupsort_register
{
	es_xsetinfo _keygroupsort_temp1 0
	es_xsetinfo _keygroupsort_temp2 0
	es_xsetinfo _keygroupsort_kg_to_sort 0
	es_xsetinfo _keygroupsort_numargs 0
	es_xsetinfo _keygroupsort_counter 0
	es_xsetinfo _keygroupsort_argindex 0
	es_xsetinfo _keygroupsort_key 0
	es_xsetinfo _keygroupsort_val 0
	es_xsetinfo _keygroupsort_create_temp1 0
	es_xsetinfo _keygroupsort_create_temp2 0
	es_xsetinfo _keygroupsort_create_temp3 0
	es_xsetinfo _keygroupsort_create_exists 0
	es_xsetinfo _keygroupsort_highval 0
	es_xsetinfo _keygroupsort_highkeyname 0
	es_xsetinfo _keygroupsort_sortfield 0
	es_xsetinfo _keygroupsort_numentries 0
	es_xsetinfo _keygroupsort_keyvalname 0
	es_xsetinfo _keygroupsort_keyname 0
	es_xregcmd keygroupsort corelib/keygroupsort/_keygroupsort "Sort a keygroup"
}

block _keygroupsort
{
	es_xgetargc _keygroupsort_numargs
	if (server_var(_keygroupsort_numargs) > 1) do
	{
		es_xgetargv _keygroupsort_kg_to_sort 1
		es_xgetargv _keygroupsort_sortfield 2
		es es_xexists _keygroupsort_create_exists keygroup server_var(_keygroupsort_kg_to_sort)
		if (server_var(_keygroupsort_create_exists) != 0) do
		{
			es_doblock corelib/keygroupsort/_keygroupsort_createsortkey
			es_doblock corelib/keygroupsort/_keygroupsort_createnewkeygroup
			es_xkeygroupdelete sorted
		}
		else do
		{
			es_xdbgmsg 0 Keygroup does not exist
		}
	}
	else do
	{
		es_xdbgmsg 0 Syntax: keygroupsort <keygroupname> <field to sort> - Sorts a keygroup by field to sort
	}
}

block _keygroupsort_createsortkey
{
			es_xexists _keygroupsort_create_temp1 keygroup sorted
			if (server_var(_keygroupsort_create_temp1) == 1) do
			{
				es_xkeygroupdelete sorted
			}
			es_xkeygroupcreate sorted
			es es_xexists _keygroupsort_create_temp1 keygroup tempsortgroup
			if (server_var(_keygroupsort_create_temp1) == 1) do
			{
				es_xkeygroupdelete tempsortgroup
			}
			es es_xkeygrouprename server_var(_keygroupsort_kg_to_sort) tempcreatesortkeygroup
			es es_xkeygroupsave tempcreatesortkeygroup |corelib/keygroupsort
			es es_xkeygrouprename tempcreatesortkeygroup tempsortgroup
			es es_xkeygroupload tempcreatesortkeygroup |corelib/keygroupsort
			es es_keygrouprename tempcreatesortkeygroup server_var(_keygroupsort_kg_to_sort)
			es_xsetinfo _keygroupsort_numentries 0
			es_xsetinfo _keygroupsort_highval -99999999
			es es_xforeachkey _keygroupsort_create_temp1 in server_var(_keygroupsort_kg_to_sort) "es_doblock corelib/keygroupsort/_keygroupsort_outerloop"
			es_keygroupdelete tempsortgroup
}

block _keygroupsort_createnewkeygroup
{
	es_keygroupcreate _keygroupsort_temp
	es_foreachkey _keygroupsort_key in sorted "es_xdoblock corelib/keygroupsort/_keygroupsort_newkey"
	es es_xkeygroupdelete server_var(_keygroupsort_kg_to_sort)
	es es_xkeygrouprename _keygroupsort_temp server_var(_keygroupsort_kg_to_sort)
}

block _keygroupsort_newkey
{
	es_keygetvalue _keygroupsort_keyname sorted server_var(_keygroupsort_key) keyname
	es_keycreate _keygroupsort_temp server_var(_keygroupsort_keyname)
	es_foreachval _keygroupsort_keyvalname in server_var(_keygroupsort_kg_to_sort) server_var(_keygroupsort_keyname) "es_xdoblock corelib/keygroupsort/_keygroupsort_copy"
}

block _keygroupsort_copy
{
	es_keygetvalue _keygroupsort_val server_var(_keygroupsort_kg_to_sort) server_var(_keygroupsort_keyname) server_var(_keygroupsort_keyvalname)
	es_keysetvalue _keygroupsort_temp server_var(_keygroupsort_keyname) server_var(_keygroupsort_keyvalname) server_var(_keygroupsort_val)
}

block _keygroupsort_outerloop
{
	es_xmath _keygroupsort_numentries + 1
	es_xsetinfo _keygroupsort_highval -99999999
	es_xforeachkey _keygroupsort_create_temp2 in tempsortgroup "es_xdoblock corelib/keygroupsort/keygroupsort_innerloop"
	es es_xkeycreate sorted server_var(_keygroupsort_numentries)
	es es_xkeysetvalue sorted server_var(_keygroupsort_numentries) keyname server_var(_keygroupsort_highkeyname)
	es es_xkeysetvalue sorted server_var(_keygroupsort_numentries) sortfield server_var(_keygroupsort_highval)
	es es_xkeydelete tempsortgroup server_var(_keygroupsort_highkeyname)
}

block keygroupsort_innerloop
{
	es es_xkeygetvalue _keygroupsort_create_temp3 tempsortgroup server_var(_keygroupsort_create_temp2) server_var(_keygroupsort_sortfield)
	if (server_var(_keygroupsort_create_temp3) => server_var(_keygroupsort_highval)) do
	{
		es es_xsetinfo _keygroupsort_highval server_var(_keygroupsort_create_temp3)
		es es_xsetinfo _keygroupsort_highkeyname server_var(_keygroupsort_create_temp2)
	}
}

