#include "AdeptHeaders.hpp"
#include "GameSpy.h"
#include <GameOS/GUNGameList.h>

#ifndef NEW_GAMESPY
/******
cengtest.c
GameSpy C Engine SDK
  
Copyright 1999 GameSpy Industries, Inc

Suite E-204
2900 Bristol Street
Costa Mesa, CA 92626
(714)549-7689
Fax(714)549-0757

******

 Please see the GameSpy C Engine SDK documentation for more 
 information

******/
#include "goaceng.h"
#include <stdio.h>
#include <string.h>
#ifndef _WIN32_WCE
#include <assert.h>
#else
#define assert(a)
#endif

#ifdef _WIN32_WCE
#include <tchar.h>
void RetailOutputA(CHAR *tszErr, ...);
#define printf RetailOutputA
#endif

#define GAMESPY_ASYNC

//========================================================================
//========================================================================
void ListCallBack1(GServerList serverlist, int msg, void *instance, void *param1, void *param2)
{
	GServer server;
	if (msg == LIST_PROGRESS)
	{
		server = (GServer)param1;
		printf("%s:%d [%d] %s %d/%d %s\n",ServerGetAddress(server),ServerGetQueryPort(server), ServerGetPing(server),ServerGetStringValue(server, "hostname","(NONE)"), ServerGetIntValue(server,"numplayers",0), ServerGetIntValue(server,"maxplayers",0), ServerGetStringValue(server,"mapname","(NO MAP)"));
	} else
		printf("%d %d\n",msg,ServerListState(serverlist));

}
//========================================================================
//========================================================================
void sampleKeyEnumFn(char *key, char *value, void *instance)
{
	printf("\t%s: %s\n",key, value);

}

//========================================================================
//========================================================================
void ListCallBack2(GServerList serverlist, int msg, void *instance, void *param1, void *param2)
{
	GServer server;
	if (msg == LIST_PROGRESS)
	{
		server = (GServer)param1;
		printf("%s:%d| \n",ServerGetAddress(server),ServerGetQueryPort(server));
		ServerEnumKeys(server, sampleKeyEnumFn, NULL);
		printf("\n");
	}

}


//========================================================================
//========================================================================
void PrintServerList(GServerList sl)
{
	GServer server;
	int i;

	for (i = 0; i < ServerListCount(sl) ; i++)
	{
		server = ServerListGetServer(sl, i);
		assert(server != NULL);
		printf("%s:%d [%d] %s %d/%d %s\n",ServerGetAddress(server),ServerGetQueryPort(server), ServerGetPing(server),ServerGetStringValue(server, "hostname","(NONE)"), ServerGetIntValue(server,"numplayers",0), ServerGetIntValue(server,"maxplayers",0), ServerGetStringValue(server,"mapname","(NO MAP)"));
	}
	printf("\n\n\n");

}

//========================================================================
//========================================================================
char *GameSpyOld::GetKeyName(void)
{
	static const unsigned char szKey1[] = { 0x5C,0x2B,0xCA,0x29,0x6E,0x2E,0xA4, };
	static const unsigned char szKey2[] = { 0x29,0x65,0xA8,0x71,0x0B,0x48,0xA4, };
	static char szKey[10];
	char *p = &szKey[-1];
	unsigned char const *p1 = szKey1;
	unsigned char const *p2 = szKey2;

	do
	{
		*++p = (unsigned char)(*p2++^*p1++);
	} while (*p);

	return szKey;
}

//========================================================================
//========================================================================
static GServerList s_sl=NULL;

bool GameSpyOld::Querying(void)
{
	return !!s_sl;
}

//========================================================================
//========================================================================
bool GameSpyOld::SynchronizeServerList(void)
{
#ifdef GAMESPY_ASYNC
	if (s_sl)
	{
		if (!IsServerListIdle(s_sl))
			ServerListThink(s_sl);
		else
			return false;
	}
#endif

	return true;
}


//========================================================================
//========================================================================
bool GameSpyOld::BeginQueryingGameSpy(void)
{
	EndQueryingGameSpy();

	s_sl = ServerListNew(GAMESPY_MASTER_GAMENAME,GAMESPY_MASTER_ENGINENAME,GetKeyName(),10,ListCallBack1,GCALLBACK_FUNCTION,NULL);

	
#ifdef GAMESPY_ASYNC
	if (s_sl)
	{
		char szAddress[256];
		GetGameSpyAddress(szAddress, sizeof(szAddress));

		if (ServerListUpdate(s_sl,true, szAddress, GetServerListPort()) != 0)
		{
			GameSpyOld::status = -GS_FIND_QSERVER;
			ServerListFree(s_sl);
			s_sl=NULL;
		}
	}
#else
	if (s_sl)
	{
		if (ServerListUpdate(s_sl,0) != 0)
		{
			GameSpyOld::status = -GS_FIND_QSERVER;
			ServerListFree(s_sl);
			s_sl=NULL;
		}
	}
#endif
	
	return (s_sl != NULL);
}

//========================================================================
//========================================================================
void GameSpyOld::EndQueryingGameSpy(void)
{
	if (s_sl)
	{
		ServerListFree(s_sl);
		s_sl=NULL;
	}
}

//========================================================================
//========================================================================
char *GameSpyOld::GetServerAddress(int index)
{
	GServer server;

	server = ServerListGetServer(s_sl, index);

	if (server)
	{
		return ServerGetAddress(server);
		//ServerGetAddress(server)
		//ServerGetQueryPort(server)
		//ServerGetPing(server)
		//ServerGetStringValue(server, "hostname","(NONE)")
		//ServerGetIntValue(server,"numplayers",0)
		//ServerGetIntValue(server,"maxplayers",0)
		//ServerGetStringValue(server,"mapname","(NO MAP)"));
	}

	return NULL;
}

//========================================================================
//========================================================================
char *GameSpyOld::GetServerString(int index, const char *szKey, const char *szDefault)
{
	GServer server;

	server = ServerListGetServer(s_sl, index);

	if (server)
	{
		return ServerGetStringValue(server, (char *)szKey, (char *)szDefault);
	}

	return NULL;
}

//========================================================================
//========================================================================
char *GameSpyOld::GetPlayerString(int index, int playerindex, const char *szKey, const char *szDefault)
{
	GServer server;

	server = ServerListGetServer(s_sl, index);

	if (server)
	{
		return ServerGetPlayerStringValue(server, playerindex, (char *)szKey, (char *)szDefault);
	}

	return NULL;
}

//========================================================================
//========================================================================
int GameSpyOld::GetNumberOfGames(void)
{
	if (s_sl == NULL)
		return 0;

	return ServerListCount(s_sl);
}

#endif