// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#include "MatchMakingQueries.h"

#define BD_LOG_LEVEL "MM Queries"

FindSessionByIDResult::FindSessionByIDResult()
{
}

FindSessionByIDResult::~FindSessionByIDResult()
{
}

bdBool FindSessionByIDResult::handleResult(const bdUInt numResults, 
										   bdBitBufferRef buffer)
{
	reset();

	bdBool ok = true;	
	BD_ASSERT(numResults <= 1, "Only expecting 1 or 0 results.");

	if(numResults > 0)
	{
		ok = m_customMMInfo.deserialize(buffer);
		if(ok)
		{		
			m_numResults = 1;
		}
	}

	return ok;
}

FindSessionsByGameType::FindSessionsByGameType(const bdUInt maxNumResults,
											   const bdInt gameType)
: bdCustomQueryParams(FIND_SESSIONS_BY_GAME_TYPE, maxNumResults),
  m_gameType(gameType)
{
	if(m_maxNumResults > MAX_FIND_SESSIONS_BY_GAME_TYPE_RESULTS)
	{
		BD_WARN(BD_LOG_LEVEL, "Results class can only store %u results, %u requested.", 
			MAX_FIND_SESSIONS_BY_GAME_TYPE_RESULTS, m_maxNumResults);
	}
}

FindSessionsByGameType::~FindSessionsByGameType()
{
}

void FindSessionsByGameType::serialize(bdBitBuffer& buffer)
{
    bdCustomQueryParams::serialize(buffer);
	
	buffer.writeInt32(m_gameType);
}

FindSessionsByGameTypeResult::FindSessionsByGameTypeResult()
{
}

FindSessionsByGameTypeResult::~FindSessionsByGameTypeResult()
{
}

bdBool FindSessionsByGameTypeResult::handleResult(const bdUInt numResults, 
												  bdBitBufferRef buffer)
{
	reset();

	bdBool ok = true;

	const bdUInt resultsToRead = BD_MIN(numResults, MAX_FIND_SESSIONS_BY_GAME_TYPE_RESULTS);
	if(resultsToRead != numResults)
	{
		BD_WARN(BD_LOG_LEVEL, "Received %u results but can only store %u.", 
			numResults, MAX_FIND_SESSIONS_BY_GAME_TYPE_RESULTS);
	}

	for(bdUInt i=0; ok && (i < resultsToRead); i++)
	{
		ok = m_customMMInfo[i].deserialize(buffer);
		if(ok)
		{
			m_numResults++;
		}
	}

	return ok;
}
