2013-09-02 16:53:55 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2013 Glenn De Jonghe
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2014-02-20 07:03:25 -05:00
|
|
|
#include "achievements/achievements_status.hpp"
|
2013-09-02 16:53:55 -04:00
|
|
|
|
|
|
|
#include "achievements/achievement_info.hpp"
|
|
|
|
#include "achievements/achievements_manager.hpp"
|
2014-02-20 06:04:03 -05:00
|
|
|
#include "io/utf_writer.hpp"
|
2013-09-02 16:53:55 -04:00
|
|
|
#include "utils/log.hpp"
|
2013-09-03 21:57:04 -04:00
|
|
|
#include "utils/ptr_vector.hpp"
|
2013-09-02 16:53:55 -04:00
|
|
|
#include "utils/translation.hpp"
|
2013-09-03 21:57:04 -04:00
|
|
|
#include "online/current_user.hpp"
|
2013-09-02 16:53:55 -04:00
|
|
|
|
|
|
|
#include <sstream>
|
2014-02-04 16:22:38 -05:00
|
|
|
#include <fstream>
|
2013-09-02 16:53:55 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2014-02-04 16:22:38 -05:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
AchievementsStatus::AchievementsStatus()
|
2013-09-02 16:53:55 -04:00
|
|
|
{
|
2014-02-20 06:04:03 -05:00
|
|
|
m_valid = true;
|
|
|
|
m_online = true;
|
|
|
|
} // AchievementsStatus
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
AchievementsStatus::~AchievementsStatus()
|
|
|
|
{
|
|
|
|
deleteAchievements();
|
|
|
|
} // ~AchievementsStatus
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Loads the saved state of all achievements from an XML file.
|
|
|
|
* \param input The XML node to load the data from.
|
|
|
|
*/
|
|
|
|
void AchievementsStatus::load(const XMLNode * input)
|
|
|
|
{
|
2013-09-02 16:53:55 -04:00
|
|
|
std::vector<XMLNode*> xml_achievements;
|
|
|
|
input->getNodes("achievement", xml_achievements);
|
2014-02-20 06:04:03 -05:00
|
|
|
for (unsigned int i = 0; i < xml_achievements.size(); i++)
|
2013-09-02 16:53:55 -04:00
|
|
|
{
|
|
|
|
uint32_t achievement_id(0);
|
|
|
|
xml_achievements[i]->get("id", &achievement_id);
|
2013-09-04 15:44:10 -04:00
|
|
|
Achievement * achievement = getAchievement(achievement_id);
|
2014-02-20 06:04:03 -05:00
|
|
|
if (achievement == NULL)
|
2013-09-02 16:53:55 -04:00
|
|
|
{
|
2014-02-20 06:04:03 -05:00
|
|
|
Log::warn("AchievementsStatus",
|
|
|
|
"Found saved achievement data for a non-existent "
|
|
|
|
"achievement. Discarding.");
|
2013-09-02 16:53:55 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
achievement->load(xml_achievements[i]);
|
2014-02-20 06:04:03 -05:00
|
|
|
} // for i in xml_achievements
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
} // load
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::add(Achievement *achievement)
|
2013-09-02 16:53:55 -04:00
|
|
|
{
|
2014-02-20 06:04:03 -05:00
|
|
|
m_achievements[achievement->getID()] = achievement;
|
|
|
|
} // add
|
2013-09-10 16:41:57 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::deleteAchievements()
|
2013-09-10 16:41:57 -04:00
|
|
|
{
|
|
|
|
std::map<uint32_t, Achievement *>::iterator it;
|
|
|
|
for ( it = m_achievements.begin(); it != m_achievements.end(); ++it ) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
2013-09-02 16:53:55 -04:00
|
|
|
m_achievements.clear();
|
2014-02-20 06:04:03 -05:00
|
|
|
} // deleteAchievements
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Saves the achievement status to a file. Achievements are stored as part
|
|
|
|
* of the player data file players.xml.
|
|
|
|
* \param out File to write to.
|
|
|
|
*/
|
|
|
|
void AchievementsStatus::save(UTFWriter &out)
|
2013-09-10 16:41:57 -04:00
|
|
|
{
|
2014-02-21 06:15:36 -05:00
|
|
|
out << L" <achievements online=\"" << m_online << L"\"> \n";
|
2013-09-02 16:53:55 -04:00
|
|
|
std::map<uint32_t, Achievement*>::const_iterator i;
|
|
|
|
for(i = m_achievements.begin(); i != m_achievements.end(); i++)
|
|
|
|
{
|
|
|
|
if (i->second != NULL)
|
|
|
|
i->second->save(out);
|
|
|
|
}
|
2014-02-21 06:15:36 -05:00
|
|
|
out << L" </achievements>\n";
|
2014-02-20 06:04:03 -05:00
|
|
|
} // save
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
Achievement * AchievementsStatus::getAchievement(uint32_t id)
|
2013-09-02 16:53:55 -04:00
|
|
|
{
|
|
|
|
if ( m_achievements.find(id) != m_achievements.end())
|
|
|
|
return m_achievements[id];
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-09-03 21:57:04 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::sync(const std::vector<uint32_t> & achieved_ids)
|
2013-09-03 21:57:04 -04:00
|
|
|
{
|
2013-09-06 13:56:05 -04:00
|
|
|
for(unsigned int i =0; i < achieved_ids.size(); ++i)
|
|
|
|
{
|
|
|
|
Achievement * achievement = getAchievement(achieved_ids[i]);
|
|
|
|
if(achievement != NULL)
|
|
|
|
achievement->setAchieved();
|
|
|
|
}
|
2013-09-03 21:57:04 -04:00
|
|
|
}
|
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::onRaceEnd()
|
2013-09-03 21:57:04 -04:00
|
|
|
{
|
|
|
|
//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();
|
|
|
|
}
|
|
|
|
}
|