From c1560ddb02cecc5db498fda27a122c640c612ae6 Mon Sep 17 00:00:00 2001 From: hiker Date: Wed, 6 Jan 2016 08:10:03 +1100 Subject: [PATCH] Renamed getPlayerID() to getGlobalPlayerId(); started to refactor StartGameProtocol. --- src/network/game_setup.cpp | 9 ++-- src/network/network_player_profile.cpp | 2 +- src/network/network_player_profile.hpp | 10 ++-- .../protocols/game_events_protocol.cpp | 2 +- .../protocols/server_lobby_room_protocol.cpp | 20 +++---- src/network/protocols/start_game_protocol.cpp | 53 ++++++++++--------- src/states_screens/networking_lobby.cpp | 2 +- 7 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/network/game_setup.cpp b/src/network/game_setup.cpp index 8f8a866c2..0c9b4ac6d 100644 --- a/src/network/game_setup.cpp +++ b/src/network/game_setup.cpp @@ -52,7 +52,7 @@ void GameSetup::addPlayer(NetworkPlayerProfile* profile) { m_players.push_back(profile); Log::info("GameSetup", "New player in the game setup. Race id : %d.", - profile->getPlayerID()); + profile->getGlobalPlayerId()); } // addPlayer //----------------------------------------------------------------------------- @@ -106,7 +106,7 @@ void GameSetup::setPlayerKart(uint8_t player_id, const std::string &kart_name) bool found = false; for (unsigned int i = 0; i < m_players.size(); i++) { - if (m_players[i]->getPlayerID() == player_id) + if (m_players[i]->getGlobalPlayerId() == player_id) { m_players[i]->setKartName(kart_name); Log::info("GameSetup::setPlayerKart", "Player %d took kart %s", @@ -130,7 +130,8 @@ void GameSetup::bindKartsToProfiles() for (unsigned int i = 0; i < m_players.size(); i++) { Log::info("GameSetup", "Player %d has id %d and kart %s", i, - m_players[i]->getPlayerID(), m_players[i]->getKartName().c_str()); + m_players[i]->getGlobalPlayerId(), + m_players[i]->getKartName().c_str()); } for (unsigned int i = 0; i < karts.size(); i++) { @@ -166,7 +167,7 @@ const NetworkPlayerProfile* GameSetup::getProfile(uint8_t player_id) { for (unsigned int i = 0; i < m_players.size(); i++) { - if (m_players[i]->getPlayerID()== player_id) + if (m_players[i]->getGlobalPlayerId()== player_id) { return m_players[i]; } diff --git a/src/network/network_player_profile.cpp b/src/network/network_player_profile.cpp index 2d7035e9e..b0a6d7643 100644 --- a/src/network/network_player_profile.cpp +++ b/src/network/network_player_profile.cpp @@ -26,7 +26,7 @@ NetworkPlayerProfile::NetworkPlayerProfile(int race_player_id, const irr::core::stringw &name) { - m_race_player_id = race_player_id; + m_global_player_id = race_player_id; m_kart_name = ""; m_world_kart_id = 0; m_per_player_difficulty = PLAYER_DIFFICULTY_NORMAL; diff --git a/src/network/network_player_profile.hpp b/src/network/network_player_profile.hpp index d07d9cc9c..c79a77095 100644 --- a/src/network/network_player_profile.hpp +++ b/src/network/network_player_profile.hpp @@ -40,7 +40,7 @@ private: /** The unique id of the player for this race. The number is assigned * by the server (and it might not be the index of this player in the * peer list. */ - uint8_t m_race_player_id; + uint8_t m_global_player_id; /** The selected kart id. */ std::string m_kart_name; @@ -59,11 +59,11 @@ public: ~NetworkPlayerProfile(); // ------------------------------------------------------------------------ - /** Sets the player id of this player. */ - void setPlayerID(int player_id) { m_race_player_id = player_id; } + /** Sets the global player id of this player. */ + void setGlobalPlayerId(int player_id) { m_global_player_id = player_id; } // ------------------------------------------------------------------------ - /** Returns the loca ID of this player in this race. */ - int getPlayerID() const { return m_race_player_id; } + /** Returns the global ID of this player in this race. */ + int getGlobalPlayerId() const { return m_global_player_id; } // ------------------------------------------------------------------------ /** Sets the kart name for this player. */ void setKartName(const std::string &kart_name) { m_kart_name = kart_name; } diff --git a/src/network/protocols/game_events_protocol.cpp b/src/network/protocols/game_events_protocol.cpp index 1cc06c99e..22f625bdf 100644 --- a/src/network/protocols/game_events_protocol.cpp +++ b/src/network/protocols/game_events_protocol.cpp @@ -105,7 +105,7 @@ void GameEventsProtocol::collectedItem(Item* item, AbstractKart* kart) + (kart->getPowerup()->getNum() & 0x0f); ns.ai8(0x01).ai32(item->getItemId()).ai8(powerup) - .ai8(player_profile->getPlayerID()); + .ai8(player_profile->getGlobalPlayerId()); ProtocolManager::getInstance()->sendMessage(this, peers[i], ns, /*reliable*/true); Log::info("GameEventsProtocol", diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index 2577bd1af..bfc76347e 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -396,10 +396,10 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event) { NetworkString msg(3); msg.ai8(LE_PLAYER_DISCONNECTED).ai8(1) - .ai8(peer->getPlayerProfile()->getPlayerID()); + .ai8(peer->getPlayerProfile()->getGlobalPlayerId()); sendMessage(msg); Log::info("ServerLobbyRoomProtocol", "Player disconnected : id %d", - peer->getPlayerProfile()->getPlayerID()); + peer->getPlayerProfile()->getGlobalPlayerId()); m_setup->removePlayer(peer->getPlayerProfile()); // Remove the profile from the peer (to avoid double free) peer->setPlayerProfile(NULL); @@ -483,7 +483,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) // to the list of players later, so the new player's info is not included) for (unsigned int i = 0; i < players.size(); i++) { - message_ack.ai8(1).ai8(players[i]->getPlayerID()) + message_ack.ai8(1).ai8(players[i]->getGlobalPlayerId()) .encodeString(players[i]->getName()); } sendMessage(peer, message_ack); @@ -555,7 +555,7 @@ void ServerLobbyRoomProtocol::kartSelectionRequested(Event* event) // send a kart update to everyone NetworkString answer(3+1+kart_name.size()); // kart update (3), 1, race id - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); answer.ai8(LE_KART_SELECTION_UPDATE).ai8(1).ai8(player_id) .encodeString(kart_name); sendMessage(answer); @@ -582,7 +582,7 @@ void ServerLobbyRoomProtocol::playerMajorVote(Event* event) return; if (!isByteCorrect(event, 5, 4)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); uint32_t major = data.getUInt32(6); m_setup->getRaceConfig()->setPlayerMajorVote(player_id, major); // Send the vote to everybody (including the sender) @@ -615,7 +615,7 @@ void ServerLobbyRoomProtocol::playerRaceCountVote(Event* event) return; if (!isByteCorrect(event, 5, 1)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); m_setup->getRaceConfig()->setPlayerRaceCountVote(player_id, data[6]); // Send the vote to everybody (including the sender) data.removeFront(5); // remove the token @@ -647,7 +647,7 @@ void ServerLobbyRoomProtocol::playerMinorVote(Event* event) return; if (!isByteCorrect(event, 5, 4)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); uint32_t minor = data.getUInt32(6); m_setup->getRaceConfig()->setPlayerMinorVote(player_id, minor); // Send the vote to everybody (including the sender) @@ -682,7 +682,7 @@ void ServerLobbyRoomProtocol::playerTrackVote(Event* event) int N = data.decodeString(5, &track_name); if (!isByteCorrect(event, N+5, 1)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); m_setup->getRaceConfig()->setPlayerTrackVote(player_id, track_name, data[N+6]); // Send the vote to everybody (including the sender) data.removeFront(5); // remove the token @@ -718,7 +718,7 @@ void ServerLobbyRoomProtocol::playerReversedVote(Event* event) return; if (!isByteCorrect(event, 7, 1)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); m_setup->getRaceConfig()->setPlayerReversedVote(player_id, data[6]!=0, data[8]); // Send the vote to everybody (including the sender) @@ -753,7 +753,7 @@ void ServerLobbyRoomProtocol::playerLapsVote(Event* event) return; if (!isByteCorrect(event, 7, 1)) return; - uint8_t player_id = peer->getPlayerProfile()->getPlayerID(); + uint8_t player_id = peer->getPlayerProfile()->getGlobalPlayerId(); m_setup->getRaceConfig()->setPlayerLapsVote(player_id, data[6], data[8]); // Send the vote to everybody (including the sender) data.removeFront(5); // remove the token diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index 7dcc7abdf..d38cfbe48 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -41,12 +41,28 @@ StartGameProtocol::~StartGameProtocol() } // ~StartGameProtocol // ---------------------------------------------------------------------------- +/** Setup the actual game. It first starts the SynchronisationProtocol, + * and then + */ void StartGameProtocol::setup() { m_state = NONE; m_ready_count = 0; m_ready = false; Log::info("SynchronizationProtocol", "Ready !"); + + Protocol *p = new SynchronizationProtocol(); + p->requestStart(); + Log::info("StartGameProtocol", "SynchronizationProtocol started."); + + // Race startup sequence + // --------------------- + // builds it and starts + NetworkWorld::getInstance()->start(); + race_manager->setNumKarts(m_game_setup->getPlayerCount()); + race_manager->setNumPlayers(m_game_setup->getPlayerCount()); + race_manager->setNumLocalPlayers(1); + } // setup // ---------------------------------------------------------------------------- @@ -101,7 +117,7 @@ bool StartGameProtocol::notifyEventAsynchronous(Event* event) // ---------------------------------------------------------------------------- bool compareKarts(NetworkPlayerProfile* a, NetworkPlayerProfile* b) { - return (a->getPlayerID() < b->getPlayerID()); + return (a->getGlobalPlayerId() < b->getGlobalPlayerId()); } // compareKarts // ---------------------------------------------------------------------------- @@ -109,34 +125,23 @@ void StartGameProtocol::update() { if (m_state == NONE) { - Protocol *p = new SynchronizationProtocol(); - p->requestStart(); - Log::info("StartGameProtocol", "SynchronizationProtocol started."); - - // Race startup sequence - // --------------------- - // builds it and starts - NetworkWorld::getInstance()->start(); - race_manager->setNumKarts(m_game_setup->getPlayerCount()); - race_manager->setNumPlayers(m_game_setup->getPlayerCount()); - race_manager->setNumLocalPlayers(1); std::vector players = m_game_setup->getPlayers(); std::sort(players.begin(), players.end(), compareKarts); // have to add self first GameSetup *setup = STKHost::get()->getGameSetup(); for (unsigned int i = 0; i < players.size(); i++) { - bool is_me = setup->isLocalMaster(players[i]->getPlayerID()); + bool is_me = setup->isLocalMaster(players[i]->getGlobalPlayerId()); if (is_me) { NetworkPlayerProfile* profile = players[i]; - RemoteKartInfo rki(profile->getPlayerID(), profile->getKartName(), + RemoteKartInfo rki(profile->getGlobalPlayerId(), profile->getKartName(), profile->getName(), - profile->getPlayerID(), !is_me); + profile->getGlobalPlayerId(), !is_me); rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty()); - rki.setGlobalPlayerId(profile->getPlayerID()); + rki.setGlobalPlayerId(profile->getGlobalPlayerId()); rki.setLocalPlayerId(is_me?0:1); - rki.setHostId(profile->getPlayerID()); + rki.setHostId(profile->getGlobalPlayerId()); PlayerProfile* profile_to_use = PlayerManager::getCurrentPlayer(); assert(profile_to_use); InputDevice* device = input_manager->getDeviceManager() @@ -165,22 +170,22 @@ void StartGameProtocol::update() } for (unsigned int i = 0; i < players.size(); i++) { - bool is_me = setup->isLocalMaster(players[i]->getPlayerID()); + bool is_me = setup->isLocalMaster(players[i]->getGlobalPlayerId()); NetworkPlayerProfile* profile = players[i]; - RemoteKartInfo rki(profile->getPlayerID(), profile->getKartName(), + RemoteKartInfo rki(profile->getGlobalPlayerId(), profile->getKartName(), profile->getName(), - profile->getPlayerID(), !is_me); + profile->getGlobalPlayerId(), !is_me); rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty()); - rki.setGlobalPlayerId(profile->getPlayerID()); + rki.setGlobalPlayerId(profile->getGlobalPlayerId()); // on the server, the race id must be the local one. rki.setLocalPlayerId(NetworkConfig::get()->isServer() - ? profile->getPlayerID() + ? profile->getGlobalPlayerId() : (is_me ? 0 : 1) ); - rki.setHostId(profile->getPlayerID()); + rki.setHostId(profile->getGlobalPlayerId()); Log::info("StartGameProtocol", "Creating kart %s for Player#%d with race_id %d", profile->getKartName().c_str(), i, - profile->getPlayerID()); + profile->getGlobalPlayerId()); if (!is_me) { StateManager::get()->createActivePlayer( NULL, NULL ); diff --git a/src/states_screens/networking_lobby.cpp b/src/states_screens/networking_lobby.cpp index e11640162..db592438e 100644 --- a/src/states_screens/networking_lobby.cpp +++ b/src/states_screens/networking_lobby.cpp @@ -180,7 +180,7 @@ void NetworkingLobby::onDialogClose() // ---------------------------------------------------------------------------- void NetworkingLobby::addPlayer(NetworkPlayerProfile *profile) { - m_player_list->addItem(StringUtils::toString(profile->getPlayerID()), + m_player_list->addItem(StringUtils::toString(profile->getGlobalPlayerId()), profile->getName()); } // addPlayer