Skip to content

Commit

Permalink
improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed May 23, 2024
1 parent 7d0e73e commit a3e9b40
Showing 4 changed files with 43 additions and 35 deletions.
43 changes: 23 additions & 20 deletions src/creatures/players/cyclopedia/player_cyclopedia.cpp
Original file line number Diff line number Diff line change
@@ -86,15 +86,15 @@ void PlayerCyclopedia::loadDeathHistory() {
g_logger().debug("Checking and updating death history of player {} took {} milliseconds.", m_player.getName(), bm_check.duration());
}

void PlayerCyclopedia::updateStoreSummary(uint8_t type, uint16_t amount, uint16_t id) {
void PlayerCyclopedia::updateStoreSummary(uint8_t type, uint16_t amount, const std::string &id) {
switch (type) {
case Summary_t::HOUSE_ITEMS:
case Summary_t::BLESSINGS:
insertValue(type, amount, id);
break;
case Summary_t::ALL_BLESSINGS:
for (int i = 1; i < 8; ++i) {
insertValue(static_cast<uint8_t>(Summary_t::BLESSINGS), amount, i);
insertValue(static_cast<uint8_t>(Summary_t::BLESSINGS), amount, fmt::format("{}", i));
}
break;
default:
@@ -104,13 +104,13 @@ void PlayerCyclopedia::updateStoreSummary(uint8_t type, uint16_t amount, uint16_
}

uint16_t PlayerCyclopedia::getAmount(uint8_t type) {
auto kv = m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type))->get("amount");
return static_cast<uint16_t>(kv ? kv->getNumber() : 0);
auto kvScope = m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type))->get("amount");
return static_cast<uint16_t>(kvScope ? kvScope->getNumber() : 0);
}

void PlayerCyclopedia::updateAmount(uint8_t type, uint16_t toAddPoints) {
auto oldPoints = getAmount(type);
m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type))->set("amount", oldPoints + toAddPoints);
void PlayerCyclopedia::updateAmount(uint8_t type, uint16_t amount) {
auto oldAmount = getAmount(type);
m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type))->set("amount", oldAmount + amount);
}

std::vector<RecentDeathEntry> PlayerCyclopedia::getDeathHistory() const {
@@ -133,24 +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<uint16_t, uint16_t> PlayerCyclopedia::getResult(uint8_t type) const {
std::map<std::string, uint16_t> PlayerCyclopedia::getResult(uint8_t type) const {
auto kvScope = m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type));
std::map<uint16_t, uint16_t> result = {};
for (const auto &id : kvScope->keys()) {
auto kv = kvScope->scoped(id)->get("amount");
result[std::stoull(id)] = static_cast<uint16_t>(kv ? kv->getNumber() : 0);
std::map<std::string, uint16_t> result;
for (const auto &scope : kvScope->keys()) {
size_t pos = scope.find('.');
if (pos == std::string::npos) {
g_logger().error("Invalid key format: {}", 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));
}
return result;
}

void PlayerCyclopedia::insertValue(uint8_t type, uint16_t amount, uint16_t id) {
auto kv = m_player.kv()->scoped("summary")->scoped(g_game().getSummaryKeyByType(type));
auto oldAmount = 0;
void PlayerCyclopedia::insertValue(uint8_t type, uint16_t amount, const std::string &id) {
auto result = getResult(type);
auto it = result.find(id);
if (it != result.end()) {
oldAmount = it->second;
}
kv->scoped(fmt::format("{}", id))->set("amount", oldAmount + amount);
g_logger().info("type: {}, old: {}, amount: {}, newamount: {}, id: {}", type, oldAmount, amount, oldAmount + amount, 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);
}
8 changes: 4 additions & 4 deletions src/creatures/players/cyclopedia/player_cyclopedia.hpp
Original file line number Diff line number Diff line change
@@ -35,9 +35,9 @@ class PlayerCyclopedia {
void loadRecentKills();
void loadDeathHistory();

void updateStoreSummary(uint8_t type, uint16_t amount = 1, uint16_t id = 0);
void updateStoreSummary(uint8_t type, uint16_t amount = 1, const std::string &id = "");
uint16_t getAmount(uint8_t type);
void updateAmount(uint8_t type, uint16_t toAddPoints);
void updateAmount(uint8_t type, uint16_t amount = 1);

[[nodiscard]] std::vector<RecentDeathEntry> getDeathHistory() const;
void insertDeathOnHistory(std::string cause, uint32_t timestamp);
@@ -47,8 +47,8 @@ class PlayerCyclopedia {

Summary getSummary();

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

private:
std::vector<RecentDeathEntry> m_deathHistory;
2 changes: 1 addition & 1 deletion src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
@@ -4370,7 +4370,7 @@ int PlayerFunctions::luaPlayerCreateTransactionSummary(lua_State* L) {
}

auto amount = getNumber<uint16_t>(L, 3, 1);
auto id = getNumber<uint16_t>(L, 4, 0);
auto id = getString(L, 4, "");

g_logger().info("type: {}, amount: {}, id: {}", type, amount, id);
player->cyclopedia()->updateStoreSummary(type, amount, id);
25 changes: 15 additions & 10 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
@@ -3935,18 +3935,23 @@ void ProtocolGame::sendCyclopediaCharacterStoreSummary() {
msg.addByte(CYCLOPEDIA_CHARACTERINFO_STORESUMMARY);
msg.addByte(0x00); // 0x00 Here means 'no error'
msg.add<uint32_t>(player->getXpBoostTime()); // Remaining Store Xp Boost Time
msg.add<uint32_t>(0); // RemainingDailyRewardXpBoostTime
auto remaining = player->kv()->get("daily-reward-xp-boost");
msg.add<uint32_t>(remaining ? static_cast<uint32_t>(remaining->getNumber()) : 0x00); // Remaining Daily Reward Xp Boost Time

auto cyclopediaSummary = player->cyclopedia()->getSummary();

// getBlessingsObtained
auto blessings = player->getBlessingNames();
msg.addByte(static_cast<uint8_t>(blessings.size()));
auto amountsByBlessing = player->cyclopedia()->getResult(static_cast<uint8_t>(Summary_t::BLESSINGS));
for (const auto &bless_it : blessings) {
msg.addString(bless_it.second, "ProtocolGame::sendCyclopediaCharacterStoreSummary - blessing.name");
msg.addByte(static_cast<uint8_t>(amountsByBlessing[bless_it.first]));
g_logger().info("bless id: {}, name: {}, found(count): {}", bless_it.first, bless_it.second, amountsByBlessing[bless_it.first]);
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;
});
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);
}

uint8_t preySlotsUnlocked = 0;
@@ -3996,8 +4001,8 @@ 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[hItem_it.first];
msg.add<uint16_t>(hItem_it.first); // Item ID
const ItemType &it = Item::items[std::stoll(hItem_it.first)];
msg.add<uint16_t>(it.id); // Item ID
msg.addString(it.name, "ProtocolGame::sendCyclopediaCharacterStoreSummary - houseItem.name");
msg.addByte(hItem_it.second);
}

0 comments on commit a3e9b40

Please sign in to comment.