Added TallyScore method to AchievementManager

Added a TallyScore method to AchievementManager to calculate the player's current scores (soft and hard) against the total number of points across all achievements. Includes a PointSpread structure to hold all points and counts. The Unlock Map now includes point values for each achievement to aid in this calculation; this is populated at insert time, and Unofficial achievements are tallied as zero.
This commit is contained in:
LillyJadeKatrin 2023-05-23 08:38:53 -04:00
parent 22af13f9e0
commit aa0224a8ab
2 changed files with 43 additions and 3 deletions

View file

@ -185,8 +185,11 @@ void AchievementManager::ActivateDeactivateAchievements()
bool encore = Config::Get(Config::RA_ENCORE_ENABLED);
for (u32 ix = 0; ix < m_game_data.num_achievements; ix++)
{
auto iter =
m_unlock_map.insert({m_game_data.achievements[ix].id, UnlockStatus{.game_data_index = ix}});
u32 points = (m_game_data.achievements[ix].category == RC_ACHIEVEMENT_CATEGORY_UNOFFICIAL) ?
0 :
m_game_data.achievements[ix].points;
auto iter = m_unlock_map.insert(
{m_game_data.achievements[ix].id, UnlockStatus{.game_data_index = ix, .points = points}});
ActivateDeactivateAchievement(iter.first->first, enabled, unofficial, encore);
}
}
@ -573,6 +576,30 @@ void AchievementManager::HandleLeaderboardTriggeredEvent(const rc_runtime_event_
}
}
AchievementManager::PointSpread AchievementManager::TallyScore() const
{
PointSpread spread{};
for (const auto& entry : m_unlock_map)
{
u32 points = entry.second.points;
spread.total_count++;
spread.total_points += points;
if (entry.second.remote_unlock_status == UnlockStatus::UnlockType::HARDCORE ||
(hardcore_mode_enabled && entry.second.session_unlock_count > 0))
{
spread.hard_unlocks++;
spread.hard_points += points;
}
else if (entry.second.remote_unlock_status == UnlockStatus::UnlockType::SOFTCORE ||
entry.second.session_unlock_count > 0)
{
spread.soft_unlocks++;
spread.soft_points += points;
}
}
return spread;
}
// Every RetroAchievements API call, with only a partial exception for fetch_image, follows
// the same design pattern (here, X is the name of the call):
// Create a specific rc_api_X_request_t struct and populate with the necessary values

View file

@ -41,6 +41,16 @@ public:
};
using ResponseCallback = std::function<void(ResponseType)>;
struct PointSpread
{
u32 total_count;
u32 total_points;
u32 hard_unlocks;
u32 hard_points;
u32 soft_unlocks;
u32 soft_points;
};
static AchievementManager* GetInstance();
void Init();
ResponseType Login(const std::string& password);
@ -84,6 +94,8 @@ private:
void HandleLeaderboardCanceledEvent(const rc_runtime_event_t* runtime_event);
void HandleLeaderboardTriggeredEvent(const rc_runtime_event_t* runtime_event);
PointSpread TallyScore() const;
template <typename RcRequest, typename RcResponse>
ResponseType Request(RcRequest rc_request, RcResponse* rc_response,
const std::function<int(rc_api_request_t*, const RcRequest*)>& init_request,
@ -107,7 +119,8 @@ private:
SOFTCORE,
HARDCORE
} remote_unlock_status = UnlockType::LOCKED;
int session_unlock_count = 0;
u32 session_unlock_count = 0;
u32 points = 0;
};
std::unordered_map<AchievementId, UnlockStatus> m_unlock_map;