Skip to content

Commit

Permalink
Fix crash when deleting the current player (also prevent the user from
Browse files Browse the repository at this point in the history
deleting the last user, since STK (after login) needs a current player).
  • Loading branch information
hiker committed May 24, 2014
1 parent 6ca4091 commit fc32fcc
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/config/player_manager.cpp
Expand Up @@ -366,6 +366,20 @@ void PlayerManager::addDefaultPlayer()
m_all_players.push_back(new Online::OnlinePlayerProfile(_LTR("Guest"), /*guest*/true));
} // addDefaultPlayer

// ----------------------------------------------------------------------------
/** Returns the number of 'real' (non-guest) players.
*/
unsigned int PlayerManager::getNumNonGuestPlayers() const
{
unsigned int count=0;
const PlayerProfile *player;
for_in(player, m_all_players)
{
if(!player->isGuestAccount()) count ++;
}
return count;
} // getNumNonGuestPlayers

// ----------------------------------------------------------------------------
/** This returns a unique id. This is 1 + largest id used so far.
*/
Expand Down
1 change: 1 addition & 0 deletions src/config/player_manager.hpp
Expand Up @@ -96,6 +96,7 @@ class PlayerManager : public NoCopy
void setCurrentPlayer(PlayerProfile *player);
const PlayerProfile *getPlayerById(unsigned int id);
void enforceCurrentPlayer();
unsigned int getNumNonGuestPlayers() const;
static void setUserDetails(Online::HTTPRequest *request,
const std::string &action,
const std::string &php_name = "");
Expand Down
24 changes: 24 additions & 0 deletions src/states_screens/user_screen.cpp
Expand Up @@ -483,6 +483,15 @@ void BaseUserScreen::newUserAdded(const irr::core::stringw &local_name,
*/
void BaseUserScreen::deletePlayer()
{
// Check that there is at least one player left: we need to have a
// valid current player, so the last player can not be deleted.
if(PlayerManager::get()->getNumNonGuestPlayers()==1)
{
m_info_widget->setText("You can't delete the only player.", true);
m_info_widget->setErrorColor();
return;
}

PlayerProfile *player = getSelectedPlayer();
irr::core::stringw message =
//I18N: In the player info dialog (when deleting)
Expand Down Expand Up @@ -517,6 +526,21 @@ void BaseUserScreen::doDeletePlayer()
PlayerProfile *player = getSelectedPlayer();
PlayerManager::get()->deletePlayer(player);
GUIEngine::ModalDialog::dismiss();

// Special case: the current player was deleted. We have to make sure
// that there is still a current player (all of STK depends on that).
if(!PlayerManager::getCurrentPlayer())
{
for(unsigned int i=0; i<PlayerManager::get()->getNumPlayers(); i++)
{
PlayerProfile *player = PlayerManager::get()->getPlayer(i);
if(!player->isGuestAccount())
{
PlayerManager::get()->setCurrentPlayer(player);
break;
}
}
}
init();
} // doDeletePlayer

Expand Down

0 comments on commit fc32fcc

Please sign in to comment.