From f0830c37d71e159b76e69090e2c07af4a59faa7e Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 24 Sep 2024 17:32:22 -0300 Subject: [PATCH] refactor: removeMoneyBank (#2887) Improved the `Player.removeMoneyBank` function to make it more efficient. It now uses intermediate variables to avoid repeated calls and simplifies the construction of the message. The logic and messages have been kept as they were. --- data/libs/functions/player.lua | 43 ++++++++++++---------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/data/libs/functions/player.lua b/data/libs/functions/player.lua index a2fb8bc0de6..8dbd0cf9aff 100644 --- a/data/libs/functions/player.lua +++ b/data/libs/functions/player.lua @@ -198,43 +198,30 @@ function Player.withdrawMoney(self, amount) return Bank.withdraw(self, amount) end --- player:removeMoneyBank(money) -function Player:removeMoneyBank(amount) - if type(amount) == "string" then - amount = tonumber(amount) - end - - local moneyCount = self:getMoney() - local bankCount = self:getBankBalance() +function Player.removeMoneyBank(self, amount) + local inventoryMoney = self:getMoney() + local bankBalance = self:getBankBalance() - -- The player have all the money with him - if amount <= moneyCount then - -- Removes player inventory money + if amount <= inventoryMoney then self:removeMoney(amount) - if amount > 0 then self:sendTextMessage(MESSAGE_TRADE, ("Paid %d gold from inventory."):format(amount)) end return true + end - -- The player doens't have all the money with him - elseif amount <= (moneyCount + bankCount) then - -- Check if the player has some money - if moneyCount ~= 0 then - -- Removes player inventory money - self:removeMoney(moneyCount) - local remains = amount - moneyCount - - -- Removes player bank money - Bank.debit(self, remains) + if amount <= (inventoryMoney + bankBalance) then + local remainingAmount = amount - if amount > 0 then - self:sendTextMessage(MESSAGE_TRADE, ("Paid %s from inventory and %s gold from bank account. Your account balance is now %s gold."):format(FormatNumber(moneyCount), FormatNumber(amount - moneyCount), FormatNumber(self:getBankBalance()))) - end - return true + if inventoryMoney > 0 then + self:removeMoney(inventoryMoney) + remainingAmount = remainingAmount - inventoryMoney end - self:setBankBalance(bankCount - amount) - self:sendTextMessage(MESSAGE_TRADE, ("Paid %s gold from bank account. Your account balance is now %s gold."):format(FormatNumber(amount), FormatNumber(self:getBankBalance()))) + + Bank.debit(self, remainingAmount) + + self:setBankBalance(bankBalance - remainingAmount) + self:sendTextMessage(MESSAGE_TRADE, ("Paid %s from inventory and %s gold from bank account. Your account balance is now %s gold."):format(FormatNumber(amount - remainingAmount), FormatNumber(remainingAmount), FormatNumber(self:getBankBalance()))) return true end return false