Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
refactor: Use ox_lib and ox_core accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
antond15 committed Jul 28, 2022
1 parent 30dfe3e commit cbeb94d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 42 deletions.
15 changes: 11 additions & 4 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ local OpenBank = function(bank)
})

if not isInBank then
SendNotify('Please note that you can only deposit money at bank', 'inform')
lib.notify({
type = 'inform',
description = 'Please note that you can only deposit money at bank'
})
end
end)
end
Expand All @@ -26,9 +29,10 @@ local CloseBank = function()
SetNuiFocus(false, false)
end

local currentResource = GetCurrentResourceName()
AddEventHandler('onResourceStop', function(resource)
if resource == currentResource then CloseBank() end
if resource == cache.resource then
CloseBank()
end
end)


Expand All @@ -49,7 +53,10 @@ RegisterNUICallback('deposit', function(data)
if isInBank then
TriggerServerEvent('orp_banking:deposit', data)
else
SendNotify('You cannot deposit money at an ATM', 'error')
lib.notify({
type = 'error',
description = 'You cannot deposit money at an ATM'
})
end
end)

Expand Down
13 changes: 0 additions & 13 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,3 @@ Config.BankZones = {
maxZ = 33.12
}
}

SendNotify = function(msg, type)

exports.ox_inventory:notify({
text = msg,
type = type
})

-- exports['t-notify']:Alert({ style = type, message = msg }) -- https://github.com/TasoOneAsia/t-notify

end

RegisterNetEvent('orp_banking:notify', SendNotify)
6 changes: 5 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ version '2.0.0'


shared_script '@ox_lib/init.lua'
server_script 'server.lua'

server_scripts {
'@ox_core/imports/server.lua',
'server.lua'
}

client_scripts {
'config.lua',
Expand Down
84 changes: 60 additions & 24 deletions server.lua
Original file line number Diff line number Diff line change
@@ -1,61 +1,97 @@
local inventory = exports.ox_inventory
local accounts = exports.ox_accounts
local ox_inventory = exports.ox_inventory

lib.callback.register('orp_banking:getBalance', function(source)
return accounts:get(source, 'bank') or 0
local accounts = Ox.GetPlayer(source).getAccounts()
return accounts.get('bank') or 0
end)



RegisterNetEvent('orp_banking:deposit', function(data)
local amount = tonumber(data?.amount)

if not amount or amount <= 0 or amount > inventory:GetItem(source, 'money', false, true) then
TriggerClientEvent('orp_banking:notify', source, 'Invalid amount', 'error')
if not amount or amount <= 0 or amount > ox_inventory:GetItem(source, 'money', nil, true) then
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'Invalid amount'
})
else
inventory:RemoveItem(source, 'money', amount)
accounts:add(source, 'bank', amount)
TriggerClientEvent('orp_banking:update', source, accounts:get(source, 'bank'))
TriggerClientEvent('orp_banking:notify', source, 'You have successfully deposited $'.. amount, 'success')
local accounts = Ox.GetPlayer(source).getAccounts()
accounts.add('bank', amount)
ox_inventory:RemoveItem(source, 'money', amount)

TriggerClientEvent('orp_banking:update', source, accounts.get('bank'))
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = 'You have successfully deposited $'.. amount
})
end
end)

RegisterNetEvent('orp_banking:withdraw', function(data)
local balance = accounts:get(source, 'bank')
local accounts = Ox.GetPlayer(source).getAccounts()
local balance = accounts.get('bank')
local amount = tonumber(data?.amount)

if not amount or amount <= 0 or amount > balance then
TriggerClientEvent('orp_banking:notify', source, 'Invalid amount', 'error')
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'Invalid amount'
})
else
accounts:remove(source, 'bank', amount)
inventory:AddItem(source, 'money', amount)
accounts.remove('bank', amount)
ox_inventory:AddItem(source, 'money', amount)

TriggerClientEvent('orp_banking:update', source, balance - amount)
TriggerClientEvent('orp_banking:notify', source, 'You have successfully withdrawn $'.. amount, 'success')
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = 'You have successfully withdrawn $'.. amount
})
end
end)

RegisterNetEvent('orp_banking:transfer', function(data)
local amount = tonumber(data?.amount)

if type(data?.target) ~= 'number' then
TriggerClientEvent('orp_banking:notify', source, 'Recipient not found', 'error')
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'Recipient not found'
})
elseif data?.target == source then
TriggerClientEvent('orp_banking:notify', source, 'You cannot transfer money to yourself', 'error')
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'You cannot transfer money to yourself'
})
elseif not amount or amount <= 0 then
TriggerClientEvent('orp_banking:notify', source, 'Invalid amount', 'error')
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'Invalid amount'
})
else
local balance = accounts:get(source, 'bank')
local accounts = Ox.GetPlayer(source).getAccounts()
local balance = accounts.get('bank')

if balance <= 0 or balance < amount or amount <= 0 then
TriggerClientEvent('orp_banking:notify', source, 'You don\'t have enough money for this transfer', 'error')
TriggerClientEvent('ox_lib:notify', source, {
type = 'error',
description = 'You don\'t have enough money for this transfer'
})
else
accounts:remove(source, 'bank', amount)
accounts.remove('bank', amount)
TriggerClientEvent('orp_banking:update', source, balance - amount)
TriggerClientEvent('orp_banking:notify', source, 'You have successfully transferred $'.. amount, 'success')
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = 'You have successfully transferred $'.. amount
})

accounts:add(data.target, 'bank', amount)
TriggerClientEvent('orp_banking:update', source, accounts:get(data.target, 'bank'))
TriggerClientEvent('orp_banking:notify', source, 'You just received $'.. amount ..' via bank transfer', 'success')
local target = Ox.GetPlayer(data.target).getAccounts()
target.add('bank', amount)
TriggerClientEvent('orp_banking:update', source, target.get('bank'))
TriggerClientEvent('ox_lib:notify', source, {
type = 'success',
description = 'You just received $'.. amount ..' via bank transfer'
})
end
end
end)

0 comments on commit cbeb94d

Please sign in to comment.