Esempio n. 1
0
/**
 * Listens to the packages sent by the server.
 *	- if the m_package is a GameState object: applies the changes to the scene.
 *	- if the m_clientTable in the m_package has changed: stores the updated one
 *	- if its the first run: replaces the dummy player with the real one created on the server
 *	- if its a lua command: it is a response to a lua command sent to the server earlier
 */
void Client::listen()
{
	if (m_clientId == k_clientIdNone)
	{
		return;
	}

	m_serviceResult = 1;

	do
	{
		m_serviceResult = enet_host_service(m_pClientHost, &m_event, 1);

		if (m_serviceResult > 0)
		{
			switch (m_event.type)
			{
				case ENET_EVENT_TYPE_CONNECT:
					TRACE_NETWORK("A new client connected from " << m_event.peer->address.host << ":" << m_event.peer->address.port, 0);
					m_event.peer->data = (void*)"New User";

					break;

				case ENET_EVENT_TYPE_RECEIVE:
					if (unmarshal(m_netObject, std::string((char*) m_event.packet->data, m_event.packet->dataLength)))
					{
						switch (m_netObject.type)
						{
							// getting back escape char
							case events::KeyEvent::NETOBJ_KEY_DOWN:
								//disconnect();

								break;

							case GameState::NETOBJ_GAMESTATE:
								if (unmarshal(m_package, std::string((char*) m_event.packet->data, m_event.packet->dataLength)))
								{
									///
									//// apply the changes to our entities
									//m_package.apply(m_pEngineCore->getRootNode(), EngineCore::getInstance()->getNodeIdDirectory());

									//// if the clientList changed -> save it to the local variable
									//if (!m_package.getClientTable().empty())
									//{
									//	m_clientTable = m_package.getClientTable();
									//}

									//if (m_pEngineCore->getPlayer()->getName().empty())
									//{
									//	// replace the dummy player with the real one
									//	if (m_pEngineCore->getNodeDirectory().find(m_clientTable[m_pPeer->connectID].clientName) != m_pEngineCore->getNodeDirectory().end())
									//	{
									//		// remove the dummy player from the bsp map
									//		//m_pEngineCore->getMap()->clearObservers();
									//		// delete dummy player
									//		delete m_pEngineCore->getPlayer();
									//		m_pEngineCore->setPlayer(std::static_pointer_cast<Player>(m_pEngineCore->getNodeDirectory().at(m_clientTable[m_pPeer->connectID].clientName)).get());

									//		// add the player to the bsp map
									//		//m_pEngineCore->getMap()->addObserver(m_clientId, m_pEngineCore->getPlayer());
									//	}
									//}
								}
								m_gamePaused = false;

								break;

							case events::LuaCommand::NETOBJ_LUACOMM:
								if (unmarshal(m_luaResponse, std::string((char*) m_event.packet->data, m_event.packet->dataLength)))
								{
									///logToConsole(m_luaResponse.command);
								}

								break;

							case events::ChatMessage::NETOBJ_CHATMSG:
								if (unmarshal(m_chatMessage, std::string((char*) m_event.packet->data, m_event.packet->dataLength)))
								{
									printToChatHistory(m_chatMessage.message);
								}

								break;
						}
					}

					enet_packet_destroy(m_event.packet);

					break;

				case ENET_EVENT_TYPE_DISCONNECT:
					TRACE_NETWORK(m_event.peer->data << " disconnected.", 0);

					break;

				default:
					GX_ASSERT(0 && "Error: unknown event type received.");
					//TRACE_ERROR("Error: unknown event type received.", 0);
			}
		}
	}
	while (m_serviceResult > 0);
}
Esempio n. 2
0
void GX::Entity::setBucket(int bucket)
{
	GX_ASSERT(false);
}