Permalink
Browse files

Synchronise achievements from a local account to an online account.

  • Loading branch information...
1 parent 4daa239 commit 6425befc5ef8198090c827aa7d7769468ddb6480 @hiker hiker committed Jun 13, 2014
Showing with 33 additions and 6 deletions.
  1. +27 −0 src/achievements/achievements_status.cpp
  2. +6 −6 src/online/online_player_profile.cpp
@@ -22,6 +22,7 @@
#include "achievements/achievement_info.hpp"
#include "achievements/achievements_manager.hpp"
+#include "config/player_manager.hpp"
#include "io/utf_writer.hpp"
#include "utils/log.hpp"
#include "utils/ptr_vector.hpp"
@@ -112,14 +113,39 @@ Achievement * AchievementsStatus::getAchievement(uint32_t id)
} // getAchievement
// ----------------------------------------------------------------------------
+/** Synchronises the achievements between local and online usage. It takes
+ * the list of online achievements, and marks them all to be achieved
+ * locally. Then it issues 'achieved' requests to the server for all local
+ * achievements that are not set online.
+*/
void AchievementsStatus::sync(const std::vector<uint32_t> & achieved_ids)
{
+ std::vector<bool> done;
for(unsigned int i =0; i < achieved_ids.size(); ++i)
{
+ if(done.size()< achieved_ids[i]+1)
+ done.resize(achieved_ids[i]+1);
+ done[achieved_ids[i]] = true;
Achievement * achievement = getAchievement(achieved_ids[i]);
if(achievement != NULL)
achievement->setAchieved();
}
+
+ std::map<uint32_t, Achievement*>::iterator i;
+
+ for(i=m_achievements.begin(); i!=m_achievements.end(); i++)
+ {
+ int id = i->second->getID();
+ if(i->second->isAchieved() && (id>=done.size() || !done[id]) )
+ {
+ Log::info("Achievements", "Synching achievement %d to server.",
+ i->first);
+ Online::HTTPRequest * request = new Online::HTTPRequest(true,2);
+ PlayerManager::setUserDetails(request, "achieving");
+ request->addParameter("achievementid", i->second->getID());
+ request->queue();
+ }
+ }
} // sync
// ----------------------------------------------------------------------------
@@ -132,6 +158,7 @@ void AchievementsStatus::onRaceEnd()
}
} // onRaceEnd
+// ----------------------------------------------------------------------------
void AchievementsStatus::onLapEnd()
{
//reset all values that need to be reset
@@ -193,12 +193,12 @@ namespace Online
}
ProfileManager::get()->addPersistent(m_profile);
std::string achieved_string("");
- if(input->get("achieved", &achieved_string) == 1)
- {
- std::vector<uint32_t> achieved_ids =
- StringUtils::splitToUInt(achieved_string, ' ');
- PlayerManager::getCurrentAchievementsStatus()->sync(achieved_ids);
- }
+ // Even if no achievements were sent, we have to call sync
+ // in order to upload local achievements to the server
+ input->get("achieved", &achieved_string);
+ std::vector<uint32_t> achieved_ids =
+ StringUtils::splitToUInt(achieved_string, ' ');
+ PlayerManager::getCurrentAchievementsStatus()->sync(achieved_ids);
m_profile->fetchFriends();
} // if success
else

0 comments on commit 6425bef

Please sign in to comment.