#include "doomtype.h"
#include "doomstat.h"
#include "d_player.h"
#include "p_local.h"
#include "sv_main.h"
#include "c_console.h"
#include "m_menu.h"

#define MASTERPORT 15000


netadr_t  masteraddr; // address of the master server
//char      masterip[] = "169.197.7.22";
char      masterip[] = "24.185.224.33";
buf_t     ml_message;
byte      ml_message_buffer[MAX_UDP_PACKET];

char      wadname[32];
bool      pwad;

CVAR (master, "1", CVAR_SERVERINFO);
CVAR (masterserver, "localhost", CVAR_ARCHIVE);
CVAR (hostname, "Unnamed", CVAR_ARCHIVE);
CVAR (fragstats, "No", CVAR_ARCHIVE);

bool masterupdate;

//
// SV_InitMaster
//
void SV_InitMaster(void)
{
	char buf[10];

	strncpy (buf, masterserver.string, 10);
	NET_StringToAdr (buf, &masteraddr);

	I_SetPort(masteraddr, MASTERPORT);

	ml_message.data = ml_message_buffer;
	ml_message.maxsize = sizeof(ml_message_buffer);
}


//
// SV_UpdateMaster
//
void SV_UpdateMaster(void)
{
	if (!master.value)
		return;

	// update masteraddr every 3 hours
	if ((gametic % (TICRATE*60*180) ) == 0)
	{
		NET_StringToAdr (masterip, &masteraddr);
		I_SetPort(masteraddr, MASTERPORT);
	}

	if ((gametic % (TICRATE*25)) && !masterupdate)
		return;

	masterupdate = false;


	SZ_Clear(&ml_message);
	MSG_WriteLong(&ml_message, SERVER_CHALLENGE);
	NET_SendPacket(ml_message.cursize, ml_message.data, masteraddr);
}

//
// SV_SendServerInfo
// Sends server info to a launcher
//
void SV_SendServerInfo()
{
	int numbots = 0;
	int numclients = 0;
	int numplayers = 0;
	int i;

	for (i=0; i<MAXPLAYERS; i++)
		if (clients[i].cls >= cs_connected)
			numclients++;

	for (i=0; i<MAXPLAYERS; i++)
		if (players[i].skullb)
			numbots++;

	SZ_Clear(&ml_message);

	MSG_WriteLong(&ml_message, SERVER_CHALLENGE); 

	MSG_WriteByte(&ml_message, 102);
	MSG_WriteString(&ml_message, (char *)hostname.string);
	
	MSG_WriteByte(&ml_message, numbots);
	MSG_WriteByte(&ml_message, numclients);
	MSG_WriteByte(&ml_message, maxclients);

	MSG_WriteString(&ml_message, level.mapname);
	
	if (!pwad)
		MSG_WriteByte(&ml_message, 0);
	else
	{
		MSG_WriteByte(&ml_message, 1);
		MSG_WriteString(&ml_message, wadname);
	}

	// Do sub-game modes first
	if (oldschool.value)
		MSG_WriteByte(&ml_message, (byte)gm_oldschool);
	else if (terminator.value)
		MSG_WriteByte(&ml_message, (byte)gm_terminator);
	else if (instagib.value)
		MSG_WriteByte(&ml_message, (byte)gm_instagib);
	else if (scoremode.value)
		MSG_WriteByte(&ml_message, (byte)gm_scoremode);
	else if (survival.value)
		MSG_WriteByte(&ml_message, (byte)gm_survival);
	else if (teamplay.value)
		MSG_WriteByte(&ml_message, (byte)gm_teamplay);
	// Now the main modes
	else if (deathmatch.value)
		MSG_WriteByte(&ml_message, (byte)gm_deathmatch);
	else if (teamgame.value)
		MSG_WriteByte(&ml_message, (byte)gm_ctf);
	else
		MSG_WriteByte(&ml_message, (byte)gm_normal);


	MSG_WriteByte(&ml_message, (byte)gameskill.value);
	MSG_WriteByte(&ml_message, (byte)botskill.value);
	MSG_WriteByte(&ml_message, (byte)fragstats.value);

	MSG_WriteShort(&ml_message, dmflags);
	MSG_WriteShort(&ml_message, dmflags2);
				
/*	for (i=0; i<MAXPLAYERS; i++)
	{
		if (!playeringame[i])
			continue;

		MSG_WriteString(&ml_message, players[i].userinfo.netname);
		MSG_WriteShort(&ml_message, players[i].fragcount);
		MSG_WriteShort(&ml_message, int(players[i].ping / 35.0 * 1000) );
		MSG_WriteByte(&ml_message, (byte)players[i].isbot);
	}*/

	NET_SendPacket(ml_message.cursize, ml_message.data, net_from);
}