| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 5 | #include "DolphinQt/Achievements/AchievementLeaderboardWidget.h" |
| 6 | |
| 7 | #include <QGroupBox> |
| 8 | #include <QLabel> |
| 9 | #include <QLineEdit> |
| 10 | #include <QString> |
| 11 | #include <QVBoxLayout> |
| 12 | |
| 13 | #include "Common/CommonTypes.h" |
| 14 | #include "Core/AchievementManager.h" |
| 15 | |
| 16 | #include "DolphinQt/QtUtils/ClearLayoutRecursively.h" |
| 17 | |
| 18 | AchievementLeaderboardWidget::AchievementLeaderboardWidget(QWidget* parent) : QWidget(parent) |
| 19 | { |
| 20 | m_common_box = new QGroupBox(); |
| 21 | m_common_layout = new QGridLayout(); |
| 22 | |
| 23 | m_common_box->setLayout(m_common_layout); |
| 24 | |
| 25 | auto* layout = new QVBoxLayout; |
| 26 | layout->setContentsMargins(0, 0, 0, 0); |
| 27 | layout->setAlignment(Qt::AlignTop); |
| 28 | layout->addWidget(m_common_box); |
| 29 | layout->setSizeConstraint(QLayout::SetFixedSize); |
| 30 | setLayout(layout); |
| 31 | } |
| 32 | |
| 33 | void AchievementLeaderboardWidget::UpdateData(bool clean_all) |
| 34 | { |
| 35 | if (clean_all) |
| 36 | { |
| 37 | ClearLayoutRecursively(m_common_layout); |
| 38 | |
| 39 | auto& instance = AchievementManager::GetInstance(); |
| 40 | if (!instance.IsGameLoaded()) |
| 41 | return; |
| 42 | auto* client = instance.GetClient(); |
| 43 | auto* leaderboard_list = |
| 44 | rc_client_create_leaderboard_list(client, RC_CLIENT_LEADERBOARD_LIST_GROUPING_TRACKING); |
| 45 | |
| 46 | u32 row = 0; |
| 47 | for (u32 bucket = 0; bucket < leaderboard_list->num_buckets; bucket++) |
| 48 | { |
| 49 | const auto& leaderboard_bucket = leaderboard_list->buckets[bucket]; |
| 50 | m_common_layout->addWidget(new QLabel(tr(leaderboard_bucket.label)), row, 0); |
| 51 | row += 2; |
| 52 | for (u32 board = 0; board < leaderboard_bucket.num_leaderboards; board++) |
| 53 | { |
| 54 | const auto* leaderboard = leaderboard_bucket.leaderboards[board]; |
| 55 | m_leaderboard_order[leaderboard->id] = row; |
| 56 | QLabel* a_title = new QLabel(QString::fromUtf8(leaderboard->title)); |
| 57 | a_title->setWordWrap(true); |
| 58 | a_title->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 59 | QLabel* a_description = new QLabel(QString::fromUtf8(leaderboard->description)); |
| 60 | a_description->setWordWrap(true); |
| 61 | a_description->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 62 | QVBoxLayout* a_col_left = new QVBoxLayout(); |
| 63 | a_col_left->addWidget(a_title); |
| 64 | a_col_left->addWidget(a_description); |
| 65 | QFrame* a_divider = new QFrame(); |
| 66 | a_divider->setFrameShape(QFrame::HLine); |
| 67 | m_common_layout->addWidget(a_divider, row - 1, 0); |
| 68 | m_common_layout->addLayout(a_col_left, row, 0); |
| 69 | for (size_t ix = 0; ix < 4; ix++) |
| 70 | { |
| 71 | QVBoxLayout* a_col = new QVBoxLayout(); |
| 72 | for (size_t jx = 0; jx < 3; jx++) |
| 73 | a_col->addWidget(new QLabel(QStringLiteral("---"))); |
| 74 | QFrame* a_divider_2 = new QFrame(); |
| 75 | a_divider_2->setFrameShape(QFrame::HLine); |
| 76 | m_common_layout->addWidget(a_divider_2, row - 1, static_cast<int>(ix) + 1); |
| 77 | m_common_layout->addLayout(a_col, row, static_cast<int>(ix) + 1); |
| 78 | } |
| 79 | row += 2; |
| 80 | } |
| 81 | } |
| 82 | rc_client_destroy_leaderboard_list(leaderboard_list); |
| 83 | } |
| 84 | for (auto row : m_leaderboard_order) |
| 85 | { |
| 86 | UpdateRow(row.first); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void AchievementLeaderboardWidget::UpdateData( |
| 91 | const std::set<AchievementManager::AchievementId>& update_ids) |
| 92 | { |
| 93 | for (auto row : m_leaderboard_order) |
| 94 | { |
| 95 | if (update_ids.contains(row.first)) |
| 96 | { |
| 97 | UpdateRow(row.first); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void AchievementLeaderboardWidget::UpdateRow(AchievementManager::AchievementId leaderboard_id) |
| 103 | { |
| 104 | const auto leaderboard_itr = m_leaderboard_order.find(leaderboard_id); |
| 105 | if (leaderboard_itr == m_leaderboard_order.end()) |
| 106 | return; |
| 107 | const int row = leaderboard_itr->second; |
| 108 | |
| 109 | const AchievementManager::LeaderboardStatus* board; |
| 110 | { |
| 111 | std::lock_guard lg{AchievementManager::GetInstance().GetLock()}; |
| 112 | board = AchievementManager::GetInstance().GetLeaderboardInfo(leaderboard_id); |
| 113 | } |
| 114 | if (!board) |
| 115 | return; |
| 116 | |
| 117 | // Each leaderboard entry is displayed with four values. These are *generally* intended to be, |
| 118 | // in order, the first place entry, the entry one above the player, the player's entry, and |
| 119 | // the entry one below the player. |
| 120 | // Edge cases: |
| 121 | // * If there are fewer than four entries in the leaderboard, all entries will be shown in |
| 122 | // order and the remainder of the list will be padded with empty values. |
| 123 | // * If the player does not currently have a score in the leaderboard, or is in the top 3, |
| 124 | // the four slots will be the top four players in order. |
| 125 | // * If the player is last place, the player will be in the fourth slot, and the second and |
| 126 | // third slots will be the two players above them. The first slot will always be first place. |
| 127 | std::array<u32, 4> to_display{1, 2, 3, 4}; |
| 128 | if (board->player_index > to_display.size() - 1) |
| 129 | { |
| 130 | // If the rank one below than the player is found, offset = 1. |
| 131 | u32 offset = static_cast<u32>(board->entries.count(board->player_index + 1)); |
| 132 | // Example: player is 10th place but not last |
| 133 | // to_display = {1, 10-3+1+1, 10-3+1+2, 10-3+1+3} = {1, 9, 10, 11} |
| 134 | // Example: player is 15th place and is last |
| 135 | // to_display = {1, 15-3+0+1, 15-3+0+2, 15-3+0+3} = {1, 13, 14, 15} |
| 136 | for (size_t ix = 1; ix < to_display.size(); ++ix) |
| 137 | to_display[ix] = board->player_index - 3 + offset + static_cast<u32>(ix); |
| 138 | } |
| 139 | for (size_t ix = 0; ix < to_display.size(); ++ix) |
| 140 | { |
| 141 | const auto it = board->entries.find(to_display[ix]); |
| 142 | if (it != board->entries.end()) |
| 143 | { |
| 144 | QVBoxLayout* a_col = new QVBoxLayout(); |
| 145 | a_col->addWidget(new QLabel(tr("Rank %1").arg(it->second.rank))); |
| 146 | a_col->addWidget(new QLabel(QString::fromStdString(it->second.username))); |
| 147 | a_col->addWidget(new QLabel(QString::fromUtf8(it->second.score.data()))); |
| 148 | auto old_item = m_common_layout->itemAtPosition(row, static_cast<int>(ix) + 1); |
| 149 | m_common_layout->removeItem(old_item); |
| 150 | ClearLayoutRecursively(static_cast<QLayout*>(old_item)); |
| 151 | m_common_layout->addLayout(a_col, row, static_cast<int>(ix) + 1); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #endif // USE_RETRO_ACHIEVEMENTS |