Update Achievement Sort and Progress on RP

Whenever a request to update the Rich Presence comes in, typically every ten seconds, the Achievement Progress Widget will update the sort order of the achievements and all of their measured values.
This commit is contained in:
LillyJadeKatrin 2024-06-20 00:35:46 -04:00
parent 9b9e6c4582
commit 1a33e28452
3 changed files with 40 additions and 33 deletions

View File

@ -61,33 +61,36 @@ AchievementBox::AchievementBox(QWidget* parent, rc_client_achievement_t* achieve
void AchievementBox::UpdateData() void AchievementBox::UpdateData()
{ {
std::lock_guard lg{AchievementManager::GetInstance().GetLock()};
// rc_client guarantees m_achievement will be valid as long as the game is loaded
if (!AchievementManager::GetInstance().IsGameLoaded())
return;
const auto& badge = AchievementManager::GetInstance().GetAchievementBadge(
m_achievement->id, m_achievement->state != RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED);
std::string_view color = AchievementManager::GRAY;
if (m_achievement->unlocked & RC_CLIENT_ACHIEVEMENT_UNLOCKED_HARDCORE)
color = AchievementManager::GOLD;
else if (m_achievement->unlocked & RC_CLIENT_ACHIEVEMENT_UNLOCKED_SOFTCORE)
color = AchievementManager::BLUE;
QImage i_badge(&badge.data.front(), badge.width, badge.height, QImage::Format_RGBA8888);
m_badge->setPixmap(
QPixmap::fromImage(i_badge).scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
m_badge->adjustSize();
m_badge->setStyleSheet(QStringLiteral("border: 4px solid %1").arg(QtUtils::FromStdString(color)));
if (m_achievement->state == RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED)
{ {
m_status->setText( std::lock_guard lg{AchievementManager::GetInstance().GetLock()};
tr("Unlocked at %1") // rc_client guarantees m_achievement will be valid as long as the game is loaded
.arg(QDateTime::fromSecsSinceEpoch(m_achievement->unlock_time).toString())); if (!AchievementManager::GetInstance().IsGameLoaded())
} return;
else
{ const auto& badge = AchievementManager::GetInstance().GetAchievementBadge(
m_status->setText(tr("Locked")); m_achievement->id, m_achievement->state != RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED);
std::string_view color = AchievementManager::GRAY;
if (m_achievement->unlocked & RC_CLIENT_ACHIEVEMENT_UNLOCKED_HARDCORE)
color = AchievementManager::GOLD;
else if (m_achievement->unlocked & RC_CLIENT_ACHIEVEMENT_UNLOCKED_SOFTCORE)
color = AchievementManager::BLUE;
QImage i_badge(&badge.data.front(), badge.width, badge.height, QImage::Format_RGBA8888);
m_badge->setPixmap(
QPixmap::fromImage(i_badge).scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
m_badge->adjustSize();
m_badge->setStyleSheet(
QStringLiteral("border: 4px solid %1").arg(QtUtils::FromStdString(color)));
if (m_achievement->state == RC_CLIENT_ACHIEVEMENT_STATE_UNLOCKED)
{
m_status->setText(
tr("Unlocked at %1")
.arg(QDateTime::fromSecsSinceEpoch(m_achievement->unlock_time).toString()));
}
else
{
m_status->setText(tr("Locked"));
}
} }
UpdateProgress(); UpdateProgress();

View File

@ -51,7 +51,7 @@ void AchievementProgressWidget::UpdateData(bool clean_all)
m_common_layout->removeWidget(widget); m_common_layout->removeWidget(widget);
if (std::strcmp(widget->metaObject()->className(), "QLabel") == 0) if (std::strcmp(widget->metaObject()->className(), "QLabel") == 0)
{ {
delete widget; widget->deleteLater();
delete item; delete item;
} }
} }
@ -64,22 +64,26 @@ void AchievementProgressWidget::UpdateData(bool clean_all)
auto* achievement_list = auto* achievement_list =
rc_client_create_achievement_list(client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL, rc_client_create_achievement_list(client, RC_CLIENT_ACHIEVEMENT_CATEGORY_CORE_AND_UNOFFICIAL,
RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_PROGRESS); RC_CLIENT_ACHIEVEMENT_LIST_GROUPING_PROGRESS);
if (!achievement_list)
return;
for (u32 ix = 0; ix < achievement_list->num_buckets; ix++) for (u32 ix = 0; ix < achievement_list->num_buckets; ix++)
{ {
m_common_layout->addWidget(new QLabel(tr(achievement_list->buckets[ix].label))); m_common_layout->addWidget(new QLabel(tr(achievement_list->buckets[ix].label)));
for (u32 jx = 0; jx < achievement_list->buckets[ix].num_achievements; jx++) for (u32 jx = 0; jx < achievement_list->buckets[ix].num_achievements; jx++)
{ {
auto* achievement = achievement_list->buckets[ix].achievements[jx]; auto* achievement = achievement_list->buckets[ix].achievements[jx];
auto box_itr = m_achievement_boxes.find(achievement->id); auto box_itr = m_achievement_boxes.lower_bound(achievement->id);
if (box_itr == m_achievement_boxes.end()) if (box_itr != m_achievement_boxes.end() && box_itr->first == achievement->id)
{ {
m_achievement_boxes[achievement->id] = std::make_shared<AchievementBox>(this, achievement); box_itr->second->UpdateProgress();
m_common_layout->addWidget(box_itr->second.get());
} }
else else
{ {
box_itr->second->UpdateProgress(); const auto new_box_itr = m_achievement_boxes.try_emplace(
box_itr, achievement->id, std::make_shared<AchievementBox>(this, achievement));
m_common_layout->addWidget(new_box_itr->second.get());
} }
m_common_layout->addWidget(m_achievement_boxes[achievement->id].get());
} }
} }
rc_client_destroy_achievement_list(achievement_list); rc_client_destroy_achievement_list(achievement_list);

View File

@ -89,7 +89,7 @@ void AchievementsWindow::UpdateData(AchievementManager::UpdatedItems updated_ite
{ {
m_header_widget->UpdateData(); m_header_widget->UpdateData();
} }
if (updated_items.all_achievements) if (updated_items.all_achievements || updated_items.rich_presence)
m_progress_widget->UpdateData(false); m_progress_widget->UpdateData(false);
else if (updated_items.achievements.size() > 0) else if (updated_items.achievements.size() > 0)
m_progress_widget->UpdateData(updated_items.achievements); m_progress_widget->UpdateData(updated_items.achievements);