2013-09-02 16:53:55 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2013-2015 Glenn De Jonghe
|
|
|
|
// (C) 2014-2015 Joerg Henrichs
|
2013-09-02 16:53:55 -04:00
|
|
|
//
|
|
|
|
// 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-06-13 09:27:52 -04:00
|
|
|
#include "config/player_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"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2014-02-23 16:21:15 -05:00
|
|
|
/** Constructor for an Achievement.
|
|
|
|
*/
|
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;
|
2018-10-01 19:14:23 -04:00
|
|
|
for (unsigned int i=0; i<ACHIEVE_DATA_NUM; i++)
|
|
|
|
{
|
|
|
|
m_variables[i].counter = 0;
|
|
|
|
}
|
2014-02-20 06:04:03 -05:00
|
|
|
} // AchievementsStatus
|
2013-09-02 16:53:55 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-02-23 16:21:15 -05:00
|
|
|
/** Removes all achievements.
|
|
|
|
*/
|
2014-02-20 06:04:03 -05:00
|
|
|
AchievementsStatus::~AchievementsStatus()
|
|
|
|
{
|
2014-02-23 16:21:15 -05:00
|
|
|
std::map<uint32_t, Achievement *>::iterator it;
|
|
|
|
for (it = m_achievements.begin(); it != m_achievements.end(); ++it) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
m_achievements.clear();
|
2014-02-20 06:04:03 -05:00
|
|
|
} // ~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
|
|
|
|
2018-10-01 19:14:23 -04:00
|
|
|
// Load achievement data
|
|
|
|
int data_version = -1;
|
|
|
|
const XMLNode *n = input->getNode("data");
|
|
|
|
if (n!=NULL)
|
|
|
|
n->get("version", &data_version);
|
|
|
|
if (data_version == DATA_VERSION)
|
|
|
|
{
|
|
|
|
std::vector<XMLNode*> xml_achievement_data;
|
|
|
|
input->getNodes("var", xml_achievement_data);
|
|
|
|
for (unsigned int i = 0; i < xml_achievement_data.size(); i++)
|
|
|
|
{
|
|
|
|
if (i>=ACHIEVE_DATA_NUM)
|
|
|
|
{
|
|
|
|
Log::warn("AchievementsStatus",
|
|
|
|
"Found more saved achievement data "
|
|
|
|
"than there should be. Discarding.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
xml_achievement_data[i]->get("counter",&m_variables[i].counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there is nothing valid to load ; we keep the init values
|
|
|
|
|
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
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** 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
|
|
|
{
|
2018-06-03 21:04:46 -04:00
|
|
|
out << " <achievements online=\"" << m_online << "\"> \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);
|
|
|
|
}
|
2018-10-01 19:14:23 -04:00
|
|
|
out << " <data version=\"1\"/>\n";
|
|
|
|
for(int i=0;i<ACHIEVE_DATA_NUM;i++)
|
|
|
|
{
|
|
|
|
out << " <var counter=\"" << m_variables[i].counter << "\"/>\n";
|
|
|
|
}
|
2018-06-03 21:04:46 -04:00
|
|
|
out << " </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;
|
2014-03-12 07:40:43 -04:00
|
|
|
} // getAchievement
|
2013-09-03 21:57:04 -04:00
|
|
|
|
2014-02-20 06:04:03 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-06-13 09:27:52 -04:00
|
|
|
/** Synchronises the achievements between local and online usage. It takes
|
2014-07-08 14:59:34 -04:00
|
|
|
* the list of online achievements, and marks them all to be achieved
|
2014-06-13 09:27:52 -04:00
|
|
|
* locally. Then it issues 'achieved' requests to the server for all local
|
|
|
|
* achievements that are not set online.
|
|
|
|
*/
|
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
|
|
|
{
|
2014-06-13 09:27:52 -04:00
|
|
|
std::vector<bool> done;
|
2013-09-06 13:56:05 -04:00
|
|
|
for(unsigned int i =0; i < achieved_ids.size(); ++i)
|
|
|
|
{
|
2014-06-13 09:27:52 -04:00
|
|
|
if(done.size()< achieved_ids[i]+1)
|
|
|
|
done.resize(achieved_ids[i]+1);
|
|
|
|
done[achieved_ids[i]] = true;
|
2013-09-06 13:56:05 -04:00
|
|
|
Achievement * achievement = getAchievement(achieved_ids[i]);
|
|
|
|
if(achievement != NULL)
|
|
|
|
achievement->setAchieved();
|
|
|
|
}
|
2014-06-13 09:27:52 -04:00
|
|
|
|
|
|
|
std::map<uint32_t, Achievement*>::iterator i;
|
|
|
|
|
2014-06-16 09:22:27 -04:00
|
|
|
// String to collect all local ids that are not synched
|
|
|
|
// to the online account
|
|
|
|
std::string ids;
|
2014-06-13 09:27:52 -04:00
|
|
|
for(i=m_achievements.begin(); i!=m_achievements.end(); i++)
|
|
|
|
{
|
2014-06-16 09:22:27 -04:00
|
|
|
unsigned int id = i->second->getID();
|
2014-06-13 09:27:52 -04:00
|
|
|
if(i->second->isAchieved() && (id>=done.size() || !done[id]) )
|
|
|
|
{
|
2014-06-16 09:22:27 -04:00
|
|
|
ids=ids+StringUtils::toString(id)+",";
|
2014-06-13 09:27:52 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 09:22:27 -04:00
|
|
|
|
|
|
|
if(ids.size()>0)
|
|
|
|
{
|
2014-06-16 19:12:48 -04:00
|
|
|
ids = ids.substr(0, ids.size() - 1); // delete the last "," in the string
|
2014-09-28 22:09:19 -04:00
|
|
|
Log::info("Achievements", "Synching achievement %s to server.",
|
2014-06-16 09:22:27 -04:00
|
|
|
ids.c_str());
|
|
|
|
Online::HTTPRequest * request = new Online::HTTPRequest(true, 2);
|
|
|
|
PlayerManager::setUserDetails(request, "achieving");
|
|
|
|
request->addParameter("achievementid", ids);
|
|
|
|
request->queue();
|
|
|
|
}
|
2014-03-12 07:40:43 -04:00
|
|
|
} // sync
|
2013-09-03 21:57:04 -04:00
|
|
|
|
2018-10-01 19:14:23 -04:00
|
|
|
/* This function checks over achievements to update their goals
|
|
|
|
FIXME It is currently hard-coded to specific achievements,
|
|
|
|
until it can entirely supersedes the previous system and
|
|
|
|
removes its complications. */
|
|
|
|
void AchievementsStatus::updateAchievementsProgress(unsigned int achieve_data_id)
|
|
|
|
{
|
|
|
|
Achievement *gold_driver = PlayerManager::getCurrentAchievementsStatus()->getAchievement(AchievementInfo::ACHIEVE_GOLD_DRIVER);
|
|
|
|
Achievement *unstoppable = PlayerManager::getCurrentAchievementsStatus()->getAchievement(AchievementInfo::ACHIEVE_UNSTOPPABLE);
|
|
|
|
|
|
|
|
if (!unstoppable->isAchieved())
|
|
|
|
{
|
|
|
|
unstoppable->reset();
|
|
|
|
unstoppable->increase("wins", "wins", m_variables[ACHIEVE_CONS_WON_RACES].counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gold_driver->isAchieved())
|
|
|
|
{
|
|
|
|
gold_driver->reset();
|
|
|
|
gold_driver->increase("standard", "standard", m_variables[ACHIEVE_WON_NORMAL_RACES].counter);
|
|
|
|
gold_driver->increase("std_timetrial", "std_timetrial", m_variables[ACHIEVE_WON_TT_RACES].counter);
|
|
|
|
gold_driver->increase("follow_leader", "follow_leader", m_variables[ACHIEVE_WON_FTL_RACES].counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::increaseDataVar(unsigned int achieve_data_id, int increase)
|
|
|
|
{
|
|
|
|
if (achieve_data_id<ACHIEVE_DATA_NUM)
|
|
|
|
{
|
|
|
|
m_variables[achieve_data_id].counter += increase;
|
|
|
|
updateAchievementsProgress(achieve_data_id);
|
|
|
|
if (m_variables[achieve_data_id].counter > 10000000)
|
|
|
|
m_variables[achieve_data_id].counter = 10000000;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error("Achievements", "Achievement data id %i don't match any variable.",
|
|
|
|
achieve_data_id);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // increaseDataVar
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void AchievementsStatus::resetDataVar(unsigned int achieve_data_id)
|
|
|
|
{
|
|
|
|
if (achieve_data_id<ACHIEVE_DATA_NUM)
|
|
|
|
{
|
|
|
|
m_variables[achieve_data_id].counter = 0;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error("Achievements", "Achievement data id %i don't match any variable.",
|
|
|
|
achieve_data_id);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // resetDataVar
|
|
|
|
|
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();
|
|
|
|
}
|
2014-03-12 07:40:43 -04:00
|
|
|
} // onRaceEnd
|
2014-03-17 17:07:55 -04:00
|
|
|
|
2014-06-13 09:27:52 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
2014-03-17 17:07:55 -04:00
|
|
|
void AchievementsStatus::onLapEnd()
|
|
|
|
{
|
|
|
|
//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->onLapEnd();
|
|
|
|
}
|
2014-03-29 06:33:43 -04:00
|
|
|
} // onLapEnd
|