//-----------------------------------------------------------------------------
//
//  $Logfile:: /Source/cl_gamespy.c                                           $
// $Revision:: 2                                                              $
//   $Author:: Burgermac                                                      $
//     $Date:: 12/27/00 7:18 PM                                               $
//
// Copyright (C) 1998 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// DESCRIPTION:
// Gamespy support

#include "client.h"

#include "nonport.h"
#include "gutil.h"
#include "goaceng.h"

static GServerList serverList;

static void UpdateServerListCallBack( GServerList serverlist,int msg,void *instance,void *param1,void *param2 )
{
	GServer server;
	netadr_t a;
	const char *address_string;

	if ( msg == LIST_PROGRESS ) // a progress or new server message
	{
		// Put each item that we get into the server list
		server = ( GServer )param1;

		address_string = va( "%s:%s", ServerGetAddress( server ), ServerGetStringValue( server, "hostport","0" ) );

		if ( NET_StringToAdr( address_string, &a ) )
		{
			const char *info;

			info = va( "%s %s (%d/%d) (%d ms)",
			           ServerGetStringValue( server, "hostname","(NONE)" ),
			           ServerGetStringValue( server, "mapname","(NONE)" ),
			           ServerGetIntValue( server,    "numplayers",0 ),
			           ServerGetIntValue( server,    "maxplayers",0 ),
			           ServerGetIntValue( server,    "ping",0 )
			           );

			MENU_AddToServerList( a, info );
		}
	}
	else if ( msg == LIST_STATECHANGED )
	{
		switch( ServerListState( serverlist ) )
		{
		case sl_idle:      // done updating
			Cvar_Set( "dm_serverstatus", "Done Updating." );
			ServerListClear( serverlist ); /* FS */
			ServerListFree( serverlist ); /* FS */
			serverlist = NULL; /* FS */
			break;
		case sl_listxfer:  // getting list
			Cvar_Set( "dm_serverstatus", "Getting List." );
			break;
		case sl_lanlist:  // searching lan
			Cvar_Set( "dm_serverstatus", "Searching LAN." );
			break;
		case sl_querying:  // querying servers
			Cvar_Set( "dm_serverstatus", "Querying Servers." );
			break;
		}
	}
}

void CL_ServerListThink( void )
{
	// Check to see if the multiplayer menu is up and only think at that time
	if ( cls.currentconsole && serverList )
	{
		ServerListThink( serverList );
	}
}

void CL_RefreshServerList( void )
{
	char secret_key[9];

	//set the secret key, in a semi-obfuscated manner
	secret_key[0] = 'I';
	secret_key[1] = 'j';
	secret_key[2] = '1';
	secret_key[3] = 'u';
	secret_key[4] = 'A';
	secret_key[5] = 'B';
	secret_key[6] = '\0';

	serverList = ServerListNew( GAME_NAME,
	                            GAME_NAME,
	                            secret_key,
	                            10,
	                            UpdateServerListCallBack,
	                            GCALLBACK_FUNCTION,
	                            NULL
	                            );

	Cvar_Set( "dm_serverstatus", "Status" );

	ServerListUpdate( serverList, true );
}
