Legacy:Session

From Unreal Wiki, The Unreal Engine Documentation Site
UT2003 :: Object >> Session (Ladder1.46)
class Session extends Object within SessionMaster
	transient;

//-----------------------------------------------------------
// Ladder.Session
// Sessions concept by El_Muerte[TDS]
// http://wiki.beyondunreal.com/wiki/Sessions
//
// Data container used by webadmin - tracked by SessionMaster
// Session "hashes" (10 digit number unique to each Session object)
// are generated by SessionMaster at server startup
//
// Session hashes are passed via form submittal, often taking advantage
// of the Webserver's ability to parse directly injected form labels
// (?VarName=VarValue)
//
// Session hashes are queried from the WebRequest object
// each connection - this allows easy access to unlimited amounts
// of related data across the entire class, without risking corruption
// across threads (such as would be the case with global variables)
//-----------------------------------------------------------

struct SessionData
{
    var string Key;
    var string Value;
};

struct SessionMap
{
	var string MapName;
	var int MapListOrder;
	var bool bRequired;
};

struct SessionMutator
{
	var string MutatorName;
	var bool bRequired;
};

struct SessionServerActor
{
	var string 	SAName;
	var bool	bRequired;
};

// contains the ProfileConfigSet that this session applies to
var private ProfileConfigSet PCS;

// contains the data for this session
var private array<SessionData> 			Data;
var private array<SessionMutator> 		Mutators;
var private array<SessionMap> 			Maps;
var private array<SessionServerActor>	ServerActors;

// contains the unique identifier
var private string hash;

// Input functions
final function bool SetHash(string NewHash)
{
	if (NewHash!="")
	{
		Hash = NewHash;
		return true;
	}

	return false;
}

function bool SetPCS(ProfileConfigSet TempPCS)
{
	if (TempPCS != None)
	{
		PCS = TempPCS;
		return true;
	}

	return false;
}

function bool setValue(string Dataname, string value, bool bAddIfNotExists, optional out string oldValue)
{
	local int i;
	local SessionData KVP;

	for (i = 0; i<data.length; i++)
	{
		if (data[i].key ~= Dataname)
		{
			oldValue = data[i].value;
			data[i].value = value;
			return true;
		}
	}

	if (bAddIfNotExists)
	{
		KVP.Key = DataName;
		KVP.Value = Value;
		Data[Data.Length] = KVP;
		return true;
	}
	return false;
}

function bool setMutator(string NewMutator, bool bAddIfNotExists, optional bool bRequired)
{
	local int i;
	local SessionMutator tmp;

	for (i=0;i<Mutators.Length;i++)
	{
		if (Mutators[i].MutatorName ~= NewMutator)
		{
			Mutators[i].bRequired = bRequired;
			return true;
		}
	}

	if (bAddIfNotExists)
	{
		tmp.MutatorName = NewMutator;
		tmp.bRequired = bRequired;
		Mutators[Mutators.Length] = tmp;
		return true;
	}

	return false;
}

function bool setMap(string NewMap, int NewOrder, bool bAddIfNotExists, optional bool bRequired)
{
	local int i;
	local SessionMap tmp;

	for (i=0;i<Maps.Length;i++)
	{
		if (Maps[i].MapName ~= NewMap)
		{
			Maps[i].MapListOrder = NewOrder;
			Maps[i].bRequired = bRequired;
			return true;
		}
	}

	if (bAddIfNotExists)
	{
		tmp.MapName = NewMap;
		tmp.bRequired = bRequired;
		tmp.MapListOrder = NewOrder;
		Maps[Maps.Length] = tmp;
		return true;
	}
	return false;
}

function bool SetServerActor(string NewActor, bool bAddIfNotExists, optional bool bRequired)
{
	local int i;
	local SessionServerActor tmp;

	for (i=0;i<ServerActors.Length;i++)
	{
		if (ServerActors[i].SAName ~= NewActor)
		{
			ServerActors[i].bRequired = bRequired;
			return true;
		}
	}

	if (bAddIfNotExists)
	{
		tmp.SAName = NewActor;
		tmp.bRequired = bRequired;

		ServerActors[ServerActors.Length] = tmp;
		return true;
	}

	return false;
}

// Output functions
final function string GetHash()
{
	return Hash;
}

function ProfileConfigSet GetPCS()
{
	if (PCS != None)
		return PCS;

	return None;
}

function int getIdValue(int idx, out string DataName, out string DataValue)
{
	if (idx < 0 || idx >= Data.Length)
		return -1;

	DataName = Data[idx].Key;
	DataValue = Data[idx].Value;
	return idx;
}

function string getValue(string name, optional string sdefault)
{
	local int i;
	for (i = 0; i<data.length; i++)
		if (data[i].key == name) return data[i].value;

	return sdefault;
}

function string getMutator(int idx)
{
	if (idx < 0 || idx >= Mutators.Length)
		return "";

	return Mutators[idx].MutatorName;
}

function string getMap(int idx)
{
	if (idx < 0 || idx >= Maps.Length)
		return "";

	return Maps[idx].MapName;
}

function string getServerActor(int idx)
{
	if (idx < 0 || idx >= ServerActors.Length)
		return "";

	return ServerActors[idx].SAName;
}

// Maintenance
final function ClearMutators()
{
	Mutators.Length = 0;
}

final function ClearMaps()
{
	Maps.Length = 0;
}

final function ClearData()
{
	Data.Length = 0;
}

final function ClearServerActors()
{
	ServerActors.Length = 0;
}

function bool delValue(string DataName)
{
	local int i;

	for (i=0;i<Data.Length;i++)
	{
		if (Data[i].Key ~= DataName)
		{
			Data.Remove(i,1);
			return true;
		}
	}

	return false;
}

function bool delMap(string MapName)
{
	local int i;

	for (i=0;i<Maps.Length;i++)
	{
		if (Maps[i].MapName ~= MapName)
		{
			Maps.Remove(i,1);
			return true;
		}
	}

	return false;
}

function bool delMutator(string Mutator)
{
	local int i;

	for (i=0;i<Mutators.Length;i++)
	{
		if (Mutators[i].MutatorName ~= Mutator)
		{
			Mutators.Remove(i,1);
			return true;
		}
	}

	return false;
}

function bool delServerActor(string ActorName)
{
	local int i;
	for (i=0;i<ServerActors.Length;i++)
	{
		if (ServerActors[i].SAName ~= ActorName)
		{
			ServerActors.Remove(i,1);
			return true;
		}
	}
	return false;
}

// Utility
final function ResetSession()
{
	ClearMutators();
	ClearMaps();
	ClearData();
	ClearServerActors();

	PCS = None;
}

final function int GetMutatorLength()
{
	return Mutators.Length;
}

final function int GetDataLength()
{
	return Data.Length;
}

final function int GetMapLength()
{
	return Maps.Length;
}

final function int GetSALength()
{
	return ServerActors.Length;
}

final function int MapOrder(int idx)
{
	if (idx < 0 || idx >= Maps.Length)
		return -1;

	return Maps[idx].MapListOrder;
}

final function bool HasData(string DataName)
{
	local int i;

	for (i=0;i<Data.Length;i++)
		if (Data[i].key ~= DataName)
			return true;
	return false;
}

final function bool HasMutator(string MutatorName)
{
	local int i;

	for (i=0;i<Mutators.Length;i++)
		if (Mutators[i].MutatorName ~= MutatorName)
			return true;
	return false;
}

final function bool HasMap(string MapName)
{
	local int i;
	for (i=0;i<Maps.Length;i++)
		if (Maps[i].MapName ~= MapName)
			return true;
	return false;
}

final function bool HasServerActor(string ActorName)
{
	local int i;
	for (i=0;i<ServerActors.Length;i++)
		if (ServerActors[i].SAName ~= ActorName)
			return true;
	return false;
}

final function bool MutRequired(string MutatorName)
{
	local int i;

	for (i=0;i<Mutators.Length;i++)
		if ((Mutators[i].MutatorName ~= MutatorName) && (Mutators[i].bRequired))
			return true;
	return false;
}

final function bool MapRequired(string MapName)
{
	local int i;
	for (i=0;i<Maps.Length;i++)
		if (Maps[i].MapName ~= MapName)
			return Maps[i].bRequired;
	return false;
}

final function bool SARequired(string ActorName)
{
	local int i;
	for (i=0;i<ServerActors.Length;i++)
		if (ServerActors[i].SAName ~= ActorName)
			return ServerActors[i].bRequired;
	return false;
}