From d643fba748b329076159ec3f5f98f3c631ec4b67 Mon Sep 17 00:00:00 2001 From: Luan Santos Date: Sun, 17 Mar 2024 13:12:27 -0700 Subject: [PATCH] fix: check if bankable is valid player before emitting metric (#2453) --- src/game/bank/bank.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/game/bank/bank.cpp b/src/game/bank/bank.cpp index 5f8db90fd1a..30345e0495b 100644 --- a/src/game/bank/bank.cpp +++ b/src/game/bank/bank.cpp @@ -97,16 +97,17 @@ bool Bank::transferTo(const std::shared_ptr destination, uint64_t amount) g_logger().error("Bank::transferTo: destinationBankable is nullptr"); return false; } - if (destinationBankable->getPlayer() != nullptr) { - auto player = destinationBankable->getPlayer(); - auto name = asLowerCaseString(player->getName()); + + auto destinationPlayer = destinationBankable->getPlayer(); + if (destinationPlayer != nullptr) { + auto name = asLowerCaseString(destinationPlayer->getName()); replaceString(name, " ", ""); if (deniedNames.contains(name)) { g_logger().warn("Bank::transferTo: denied name: {}", name); return false; } - if (player->getTown()->getID() < minTownId) { - g_logger().warn("Bank::transferTo: denied town: {}", player->getTown()->getID()); + if (destinationPlayer->getTown()->getID() < minTownId) { + g_logger().warn("Bank::transferTo: denied town: {}", destinationPlayer->getTown()->getID()); return false; } } @@ -114,8 +115,15 @@ bool Bank::transferTo(const std::shared_ptr destination, uint64_t amount) if (!(debit(amount) && destination->credit(amount))) { return false; } - g_metrics().addCounter("balance_increase", amount, { { "player", destination->getBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); - g_metrics().addCounter("balance_decrease", amount, { { "player", getBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); + + if (destinationPlayer) { + g_metrics().addCounter("balance_increase", amount, { { "player", destinationPlayer->getName() }, { "context", "bank_transfer" } }); + } + + if (bankable->getPlayer()) { + g_metrics().addCounter("balance_decrease", amount, { { "player", bankable->getPlayer()->getName() }, { "context", "bank_transfer" } }); + } + return true; } @@ -151,6 +159,8 @@ bool Bank::deposit(const std::shared_ptr destination, uint64_t amount) { if (!g_game().removeMoney(bankable->getPlayer(), amount)) { return false; } - g_metrics().addCounter("balance_increase", amount, { { "player", bankable->getPlayer()->getName() }, { "context", "bank_deposit" } }); + if (bankable->getPlayer() != nullptr) { + g_metrics().addCounter("balance_decrease", amount, { { "player", bankable->getPlayer()->getName() }, { "context", "bank_deposit" } }); + } return destination->credit(amount); }