Added host id which is used to determine local/remote players.
This commit is contained in:
parent
f0c5f43fe2
commit
4d28bb484f
@ -42,6 +42,9 @@ private:
|
|||||||
* peer list. */
|
* peer list. */
|
||||||
uint8_t m_global_player_id;
|
uint8_t m_global_player_id;
|
||||||
|
|
||||||
|
/** Host id of this player. */
|
||||||
|
uint8_t m_host_id;
|
||||||
|
|
||||||
/** The selected kart id. */
|
/** The selected kart id. */
|
||||||
std::string m_kart_name;
|
std::string m_kart_name;
|
||||||
|
|
||||||
@ -65,6 +68,12 @@ public:
|
|||||||
/** Returns the global ID of this player in this race. */
|
/** Returns the global ID of this player in this race. */
|
||||||
int getGlobalPlayerId() const { return m_global_player_id; }
|
int getGlobalPlayerId() const { return m_global_player_id; }
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
/** Sets the host id of this player. */
|
||||||
|
void setHostId(int host_id) { m_host_id = host_id; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
/** Returns the host id of this player. */
|
||||||
|
uint8_t getHostId() const { return m_host_id; }
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
/** Sets the kart name for this player. */
|
/** Sets the kart name for this player. */
|
||||||
void setKartName(const std::string &kart_name) { m_kart_name = kart_name; }
|
void setKartName(const std::string &kart_name) { m_kart_name = kart_name; }
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
@ -376,16 +376,16 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event)
|
|||||||
*
|
*
|
||||||
* Format of the data :
|
* Format of the data :
|
||||||
* Byte 0 1 2 3 7 8
|
* Byte 0 1 2 3 7 8
|
||||||
* ---------------------------------------------------------
|
* ------------------------------------------------------------------
|
||||||
* Size | 1 | 1 | 1 | 4 | |
|
* Size | 1 | 1 | 1 | 4 | 1 | |
|
||||||
* Data | 1 | 0 <= race id < 16 | 4 | priv token | playernames* |
|
* Data | 1 | 0 <= race id < 16 | 4 | priv token | hostid | playernames* |
|
||||||
* ---------------------------------------------------------
|
* ------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
|
void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
|
||||||
{
|
{
|
||||||
NetworkString &data = event->data();
|
NetworkString &data = event->data();
|
||||||
// At least 12 bytes should remain now
|
// At least 12 bytes should remain now
|
||||||
if (data.size() < 7|| data[0] != 1 || data[2] != 4)
|
if (data.size() < 8|| data[0] != 1 || data[2] != 4)
|
||||||
{
|
{
|
||||||
Log::error("ClientLobbyRoomProtocol",
|
Log::error("ClientLobbyRoomProtocol",
|
||||||
"A message notifying an accepted connection wasn't "
|
"A message notifying an accepted connection wasn't "
|
||||||
@ -406,7 +406,11 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
|
|||||||
else
|
else
|
||||||
name = PlayerManager::getCurrentPlayer()->getName();
|
name = PlayerManager::getCurrentPlayer()->getName();
|
||||||
uint8_t my_player_id = data.getUInt8(1);
|
uint8_t my_player_id = data.getUInt8(1);
|
||||||
|
uint8_t my_host_id = data.getUInt8(7);
|
||||||
|
STKHost::get()->setMyHostId(my_host_id);
|
||||||
|
|
||||||
NetworkPlayerProfile* profile = new NetworkPlayerProfile(my_player_id, name);
|
NetworkPlayerProfile* profile = new NetworkPlayerProfile(my_player_id, name);
|
||||||
|
profile->setHostId(my_host_id);
|
||||||
STKHost::get()->getGameSetup()->setLocalMaster(my_player_id);
|
STKHost::get()->getGameSetup()->setLocalMaster(my_player_id);
|
||||||
m_setup->addPlayer(profile);
|
m_setup->addPlayer(profile);
|
||||||
// connection token
|
// connection token
|
||||||
@ -417,21 +421,22 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event)
|
|||||||
|
|
||||||
// Add all players
|
// Add all players
|
||||||
// ===============
|
// ===============
|
||||||
int n = 7;
|
int n = 8;
|
||||||
while (n < data.size())
|
while (n < data.size())
|
||||||
{
|
{
|
||||||
if (data[n] != 1 )
|
if (data[n] != 1 )
|
||||||
Log::error("ClientLobbyRoomProtocol",
|
Log::error("ClientLobbyRoomProtocol",
|
||||||
"Bad format in players list.");
|
"Bad format in players list.");
|
||||||
|
|
||||||
uint8_t race_player_id = data[n + 1];
|
uint8_t player_id = data[n + 1];
|
||||||
irr::core::stringw name;
|
irr::core::stringw name;
|
||||||
int bytes_read = data.decodeStringW(n + 2, &name);
|
int bytes_read = data.decodeStringW(n + 2, &name);
|
||||||
|
uint8_t host_id = data.getUInt8(n+2+bytes_read);
|
||||||
|
|
||||||
NetworkPlayerProfile* profile2 =
|
NetworkPlayerProfile* profile2 =
|
||||||
new NetworkPlayerProfile(race_player_id, name);
|
new NetworkPlayerProfile(player_id, name);
|
||||||
m_setup->addPlayer(profile2);
|
m_setup->addPlayer(profile2);
|
||||||
n += bytes_read+2;
|
n += bytes_read+3;
|
||||||
// Inform the network lobby of all players so that the GUI can
|
// Inform the network lobby of all players so that the GUI can
|
||||||
// show all currently connected players.
|
// show all currently connected players.
|
||||||
NetworkingLobby::getInstance()->addPlayer(profile2);
|
NetworkingLobby::getInstance()->addPlayer(profile2);
|
||||||
|
@ -455,12 +455,14 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
|
|||||||
if(m_setup->getLocalMasterID()==0)
|
if(m_setup->getLocalMasterID()==0)
|
||||||
m_setup->setLocalMaster(new_player_id);
|
m_setup->setLocalMaster(new_player_id);
|
||||||
|
|
||||||
|
int new_host_id = STKHost::get()->getPeerCount();
|
||||||
|
|
||||||
// Notify everybody that there is a new player
|
// Notify everybody that there is a new player
|
||||||
// -------------------------------------------
|
// -------------------------------------------
|
||||||
NetworkString message(8);
|
NetworkString message(8);
|
||||||
// size of id -- id -- size of local id -- local id;
|
// size of id -- id -- size of local id -- local id;
|
||||||
message.ai8(LE_NEW_PLAYER_CONNECTED).ai8(1).ai8(new_player_id)
|
message.ai8(LE_NEW_PLAYER_CONNECTED).ai8(1).ai8(new_player_id)
|
||||||
.encodeString(name_u8);
|
.encodeString(name_u8).addUInt8(new_host_id);
|
||||||
ProtocolManager::getInstance()->sendMessageExcept(this, peer, message);
|
ProtocolManager::getInstance()->sendMessageExcept(this, peer, message);
|
||||||
|
|
||||||
// Now answer to the peer that just connected
|
// Now answer to the peer that just connected
|
||||||
@ -478,17 +480,19 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event)
|
|||||||
NetworkString message_ack(13 + players.size() * 7);
|
NetworkString message_ack(13 + players.size() * 7);
|
||||||
// connection success -- size of token -- token
|
// connection success -- size of token -- token
|
||||||
message_ack.ai8(LE_CONNECTION_ACCEPTED).ai8(1).ai8(new_player_id).ai8(4)
|
message_ack.ai8(LE_CONNECTION_ACCEPTED).ai8(1).ai8(new_player_id).ai8(4)
|
||||||
.ai32(token);
|
.ai32(token).addUInt8(new_host_id);
|
||||||
// Add all players so that this user knows (this new player is only added
|
// Add all players so that this user knows (this new player is only added
|
||||||
// to the list of players later, so the new player's info is not included)
|
// to the list of players later, so the new player's info is not included)
|
||||||
for (unsigned int i = 0; i < players.size(); i++)
|
for (unsigned int i = 0; i < players.size(); i++)
|
||||||
{
|
{
|
||||||
message_ack.ai8(1).ai8(players[i]->getGlobalPlayerId())
|
message_ack.ai8(1).ai8(players[i]->getGlobalPlayerId())
|
||||||
.encodeString(players[i]->getName());
|
.encodeString(players[i]->getName())
|
||||||
|
.addUInt8(players[i]->getHostId());
|
||||||
}
|
}
|
||||||
sendMessage(peer, message_ack);
|
sendMessage(peer, message_ack);
|
||||||
|
|
||||||
NetworkPlayerProfile* profile = new NetworkPlayerProfile(new_player_id, name);
|
NetworkPlayerProfile* profile = new NetworkPlayerProfile(new_player_id, name);
|
||||||
|
profile->setHostId(new_host_id);
|
||||||
m_setup->addPlayer(profile);
|
m_setup->addPlayer(profile);
|
||||||
peer->setPlayerProfile(profile);
|
peer->setPlayerProfile(profile);
|
||||||
peer->setClientServerToken(token);
|
peer->setClientServerToken(token);
|
||||||
|
@ -69,26 +69,25 @@ void StartGameProtocol::setup()
|
|||||||
// Create the kart information for the race manager:
|
// Create the kart information for the race manager:
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
std::vector<NetworkPlayerProfile*> players = m_game_setup->getPlayers();
|
std::vector<NetworkPlayerProfile*> players = m_game_setup->getPlayers();
|
||||||
|
int local_player_id = 0;
|
||||||
for (unsigned int i = 0; i < players.size(); i++)
|
for (unsigned int i = 0; i < players.size(); i++)
|
||||||
{
|
{
|
||||||
NetworkPlayerProfile* profile = players[i];
|
NetworkPlayerProfile* profile = players[i];
|
||||||
bool is_me = profile->getGlobalPlayerId()
|
bool is_local = profile->getHostId()
|
||||||
== STKHost::get()->getGameSetup()->getLocalMasterID();
|
== STKHost::get()->getMyHostId();
|
||||||
RemoteKartInfo rki(profile->getGlobalPlayerId(),
|
RemoteKartInfo rki(profile->getGlobalPlayerId(),
|
||||||
profile->getKartName(),
|
profile->getKartName(),
|
||||||
profile->getName(),
|
profile->getName(),
|
||||||
/*hostid*/profile->getGlobalPlayerId(),
|
profile->getHostId(),
|
||||||
!is_me);
|
!is_local);
|
||||||
rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty());
|
rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty());
|
||||||
rki.setLocalPlayerId(0);
|
rki.setLocalPlayerId(local_player_id);
|
||||||
// FIXME: for now (only one local player) the global player id
|
if(is_local) local_player_id++;
|
||||||
// can be used as host id.
|
|
||||||
rki.setHostId(profile->getGlobalPlayerId());
|
|
||||||
|
|
||||||
// Inform the race manager about the data for this kart.
|
// Inform the race manager about the data for this kart.
|
||||||
race_manager->setPlayerKart(i, rki);
|
race_manager->setPlayerKart(i, rki);
|
||||||
|
|
||||||
if(is_me)
|
if(is_local)
|
||||||
{
|
{
|
||||||
PlayerProfile* profile_to_use = PlayerManager::getCurrentPlayer();
|
PlayerProfile* profile_to_use = PlayerManager::getCurrentPlayer();
|
||||||
assert(profile_to_use);
|
assert(profile_to_use);
|
||||||
@ -109,7 +108,7 @@ void StartGameProtocol::setup()
|
|||||||
race_manager->setLocalKartInfo(new_player_id,
|
race_manager->setLocalKartInfo(new_player_id,
|
||||||
profile->getKartName());
|
profile->getKartName());
|
||||||
NetworkWorld::getInstance()->setSelfKart(profile->getKartName());
|
NetworkWorld::getInstance()->setSelfKart(profile->getKartName());
|
||||||
} // if is_me
|
} // if is_local
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StateManager::get()->createActivePlayer( NULL, NULL );
|
StateManager::get()->createActivePlayer( NULL, NULL );
|
||||||
|
@ -215,6 +215,9 @@ void STKHost::create()
|
|||||||
*/
|
*/
|
||||||
STKHost::STKHost(uint32_t server_id, uint32_t host_id)
|
STKHost::STKHost(uint32_t server_id, uint32_t host_id)
|
||||||
{
|
{
|
||||||
|
// Will be overwritten with the correct value once a connection with the
|
||||||
|
// server is made.
|
||||||
|
m_host_id = 0;
|
||||||
init();
|
init();
|
||||||
|
|
||||||
m_network = new Network(/*peer_count*/1, /*channel_limit*/2,
|
m_network = new Network(/*peer_count*/1, /*channel_limit*/2,
|
||||||
@ -236,6 +239,7 @@ STKHost::STKHost(uint32_t server_id, uint32_t host_id)
|
|||||||
STKHost::STKHost(const irr::core::stringw &server_name)
|
STKHost::STKHost(const irr::core::stringw &server_name)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
|
m_host_id = 0; // indicates a server host.
|
||||||
|
|
||||||
ENetAddress addr;
|
ENetAddress addr;
|
||||||
addr.host = STKHost::HOST_ANY;
|
addr.host = STKHost::HOST_ANY;
|
||||||
|
@ -74,6 +74,9 @@ private:
|
|||||||
/** The list of peers connected to this instance. */
|
/** The list of peers connected to this instance. */
|
||||||
std::vector<STKPeer*> m_peers;
|
std::vector<STKPeer*> m_peers;
|
||||||
|
|
||||||
|
/** Host id of this host. */
|
||||||
|
uint8_t m_host_id;
|
||||||
|
|
||||||
/** Stores data about the online game to play. */
|
/** Stores data about the online game to play. */
|
||||||
GameSetup* m_game_setup;
|
GameSetup* m_game_setup;
|
||||||
|
|
||||||
@ -197,7 +200,6 @@ public:
|
|||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
/** Returns a const reference to the list of peers. */
|
/** Returns a const reference to the list of peers. */
|
||||||
const std::vector<STKPeer*> &getPeers() { return m_peers; }
|
const std::vector<STKPeer*> &getPeers() { return m_peers; }
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
/** Returns the number of currently connected peers. */
|
/** Returns the number of currently connected peers. */
|
||||||
unsigned int getPeerCount() { return (int)m_peers.size(); }
|
unsigned int getPeerCount() { return (int)m_peers.size(); }
|
||||||
@ -214,6 +216,12 @@ public:
|
|||||||
return m_is_registered;
|
return m_is_registered;
|
||||||
} // isRegistered
|
} // isRegistered
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
/** Sets the global host id of this host. */
|
||||||
|
void setMyHostId(uint8_t my_host_id) { m_host_id = my_host_id; }
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
/** Returns the host id of this host. */
|
||||||
|
uint8_t getMyHostId() const { return m_host_id; }
|
||||||
|
// --------------------------------------------------------------------
|
||||||
}; // class STKHost
|
}; // class STKHost
|
||||||
|
|
||||||
#endif // STK_HOST_HPP
|
#endif // STK_HOST_HPP
|
||||||
|
Loading…
x
Reference in New Issue
Block a user