//
// Asura_Demonware_CustomMatchMakingInfo.cpp
//
// Created by Mark Dieterman - 19/10/07
//
// Copyright (C) 2007 Rebellion (Liverpool), All Rights Reserved.
//
// Wrapper class to allow integration of DemonWare's Matchmaking+ SDK
//
// Current DW version 1.80
//

// Asura include
#include "Core/Asura.h"

#if defined(ASURA_SUPPORT_EXTERNAL_NETWORK) && defined (ASURA_SUPPORT_DEMONWARE)

#include "Core/Asura_MemoryManagement_DebugOff.h"
#include "DemonWare/bdLobby/bdMatchMaking/bdMatchMakingInfo.h"
#include "DemonWare/bdLobby/bdLobbyCommon/bdCustomQueryParams.h"
#include "DemonWare/bdLobby/bdLobbyCommon/bdQueryResult.h"
#include <bdLobby/bdMatchMaking/bdMatchMakingInfo.h>

#include "Network/Asura_Demonware_CustomMatchMakingInfo.h"

#include <bdCore/bdSocket/bdCommonAddrInfo.h>

#define BD_LOG_LEVEL "MM Info"

Asura_Demonware_CustomMatchMakingInfo::Asura_Demonware_CustomMatchMakingInfo()
: m_iLevelNum(0),
m_iGameType(0)
{
	bdMemset(m_szSessionName, '\0', sizeof(m_szSessionName));
}

Asura_Demonware_CustomMatchMakingInfo::~Asura_Demonware_CustomMatchMakingInfo()
{
}

bdUWord Asura_Demonware_CustomMatchMakingInfo::SetSessionName(const bdNChar8 *const pszSessionName)
{
	bdUWord sessionNameLength = 0;
	if (pszSessionName != BD_NULL)
	{
		sessionNameLength = bdStrlcpy(m_szSessionName, pszSessionName, sizeof(m_szSessionName));
		if (sessionNameLength >= sizeof(m_szSessionName))
		{
			BD_WARN(BD_LOG_LEVEL, "Session name truncated.");
		}
	}
	return sessionNameLength;
}

bdUWord Asura_Demonware_CustomMatchMakingInfo::GetSessionName(bdNChar8 *const pszBuf,
											  const bdUInt uBufSize) const
{
	const bdUWord sessionNameLength = bdStrlcpy(pszBuf, m_szSessionName, uBufSize);
	if(sessionNameLength >= uBufSize)
	{
		BD_WARN(BD_LOG_LEVEL, "Session name truncated");
	}
	return sessionNameLength;
}

void Asura_Demonware_CustomMatchMakingInfo::SetLevelNum(const bdInt iLevelNum)
{
	m_iLevelNum = iLevelNum;
}

bdInt Asura_Demonware_CustomMatchMakingInfo::GetLevelNum() const
{
	return m_iLevelNum;
}

void Asura_Demonware_CustomMatchMakingInfo::SetGameType(const bdInt iGameType)
{
	m_iGameType = iGameType;
}

bdInt Asura_Demonware_CustomMatchMakingInfo::GetGameType() const
{
	return m_iGameType;
}

void Asura_Demonware_CustomMatchMakingInfo::Serialize(bdBitBuffer& xBuffer)
{
	bdMatchMakingInfo::serialize(xBuffer);

	xBuffer.writeString(m_szSessionName, sizeof(m_szSessionName));
	xBuffer.writeInt32(m_iLevelNum);
	xBuffer.writeInt32(m_iGameType);
}

bdBool Asura_Demonware_CustomMatchMakingInfo::Deserialize(bdBitBufferRef xBuffer)
{
	bdBool ok = bdMatchMakingInfo::deserialize(xBuffer);

	ok = ok && xBuffer->readString(m_szSessionName, sizeof(m_szSessionName));
	ok = ok && xBuffer->readInt32(m_iLevelNum);
	ok = ok && xBuffer->readInt32(m_iGameType);
	return ok;
}

void BD_CALL printInfo(const Asura_Demonware_CustomMatchMakingInfo& xSessionData,
					   bdNChar8 *pszBuf,
					   const bdUInt uSize)
{
	if(uSize > 0)
	{
		bdNChar8 szAddrStr[BD_COMMON_ADDR_INFO_SIZE];
		bdCommonAddrInfo::getBriefInfo(xSessionData.getHostAddr(), szAddrStr, sizeof(szAddrStr));

		bdNChar8 szSessionName[BD_MAX_SESSIONNAME_LENGTH];
		xSessionData.GetSessionName(szSessionName, sizeof(szSessionName));

		const bdInt iGameType = xSessionData.GetGameType();

		bdSnprintf(pszBuf, uSize, "%s: GameType %u\n %s", szSessionName, iGameType, szAddrStr);
	}
}
#endif