From 0a56230ebdaefe591f16f2111f794d2c7ca760b0 Mon Sep 17 00:00:00 2001 From: Luan Santos Date: Sun, 17 Mar 2024 10:29:59 -0700 Subject: [PATCH 1/3] fix: check if bankable is valid player before emitting metric --- src/game/bank/bank.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/game/bank/bank.cpp b/src/game/bank/bank.cpp index 5f8db90fd1a..56ca39ba3b0 100644 --- a/src/game/bank/bank.cpp +++ b/src/game/bank/bank.cpp @@ -114,8 +114,10 @@ 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 (destinationBankable->getPlayer() != nullptr) { + g_metrics().addCounter("balance_increase", amount, { { "player", destinationBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); + g_metrics().addCounter("balance_decrease", amount, { { "player", getBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); + } return true; } @@ -151,6 +153,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); } From 888ac90c634e6ff06dcf289e8490c2822807c614 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Sun, 17 Mar 2024 14:48:45 -0300 Subject: [PATCH 2/3] fix: compile error --- src/game/bank/bank.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game/bank/bank.cpp b/src/game/bank/bank.cpp index 56ca39ba3b0..a90a4229998 100644 --- a/src/game/bank/bank.cpp +++ b/src/game/bank/bank.cpp @@ -115,7 +115,7 @@ bool Bank::transferTo(const std::shared_ptr destination, uint64_t amount) return false; } if (destinationBankable->getPlayer() != nullptr) { - g_metrics().addCounter("balance_increase", amount, { { "player", destinationBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); + g_metrics().addCounter("balance_increase", amount, { { "player", destinationBankable->getPlayer()->getName() }, { "context", "bank_transfer" } }); g_metrics().addCounter("balance_decrease", amount, { { "player", getBankable()->getPlayer()->getName() }, { "context", "bank_transfer" } }); } return true; From efebe19b3b657eb772dd77eb5719a9c25ceb3bd2 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Sun, 17 Mar 2024 15:27:43 -0300 Subject: [PATCH 3/3] fix: bankable player check --- src/game/bank/bank.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/game/bank/bank.cpp b/src/game/bank/bank.cpp index a90a4229998..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,10 +115,15 @@ bool Bank::transferTo(const std::shared_ptr destination, uint64_t amount) if (!(debit(amount) && destination->credit(amount))) { return false; } - if (destinationBankable->getPlayer() != nullptr) { - g_metrics().addCounter("balance_increase", amount, { { "player", destinationBankable->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; }