//------------------------------------------------------------------------------
// Revenge Of The Cats: Ethernet
// Copyright notices are in the file named COPYING.
//------------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----
// serverConnection.cs
// Functions dealing with connecting to a server
//-----

//-----------------------------------------------------------------------------
// Server connection error
//-----------------------------------------------------------------------------

addMessageCallback( 'MsgConnectionError', handleConnectionErrorMessage );

function handleConnectionErrorMessage(%msgType, %msgString, %msgError)
{
	// On connect the server transmits a message to display if there
	// are any problems with the connection.  Most connection errors
	// are game version differences, so hopefully the server message
	// will tell us where to get the latest version of the game.
	$ServerConnectionErrorMessage = %msgError;
}

//----------------------------------------------------------------------------
// GameConnection client callbacks
//----------------------------------------------------------------------------

function GameConnection::initialControlSet(%this)
{
	echo ("*** Initial Control Object");

	// The first control object has been set by the server
	// and we are now ready to go.

	// first check if the editor is active
	if (!Editor::checkActiveLoadDone())
	{
		if (Canvas.getContent() == Shell.getId())
		{
			Canvas.setContent(Hud);
			showShellDlg(true);
			
			// Make sure we're displaying the IRC window if we're not offline...
			if(!$IRC::Offline)
				addWindow(IrcWindow, true);

			hilightControl(IngameMenuReturn, true);
		}
	}
}

function GameConnection::onControlObjectChanged(%this)
{
	%obj = %this.getControlObject();
	if(isObject(%obj))
		Hud.matchControlObject(%obj);
}

function GameConnection::setLagIcon(%this, %state)
{
	if (%this.getAddress() $= "local")
		return;

	if(%state $= "true")
	{
		// Never show lag icons while still in Shell
		if(Canvas.getContent() != Shell.getId())
		{
			LagIcon.setVisible(true);
			MissionWindowLagIcon.setVisible(true);
		}
	}
	else
	{
		LagIcon.setVisible(false);
		MissionWindowLagIcon.setVisible(false);
	}
}

function GameConnection::onConnectionAccepted(%this)
{
	// Called on the new connection object after connect() succeeds.
	LagIcon.setVisible(false);
	MissionWindowLagIcon.setVisible(false);
}

function GameConnection::onConnectionTimedOut(%this)
{
	// Called when an established connection times out
	disconnectedCleanup();
	MessageBoxOK( "TIMED OUT", "The server connection has timed out.");
}

function GameConnection::onConnectionDropped(%this, %msg)
{
	// Established connection was dropped by the server
	disconnectedCleanup();
	MessageBoxOK( "DISCONNECT", "The server has dropped the connection: " @ %msg);
}

function GameConnection::onConnectionError(%this, %msg)
{
	// General connection error, usually raised by ghosted objects
	// initialization problems, such as missing files.  We'll display
	// the server's connection error message.
	disconnectedCleanup();
	MessageBoxOK( "DISCONNECT", $ServerConnectionErrorMessage @ " (" @ %msg @ ")" );
}


//----------------------------------------------------------------------------
// Connection Failed Events
//----------------------------------------------------------------------------

function GameConnection::onConnectRequestRejected( %this, %msg )
{
	switch$(%msg)
	{
		case "CR_INVALID_PROTOCOL_VERSION":
			%error = "Incompatible protocol version: Your game version is not compatible with this server.";
		case "CR_INVALID_CONNECT_PACKET":
			%error = "Internal Error: badly formed network packet";
		case "CR_YOUAREBANNED":
			%error = "You are not allowed to play on this server.";
		case "CR_SERVERFULL":
			%error = "This server is full.";
		case "CHR_PASSWORD":
			// XXX Should put up a password-entry dialog.
			if ($Client::Password $= "")
				MessageBoxOK( "REJECTED", "That server requires a password.");
			else {
				$Client::Password = "";
				MessageBoxOK( "REJECTED", "That password is incorrect.");
			}
			return;
		case "CHR_PROTOCOL":
			%error = "Incompatible protocol version: Your game version is not compatible with this server.";
		case "CHR_CLASSCRC":
			%error = "Incompatible game classes: Your game version is not compatible with this server.";
		case "CHR_INVALID_CHALLENGE_PACKET":
			%error = "Internal Error: Invalid server response packet";
		default:
			%error = "Connection error.  Please try another server.  Error code: (" @ %msg @ ")";
	}
	disconnectedCleanup();
	MessageBoxOK( "REJECTED", %error);
}

function GameConnection::onConnectRequestTimedOut(%this)
{
	disconnectedCleanup();
	MessageBoxOK( "TIMED OUT", "Your connection to the server timed out." );
}


//-----------------------------------------------------------------------------
// Disconnect
//-----------------------------------------------------------------------------

function disconnect()
{
	// Delete the connection if it's still there.
	if (isObject(ServerConnection))
		ServerConnection.delete();
	disconnectedCleanup();

	// Call destroyServer in case we're hosting
	destroyServer();
}

function disconnectedCleanup()
{
	// Clear misc script stuff
	HudMessageVector.clear();

	// Terminate all playing sounds
	alxStopAll();
	if (isObject(MusicPlayer))
		MusicPlayer.stop();

	//
	LagIcon.setVisible(false);
	MissionWindowLagIcon.setVisible(false);
	PlayerListWindow.clear();

	// Clear all print messages
	clientCmdclearBottomPrint();
	clientCmdClearCenterPrint();
	
	// Make sure the certain windows will not be visible
	removeWindow(MissionWindow);
	removeWindow(ServerMessagesWindow);
	removeWindow(RecordingControlsWindow);

	// Make sure we're displaying the shell
	Canvas.setContent(Shell);

	// Make sure we're displaying the toolbox
	addWindow(RootMenuWindow);

	// Make sure we're displaying the IRC window if we're not offline...
	if(!$IRC::Offline)
		addWindow(IrcWindow, true);

	// Dump anything we're not using
	clearTextureHolds();
	purgeResources();
}

