From 6e62854b81d66c70cc2123f798fa7c7b70594f1e Mon Sep 17 00:00:00 2001 From: Pedro Henrique Alves Cruz Date: Mon, 16 Dec 2024 00:19:21 -0300 Subject: [PATCH] fix: sonar issues and save wheel on async save --- src/creatures/players/wheel/player_wheel.cpp | 50 ++++++++------------ src/creatures/players/wheel/player_wheel.hpp | 2 +- src/game/scheduling/save_manager.cpp | 2 +- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/creatures/players/wheel/player_wheel.cpp b/src/creatures/players/wheel/player_wheel.cpp index fa2ad87c35c..47d831c5d8a 100644 --- a/src/creatures/players/wheel/player_wheel.cpp +++ b/src/creatures/players/wheel/player_wheel.cpp @@ -307,7 +307,7 @@ namespace { return 0; } - static std::vector WheelOfDestinyPromotionScrolls = { + std::vector WheelOfDestinyPromotionScrolls = { { 43946, "abridged", 3 }, { 43947, "basic", 5 }, { 43948, "revised", 9 }, @@ -996,7 +996,7 @@ std::vector PlayerWheel::getRevealedGems() const { sortedUnlockedGemGUIDs.emplace_back(uuid); } - std::sort(sortedUnlockedGemGUIDs.begin(), sortedUnlockedGemGUIDs.end(), [](const std::string &a, const std::string &b) { + std::ranges::sort(sortedUnlockedGemGUIDs, [](const std::string &a, const std::string &b) { if (std::ranges::all_of(a, ::isdigit) && std::ranges::all_of(b, ::isdigit)) { return std::stoull(a) < std::stoull(b); } else { @@ -1101,7 +1101,7 @@ void PlayerWheel::revealGem(WheelGemQuality_t quality) { g_logger().debug("[{}] {}", __FUNCTION__, gem.toString()); m_revealedGems.emplace_back(gem); - std::sort(m_revealedGems.begin(), m_revealedGems.end(), [](const auto &gem1, const auto &gem2) { + std::ranges::sort(m_revealedGems, [](const auto &gem1, const auto &gem2) { if (std::ranges::all_of(gem1.uuid, ::isdigit) && std::ranges::all_of(gem2.uuid, ::isdigit)) { return std::stoull(gem1.uuid) < std::stoull(gem2.uuid); } else { @@ -1243,7 +1243,7 @@ void PlayerWheel::setActiveGem(WheelGemAffinity_t affinity, uint16_t index) { } void PlayerWheel::removeActiveGem(WheelGemAffinity_t affinity) { - m_activeGems[static_cast(affinity)] = {}; + m_activeGems[static_cast(affinity)] = emptyGem; } void PlayerWheel::addRevelationBonus(WheelGemAffinity_t affinity, uint16_t points) { @@ -1627,14 +1627,9 @@ void PlayerWheel::saveSlotPointsOnPressSaveButton(NetworkMessage &msg) { } void PlayerWheel::loadActiveGems() { - const auto &activeGemScope = gemsKV()->scoped("active"); - if (!activeGemScope) { - return; - } - for (const auto &affinity : magic_enum::enum_values()) { std::string key(magic_enum::enum_name(affinity)); - auto uuidKV = activeGemScope->get(key); + auto uuidKV = gemsKV()->scoped("active")->get(key); if (!uuidKV.has_value()) { continue; } @@ -1657,18 +1652,13 @@ void PlayerWheel::loadActiveGems() { } void PlayerWheel::saveActiveGems() const { - const auto &activeGemScope = gemsKV()->scoped("active"); - if (!activeGemScope) { - return; - } - - for (auto gemAffinity : magic_enum::enum_values()) { - const std::string key(magic_enum::enum_name(gemAffinity)); - const auto &gem = m_activeGems[static_cast(gemAffinity)]; + for (const auto &affinity : magic_enum::enum_values()) { + const std::string key(magic_enum::enum_name(affinity)); + const auto &gem = m_activeGems[static_cast(affinity)]; if (gem) { - activeGemScope->set(key, gem.uuid); + gemsKV()->scoped("active")->set(key, gem.uuid); } else { - activeGemScope->remove(key); + gemsKV()->scoped("active")->remove(key); } } } @@ -1684,7 +1674,7 @@ void PlayerWheel::loadRevealedGems() { sortedUnlockedGemGUIDs.emplace_back(uuid); } - std::sort(sortedUnlockedGemGUIDs.begin(), sortedUnlockedGemGUIDs.end(), [](const std::string &a, const std::string &b) { + std::ranges::sort(sortedUnlockedGemGUIDs, [](const std::string &a, const std::string &b) { if (std::ranges::all_of(a, ::isdigit) && std::ranges::all_of(b, ::isdigit)) { return std::stoull(a) < std::stoull(b); } else { @@ -1711,8 +1701,8 @@ void PlayerWheel::saveRevealedGems() const { } } -bool PlayerWheel::hasScroll(const std::string &scrollName) { - auto it = std::ranges::find_if(m_unlockedScrolls, [scrollName](const PromotionScroll &promotionScroll) { +bool PlayerWheel::scrollAccquired(const std::string &scrollName) { + auto it = std::ranges::find_if(m_unlockedScrolls, [&scrollName](const PromotionScroll &promotionScroll) { return scrollName == promotionScroll.name; }); @@ -1720,15 +1710,17 @@ bool PlayerWheel::hasScroll(const std::string &scrollName) { } bool PlayerWheel::unlockScroll(const std::string &scrollName) { - if (hasScroll(scrollName)) { + if (scrollAccquired(scrollName)) { return false; } - for (const auto &[itemId, name, extraPoints] : WheelOfDestinyPromotionScrolls) { - if (scrollName == name) { - m_unlockedScrolls.emplace_back(itemId, name, extraPoints); - return true; - } + auto it = std::ranges::find_if(WheelOfDestinyPromotionScrolls, [&scrollName](const auto& scroll) { + return scroll.name == scrollName; + }); + + if (it != WheelOfDestinyPromotionScrolls.end()) { + m_unlockedScrolls.emplace_back(*it); + return true; } return false; diff --git a/src/creatures/players/wheel/player_wheel.hpp b/src/creatures/players/wheel/player_wheel.hpp index 6a2f8b53727..fa5fcd51c61 100644 --- a/src/creatures/players/wheel/player_wheel.hpp +++ b/src/creatures/players/wheel/player_wheel.hpp @@ -74,7 +74,7 @@ class PlayerWheel { void loadRevealedGems(); void saveRevealedGems() const; - bool hasScroll(const std::string &scrollName); + bool scrollAccquired(const std::string &scrollName); bool unlockScroll(const std::string &scrollName); void loadKVScrolls(); void saveKVScrolls() const; diff --git a/src/game/scheduling/save_manager.cpp b/src/game/scheduling/save_manager.cpp index 45cd9392194..da421d0a308 100644 --- a/src/game/scheduling/save_manager.cpp +++ b/src/game/scheduling/save_manager.cpp @@ -121,7 +121,7 @@ bool SaveManager::doSavePlayer(std::shared_ptr player) { } bool SaveManager::savePlayer(std::shared_ptr player) { - if (player->isOnline()) { + if (player->isOnline() && g_game().getGameState() != GAME_STATE_SHUTDOWN) { schedulePlayer(player); return true; }