
#pragma once

#ifdef _DEBUG
#define USE_NCAA
#endif

#define GAMESPY_MASTER_INTERNET_HEARTBEAT_PORT	27900
#define GAMESPY_MASTER_INTERNET_QUERY_PORT		27950
#define GAMESPY_MASTER_INTERNET_SERVERLIST_PORT	28900
#define GAMESPY_MASTER_INTERNET_ADDR			"master.gamespy.com"

#ifdef LAB_ONLY
#define GAMESPY_MASTER_LAN_HEARTBEAT_PORT		27900
#define GAMESPY_MASTER_LAN_QUERY_PORT			27950
#define GAMESPY_MASTER_LAN_SERVERLIST_PORT		27900
#define GAMESPY_MASTER_LAN_ADDR					"ncaa"
#else
#define GAMESPY_MASTER_LAN_HEARTBEAT_PORT		GAMESPY_MASTER_INTERNET_HEARTBEAT_PORT  
#define GAMESPY_MASTER_LAN_QUERY_PORT			GAMESPY_MASTER_INTERNET_QUERY_PORT	   
#define GAMESPY_MASTER_LAN_SERVERLIST_PORT		GAMESPY_MASTER_INTERNET_SERVERLIST_PORT 
#define GAMESPY_MASTER_LAN_ADDR					GAMESPY_MASTER_INTERNET_ADDR			   
#endif

#ifdef USE_NCAA
#define GAMESPY_MASTER_HEARTBEAT_PORT		GAMESPY_MASTER_LAN_HEARTBEAT_PORT
#define GAMESPY_MASTER_QUERY_PORT			GAMESPY_MASTER_LAN_QUERY_PORT	
#define GAMESPY_MASTER_SERVERLIST_PORT		GAMESPY_MASTER_LAN_SERVERLIST_PORT
#define GAMESPY_MASTER_ADDR					GAMESPY_MASTER_LAN_ADDR			
#else
#define GAMESPY_MASTER_HEARTBEAT_PORT		GAMESPY_MASTER_INTERNET_HEARTBEAT_PORT
#define GAMESPY_MASTER_QUERY_PORT			GAMESPY_MASTER_INTERNET_QUERY_PORT	
#define GAMESPY_MASTER_SERVERLIST_PORT		GAMESPY_MASTER_INTERNET_SERVERLIST_PORT
#define GAMESPY_MASTER_ADDR					GAMESPY_MASTER_INTERNET_ADDR			
#endif

#define GAMESPY_MASTER_ENGINENAME			"mech4"
#define GAMESPY_MASTER_SERVER_KEY			"GameSpy Master Server"
#define GAMESPY_MASTER_HEARTBEAT_PORT_KEY	"GameSpy Heartbeat Port"
#define GAMESPY_MASTER_QUERY_PORT_KEY		"GameSpy Query Port"
#define GAMESPY_MASTER_SERVERLIST_PORT_KEY	"GameSpy ServerList Port"

#define FixSZString(s) ((s)[sizeof(s)-1]) = 0

#define NEW_GAMESPY
#ifndef NEW_GAMESPY
namespace GameSpyOld
{
	extern char *GetKeyName(void);
	extern bool BeginAdvertisingOnGameSpy(const char * GameName, const char *PlayerName, int MaxPlayers, const char *Password, const GUID &guidInstance, bool Secure, DWORD dwFlags);
	extern void EndAdvertisingOnGameSpy(void);
	extern bool ProcessGameSpy(void);

	extern bool BeginQueryingGameSpy(void);
	extern void EndQueryingGameSpy(void);
	extern bool Querying(void);
	extern char *GetServerAddress(int index);
	extern char *GetServerString(int index, const char *szKey, const char *szDefault="");
	extern char *GetPlayerString(int index, int playerindex, const char *szKey, const char *szDefault="");
	extern int GetNumberOfGames(void);
	extern bool SynchronizeServerList(void);
	extern void SetKey(const char *szKey, const char *szValue);
	extern bool AddPlayer(int id, const char *szName, const char *szTeam, int reserved);
	extern bool RemovePlayer(int id, const char *szName, const char *szTeam, int reserved);


	extern void GetGameSpyAddress(char *szGameSpyAddress, int size);
	extern int GetHeartbeatPort(void);
	extern int GetQueryPort(void);
	extern int GetServerListPort(void);

	extern int __stdcall GetStatus(void);
	extern int __stdcall GetNetStatus(void);
	extern int __stdcall GetLastError(void);
	extern int status;

	extern char *szGameSpyAddress;
};
#endif

//========================================================================
// GameSpy
//
// This is the main class the GameSpyHost and GameSpyClient inhereit from.
// It contains the gereral utility functions that are used by both of
// these, such as GetSecretKey() and GetMasterAddress().
//
//========================================================================
class GameSpy
{
public:
	enum GSS
	{
		GSS_RESOLVING_DNS,
		GSS_CONNECTING,
		GSS_REGISTERING,
		GSS_QUERYING,
		GSS_DOWNLOADING_GAMES,
		GSS_DOWNLOADING_PLAYERS,
		GSS_DISCONNECTED,
		GSS_ERR_CANNOTFINDMASTER,
	};
	virtual bool __stdcall Release(void) = 0;
	virtual bool __stdcall Synchronize(void) = 0;

	static int __stdcall GetStatus(void);
	static int __stdcall GetNetStatus(void);
	static int __stdcall GetLastError(void);

protected:
	static const char * __stdcall GetSecretKey(void);
	static const char * __stdcall GetMasterAddress(void);
	static int __stdcall GetHeartbeatPort(void);
	static int __stdcall GetServerListPort(void);
	static int __stdcall GetQueryPort(void);

};

//========================================================================
// GameSpyHost
//
// This is the class that implements the members necessary to advertise
// a GameSpy server.
//========================================================================
class GameSpyHost : public GameSpy
{
public:
	static GameSpyHost * __stdcall Create(char *szMasterServer);
	virtual bool __stdcall BeginAdvertising(const char *szGameName, int MaxPlayers, const char *szPassword, const GUID &guidInstance, int reserved) = 0;
	virtual bool __stdcall EndAdvertising(void) = 0;
	virtual bool __stdcall SetKey(const char *szKey, const char *szVal, DWORD dwPlayer=0) = 0;
	virtual bool __stdcall SetFlags(const int flags) = 0;
	virtual bool __stdcall AddPlayer(int id, const char *szName, const char *szTeam, int reserved) = 0;
	virtual bool __stdcall RemovePlayer(int id, const char *szName, const char *szTeam, int reserved) = 0;
};

//========================================================================
//========================================================================
class GameSpyClient : public GameSpy
{
public:
	static GameSpyClient * __stdcall Create(char *szMasterServer);
	virtual int __stdcall GetNumberOfGames(void) = 0;
	virtual const char *__stdcall GetServerString(int index, const char *szKey) = 0;
	virtual const char *__stdcall GetServerAddress(int index) = 0;
	virtual const char *__stdcall GetPlayerString(int index, int playerindex, const char *szKey) = 0;
	virtual bool __stdcall BeginQuerying(void) = 0;
	virtual bool __stdcall EndQuerying(void) = 0;
};


extern bool __stdcall InitGameSpyServerBrowser(char *szMasterServer);
extern bool __stdcall InitGameSpyServerAdvertiser(char *szMasterServer);

extern const char GAMESPY_KEY_PRODUCT_NAME[];
extern const char GAMESPY_KEY_GAME_TYPE[];
extern const char GAMESPY_KEY_NUM_PLAYERS[];
extern const char GAMESPY_KEY_MAX_PLAYERS[];
extern const char GAMESPY_KEY_MAP_TYPE[];
extern const char GAMESPY_KEY_GAME_NAME[];
extern const char GAMESPY_KEY_PRODUCT_VERSION[];
extern const char GAMESPY_KEY_INSTANCE[];
extern const char GAMESPY_PLAYER_KEY_PLAYER[];
extern const char GAMESPY_PLAYER_KEY_CLAN[];
extern const char GAMESPY_KEY_LOCATION[];
extern const char GAMESPY_KEY_HOSTNAME[];
extern const char GAMESPY_KEY_HOSTPORT[];
extern const char GAMESPY_KEY_GAMEMODE[];
extern const char GAMESPY_KEY_TIMELIMIT[];
extern const char GAMESPY_KEY_FRAGLIMIT[];
extern const char GAMESPY_KEY_TEAMPLAY[];
extern const char GAMESPY_KEY_RANKEDSERVER[];
extern const char GAMESPY_KEY_FLAGS[];
extern const char GAMESPY_KEY_PASSWORD_PROTECTED[];



