Skip to content

Commit

Permalink
improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed May 24, 2024
1 parent a3e9b40 commit 647445c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/creatures/players/cyclopedia/player_cyclopedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,27 @@ Summary PlayerCyclopedia::getSummary() {
return { getAmount(Summary_t::BOOSTS), getAmount(Summary_t::PREY_CARDS), getAmount(Summary_t::INSTANT_REWARDS), getAmount(Summary_t::HIRELINGS) };
}

std::map<std::string, uint16_t> PlayerCyclopedia::getResult(uint8_t type) const {
std::map<uint16_t, uint16_t> PlayerCyclopedia::getResult(uint8_t type) const {
auto kvScope = m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type));
std::map<std::string, uint16_t> result;
std::map<uint16_t, uint16_t> result; // ID, amount
for (const auto &scope : kvScope->keys()) {
size_t pos = scope.find('.');
if (pos == std::string::npos) {
g_logger().error("Invalid key format: {}", scope);
g_logger().error("[{}] Invalid key format: {}", __FUNCTION__, scope);
continue;
}
std::string id = scope.substr(0, pos);
auto amount = kvScope->scoped(id)->get("amount");
result.emplace(id, static_cast<uint16_t>(amount ? amount->getNumber() : 0));
result.emplace(std::stoll(id), static_cast<uint16_t>(amount ? amount->getNumber() : 0));
}
return result;
}

void PlayerCyclopedia::insertValue(uint8_t type, uint16_t amount, const std::string &id) {
auto result = getResult(type);
auto it = result.find(id);
auto it = result.find(std::stoll(id));
auto oldAmount = (it != result.end() ? it->second : 0);
auto newAmount = oldAmount + amount;
m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type))->scoped(id)->set("amount", newAmount);
g_logger().info("type: {}, id: {}, old amount: {}, added amount: {}, new amount: {}", type, id, oldAmount, amount, newAmount);
g_logger().debug("type: {}, id: {}, old amount: {}, added amount: {}, new amount: {}", type, id, oldAmount, amount, newAmount);
}
2 changes: 1 addition & 1 deletion src/creatures/players/cyclopedia/player_cyclopedia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PlayerCyclopedia {

Summary getSummary();

[[nodiscard]] std::map<std::string, uint16_t> getResult(uint8_t type) const;
[[nodiscard]] std::map<uint16_t, uint16_t> getResult(uint8_t type) const;
void insertValue(uint8_t type, uint16_t amount = 1, const std::string &id = "");

private:
Expand Down
15 changes: 7 additions & 8 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3944,14 +3944,13 @@ void ProtocolGame::sendCyclopediaCharacterStoreSummary() {
auto blessingNames = player->getBlessingNames();
msg.addByte(static_cast<uint8_t>(blessingNames.size()));
auto blessingsObtained = player->cyclopedia()->getResult(static_cast<uint8_t>(Summary_t::BLESSINGS));
for (const auto &name_it : blessingNames) {
msg.addString(name_it.second, "ProtocolGame::sendCyclopediaCharacterStoreSummary - blessing.name");
auto it = std::find_if(blessingsObtained.begin(), blessingsObtained.end(), [&name_it](const std::pair<std::string, uint16_t> &bName_it) {
return bName_it.first == name_it.first;
for (const auto &bName_it : blessingNames) {
msg.addString(bName_it.second, "ProtocolGame::sendCyclopediaCharacterStoreSummary - blessing.name");
uint16_t blessAmount = 0;
auto it = std::find_if(blessingsObtained.begin(), blessingsObtained.end(), [&bName_it](const auto &b_it) {
return b_it.first == bName_it.first;
});
uint16_t blessAmount = (it != blessingsObtained.end()) ? it->second : 0;
msg.addByte(static_cast<uint16_t>(blessAmount));
g_logger().info("bless id: {}, name: {}, amount: {}", name_it.first, name_it.second, blessAmount);
msg.addByte((it != blessingsObtained.end()) ? static_cast<uint16_t>(it->second) : 0x00);
}

uint8_t preySlotsUnlocked = 0;
Expand Down Expand Up @@ -4001,7 +4000,7 @@ void ProtocolGame::sendCyclopediaCharacterStoreSummary() {
auto houseItems = player->cyclopedia()->getResult(static_cast<uint8_t>(Summary_t::HOUSE_ITEMS));
msg.add<uint16_t>(houseItems.size());
for (const auto &hItem_it : houseItems) {
const ItemType &it = Item::items[std::stoll(hItem_it.first)];
const ItemType &it = Item::items[hItem_it.first];
msg.add<uint16_t>(it.id); // Item ID
msg.addString(it.name, "ProtocolGame::sendCyclopediaCharacterStoreSummary - houseItem.name");
msg.addByte(hItem_it.second);
Expand Down

0 comments on commit 647445c

Please sign in to comment.