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
This commit is contained in:
wardje 2012-06-22 17:57:22 +00:00
parent 97c5153832
commit 1caba55886
3 changed files with 49 additions and 4 deletions

View File

@ -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<std::string, GameSlot*>::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

View File

@ -64,6 +64,7 @@ public:
void addChallenge (const std::string& filename);
void save ();
bool createSlotsIfNeeded();
bool deleteSlotsIfNeeded();
const ChallengeData *getChallenge (const std::string& id);

View File

@ -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
// -----------------------------------------------------------------------------