Skip to content

Commit

Permalink
refactor(server/shops): move currency check and removal to functions
Browse files Browse the repository at this point in the history
Should be easier for people to find if they want to change it.
  • Loading branch information
thelindat committed Feb 26, 2023
1 parent 69a9334 commit 6c44b9f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions modules/shops/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,23 @@ local table = lib.table
-- http://lua-users.org/wiki/FormattingNumbers
-- credit http://richard.warburton.it
local function comma_value(n)
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
local left, num, right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

local function canAffordItem(inv, currency, price)
local canAfford = price >= 0 and Inventory.GetItem(inv, currency, false, true) >= price

return canAfford or {
type = 'error',
description = locale('cannot_afford', ('%s%s'):format((currency == 'money' and locale('$') or comma_value(price)), (currency == 'money' and comma_value(price) or ' '..Items(currency).label)))
}
end

local function removeCurrency(inv, currency, price)
Inventory.RemoveItem(inv, currency, price)
end

lib.callback.register('ox_inventory:buyItem', function(source, data)
if data.toType == 'player' then
if data.count == nil then data.count = 1 end
Expand Down Expand Up @@ -218,10 +231,10 @@ lib.callback.register('ox_inventory:buyItem', function(source, data)
return false, false, { type = 'error', description = locale('cannot_carry') }
end

local canAfford = price >= 0 and Inventory.GetItem(source, currency, false, true) >= price
local canAfford = canAffordItem(playerInv, currency, price)

if not canAfford then
return false, false, { type = 'error', description = locale('cannot_afford', ('%s%s'):format((currency == 'money' and locale('$') or comma_value(price)), (currency == 'money' and comma_value(price) or ' '..Items(currency).label))) }
if canAfford ~= true then
return false, false, canAfford
end

if not TriggerEventHooks('buyItem', {
Expand All @@ -240,7 +253,7 @@ lib.callback.register('ox_inventory:buyItem', function(source, data)

Inventory.SetSlot(playerInv, fromItem, count, metadata, data.toSlot)
playerInv.weight = newWeight
Inventory.RemoveItem(source, currency, price)
removeCurrency(playerInv, currency, price)

if fromData.count then
shop.items[data.fromSlot].count = fromData.count - count
Expand Down

0 comments on commit 6c44b9f

Please sign in to comment.