Ejemplo n.º 1
0
//-----------------------------------------------------------------------------------------------
void Server::OnReceiveVictoryPacket( const CS6Packet& packet )
{
	//reset server state
	//reset flag position
	//send resets to all clients

	std::string ipAddressOfClient = "";
	std::string portAsString = "";

	Network& theNetwork = Network::GetInstance();

	ipAddressOfClient = theNetwork.GetIPAddressAsStringFromConnection( m_listenConnectionID );
	portAsString = theNetwork.GetPortAsStringFromConnection( m_listenConnectionID );

	if( ipAddressOfClient == "" || portAsString == "" )
	{
		return;
	}

	std::string idOfClient = portAsString + ipAddressOfClient;

	auto foundIter = m_connectedAndActiveClients.begin();

	for( ; foundIter != m_connectedAndActiveClients.end(); ++foundIter )
	{
		if( foundIter->second == nullptr )
		{
			continue;
		}

		if( foundIter->second->clientID == idOfClient )
		{
			break;
		}
	}

	if( foundIter == m_connectedAndActiveClients.end() )
	{
		return;
	}


	CS6Packet packetToSend;


	GameID roomID = foundIter->second->gameID;
	if( m_gamesAndTheirClients.find( roomID ) == m_gamesAndTheirClients.end() )
	{
		return;
	}

	while( m_gamesAndTheirClients[ roomID ].size() > 0 )
	{
		auto clientIter = m_connectedAndActiveClients.find( m_gamesAndTheirClients[ roomID ].back() );
		if( clientIter->second != nullptr )
		{
			RemoveClientFromRoom( roomID, *clientIter->second );
		}		
	}
}
Ejemplo n.º 2
0
//-----------------------------------------------------------------------------------------------
void Server::OnReceiveJoinGamePacket( const CS6Packet& packet )
{

	std::string ipAddressOfClient = "";
	std::string portAsString = "";

	Network& theNetwork = Network::GetInstance();

	ipAddressOfClient = theNetwork.GetIPAddressAsStringFromConnection( m_listenConnectionID );
	portAsString = theNetwork.GetPortAsStringFromConnection( m_listenConnectionID );

	if( ipAddressOfClient == "" || portAsString == "" )
	{
		return;
	}

	std::string idOfClient = portAsString + ipAddressOfClient;

	auto foundIter = m_connectedAndActiveClients.begin();

	for( ; foundIter != m_connectedAndActiveClients.end(); ++foundIter )
	{
		if( foundIter->second == nullptr )
		{
			continue;
		}

		if( foundIter->second->clientID == idOfClient )
		{
			break;
		}
	}

	if( foundIter == m_connectedAndActiveClients.end() )
	{
		return;
	}

	if( foundIter->second != nullptr )
	{
		GameID gameIDToJoin = packet.data.joinGame.gameID;

		RemoveClientFromRoom( LOBBY_ID, *foundIter->second );

		if( m_gamesAndTheirClients.size() > gameIDToJoin )
		{
			m_gamesAndTheirClients[ gameIDToJoin ].push_back( foundIter->second->connectionID );
			foundIter->second->gameID = gameIDToJoin;

			SendAGameStartPacketToNewClient( *foundIter->second );
		}
	}
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------------------------
void Server::RemoveInactiveClients()
{
	Clock& appClock = Clock::GetMasterClock();
	float deltaSeconds = static_cast< float >( appClock.m_currentDeltaSeconds );

	for( int i = 0; i < static_cast< int >( m_connectedAndActiveClients.size() ); ++ i )
	{
		if( m_connectedAndActiveClients[ i ] == nullptr )
		{
			continue;
		}

		m_connectedAndActiveClients[ i ]->timeSinceLastReceivedMessage += deltaSeconds;

		if( m_connectedAndActiveClients[ i ]->timeSinceLastReceivedMessage >= MAX_SECONDS_OF_INACTIVITY )
		{
			RemoveClientFromRoom( m_connectedAndActiveClients[ i ]->gameID, *m_connectedAndActiveClients[ i ] );

			delete m_connectedAndActiveClients[ i ];
			m_connectedAndActiveClients[ i ] = nullptr;
		}
	}
}
/**
 * Messages are JSON objects that simulate a callback. For example the command
 * `game.bid(5)` would be simualted as `{ "game_bid": { "goldAmmount": 5 } }`.
 */
void RiskServerHandler::HandleMessage(RiskClientSocket& client, const std::string& line)
{
	// translate the message into a JsonValue
	Json::Value message;
	if (!jsonReader.parse(line, message)) {
		std::string encoded = jsonWriter.write(jsonReader.getFormatedErrorMessages());
		client.Send("{ 'message': { 'error': " + encoded.substr(0, encoded.length()-1) + " } }\n");
		return;
	}
	if (!message.getMemberNames().size()) {
		client.Send("{ 'message': { 'error': \"No command sent.\" } }\n");
		return;
	}

	// Handle or delegate message
	std::string commandName = message.getMemberNames().front();
	Json::Value& params = message[commandName];

	printf("%s: %s", commandName.c_str(), jsonWriter.write(params).c_str());

	if (commandName == "hello")
	{
		// TODO: Handle authentication
		client.Hello();
		BroadcastLobbyStateData();
	}

	else if (commandName == "personal-set")
	{
		// Handle setting personal details
		if (message[commandName].isMember("newName")) {
			client.name = message[commandName]["newName"].asString();
		}
		if (message[commandName].isMember("newColor")) {
			client.color = message[commandName]["newColor"].asString();
		}

		if (client.HasRoom()) {
			client.GetRoom()->BroadcastStateData();
		}
		BroadcastLobbyStateData();
	}

	else if (commandName == "room-create")
	{
		// Create room, if not part of a room already
		if (!client.HasRoom()) {
			Room* newRoom = new Room();
			newRoom->AddClient(&client);

			rooms.push_back(newRoom);

			BroadcastLobbyStateData();
		}
	}

	else if (commandName == "room-join")
	{
		// Join room, if not part of a room already
		if (!client.HasRoom())
		{
			Room* newRoom = NULL;
			for (std::list<Room*>::iterator i = rooms.begin(); i != rooms.end(); ++i)
			{
				if ((*i)->id == params["id"].asInt())
				{
					newRoom = *i;
					break;
				}
			}

			if (newRoom != NULL)
			{
				newRoom->AddClient(&client);
				BroadcastLobbyStateData();
			}
		}
	}

	else if (commandName == "room-leave")
	{
		// Leave room, if part of one
		RemoveClientFromRoom(client);
	}

	else if (commandName.find("room-") == 0 || commandName.find("game-") == 0)
	{
		// Delegate the command to the room, if part of a room
		if (client.HasRoom()) {
			client.GetRoom()->HandleMessage(client, message);
			if (commandName.find("room-") == 0) BroadcastLobbyStateData();
		}
	}
}