2014-02-05 22:43:47 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2012-2014 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "config/player_manager.hpp"
|
|
|
|
|
2014-02-09 07:22:45 -05:00
|
|
|
#include "achievements/achievements_manager.hpp"
|
2014-02-17 04:10:29 -05:00
|
|
|
#include "config/player_profile.hpp"
|
2014-02-05 22:43:47 -05:00
|
|
|
#include "io/file_manager.hpp"
|
|
|
|
#include "io/utf_writer.hpp"
|
|
|
|
#include "io/xml_node.hpp"
|
|
|
|
#include "utils/log.hpp"
|
|
|
|
#include "utils/translation.hpp"
|
|
|
|
|
|
|
|
PlayerManager *PlayerManager::m_player_manager = NULL;
|
|
|
|
|
2014-02-09 07:22:45 -05:00
|
|
|
|
|
|
|
/** Create the instance of the player manager.
|
|
|
|
* Also make sure that at least one player is defined (and if not,
|
|
|
|
* create a default player and a guest player).
|
|
|
|
*/
|
|
|
|
void PlayerManager::create()
|
|
|
|
{
|
|
|
|
assert(!m_player_manager);
|
|
|
|
m_player_manager = new PlayerManager();
|
|
|
|
if(m_player_manager->getNumPlayers() == 0)
|
|
|
|
{
|
|
|
|
m_player_manager->addDefaultPlayer();
|
|
|
|
m_player_manager->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // create
|
|
|
|
|
|
|
|
// ============================================================================
|
2014-02-05 22:43:47 -05:00
|
|
|
/** Constructor.
|
|
|
|
*/
|
|
|
|
PlayerManager::PlayerManager()
|
|
|
|
{
|
2014-02-10 16:12:10 -05:00
|
|
|
m_current_player = NULL;
|
2014-02-09 07:22:45 -05:00
|
|
|
load();
|
2014-02-05 22:43:47 -05:00
|
|
|
} // PlayerManager
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Destructor.
|
|
|
|
*/
|
|
|
|
PlayerManager::~PlayerManager()
|
|
|
|
{
|
|
|
|
save();
|
|
|
|
} // ~PlayerManager
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void PlayerManager::load()
|
|
|
|
{
|
|
|
|
std::string filename = file_manager->getUserConfigFile("players.xml");
|
|
|
|
|
|
|
|
const XMLNode *players = file_manager->createXMLTree(filename);
|
2014-02-09 07:22:45 -05:00
|
|
|
if(!players)
|
|
|
|
{
|
|
|
|
Log::info("player_manager", "A new players.xml file will be created.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(players->getName()!="players")
|
2014-02-05 22:43:47 -05:00
|
|
|
{
|
|
|
|
Log::info("player_manager", "The players.xml file is invalid.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 07:22:45 -05:00
|
|
|
m_current_player = NULL;
|
2014-02-05 22:43:47 -05:00
|
|
|
for(unsigned int i=0; i<players->getNumNodes(); i++)
|
|
|
|
{
|
|
|
|
const XMLNode *player_xml = players->getNode(i);
|
2014-02-09 07:22:45 -05:00
|
|
|
PlayerProfile *player = new PlayerProfile(player_xml);
|
|
|
|
m_all_players.push_back(player);
|
2014-02-27 20:36:53 -05:00
|
|
|
if(player->isDefault()){
|
2014-02-09 07:22:45 -05:00
|
|
|
m_current_player = player;
|
2014-02-27 20:36:53 -05:00
|
|
|
}
|
2014-02-05 22:43:47 -05:00
|
|
|
}
|
2014-02-10 00:54:31 -05:00
|
|
|
m_all_players.insertionSort(/*start*/0, /*desc*/true);
|
2014-02-09 07:22:45 -05:00
|
|
|
|
2014-02-27 20:36:53 -05:00
|
|
|
if(!m_current_player) // if there is no default player assigned set the current player to guest
|
2014-02-09 07:22:45 -05:00
|
|
|
{
|
2014-02-27 20:36:53 -05:00
|
|
|
m_current_player = getGuestPlayer();
|
|
|
|
m_current_player->setDefault(true);
|
2014-02-09 07:22:45 -05:00
|
|
|
}
|
|
|
|
|
2014-02-21 00:54:52 -05:00
|
|
|
delete players;
|
2014-02-05 22:43:47 -05:00
|
|
|
} // load
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void PlayerManager::save()
|
|
|
|
{
|
|
|
|
std::string filename = file_manager->getUserConfigFile("players.xml");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
UTFWriter players_file(filename.c_str());
|
|
|
|
|
|
|
|
players_file << L"<?xml version=\"1.0\"?>\n";
|
|
|
|
players_file << L"<players version=\"1\" >\n";
|
|
|
|
|
|
|
|
PlayerProfile *player;
|
|
|
|
for_in(player, m_all_players)
|
|
|
|
{
|
|
|
|
player->save(players_file);
|
|
|
|
}
|
|
|
|
players_file << L"</players>\n";
|
|
|
|
players_file.close();
|
|
|
|
}
|
|
|
|
catch (std::runtime_error& e)
|
|
|
|
{
|
|
|
|
Log::error("PlayerManager", "Failed to write config to %s.",
|
|
|
|
filename.c_str());
|
|
|
|
Log::error("PlayerManager", "Error: %s", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // save
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a new player to the list of all players.
|
|
|
|
* \param name Name of the new player.
|
|
|
|
*/
|
|
|
|
void PlayerManager::addNewPlayer(const core::stringw& name)
|
|
|
|
{
|
|
|
|
m_all_players.push_back( new PlayerProfile(name) );
|
|
|
|
} // addNewPlayer
|
|
|
|
|
2014-02-06 07:08:55 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void PlayerManager::deletePlayer(PlayerProfile *player)
|
|
|
|
{
|
|
|
|
m_all_players.erase(player);
|
|
|
|
} // deletePlayer
|
|
|
|
|
2014-02-05 22:43:47 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void PlayerManager::addDefaultPlayer()
|
|
|
|
{
|
|
|
|
std::string username = "unnamed player";
|
|
|
|
|
|
|
|
if(getenv("USERNAME")!=NULL) // for windows
|
|
|
|
username = getenv("USERNAME");
|
|
|
|
else if(getenv("USER")!=NULL) // Linux, Macs
|
|
|
|
username = getenv("USER");
|
|
|
|
else if(getenv("LOGNAME")!=NULL) // Linux, Macs
|
|
|
|
username = getenv("LOGNAME");
|
|
|
|
|
|
|
|
// Set the name as the default name for all players.
|
|
|
|
m_all_players.push_back(new PlayerProfile(username.c_str()) );
|
|
|
|
|
|
|
|
// add default guest player
|
2014-02-10 16:12:10 -05:00
|
|
|
m_all_players.push_back( new PlayerProfile(_LTR("Guest"), /*guest*/true) );
|
2014-02-05 22:43:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
} // addDefaultPlayer
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** This returns a unique id. This is 1 + larger id so far used.
|
|
|
|
*/
|
|
|
|
unsigned int PlayerManager::getUniqueId() const
|
|
|
|
{
|
|
|
|
unsigned int max_id=0;
|
|
|
|
const PlayerProfile *player;
|
|
|
|
for_in(player, m_all_players)
|
|
|
|
{
|
|
|
|
if(player->getUniqueID()>max_id)
|
|
|
|
max_id = player->getUniqueID();
|
|
|
|
}
|
|
|
|
return max_id+1;
|
|
|
|
} // getUniqueId
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2014-02-06 07:08:55 -05:00
|
|
|
const PlayerProfile *PlayerManager::getPlayerById(unsigned int id)
|
|
|
|
{
|
|
|
|
const PlayerProfile *player;
|
|
|
|
for_in(player, m_all_players)
|
|
|
|
{
|
|
|
|
if(player->getUniqueID()==id)
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
} // getPlayerById
|
|
|
|
|
2014-02-27 20:36:53 -05:00
|
|
|
|
|
|
|
PlayerProfile* PlayerManager::getGuestPlayer(){
|
|
|
|
|
|
|
|
PlayerProfile *player;
|
|
|
|
for_in(player, m_all_players)
|
|
|
|
{
|
|
|
|
if(player->isGuestAccount())
|
|
|
|
{
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
player = new PlayerProfile(_LTR("Guest"), /*guest*/true);
|
|
|
|
m_all_players.push_back( player );
|
|
|
|
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2014-02-06 07:08:55 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
PlayerProfile *PlayerManager::getPlayer(const irr::core::stringw &name)
|
|
|
|
{
|
|
|
|
PlayerProfile *player;
|
|
|
|
for_in(player, m_all_players)
|
|
|
|
{
|
|
|
|
if(player->getName()==name)
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
} // getPlayer
|
2014-02-05 22:43:47 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-02-27 20:36:53 -05:00
|
|
|
void PlayerManager::setCurrentPlayer(PlayerProfile *player, bool remember_me)
|
2014-02-09 07:22:45 -05:00
|
|
|
{
|
|
|
|
// Reset current default player
|
|
|
|
if(m_current_player)
|
|
|
|
m_current_player->setDefault(false);
|
|
|
|
m_current_player = player;
|
2014-02-27 20:36:53 -05:00
|
|
|
m_current_player->setDefault(remember_me);
|
2014-02-11 06:35:23 -05:00
|
|
|
m_current_player->computeActive();
|
2014-02-27 20:36:53 -05:00
|
|
|
m_remember_me = false;
|
2014-02-09 07:22:45 -05:00
|
|
|
} // setCurrentPlayer
|
|
|
|
|
2014-02-05 22:43:47 -05:00
|
|
|
// ----------------------------------------------------------------------------
|