class SmartCTFWelcomeNewPlayers expands Actor config(SmartCTF_ChaCha);


var config int 				PawnLastSeenMaxHistory;
var config array<string> 	PawnIdentHistory;
var config byte 			WelcomeMsgType;
var config byte 			WelcomeBackMsgType;
var config bool 			bPlayWelcomeSound;
var config bool 			bPlayWelcomeOthersSound;
var config float			fDelayWelcome;
var config float			fDelayWelcomeBack;

var 	Pawn	OtherNewpawn;

	
function BumpPawnToTop(int IdxPawn)
{
	PawnIdentHistory.Insert(PawnIdentHistory.Length,1);
	PawnIdentHistory[PawnIdentHistory.Length-1]=PawnIdentHistory[IdxPawn];
	PawnIdentHistory.Remove(IdxPawn);
}

state Welcome
{
	function Timer()
	{
		local Pawn pn;
		local PlayerReplicationInfo OtherNewpawnPRI;
		
		OtherNewpawnPRI = OtherNewpawn.PlayerReplicationInfo;
		if( OtherNewpawnPRI == None || !OtherNewpawn.bIsPlayer || ( OtherNewpawnPRI.bIsSpectator && !OtherNewpawnPRI.bWaitingPlayer ) )
		{
			return;
		}
		
		if(bPlayWelcomeSound)
		{
			//log("a");
			//log(OtherNewpawn);
			OtherNewpawn.ReceiveLocalizedMessage( class'SmartCTFAudioMsg', 7 );
		}
		if(WelcomeMsgType==1)
		{
			//log("b");
			//log(OtherNewpawn);
			OtherNewpawn.ClientMessage( class'SmartCTFCoolMsg'.static.GetString(8));
		}
		else if(WelcomeMsgType==2)
		{
			//log("c");
			Level.Game.BroadcastMessage( class'SmartCTFCoolMsg'.static.GetString(8 +64,OtherNewpawnPRI));
		}
		else if(WelcomeMsgType==3)
		{
			//log("d");
			//log(class'SmartCTFCoolMsg'.static.GetString(8+64,OtherNewpawnPRI));
			Level.Game.BroadcastLocalizedMessage( class'SmartCTFCoolMsg',8 + 64,OtherNewpawnPRI);
		}
		
		if(bPlayWelcomeOthersSound)
		{
			//log("e");
			for( pn = Level.PawnList; pn != None; pn = pn.NextPawn )
			{
				if( PlayerPawn( pn ) != None && pn.bIsPlayer && pn != OtherNewpawn)
				{
					//log("f");
					//log(pn);
					pn.ReceiveLocalizedMessage( class'SmartCTFAudioMsg', 7 );
				}
			}
		}
	}
}
state WelcomeBack
{
	function Timer()
	{
		local PlayerReplicationInfo OtherNewpawnPRI;
		
		OtherNewpawnPRI = OtherNewpawn.PlayerReplicationInfo;
		if( OtherNewpawnPRI == None || !OtherNewpawn.bIsPlayer || ( OtherNewpawnPRI.bIsSpectator && !OtherNewpawnPRI.bWaitingPlayer ) )
		{
			return;
		}
		if(WelcomeBackMsgType==1)
		{
			//log("b");
			//log(OtherNewpawn);
			OtherNewpawn.ClientMessage( class'SmartCTFCoolMsg'.static.GetString(9));
		}
		else if(WelcomeBackMsgType==2)
		{
			//log("c");
			Level.Game.BroadcastMessage( class'SmartCTFCoolMsg'.static.GetString(9 +64,OtherNewpawnPRI));
		}
		else if(WelcomeBackMsgType==3)
		{
			//log("d");
			//log(class'SmartCTFCoolMsg'.static.GetString(9+64,OtherNewpawnPRI));
			Level.Game.BroadcastLocalizedMessage( class'SmartCTFCoolMsg',9 + 64,OtherNewpawnPRI);
		}
	}
}

function CheckPawn(Pawn Other,bool bIsFirstSpawn)
{
	local int		i,iColon;
	local string	PawnIPPort,PawnIP,PawnName,PawnIdent;
	
	if( PlayerPawn( Other ) == None)
	{
		return;
	}
	
	PawnIPPort = PlayerPawn(Other).GetPlayerNetworkAddress();
	iColon = InStr( PawnIPPort, ":" );
	PawnIP = Left( PawnIPPort, iColon );
	if( PawnIP == "")
	{
		log("[WARNING] Ignored player with empty / incorrect IP address");
		return;
	}
	PawnName = Other.PlayerReplicationInfo.PlayerName;
	
	PawnIdent=PawnIP$":"$PawnName;
	
	//log(Other);
	//log(PawnIPPort);
	//log(PawnIP);
	//log(PawnName);
	//log(PawnIdent);
	
	if( PawnIdentHistory.Length > PawnLastSeenMaxHistory)
	{
		PawnIdentHistory.Remove(0,PawnIdentHistory.Length - PawnLastSeenMaxHistory);
		log("[WARNING] PawnIdentHistory too big, removing old history...");
	}
	
	for ( i = 0;  ( i < PawnIdentHistory.Length ); i++ )
	{
		if( PawnIdentHistory[i] == PawnIdent)
		{
			//log("Pawn FOUND in history :-) !");
			BumpPawnToTop(i);
			OtherNewpawn=Other;
			if(bIsFirstSpawn)
			{
				GotoState('WelcomeBack');
				SetTimer(fDelayWelcomeBack,False);
			}
			SaveConfig();
			return;
		}
	}
	
	//log("Pawn NOT found in history :( !");
	PawnIdentHistory.Insert(PawnIdentHistory.Length,1);
	PawnIdentHistory[PawnIdentHistory.Length-1]=PawnIdent;
		
	if( PawnIdentHistory.Length > PawnLastSeenMaxHistory)
	{
		//log("Need to trim history");
		PawnIdentHistory.Remove(0,1);
	}
	OtherNewpawn=Other;
	GotoState('Welcome');
	SetTimer(fDelayWelcome,False);
	
	//log("Finish");
	SaveConfig();
}

defaultproperties
{
	PawnLastSeenMaxHistory=256
	WelcomeMsgType=3
	WelcomeBackMsgType=3
	bPlayWelcomeSound=True
	bPlayWelcomeOthersSound=True
	fDelayWelcome=1.0
	fDelayWelcomeBack=1.0
	bHidden=True
}