Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removeMoneyBank #2887

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading