Skip to content

Commit

Permalink
player_achievement & player_title: improvements on checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed May 25, 2024
1 parent f79056b commit c0c8514
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/creatures/players/achievement/player_achievement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool PlayerAchievement::add(uint16_t id, bool message /* = true*/, uint32_t time
addPoints(achievement.points);
int toSaveTimeStamp = timestamp != 0 ? timestamp : (OTSYS_TIME() / 1000);
getUnlockedKV()->set(achievement.name, toSaveTimeStamp);
m_achievementsUnlocked.push_back({ achievement.id, toSaveTimeStamp });
m_achievementsUnlocked.emplace_back(achievement.id, toSaveTimeStamp);
m_achievementsUnlocked.shrink_to_fit();
return true;
}
Expand Down Expand Up @@ -80,7 +80,8 @@ bool PlayerAchievement::isUnlocked(uint16_t id) const {
}

uint16_t PlayerAchievement::getPoints() const {
return m_player.kv()->scoped("achievements")->get("points")->getNumber();
auto kvScoped = m_player.kv()->scoped("achievements")->get("points");
return kvScoped ? static_cast<uint16_t>(kvScoped->getNumber()) : 0;
}

void PlayerAchievement::addPoints(uint16_t toAddPoints) {
Expand Down Expand Up @@ -109,12 +110,12 @@ void PlayerAchievement::loadUnlockedAchievements() {

g_logger().debug("[{}] - Achievement {} found for player {}.", __FUNCTION__, achievementName, m_player.getName());

m_achievementsUnlocked.push_back({ achievement.id, getUnlockedKV()->get(achievementName)->getNumber() });
m_achievementsUnlocked.emplace_back(achievement.id, getUnlockedKV()->get(achievementName)->getNumber());
}
}

void PlayerAchievement::sendUnlockedSecretAchievements() {
std::vector<std::pair<Achievement, uint32_t>> m_achievementsUnlocked;
std::vector<std::pair<Achievement, uint32_t>> achievementsUnlocked;
uint16_t unlockedSecret = 0;
for (const auto &[achievId, achievCreatedTime] : getUnlockedAchievements()) {
Achievement achievement = g_game().getAchievementById(achievId);
Expand All @@ -126,10 +127,10 @@ void PlayerAchievement::sendUnlockedSecretAchievements() {
unlockedSecret++;
}

m_achievementsUnlocked.push_back({ achievement, achievCreatedTime });
achievementsUnlocked.emplace_back(achievement, achievCreatedTime);
}

m_player.sendCyclopediaCharacterAchievements(unlockedSecret, m_achievementsUnlocked);
m_player.sendCyclopediaCharacterAchievements(unlockedSecret, achievementsUnlocked);
}

const std::shared_ptr<KV> &PlayerAchievement::getUnlockedKV() {
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/players/achievement/player_achievement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class PlayerAchievement {
explicit PlayerAchievement(Player &player);
bool add(uint16_t id, bool message = true, uint32_t timestamp = 0);
bool remove(uint16_t id);
bool isUnlocked(uint16_t id) const;
uint16_t getPoints() const;
[[nodiscard]] bool isUnlocked(uint16_t id) const;
[[nodiscard]] uint16_t getPoints() const;
void addPoints(uint16_t toAddPoints);
void removePoints(uint16_t toRemovePoints);
std::vector<std::pair<uint16_t, uint32_t>> getUnlockedAchievements() const;
[[nodiscard]] std::vector<std::pair<uint16_t, uint32_t>> getUnlockedAchievements() const;
void loadUnlockedAchievements();
void sendUnlockedSecretAchievements();
const std::shared_ptr<KV> &getUnlockedKV();
Expand Down
3 changes: 2 additions & 1 deletion src/creatures/players/cyclopedia/player_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const std::vector<std::pair<Title, uint32_t>> &PlayerTitle::getUnlockedTitles()
}

uint8_t PlayerTitle::getCurrentTitle() const {
return static_cast<uint8_t>(m_player.kv()->scoped("titles")->get("current-title")->getNumber());
auto title = m_player.kv()->scoped("titles")->get("current-title");
return title ? static_cast<uint8_t>(title->getNumber()) : 0;
}

void PlayerTitle::setCurrentTitle(uint8_t id) {
Expand Down

0 comments on commit c0c8514

Please sign in to comment.