From 78b73aa3c83bf3543b8ffb0c3f7fad85e97461ec Mon Sep 17 00:00:00 2001 From: hiker Date: Thu, 6 Feb 2014 14:43:47 +1100 Subject: [PATCH] Added new player manager, and started to replace the saved data from user_config with player manager. --- sources.cmake | 6 +- src/achievements/achievements_manager.cpp | 8 +- src/achievements/achievements_manager.hpp | 4 +- src/achievements/achievements_slot.cpp | 8 +- src/achievements/achievements_slot.hpp | 10 +- src/challenges/game_slot.cpp | 3 +- src/challenges/game_slot.hpp | 16 +- src/challenges/unlock_manager.cpp | 16 +- src/challenges/unlock_manager.hpp | 8 +- src/config/player.cpp | 93 +++++------- src/config/player.hpp | 55 +++---- src/config/player_manager.cpp | 141 ++++++++++++++++++ src/config/player_manager.hpp | 78 ++++++++++ src/config/saved_grand_prix.cpp | 7 +- src/config/saved_grand_prix.hpp | 8 +- src/config/user_config.cpp | 32 ---- src/config/user_config.hpp | 1 - src/main.cpp | 12 +- src/modes/easter_egg_hunt.hpp | 0 .../dialogs/enter_player_name_dialog.cpp | 14 +- src/states_screens/options_screen_players.cpp | 2 +- 21 files changed, 345 insertions(+), 177 deletions(-) create mode 100644 src/config/player_manager.cpp create mode 100644 src/config/player_manager.hpp mode change 100755 => 100644 src/modes/easter_egg_hunt.hpp diff --git a/sources.cmake b/sources.cmake index 71097b569..d499bfebd 100644 --- a/sources.cmake +++ b/sources.cmake @@ -23,6 +23,7 @@ src/challenges/game_slot.cpp src/challenges/unlock_manager.cpp src/config/device_config.cpp src/config/player.cpp +src/config/player_manager.cpp src/config/saved_grand_prix.cpp src/config/stk_config.cpp src/config/user_config.cpp @@ -101,8 +102,8 @@ src/input/input_manager.cpp src/input/wiimote.cpp src/input/wiimote_manager.cpp src/io/file_manager.cpp +src/io/utf_writer.cpp src/io/xml_node.cpp -src/io/xml_writer.cpp src/items/attachment.cpp src/items/attachment_manager.cpp src/items/bowling.cpp @@ -351,6 +352,7 @@ src/challenges/game_slot.hpp src/challenges/unlock_manager.hpp src/config/device_config.hpp src/config/player.hpp +src/config/player_manager.hpp src/config/saved_grand_prix.hpp src/config/stk_config.hpp src/config/user_config.hpp @@ -428,8 +430,8 @@ src/input/input_manager.hpp src/input/wiimote.hpp src/input/wiimote_manager.hpp src/io/file_manager.hpp +src/io/utf_writer.hpp src/io/xml_node.hpp -src/io/xml_writer.hpp src/items/attachment.hpp src/items/attachment_manager.hpp src/items/attachment_plugin.hpp diff --git a/src/achievements/achievements_manager.cpp b/src/achievements/achievements_manager.cpp index 661c31a98..7fb1345f3 100644 --- a/src/achievements/achievements_manager.cpp +++ b/src/achievements/achievements_manager.cpp @@ -138,7 +138,7 @@ void AchievementsManager::parseUserConfigFile() } // load -AchievementsSlot * AchievementsManager::createNewSlot(std::string id, bool online) +AchievementsSlot * AchievementsManager::createNewSlot(unsigned int id, bool online) { AchievementsSlot* slot = new AchievementsSlot(id, online); m_slots.push_back(slot); @@ -207,7 +207,7 @@ void AchievementsManager::onRaceEnd() } // ============================================================================ -AchievementsSlot * AchievementsManager::getSlot(const std::string & id, bool online) +AchievementsSlot * AchievementsManager::getSlot(unsigned int id, bool online) { for(unsigned int i = 0; i < m_slots.size(); i++) { @@ -224,10 +224,10 @@ void AchievementsManager::updateCurrentPlayer() { if(Online::CurrentUser::get()->isRegisteredUser()) { - m_active_slot = getSlot(StringUtils::toString(Online::CurrentUser::get()->getID()), true); + m_active_slot = getSlot(Online::CurrentUser::get()->getID(), true); if(m_active_slot == NULL) { - m_active_slot = createNewSlot(StringUtils::toString(Online::CurrentUser::get()->getID()), true); + m_active_slot = createNewSlot(Online::CurrentUser::get()->getID(), true); save(); } } diff --git a/src/achievements/achievements_manager.hpp b/src/achievements/achievements_manager.hpp index 8181ba531..9886fe5e3 100644 --- a/src/achievements/achievements_manager.hpp +++ b/src/achievements/achievements_manager.hpp @@ -45,7 +45,7 @@ private : std::map m_achievements_info; AchievementsManager (); ~AchievementsManager (); - AchievementsSlot * createNewSlot(std::string id, bool online); + AchievementsSlot * createNewSlot(unsigned int id, bool online); void parseAssetFile(); void parseUserConfigFile(); @@ -59,7 +59,7 @@ public: void onRaceEnd(); void updateCurrentPlayer(); AchievementsSlot * getActive() const { return m_active_slot; } - AchievementsSlot * getSlot(const std::string & id, bool online); + AchievementsSlot * getSlot(unsigned int id, bool online); void createSlotsIfNeeded(); AchievementInfo * getAchievementInfo(uint32_t id); const std::map & getAllInfo() { return m_achievements_info;} diff --git a/src/achievements/achievements_slot.cpp b/src/achievements/achievements_slot.cpp index ec36d8771..b970d5dfb 100644 --- a/src/achievements/achievements_slot.cpp +++ b/src/achievements/achievements_slot.cpp @@ -63,11 +63,11 @@ AchievementsSlot::AchievementsSlot(const XMLNode * input) } // ============================================================================ -AchievementsSlot::AchievementsSlot(std::string id, bool online) +AchievementsSlot::AchievementsSlot(unsigned int id, bool online) { - m_valid = true; + m_valid = true; m_online = online; - m_id = id; + m_id = id; createFreshSlot(); } @@ -112,7 +112,7 @@ void AchievementsSlot::createFreshSlot() // ============================================================================ void AchievementsSlot::save(std::ofstream & out) { - out << " \n"; std::map::const_iterator i; diff --git a/src/achievements/achievements_slot.hpp b/src/achievements/achievements_slot.hpp index cfb73ab07..21fb10c0b 100644 --- a/src/achievements/achievements_slot.hpp +++ b/src/achievements/achievements_slot.hpp @@ -33,9 +33,9 @@ class AchievementsSlot { private: std::map m_achievements; - bool m_online; - bool m_valid; - std::string m_id; + bool m_online; + bool m_valid; + unsigned int m_id; void createFreshSlot(); void deleteAchievements(); @@ -48,14 +48,14 @@ private: public : AchievementsSlot(const XMLNode * input); - AchievementsSlot(std::string id, bool online); + AchievementsSlot(unsigned int id, bool online); ~AchievementsSlot(); bool isValid() const { return m_valid;} void save(std::ofstream & out); bool isOnline() const {return m_online;} void sync(const std::vector & achieved_ids); void onRaceEnd(); - const std::string & getID() const {return m_id;} + unsigned int getID() const {return m_id;} const std::map & getAllAchievements() {return m_achievements;} Achievement * getAchievement(uint32_t id); }; diff --git a/src/challenges/game_slot.cpp b/src/challenges/game_slot.cpp index 2ac0449ac..469e05964 100644 --- a/src/challenges/game_slot.cpp +++ b/src/challenges/game_slot.cpp @@ -22,7 +22,6 @@ #include "challenges/challenge.hpp" #include "challenges/challenge_data.hpp" #include "challenges/unlock_manager.hpp" -#include "io/xml_writer.hpp" //----------------------------------------------------------------------------- GameSlot::~GameSlot() @@ -238,7 +237,7 @@ void GameSlot::grandPrixFinished() void GameSlot::save(std::ofstream& out, const std::string& name) { - out << " \n"; diff --git a/src/challenges/game_slot.hpp b/src/challenges/game_slot.hpp index 11dedf407..b8319b9eb 100644 --- a/src/challenges/game_slot.hpp +++ b/src/challenges/game_slot.hpp @@ -46,7 +46,7 @@ class GameSlot * are done everywhere, assign a unique ID to each profiler. * Will save much headaches. */ - std::string m_player_unique_id; + unsigned int m_player_unique_id; /** Contains whether each feature of the challenge is locked or unlocked */ std::map m_locked_features; @@ -79,19 +79,19 @@ public: // do NOT attempt to pass 'player_unique_id' by reference here. I don't // know why (compiler bug maybe?) but this screws up everything. Better // pass by copy. - GameSlot(std::string player_unique_id) + GameSlot(unsigned int player_unique_id) { - m_player_unique_id = player_unique_id; - m_points = 0; - m_first_time = true; - m_easy_challenges = 0; + m_player_unique_id = player_unique_id; + m_points = 0; + m_first_time = true; + m_easy_challenges = 0; m_medium_challenges = 0; - m_hard_challenges = 0; + m_hard_challenges = 0; m_current_challenge = NULL; } ~GameSlot(); - const std::string& getPlayerID() const { return m_player_unique_id; } + unsigned int getPlayerID() const { return m_player_unique_id; } const std::string& getKartIdent () const { return m_kart_ident; } void setKartIdent(const std::string& kart_ident) { diff --git a/src/challenges/unlock_manager.cpp b/src/challenges/unlock_manager.cpp index 47099068e..317242ec4 100644 --- a/src/challenges/unlock_manager.cpp +++ b/src/challenges/unlock_manager.cpp @@ -47,7 +47,7 @@ UnlockManager::UnlockManager() // in main). unlock_manager = this; - m_current_game_slot = ""; + m_current_game_slot = 0; m_locked_sound = sfx_manager->createSoundSource("locked"); @@ -94,7 +94,7 @@ UnlockManager::~UnlockManager() } - std::map::iterator it; + std::map::iterator it; for (it = m_game_slots.begin(); it != m_game_slots.end(); it++) { delete it->second; @@ -222,7 +222,7 @@ void UnlockManager::load() root->getNodes("gameslot", xml_game_slots); for (unsigned int n=0; nget("playerID", &player_id)) { Log::warn("unlock_manager", "Found game slot without " @@ -279,7 +279,7 @@ void UnlockManager::save() challenge_file << "\n"; challenge_file << "\n"; - std::map::iterator it; + std::map::iterator it; for (it = m_game_slots.begin(); it != m_game_slots.end(); it++) { std::string name = "unknown player"; @@ -313,7 +313,7 @@ bool UnlockManager::createSlotsIfNeeded() { bool exists = false; - std::map::iterator it; + std::map::iterator it; for (it = m_game_slots.begin(); it != m_game_slots.end(); it++) { GameSlot* curr_slot = it->second; @@ -351,7 +351,7 @@ bool UnlockManager::createSlotsIfNeeded() bool UnlockManager::deleteSlotsIfNeeded() { bool changed = false; - std::map::iterator it = m_game_slots.begin(); + std::map::iterator it = m_game_slots.begin(); while (it != m_game_slots.end()) { bool found = false; @@ -370,7 +370,7 @@ bool UnlockManager::deleteSlotsIfNeeded() { #ifdef DEBUG printf("Deleting gameslot %s, no player found.\n", - it->second->getPlayerID().c_str()); + it->second->getPlayerID()); #endif // Iterators aren't invalidated this way m_game_slots.erase(it++); @@ -424,7 +424,7 @@ void UnlockManager::updateActiveChallengeList() //----------------------------------------------------------------------------- -void UnlockManager::setCurrentSlot(std::string slotid) +void UnlockManager::setCurrentSlot(unsigned int slotid) { m_current_game_slot = slotid; AchievementsManager::get()->updateCurrentPlayer(); diff --git a/src/challenges/unlock_manager.hpp b/src/challenges/unlock_manager.hpp index 093efcc29..dbd630cda 100644 --- a/src/challenges/unlock_manager.hpp +++ b/src/challenges/unlock_manager.hpp @@ -48,12 +48,12 @@ private: typedef std::map AllChallengesType; AllChallengesType m_all_challenges; - std::map m_game_slots; + std::map m_game_slots; void readAllChallengesInDirs(const std::vector* all_dirs); /** ID of the active player */ - std::string m_current_game_slot; + unsigned int m_current_game_slot; friend class GameSlot; @@ -73,7 +73,7 @@ public: /** Eye- (or rather ear-) candy. Play a sound when user tries to access a locked area */ void playLockSound() const; - const std::string& getCurrentSlotID() const { return m_current_game_slot; } + unsigned int getCurrentSlotID() const { return m_current_game_slot; } GameSlot* getCurrentSlot() { @@ -82,7 +82,7 @@ public: } /** \param slotid name of the player */ - void setCurrentSlot(std::string slotid); + void setCurrentSlot(unsigned int slotid); void findWhatWasUnlocked(int pointsBefore, int pointsNow, std::vector& tracks, std::vector& gps); diff --git a/src/config/player.cpp b/src/config/player.cpp index a58ca09cf..37c67eb7c 100644 --- a/src/config/player.cpp +++ b/src/config/player.cpp @@ -16,77 +16,66 @@ // 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.hpp" + +#include "config/player_manager.hpp" +#include "io/xml_node.hpp" +#include "io/utf_writer.hpp" +#include "utils/string_utils.hpp" + +#include #include -#include "config/player.hpp" -#include "utils/string_utils.hpp" -#include - - //------------------------------------------------------------------------------ -PlayerProfile::PlayerProfile(const core::stringw& name) : - m_player_group("Player", "Represents one human player"), - m_name(name, "name", &m_player_group), - m_is_guest_account(false, "guest", &m_player_group), - m_use_frequency(0, "use_frequency", &m_player_group), - m_unique_id("", "unique_id", &m_player_group) +/** Constructor to create a new player that didn't exist before. + * \param name Name of the player. + * \param is_guest True if this is a guest account. +*/ +PlayerProfile::PlayerProfile(const core::stringw& name, bool is_guest) { #ifdef DEBUG m_magic_number = 0xABCD1234; #endif - int64_t unique_id = generateUniqueId(core::stringc(name.c_str()).c_str()); + m_name = name; + m_is_guest_account = is_guest; + m_use_frequency = 0; + m_unique_id = PlayerManager::get()->getUniqueId(); - std::ostringstream to_string; - to_string << std::hex << unique_id; - m_unique_id = to_string.str(); -} +} // PlayerProfile //------------------------------------------------------------------------------ -PlayerProfile::PlayerProfile(const XMLNode* node) : - m_player_group("Player", "Represents one human player"), - m_name("-", "name", &m_player_group), - m_is_guest_account(false, "guest", &m_player_group), - m_use_frequency(0, "use_frequency", &m_player_group), - m_unique_id("", "unique_id", &m_player_group) +/** Constructor to deserialize a player that was saved to a XML file. + * \param node The XML node representing this player. +*/ +PlayerProfile::PlayerProfile(const XMLNode* node) { - //m_player_group.findYourDataInAChildOf(node); - m_name.findYourDataInAnAttributeOf(node); - m_is_guest_account.findYourDataInAnAttributeOf(node); - m_use_frequency.findYourDataInAnAttributeOf(node); - m_unique_id.findYourDataInAnAttributeOf(node); - - if ((std::string)m_unique_id == "") - { - fprintf(stderr, "** WARNING: Player has no unique ID, probably it is from an older STK version\n"); - int64_t unique_id = generateUniqueId(core::stringc(m_name.c_str()).c_str()); - std::ostringstream tostring; - tostring << std::hex << unique_id; - m_unique_id = tostring.str(); - } - + node->get("name", &m_name ); + node->get("guest", &m_is_guest_account); + node->get("use-frequency", &m_use_frequency ); + node->get("unique-id", &m_unique_id ); #ifdef DEBUG m_magic_number = 0xABCD1234; #endif } // PlayerProfile //------------------------------------------------------------------------------ +/** Writes the data for this player to the specified UTFWriter. + * \param out The utf writer to write the data to. + */ +void PlayerProfile::save(UTFWriter &out) +{ + out << L" \n"; +} // save + +//------------------------------------------------------------------------------ +/** Increments how often that account was used. Guest accounts are not counted. + */ void PlayerProfile::incrementUseFrequency() { if (m_is_guest_account) m_use_frequency = -1; else m_use_frequency++; } // incrementUseFrequency - -//------------------------------------------------------------------------------ -int64_t PlayerProfile::generateUniqueId(const char* player_name) -{ - // First create a simple hash based on he player name - int hash = 0; - for (int n=0; player_name[n] != 0; n++) - { - hash += (hash << (hash & 0xF)) ^ player_name[n]; - } - - return ((int64_t)(StkTime::getTimeSinceEpoch()) << 32) | - ((rand() << 16) & 0xFFFF0000) | - hash; -} // generateUniqueId diff --git a/src/config/player.hpp b/src/config/player.hpp index b94d8136e..30f8b16f7 100644 --- a/src/config/player.hpp +++ b/src/config/player.hpp @@ -19,13 +19,17 @@ #ifndef HEADER_PLAYER_HPP #define HEADER_PLAYER_HPP -#include #include "config/user_config.hpp" #include "utils/no_copy.hpp" #include "utils/types.hpp" + #include using namespace irr; +#include + +class UTFWriter; + /** * \brief Class for managing player profiles (name, control configuration, etc.) * A list of all possible players is stored as PlayerProfiles in the user config. @@ -36,43 +40,31 @@ class PlayerProfile : public NoCopy { protected: - /** - * For saving to config file. - * WARNING : m_player_group has to be declared before the other userconfigparams! - */ - GroupUserConfigParam m_player_group; + /** The name of the player (wide string, so it can be in native + * language). */ + core::stringw m_name; - WStringUserConfigParam m_name; - - BoolUserConfigParam m_is_guest_account; + /** True if this account is a guest account. */ + bool m_is_guest_account; #ifdef DEBUG unsigned int m_magic_number; #endif - IntUserConfigParam m_use_frequency; + /** Counts how often this player was used. */ + unsigned int m_use_frequency; - /** Profile names can change, so rather than try to make sure all renames are done everywhere, - * assign a unique ID to each profiler. Will save much headaches. - */ - StringUserConfigParam m_unique_id; - - int64_t generateUniqueId(const char* playerName); + /** A unique number for this player, used to link it to challenges etc. */ + unsigned int m_unique_id; public: - /** - * Constructor to create a new player that didn't exist before - */ - PlayerProfile(const core::stringw& name); + PlayerProfile(const core::stringw& name, bool is_guest = false); - /** - * Constructor to deserialize a player that was saved to a XML file - * (...UserConfigParam classes will automagically take care of serializing all - * create players to the user's config file) - */ PlayerProfile(const XMLNode* node); + void save(UTFWriter &out); + void incrementUseFrequency(); ~PlayerProfile() { @@ -112,15 +104,12 @@ public: } - void incrementUseFrequency(); - // please do NOT try to optimise this to return a reference, I don't know why, - // maybe compiler bug, but hell breaks loose when you do that - std::string getUniqueID() const - { - return m_unique_id; - } -}; + // ------------------------------------------------------------------------ + /** Returns the unique id of this player. */ + unsigned int getUniqueID() const { return m_unique_id; } + +}; // class PlayerProfile #endif diff --git a/src/config/player_manager.cpp b/src/config/player_manager.cpp new file mode 100644 index 000000000..b0cbba275 --- /dev/null +++ b/src/config/player_manager.cpp @@ -0,0 +1,141 @@ +// +// 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" + +#include "config/player.hpp" +#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; + +/** Constructor. + */ +PlayerManager::PlayerManager() +{ +} // PlayerManager + +// ---------------------------------------------------------------------------- +/** Destructor. + */ +PlayerManager::~PlayerManager() +{ + save(); +} // ~PlayerManager + +// ---------------------------------------------------------------------------- + +void PlayerManager::load() +{ + std::string filename = file_manager->getUserConfigFile("players.xml"); + + const XMLNode *players = file_manager->createXMLTree(filename); + if(!players || players->getName()!="players") + { + Log::info("player_manager", "The players.xml file is invalid."); + return; + } + + for(unsigned int i=0; igetNumNodes(); i++) + { + const XMLNode *player_xml = players->getNode(i); + PlayerProfile *profile = new PlayerProfile(player_xml); + m_all_players.push_back(profile); + } +} // load + +// ---------------------------------------------------------------------------- +void PlayerManager::save() +{ + std::string filename = file_manager->getUserConfigFile("players.xml"); + try + { + UTFWriter players_file(filename.c_str()); + + players_file << L"\n"; + players_file << L"\n"; + + PlayerProfile *player; + for_in(player, m_all_players) + { + player->save(players_file); + } + players_file << L"\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 + +// ---------------------------------------------------------------------------- +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 + m_all_players.push_back( new PlayerProfile(_LTR("Guest")) ); + + +} // 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 + +// ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- diff --git a/src/config/player_manager.hpp b/src/config/player_manager.hpp new file mode 100644 index 000000000..9b2d06580 --- /dev/null +++ b/src/config/player_manager.hpp @@ -0,0 +1,78 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2010-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. + +#ifndef HEADER_PLAYER_CONFIG_HPP +#define HEADER_PLAYER_CONFIG_HPP + +#include "utils/no_copy.hpp" +#include "utils/ptr_vector.hpp" + +#include + +#include // NULL + +class PlayerProfile; + +/** A special class that manages all local player accounts. + */ +class PlayerManager : public NoCopy +{ +private: + static PlayerManager* m_player_manager; + + PtrVector m_all_players; + + + PlayerManager(); + ~PlayerManager(); + + +public: + /** Static singleton get function. */ + static PlayerManager* get() + { + if(!m_player_manager) + m_player_manager = new PlayerManager(); + return m_player_manager; + } // get + // ------------------------------------------------------------------------ + static void destroy() + { + delete m_player_manager; + m_player_manager = NULL; + } // destroy + // ------------------------------------------------------------------------ + + void load(); + void save(); + unsigned int getUniqueId() const; + void addDefaultPlayer(); + void addNewPlayer(const irr::core::stringw& name); + // ------------------------------------------------------------------------ + /** Returns the number of players in the config file.*/ + unsigned int getNumPlayers() const { return m_all_players.size(); } + // ------------------------------------------------------------------------ + /** Returns a player with a given unique id. */ + const PlayerProfile &getPlayer(unsigned int n) { return m_all_players[n];} + // ------------------------------------------------------------------------ +}; // PlayerManager + + +#endif + +/*EOF*/ diff --git a/src/config/saved_grand_prix.cpp b/src/config/saved_grand_prix.cpp index 5e43b3255..3805457da 100644 --- a/src/config/saved_grand_prix.cpp +++ b/src/config/saved_grand_prix.cpp @@ -63,7 +63,7 @@ SavedGrandPrix::SavedGPKart::SavedGPKart(GroupUserConfigParam * group, } // SavedGPKart // ============================================================================ -SavedGrandPrix::SavedGrandPrix(const std::string &player_id, +SavedGrandPrix::SavedGrandPrix(unsigned int player_id, const std::string &gp_id, RaceManager::Difficulty difficulty, int player_karts, @@ -71,7 +71,7 @@ SavedGrandPrix::SavedGrandPrix(const std::string &player_id, const std::vector &kart_list) : m_savedgp_group("SavedGP", "Represents the saved state of a GP"), - m_player_id(player_id.c_str(), "player_id", &m_savedgp_group), + m_player_id(player_id), m_gp_id(gp_id.c_str(), "gp_id", &m_savedgp_group), m_difficulty((int)difficulty,"difficulty", &m_savedgp_group), m_player_karts(player_karts,"player_karts", &m_savedgp_group), @@ -94,14 +94,13 @@ SavedGrandPrix::SavedGrandPrix(const std::string &player_id, SavedGrandPrix::SavedGrandPrix(const XMLNode* node) : m_savedgp_group("SavedGP", "Represents the saved state of a GP"), - m_player_id("-", "player_id", &m_savedgp_group), + m_player_id(0), m_gp_id("-", "gp_id", &m_savedgp_group), m_difficulty(0,"difficulty", &m_savedgp_group), m_player_karts(0,"player_karts", &m_savedgp_group), m_next_track(0,"last_track", &m_savedgp_group) { //m_player_group.findYourDataInAChildOf(node); - m_player_id.findYourDataInAnAttributeOf(node); m_gp_id.findYourDataInAnAttributeOf(node); m_difficulty.findYourDataInAnAttributeOf(node); m_player_karts.findYourDataInAnAttributeOf(node); diff --git a/src/config/saved_grand_prix.hpp b/src/config/saved_grand_prix.hpp index bc5f52eb5..e8aa32217 100644 --- a/src/config/saved_grand_prix.hpp +++ b/src/config/saved_grand_prix.hpp @@ -60,7 +60,7 @@ protected: * WARNING : m_savedgp_group has to be declared before the other userconfigparams! */ GroupUserConfigParam m_savedgp_group; - StringUserConfigParam m_player_id; + unsigned int m_player_id; /** Identifier of this GP. */ StringUserConfigParam m_gp_id; @@ -81,7 +81,7 @@ public: /** * Constructor to create a new entry. */ - SavedGrandPrix(const std::string &player_id, + SavedGrandPrix(unsigned int player_id, const std::string &gp_id, RaceManager::Difficulty difficulty, int player_karts, @@ -98,7 +98,7 @@ public: // ------------------------------------------------------------------------ /** Returns the player id for this saved GP. */ - const std::string getPlayerID() const { return m_player_id; } + unsigned int getPlayerID() const { return m_player_id; } // ------------------------------------------------------------------------ /** Returns the grand prix id. */ @@ -137,7 +137,7 @@ public: /** Finds the right SavedGrandPrix given the specified data, or * NULL if no matching GP was found. */ - static SavedGrandPrix* getSavedGP(const std::string &player, + static SavedGrandPrix* getSavedGP(unsigned int player, const std::string &gpid, int difficulty, int total_karts, int player_karts) diff --git a/src/config/user_config.cpp b/src/config/user_config.cpp index c37edfc77..184edc59b 100644 --- a/src/config/user_config.cpp +++ b/src/config/user_config.cpp @@ -728,38 +728,6 @@ UserConfig::~UserConfig() UserConfigParams::m_saved_grand_prix_list.clearAndDeleteAll(); } // ~UserConfig -// ----------------------------------------------------------------------------- -void UserConfig::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"); - - - class GuestPlayerProfile : public PlayerProfile - { - public: - GuestPlayerProfile() : PlayerProfile(_LTR("Guest")) - { - m_is_guest_account = true; - } - }; - - // add default guest player - UserConfigParams::m_all_players.push_back( new GuestPlayerProfile() ); - - // Set the name as the default name for all players. - UserConfigParams::m_all_players.push_back( - new PlayerProfile(username.c_str()) ); - -} // addDefaultPlayer - // ----------------------------------------------------------------------------- /** Comparison used to sort players. Most frequent players should be diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index c01ce58df..547987682 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -826,7 +826,6 @@ public: void resetWarning() { m_warning=""; } void setWarning(irr::core::stringw& warning) { m_warning=warning; } void postLoadInit(); - void addDefaultPlayer(); }; // UserConfig diff --git a/src/main.cpp b/src/main.cpp index 643f1fae4..e9baab93b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,6 +149,7 @@ #include "config/stk_config.hpp" #include "config/user_config.hpp" #include "config/player.hpp" +#include "config/player_manager.hpp" #include "graphics/hardware_skinning.hpp" #include "graphics/irr_driver.hpp" #include "graphics/material_manager.hpp" @@ -680,8 +681,7 @@ int handleCmdLine() if(CommandLine::has("--kart", &s)) { - unlock_manager->setCurrentSlot(UserConfigParams::m_all_players[0] - .getUniqueID() ); + unlock_manager->setCurrentSlot(PlayerManager::get()->getPlayer(0).getUniqueID()); if (!unlock_manager->getCurrentSlot()->isLocked(s)) { @@ -972,6 +972,7 @@ void initUserConfig() { irr_driver = new IrrDriver(); file_manager = new FileManager(); + PlayerManager::get()->load(); user_config = new UserConfig(); // needs file_manager const bool config_ok = user_config->loadConfig(); if (UserConfigParams::m_language.toString() != "system") @@ -989,10 +990,10 @@ void initUserConfig() stk_config = new STKConfig(); // in case of --stk-config // command line parameters user_config->postLoadInit(); - if (!config_ok || UserConfigParams::m_all_players.size() == 0) + if (!config_ok || PlayerManager::get()->getNumPlayers() == 0) { - user_config->addDefaultPlayer(); - user_config->saveConfig(); + PlayerManager::get()->addDefaultPlayer(); + PlayerManager::get()->save(); } } // initUserConfig @@ -1113,6 +1114,7 @@ static void cleanSuperTuxKart() delete ParticleKindManager::get(); if(stk_config) delete stk_config; if(user_config) delete user_config; + PlayerManager::destroy(); if(unlock_manager) delete unlock_manager; if(translations) delete translations; if(file_manager) delete file_manager; diff --git a/src/modes/easter_egg_hunt.hpp b/src/modes/easter_egg_hunt.hpp old mode 100755 new mode 100644 diff --git a/src/states_screens/dialogs/enter_player_name_dialog.cpp b/src/states_screens/dialogs/enter_player_name_dialog.cpp index 1f234d675..341b5a3fb 100644 --- a/src/states_screens/dialogs/enter_player_name_dialog.cpp +++ b/src/states_screens/dialogs/enter_player_name_dialog.cpp @@ -22,6 +22,7 @@ #include "audio/sfx_manager.hpp" #include "challenges/unlock_manager.hpp" #include "config/player.hpp" +#include "config/player_manager.hpp" #include "guiengine/engine.hpp" #include "guiengine/widgets/button_widget.hpp" #include "guiengine/widgets/label_widget.hpp" @@ -102,14 +103,14 @@ void EnterPlayerNameDialog::onEnterPressedInternal() // ---- Otherwise, see if we can accept the new name TextBoxWidget* textCtrl = getWidget("textfield"); - stringw playerName = textCtrl->getText().trim(); - if (StringUtils::notEmpty(playerName)) + stringw player_name = textCtrl->getText().trim(); + if (StringUtils::notEmpty(player_name)) { // check for duplicates const int amount = UserConfigParams::m_all_players.size(); for (int n=0; n("title"); label->setText(_("Cannot add a player with this name."), false); @@ -119,7 +120,8 @@ void EnterPlayerNameDialog::onEnterPressedInternal() } // Finally, add the new player. - UserConfigParams::m_all_players.push_back( new PlayerProfile(playerName) ); + PlayerManager::get()->addNewPlayer(player_name); + UserConfigParams::m_all_players.push_back( new PlayerProfile(player_name)); bool created = unlock_manager->createSlotsIfNeeded(); if (created) unlock_manager->save(); user_config->saveConfig(); @@ -144,7 +146,7 @@ void EnterPlayerNameDialog::onUpdate(float dt) if (m_self_destroy) { TextBoxWidget* textCtrl = getWidget("textfield"); - stringw playerName = textCtrl->getText().trim(); + stringw player_name = textCtrl->getText().trim(); // irrLicht is too stupid to remove focus from deleted widgets // so do it by hand @@ -158,6 +160,6 @@ void EnterPlayerNameDialog::onUpdate(float dt) ModalDialog::dismiss(); - if (listener != NULL) listener->onNewPlayerWithName( playerName ); + if (listener != NULL) listener->onNewPlayerWithName( player_name ); } } diff --git a/src/states_screens/options_screen_players.cpp b/src/states_screens/options_screen_players.cpp index 5f7ca3832..7cc5c2cc4 100644 --- a/src/states_screens/options_screen_players.cpp +++ b/src/states_screens/options_screen_players.cpp @@ -78,7 +78,7 @@ void OptionsScreenPlayers::init() refreshPlayerList(); ButtonWidget* you = getWidget("playername"); - const std::string& playerID = unlock_manager->getCurrentSlot()->getPlayerID(); + unsigned int playerID = unlock_manager->getCurrentSlot()->getPlayerID(); core::stringw playerName = L"-"; PlayerProfile* curr; for_in (curr, UserConfigParams::m_all_players)