Skip to content

Commit

Permalink
refactor: removeMoneyBank (#2887)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
omarcopires authored Sep 24, 2024
1 parent a289602 commit f0830c3
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions data/libs/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0830c3

Please sign in to comment.