From cf77e7130c7411374f5850cab5eb6207b5951f4a Mon Sep 17 00:00:00 2001 From: Pedro Henrique Alves Cruz Date: Fri, 13 Dec 2024 00:18:27 -0300 Subject: [PATCH] perf: wheel --- src/creatures/players/wheel/player_wheel.cpp | 134 +++++++++---------- src/creatures/players/wheel/player_wheel.hpp | 7 + src/io/functions/iologindata_load_player.cpp | 1 + 3 files changed, 72 insertions(+), 70 deletions(-) diff --git a/src/creatures/players/wheel/player_wheel.cpp b/src/creatures/players/wheel/player_wheel.cpp index 532de903e35..7607d211bfc 100644 --- a/src/creatures/players/wheel/player_wheel.cpp +++ b/src/creatures/players/wheel/player_wheel.cpp @@ -855,33 +855,7 @@ uint16_t PlayerWheel::getUnusedPoints() const { const auto vocationBaseId = m_player.getVocation()->getBaseId(); const auto modsSupremeIt = modsSupremePositionByVocation.find(vocationBaseId); - for (const auto &modPosition : modsBasicPosition) { - const auto pos = static_cast(modPosition); - uint8_t grade = 0; - auto gradeKV = gemsGradeKV(WheelFragmentType_t::Lesser, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = static_cast(gradeKV->get()); - } - - totalPoints += grade == 3 ? 1 : 0; - } - - if (modsSupremeIt != modsSupremePositionByVocation.end()) { - for (const auto &modPosition : modsSupremeIt->second.get()) { - const auto pos = static_cast(modPosition); - uint8_t grade = 0; - auto gradeKV = gemsGradeKV(WheelFragmentType_t::Greater, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = gradeKV->get(); - } - - totalPoints += grade == 3 ? 1 : 0; - } - } else { - g_logger().error("[{}] supreme modifications not found for vocation base id: {}", std::source_location::current().function_name(), vocationBaseId); - } + totalPoints += m_modsMaxGrade; for (uint8_t i = WheelSlots_t::SLOT_FIRST; i <= WheelSlots_t::SLOT_LAST; ++i) { totalPoints -= getPointsBySlotType(static_cast(i)); @@ -1028,13 +1002,7 @@ std::shared_ptr PlayerWheel::gemsGradeKV(WheelFragmentType_t type, uint8_t p } uint8_t PlayerWheel::getGemGrade(WheelFragmentType_t type, uint8_t pos) const { - uint8_t grade = 0; - const auto gradeKV = gemsGradeKV(type, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = static_cast(gradeKV->get()); - } - return grade; + return type == WheelFragmentType_t::Lesser ? m_basicGrades[pos] : m_supremeGrades[pos]; } std::vector PlayerWheel::getRevealedGems() const { @@ -1384,16 +1352,11 @@ void PlayerWheel::addGems(NetworkMessage &msg) const { void PlayerWheel::addGradeModifiers(NetworkMessage &msg) const { msg.addByte(0x2E); // Modifiers for all Vocations + for (const auto &modPosition : modsBasicPosition) { const auto pos = static_cast(modPosition); msg.addByte(pos); - uint8_t grade = 0; - auto gradeKV = gemsGradeKV(WheelFragmentType_t::Lesser, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = static_cast(gradeKV->get()); - } - msg.addByte(grade); + msg.addByte(m_basicGrades[pos]); } msg.addByte(0x17); // Modifiers for specific per Vocations @@ -1405,13 +1368,7 @@ void PlayerWheel::addGradeModifiers(NetworkMessage &msg) const { for (const auto &modPosition : modsSupremeIt->second.get()) { const auto pos = static_cast(modPosition); msg.addByte(pos); - uint8_t grade = 0; - auto gradeKV = gemsGradeKV(WheelFragmentType_t::Greater, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = gradeKV->get(); - } - msg.addByte(grade); + msg.addByte(m_supremeGrades[pos]); } } else { g_logger().error("[{}] vocation base id: {}", std::source_location::current().function_name(), m_player.getVocation()->getBaseId()); @@ -1424,9 +1381,10 @@ void PlayerWheel::improveGemGrade(WheelFragmentType_t fragmentType, uint8_t pos) uint8_t quantity = 0; uint8_t grade = 0; - auto gradeKV = gemsGradeKV(fragmentType, pos)->get("grade"); - if (gradeKV.has_value()) { - grade = gradeKV->get(); + if (fragmentType == WheelFragmentType_t::Lesser) { + grade = m_basicGrades[pos]; + } else { + grade = m_supremeGrades[pos]; } ++grade; @@ -1465,7 +1423,14 @@ void PlayerWheel::improveGemGrade(WheelFragmentType_t fragmentType, uint8_t pos) return; } - gemsGradeKV(fragmentType, pos)->set("grade", grade); + if (fragmentType == WheelFragmentType_t::Lesser) { + m_basicGrades[pos] = grade; + } else { + m_supremeGrades[pos] = grade; + } + + m_modsMaxGrade += grade == 3 ? 1 : 0; + loadPlayerBonusData(); sendOpenWheelWindow(m_player.getID()); } @@ -1676,6 +1641,52 @@ void PlayerWheel::saveSlotPointsOnPressSaveButton(NetworkMessage &msg) { g_logger().debug("Player: {} is saved the all slots info in: {} milliseconds", m_player.getName(), bm_saveSlot.duration()); } +void PlayerWheel::loadKVModGrades() { + for (const auto &modPosition : modsBasicPosition) { + const auto pos = static_cast(modPosition); + auto gradeKV = gemsGradeKV(WheelFragmentType_t::Lesser, pos)->get("grade"); + + if (gradeKV.has_value()) { + uint8_t grade = gradeKV->get(); + m_basicGrades[pos] = grade; + m_modsMaxGrade += grade == 3 ? 1 : 0; + } + } + + const auto vocationBaseId = m_player.getVocation()->getBaseId(); + const auto modsSupremeIt = modsSupremePositionByVocation.find(vocationBaseId); + for (const auto &modPosition : modsSupremeIt->second.get()) { + const auto pos = static_cast(modPosition); + auto gradeKV = gemsGradeKV(WheelFragmentType_t::Greater, pos)->get("grade"); + + if (gradeKV.has_value()) { + uint8_t grade = gradeKV->get(); + m_supremeGrades[pos] = grade; + m_modsMaxGrade += grade == 3 ? 1 : 0; + } + } +} + +void PlayerWheel::saveKVModGrades() const { + for (const auto &modPosition : modsBasicPosition) { + const auto pos = static_cast(modPosition); + uint8_t grade = m_basicGrades[pos]; + if (grade > 0) { + gemsGradeKV(WheelFragmentType_t::Lesser, pos)->set("grade", grade); + } + } + + const auto vocationBaseId = m_player.getVocation()->getBaseId(); + const auto modsSupremeIt = modsSupremePositionByVocation.find(vocationBaseId); + for (const auto &modPosition : modsSupremeIt->second.get()) { + const auto pos = static_cast(modPosition); + uint8_t grade = m_supremeGrades[pos]; + if (grade > 0) { + gemsGradeKV(WheelFragmentType_t::Greater, pos)->set("grade", grade); + } + } +} + /* * Functions for load and save player database informations */ @@ -2555,24 +2566,7 @@ WheelStageEnum_t PlayerWheel::getPlayerSliceStage(const std::string &color) cons } } - const auto vocationBaseId = m_player.getVocation()->getBaseId(); - const auto modsSupremeIt = modsSupremePositionByVocation.find(vocationBaseId); - - if (modsSupremeIt != modsSupremePositionByVocation.end()) { - for (const auto &modPosition : modsSupremeIt->second.get()) { - const auto pos = static_cast(modPosition); - uint8_t grade = 0; - auto gradeKV = gemsGradeKV(WheelFragmentType_t::Greater, pos)->get("grade"); - - if (gradeKV.has_value()) { - grade = gradeKV->get(); - } - - totalPoints += grade == 3 ? 1 : 0; - } - } else { - g_logger().error("[{}] supreme modifications not found for vocation base id: {}", std::source_location::current().function_name(), vocationBaseId); - } + totalPoints += m_modsMaxGrade; if (totalPoints >= static_cast(WheelStagePointsEnum_t::THREE)) { return WheelStageEnum_t::THREE; diff --git a/src/creatures/players/wheel/player_wheel.hpp b/src/creatures/players/wheel/player_wheel.hpp index 7c3bd2348a6..7c974ece584 100644 --- a/src/creatures/players/wheel/player_wheel.hpp +++ b/src/creatures/players/wheel/player_wheel.hpp @@ -59,6 +59,9 @@ class PlayerWheel { public: explicit PlayerWheel(Player &initPlayer); + void loadKVModGrades(); + void saveKVModGrades() const; + /* * Functions for load and save player database informations */ @@ -421,6 +424,10 @@ class PlayerWheel { PlayerWheelMethodsBonusData m_playerBonusData; std::unique_ptr m_modifierContext; + uint8_t m_modsMaxGrade = {}; + std::array m_basicGrades = { 0 }; + std::array m_supremeGrades = { 0 }; + std::array(WheelStage_t::STAGE_COUNT)> m_stages = { 0 }; std::array(WheelOnThink_t::TOTAL_COUNT)> m_onThink = { 0 }; std::array(WheelStat_t::TOTAL_COUNT)> m_stats = { 0 }; diff --git a/src/io/functions/iologindata_load_player.cpp b/src/io/functions/iologindata_load_player.cpp index e724d0ceda9..2b7d3a0cb4d 100644 --- a/src/io/functions/iologindata_load_player.cpp +++ b/src/io/functions/iologindata_load_player.cpp @@ -978,6 +978,7 @@ void IOLoginDataLoad::loadPlayerInitializeSystem(const std::shared_ptr & // Wheel loading player->wheel()->loadDBPlayerSlotPointsOnLogin(); + player->wheel()->loadKVModGrades(); player->wheel()->initializePlayerData(); player->achiev()->loadUnlockedAchievements();