Moar achievements stuff. Hooks and server stuff still TODO.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13625 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
c0c744b45b
commit
178fc8613d
@ -42,6 +42,13 @@ Achievement::~Achievement()
|
||||
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
void Achievement::onRaceEnd()
|
||||
{
|
||||
if(m_achievement_info->needsResetAfterRace())
|
||||
this->reset();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
void Achievement::check()
|
||||
{
|
||||
@ -83,6 +90,18 @@ void SingleAchievement::save(std::ofstream & out)
|
||||
<< "/>\n";
|
||||
} // save
|
||||
|
||||
// ============================================================================
|
||||
void SingleAchievement::reset()
|
||||
{
|
||||
m_progress = 0;
|
||||
} // reset
|
||||
|
||||
// ============================================================================
|
||||
void SingleAchievement::increase(int increase)
|
||||
{
|
||||
m_progress += increase;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
MapAchievement::MapAchievement(AchievementInfo * info)
|
||||
: Achievement(info)
|
||||
@ -131,3 +150,20 @@ int MapAchievement::getValue(const std::string & key)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
void MapAchievement::reset()
|
||||
{
|
||||
std::map<std::string, int>::iterator iter;
|
||||
for ( iter = m_progress_map.begin(); iter != m_progress_map.end(); ++iter ) {
|
||||
iter->second = 0;
|
||||
}
|
||||
} // reset
|
||||
|
||||
// ============================================================================
|
||||
void MapAchievement::increase(const std::string & key, int increase)
|
||||
{
|
||||
if ( m_progress_map.find(key) != m_progress_map.end())
|
||||
m_progress_map[key] += increase;
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include <string>
|
||||
#include "io/xml_node.hpp"
|
||||
|
||||
class AchievementInfo;
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@ -49,6 +48,8 @@ public:
|
||||
uint32_t getID () const { return m_id; }
|
||||
virtual void load (XMLNode * input) = 0;
|
||||
virtual void save (std::ofstream & out) = 0;
|
||||
virtual void reset () = 0;
|
||||
void onRaceEnd ();
|
||||
|
||||
enum AchievementType
|
||||
{
|
||||
@ -70,6 +71,8 @@ public:
|
||||
void load (XMLNode * input);
|
||||
int getValue () const { return m_progress; }
|
||||
void save (std::ofstream & out);
|
||||
void increase (int increase = 1);
|
||||
void reset ();
|
||||
}; // class SingleAchievement
|
||||
|
||||
class MapAchievement : public Achievement
|
||||
@ -83,7 +86,9 @@ public:
|
||||
|
||||
void load (XMLNode * input);
|
||||
int getValue (const std::string & key);
|
||||
void increase (const std::string & key, int increase = 1);
|
||||
void save (std::ofstream & out);
|
||||
void reset ();
|
||||
}; // class MapAchievement
|
||||
|
||||
#endif
|
||||
|
@ -50,6 +50,7 @@ public:
|
||||
irr::core::stringw getDescription () const { return m_description; }
|
||||
virtual Achievement::AchievementType getType () = 0;
|
||||
virtual bool checkCompletion (Achievement * achievement) const = 0;
|
||||
bool needsResetAfterRace() {return m_reset_after_race; }
|
||||
}; // class AchievementInfo
|
||||
|
||||
class SingleAchievementInfo : public AchievementInfo
|
||||
|
@ -24,6 +24,8 @@
|
||||
#include "io/xml_writer.hpp"
|
||||
#include "config/player.hpp"
|
||||
#include "config/user_config.hpp"
|
||||
#include "online/current_user.hpp"
|
||||
#include "challenges/unlock_manager.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
@ -49,11 +51,14 @@ AchievementsManager::AchievementsManager()
|
||||
{
|
||||
parseDataFile();
|
||||
parseConfigFile();
|
||||
updateCurrentPlayer();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
AchievementsManager::~AchievementsManager()
|
||||
{
|
||||
m_slots.clearAndDeleteAll();
|
||||
m_achievements_info.clearAndDeleteAll();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@ -61,8 +66,8 @@ void AchievementsManager::parseDataFile()
|
||||
{
|
||||
const std::string file_name = file_manager->getDataFile("achievements.xml");
|
||||
const XMLNode *root = file_manager->createXMLTree(file_name);
|
||||
unsigned int num_nodes = root->getNumNodes();
|
||||
for(unsigned int i = 0; i < num_nodes; i++)
|
||||
int num_nodes = root->getNumNodes();
|
||||
for(int i = 0; i < num_nodes; i++)
|
||||
{
|
||||
const XMLNode *node = root->getNode(i);
|
||||
std::string type("");
|
||||
@ -125,6 +130,13 @@ void AchievementsManager::parseConfigFile()
|
||||
} // load
|
||||
|
||||
|
||||
AchievementsSlot * AchievementsManager::createNewSlot(std::string id, bool online)
|
||||
{
|
||||
AchievementsSlot* slot = new AchievementsSlot(id, false);
|
||||
m_slots.push_back(slot);
|
||||
return slot;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Creates a slot for players that don't have one yet
|
||||
@ -138,21 +150,9 @@ bool AchievementsManager::createSlotsIfNeeded()
|
||||
PtrVector<PlayerProfile>& players = UserConfigParams::m_all_players;
|
||||
for (int n=0; n<players.size(); n++)
|
||||
{
|
||||
bool exists = false;
|
||||
|
||||
for(unsigned int i = 0; i < m_slots.size(); i++)
|
||||
if (getSlot(players[n].getUniqueID(), false) == NULL )
|
||||
{
|
||||
if(!m_slots[i]->isOnline() && m_slots[i]->getID() == players[n].getUniqueID())
|
||||
{
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!exists)
|
||||
{
|
||||
AchievementsSlot* slot = new AchievementsSlot(players[n].getUniqueID(), false);
|
||||
m_slots.push_back(slot);
|
||||
createNewSlot(players[n].getUniqueID(), false);
|
||||
something_changed = true;
|
||||
}
|
||||
}
|
||||
@ -179,12 +179,52 @@ void AchievementsManager::save()
|
||||
achievements_file << "<?xml version=\"1.0\"?>\n";
|
||||
achievements_file << "<achievements>\n";
|
||||
|
||||
for (unsigned int i = 0; i < m_slots.size(); i++)
|
||||
for (int i = 0; i < m_slots.size(); i++)
|
||||
{
|
||||
m_slots[i]->save(achievements_file);
|
||||
m_slots[i].save(achievements_file);
|
||||
}
|
||||
|
||||
achievements_file << "</achievements>\n\n";
|
||||
achievements_file.close();
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
void AchievementsManager::onRaceEnd()
|
||||
{
|
||||
//reset all values that need to be reset
|
||||
m_active_slot->onRaceEnd();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
AchievementsSlot * AchievementsManager::getSlot(const std::string & id, bool online)
|
||||
{
|
||||
for(int i = 0; i < m_slots.size(); i++)
|
||||
{
|
||||
if(m_slots[i].isOnline() == online && m_slots[i].getID() == id)
|
||||
{
|
||||
return m_slots.get(i);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
void AchievementsManager::updateCurrentPlayer()
|
||||
{
|
||||
if(Online::CurrentUser::get()->isRegisteredUser())
|
||||
{
|
||||
m_active_slot = getSlot(StringUtils::toString(Online::CurrentUser::get()->getID()), true);
|
||||
if(m_active_slot == NULL)
|
||||
{
|
||||
m_active_slot = createNewSlot(StringUtils::toString(Online::CurrentUser::get()->getID()), true);
|
||||
m_active_slot->sync();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_active_slot = getSlot(unlock_manager->getCurrentPlayer()->getUniqueID(), false);
|
||||
if(m_active_slot == NULL)
|
||||
m_active_slot = createNewSlot(unlock_manager->getCurrentPlayer()->getUniqueID(), false);
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#define HEADER_ACHIEVEMENTS_MANAGER_HPP
|
||||
|
||||
#include "utils/types.hpp"
|
||||
#include "utils/ptr_vector.hpp"
|
||||
#include "achievements/achievement_info.hpp"
|
||||
#include "achievements/achievements_slot.hpp"
|
||||
|
||||
@ -39,22 +40,27 @@
|
||||
class AchievementsManager
|
||||
{
|
||||
private :
|
||||
std::vector<AchievementsSlot*> m_slots;
|
||||
std::vector<AchievementInfo *> m_achievements_info;
|
||||
AchievementsSlot * m_active_slot;
|
||||
PtrVector<AchievementsSlot> m_slots;
|
||||
PtrVector<AchievementInfo> m_achievements_info;
|
||||
AchievementsManager ();
|
||||
~AchievementsManager ();
|
||||
bool createSlotsIfNeeded();
|
||||
AchievementsSlot * createNewSlot(std::string id, bool online);
|
||||
|
||||
public:
|
||||
/**Singleton */
|
||||
static AchievementsManager * get();
|
||||
static void deallocate();
|
||||
|
||||
const std::vector<AchievementInfo *> & getAllInfo() const { return m_achievements_info;};
|
||||
const PtrVector<AchievementInfo> & getAllInfo() const { return m_achievements_info;};
|
||||
|
||||
void parseDataFile();
|
||||
void parseConfigFile();
|
||||
void save();
|
||||
void onRaceEnd();
|
||||
void updateCurrentPlayer();
|
||||
AchievementsSlot * getSlot(const std::string & id, bool online);
|
||||
}; // class AchievementsManager
|
||||
|
||||
#endif
|
||||
|
@ -22,8 +22,10 @@
|
||||
#include "achievements/achievement_info.hpp"
|
||||
#include "achievements/achievements_manager.hpp"
|
||||
#include "utils/log.hpp"
|
||||
#include "utils/ptr_vector.hpp"
|
||||
#include "utils/translation.hpp"
|
||||
#include "io/xml_writer.hpp"
|
||||
#include "online/current_user.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
@ -73,10 +75,10 @@ AchievementsSlot::AchievementsSlot(std::string id, bool online)
|
||||
void AchievementsSlot::createFreshSlot()
|
||||
{
|
||||
m_achievements.clear();
|
||||
std::vector<AchievementInfo *> all_info = AchievementsManager::get()->getAllInfo();
|
||||
for( unsigned int i=0; i < all_info.size(); i++)
|
||||
PtrVector<AchievementInfo> all_info = AchievementsManager::get()->getAllInfo();
|
||||
for(int i=0; i < all_info.size(); i++)
|
||||
{
|
||||
AchievementInfo * info = all_info[i];
|
||||
AchievementInfo * info = all_info.get(i);
|
||||
Achievement::AchievementType achievement_type = info->getType();
|
||||
Achievement * achievement;
|
||||
if(achievement_type == Achievement::AT_SINGLE)
|
||||
@ -113,3 +115,20 @@ Achievement * AchievementsSlot::findAchievement(uint32_t id)
|
||||
return m_achievements[id];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
void AchievementsSlot::sync()
|
||||
{
|
||||
assert(StringUtils::toString(Online::CurrentUser::get()->getID()) == m_id);
|
||||
//FIXME
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
void AchievementsSlot::onRaceEnd()
|
||||
{
|
||||
//reset all values that need to be reset
|
||||
std::map<uint32_t, Achievement *>::iterator iter;
|
||||
for ( iter = m_achievements.begin(); iter != m_achievements.end(); ++iter ) {
|
||||
iter->second->onRaceEnd();
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,8 @@ public :
|
||||
bool isValid() const { return m_valid;}
|
||||
void save(std::ofstream & out);
|
||||
bool isOnline() const {return m_online;}
|
||||
void sync();
|
||||
void onRaceEnd();
|
||||
const std::string & getID() const {return m_id;}
|
||||
Achievement * findAchievement(uint32_t id);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user