From 1caba55886d3e9daef7d7bc6bf011f59e933e8b2 Mon Sep 17 00:00:00 2001 From: wardje Date: Fri, 22 Jun 2012 17:57:22 +0000 Subject: [PATCH] Remove gameslots when they don't have a player Gameslots were not getting removed when players got removed. Add a check when the player options screen gets torn down. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@11325 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/challenges/unlock_manager.cpp | 47 ++++++++++++++++++- src/challenges/unlock_manager.hpp | 1 + src/states_screens/options_screen_players.cpp | 5 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/challenges/unlock_manager.cpp b/src/challenges/unlock_manager.cpp index cf15ec410..6ff204c4d 100644 --- a/src/challenges/unlock_manager.cpp +++ b/src/challenges/unlock_manager.cpp @@ -278,7 +278,9 @@ void UnlockManager::save() } // save //----------------------------------------------------------------------------- - +/** Creates a gameslot for players that don't have one yet + * \return true if any were created + */ bool UnlockManager::createSlotsIfNeeded() { bool something_changed = false; @@ -318,7 +320,48 @@ bool UnlockManager::createSlotsIfNeeded() } return something_changed; -} +} // UnlockManager::createSlotsIfNeeded + +//----------------------------------------------------------------------------- +/** Removes gameslots that refer to a non-existing player. + * \return true if any were removed + */ +bool UnlockManager::deleteSlotsIfNeeded() +{ + bool changed = false; + std::map::iterator it = m_game_slots.begin(); + while (it != m_game_slots.end()) + { + bool found = false; + const int playerAmount = UserConfigParams::m_all_players.size(); + for (int i = 0; i < playerAmount; i++) + { + if (it->second->getPlayerID() == + UserConfigParams::m_all_players[i].getUniqueID()) + { + found = true; + break; + } + } // for players + + if (!found) + { +#ifdef DEBUG + printf("Deleting gameslot %s, no player found.\n", + it->second->getPlayerID().c_str()); +#endif + // Iterators aren't invalidated this way + m_game_slots.erase(it++); + changed = true; + } + else + { + ++it; + } + } // for gameslots + + return changed; +} // UnlockManager::deleteSlotsIfNeeded //----------------------------------------------------------------------------- void UnlockManager::playLockSound() const diff --git a/src/challenges/unlock_manager.hpp b/src/challenges/unlock_manager.hpp index c2ab8b103..97ef0072d 100644 --- a/src/challenges/unlock_manager.hpp +++ b/src/challenges/unlock_manager.hpp @@ -64,6 +64,7 @@ public: void addChallenge (const std::string& filename); void save (); bool createSlotsIfNeeded(); + bool deleteSlotsIfNeeded(); const ChallengeData *getChallenge (const std::string& id); diff --git a/src/states_screens/options_screen_players.cpp b/src/states_screens/options_screen_players.cpp index 091e3b527..cb9c02d13 100644 --- a/src/states_screens/options_screen_players.cpp +++ b/src/states_screens/options_screen_players.cpp @@ -141,8 +141,9 @@ void OptionsScreenPlayers::tearDown() { Screen::tearDown(); user_config->saveConfig(); - bool changed = unlock_manager->createSlotsIfNeeded(); - if (changed) unlock_manager->save(); + bool created = unlock_manager->createSlotsIfNeeded(); + bool removed = unlock_manager->deleteSlotsIfNeeded(); + if (created || removed) unlock_manager->save(); } // tearDown // -----------------------------------------------------------------------------