Legacy:SessionMaster

From Unreal Wiki, The Unreal Engine Documentation Site
UT2003 :: Object >> SessionMaster (Ladder1.46)
//-----------------------------------------------------------
// Ladder.SessionMaster
// Sessions concept by El_Muerte[TDS]
// http://wiki.beyondunreal.com/wiki/Sessions
//
// Controls and tracks all Session objects for use by webadmin
//-----------------------------------------------------------
class SessionMaster extends Object transient;

var array<Session> Sessions;

// get a session by it's hash
function Session getSession(string hash, optional bool bCreateNew)
{
	local int i;

	for (i = 0; i < sessions.length; i++) if (Sessions[i].GetHash() == hash) return Sessions[i];
	if (bCreateNew) return createSession();
	return none;
}

// destroy an existing session
function bool destroySession(Session session)
{
	local int i;
	local string Hash;

  	Hash = Session.GetHash();
	for (i = 0; i < sessions.length; i++)
		if (Sessions[i].GetHash() == Hash)
		{

			Sessions.Remove(i, 1);
			return true;
		}

	return false;
}

// create a new session
private function Session createSession()
{
	local Session TempS;

	TempS = new class'Session';
	if (TempS.SetHash(CreateSessionHash())) Sessions[Sessions.Length] = TempS;

	return TempS;
}

// create a unique session hash
function string createSessionHash()
{
	local int i;
	local string temphash;

	do {
		temphash = Right("00000"$string(Rand(65536)), 5)$Right("00000"$string(Rand(65536)), 5);
		for (i = 0; i < sessions.length; i++)
			if (Sessions[i].GetHash() == temphash) break;
	} until (i == sessions.length);
  return temphash;
}