239 lines
7.8 KiB
Lua
239 lines
7.8 KiB
Lua
--110126
|
|
local function GetSocietyAccountName(jobname)
|
|
if not jobname or jobname == '' then return nil end
|
|
if string.sub(jobname, 1, 8) == 'society_' then
|
|
return jobname
|
|
end
|
|
return 'society_' .. jobname
|
|
end
|
|
|
|
local function AwaitSharedAccount(accountName, fn)
|
|
if not accountName or accountName == '' then return fn(nil) end
|
|
|
|
local done = false
|
|
local result = nil
|
|
|
|
TriggerEvent('esx_addonaccount:getSharedAccount', accountName, function(account)
|
|
result = fn(account)
|
|
done = true
|
|
end)
|
|
|
|
while not done do
|
|
Wait(0)
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
local BankingSystems = {
|
|
['codem-bossmenuv2'] = {
|
|
getMoney = 'GetMoneyJob',
|
|
removeMoney = 'RemoveMoneyJob',
|
|
addMoney = 'AddMoneyJob'
|
|
},
|
|
['mBossmenu'] = {
|
|
getMoney = 'GetSocietyMoney',
|
|
removeMoney = 'RemoveSocietyMoney',
|
|
addMoney = 'AddSocietyMoney'
|
|
},
|
|
['qb-management'] = {
|
|
getMoney = 'GetAccount',
|
|
removeMoney = 'RemoveMoney',
|
|
addMoney = 'AddMoney'
|
|
},
|
|
['okokBanking'] = {
|
|
getMoney = 'GetAccount',
|
|
removeMoney = 'RemoveMoney',
|
|
addMoney = 'AddMoney'
|
|
},
|
|
|
|
['qb-banking'] = {
|
|
getMoney = 'GetAccountBalance',
|
|
removeMoney = 'RemoveMoney',
|
|
addMoney = 'AddMoney'
|
|
},
|
|
['tgiann-bank'] = {
|
|
getMoney = 'GetJobAccountBalance',
|
|
removeMoney = 'RemoveJobMoney',
|
|
addMoney = 'AddJobMoney'
|
|
},
|
|
['p_banking'] = {
|
|
getMoney = 'getAccountMoney',
|
|
removeMoney = 'removeAccountMoney',
|
|
addMoney = 'addAccountMoney'
|
|
},
|
|
|
|
['Renewed-Banking'] = {
|
|
getMoney = 'GetAccountMoney',
|
|
removeMoney = 'RemoveAccountMoney',
|
|
addMoney = 'AddAccountMoney'
|
|
},
|
|
['esx_addonaccount'] = {
|
|
isNative = true,
|
|
getMoney = function(jobname)
|
|
return AwaitSharedAccount(GetSocietyAccountName(jobname), function(account)
|
|
return account and (tonumber(account.money) or 0) or 0
|
|
end)
|
|
end,
|
|
removeMoney = function(jobname, amount)
|
|
return AwaitSharedAccount(GetSocietyAccountName(jobname), function(account)
|
|
if not account then return false end
|
|
local bal = tonumber(account.money) or 0
|
|
if bal < amount then return false end
|
|
account.removeMoney(amount)
|
|
return true
|
|
end)
|
|
end,
|
|
addMoney = function(jobname, amount)
|
|
return AwaitSharedAccount(GetSocietyAccountName(jobname), function(account)
|
|
if not account then
|
|
print('account is nil/false')
|
|
return false
|
|
end
|
|
account.addMoney(amount)
|
|
return true
|
|
end)
|
|
end
|
|
}
|
|
}
|
|
|
|
local function GetActiveBankingSystem()
|
|
for resource, methods in pairs(BankingSystems) do
|
|
if GetResourceState(resource) == 'started' then
|
|
return resource, methods
|
|
end
|
|
end
|
|
return nil, nil
|
|
end
|
|
|
|
local VoidReturnSystems = {
|
|
['qb-management'] = { addMoney = true, removeMoney = true },
|
|
['qb-banking'] = { addMoney = true, removeMoney = true },
|
|
}
|
|
|
|
local function CallBankingMethod(resource, methods, methodName, jobname, amount)
|
|
if methods.isNative then
|
|
if amount then
|
|
return methods[methodName](jobname, amount)
|
|
end
|
|
return methods[methodName](jobname)
|
|
end
|
|
|
|
local success, res = pcall(function()
|
|
if amount then
|
|
return exports[resource][methods[methodName]](exports[resource], jobname, amount)
|
|
end
|
|
return exports[resource][methods[methodName]](exports[resource], jobname)
|
|
end)
|
|
|
|
if not success then return nil end
|
|
|
|
local isVoidReturn = VoidReturnSystems[resource] and VoidReturnSystems[resource][methodName]
|
|
if isVoidReturn and res == nil then
|
|
return true
|
|
end
|
|
|
|
return res
|
|
end
|
|
|
|
function GetJobMoney(job)
|
|
if Config.Framework == 'standalone' then return 0 end
|
|
|
|
local resource, methods = GetActiveBankingSystem()
|
|
if not resource then
|
|
if Config.Framework == 'esx' then
|
|
resource = 'esx_addonaccount'
|
|
methods = BankingSystems[resource]
|
|
else
|
|
for i = 1, 6 do
|
|
print("^1[codem-phone] WARNING: No supported banking system found!^0")
|
|
end
|
|
return 0
|
|
end
|
|
end
|
|
|
|
return CallBankingMethod(resource, methods, 'getMoney', job) or 0
|
|
end
|
|
|
|
function RemoveJobMoney(source, jobname, amount)
|
|
if amount <= 0 then return { success = false, message = 'Invalid amount' } end
|
|
if not source or source <= 0 then return { success = false, message = 'Invalid source' } end
|
|
if not jobname or jobname == '' then return { success = false, message = 'Invalid job name' } end
|
|
if Config.Framework == 'standalone' then
|
|
return { success = false, message = 'Standalone framework does not support job money removal' }
|
|
end
|
|
|
|
local resource, methods = GetActiveBankingSystem()
|
|
if not resource then
|
|
if Config.Framework == 'esx' then
|
|
resource = 'esx_addonaccount'
|
|
methods = BankingSystems[resource]
|
|
else
|
|
for i = 1, 6 do
|
|
print("^1[codem-phone] WARNING: No supported banking system found!^0")
|
|
end
|
|
return { success = false, message = 'No banking system found' }
|
|
end
|
|
end
|
|
|
|
local currentBalance = CallBankingMethod(resource, methods, 'getMoney', jobname)
|
|
if not currentBalance or currentBalance < amount then
|
|
return { success = false, message = 'Not enough society money' }
|
|
end
|
|
|
|
local ok = CallBankingMethod(resource, methods, 'removeMoney', jobname, amount)
|
|
if ok == false then
|
|
return { success = false, message = 'Not enough society money' }
|
|
end
|
|
|
|
if not Core.Functions.AddMoney(source, amount, 'bank') then
|
|
return { success = false, message = 'Failed to add money to bank' }
|
|
end
|
|
|
|
return { success = true, message = 'Money withdrawn successfully' }
|
|
end
|
|
|
|
function AddJobMoney(source, jobname, amount)
|
|
if amount <= 0 then return { success = false, message = 'Invalid amount' } end
|
|
local ignoreSource = source == 'ignore'
|
|
|
|
if not ignoreSource then
|
|
if type(source) ~= 'number' or source <= 0 then
|
|
return { success = false, message = 'Invalid source' }
|
|
end
|
|
end
|
|
if not jobname or jobname == '' then return { success = false, message = 'Invalid job name' } end
|
|
if Config.Framework == 'standalone' then
|
|
return { success = false, message = 'Standalone framework does not support job money removal' }
|
|
end
|
|
|
|
local resource, methods = GetActiveBankingSystem()
|
|
if not resource then
|
|
if Config.Framework == 'esx' then
|
|
resource = 'esx_addonaccount'
|
|
methods = BankingSystems[resource]
|
|
else
|
|
for i = 1, 6 do
|
|
print("^1[codem-phone] WARNING: No supported banking system found!^0")
|
|
end
|
|
return { success = false, message = 'No banking system found' }
|
|
end
|
|
end
|
|
|
|
if not ignoreSource then
|
|
if not Core.Functions.RemoveMoney(source, amount, 'bank') then
|
|
return { success = false, message = 'Failed to remove money from bank' }
|
|
end
|
|
end
|
|
|
|
local ok = CallBankingMethod(resource, methods, 'addMoney', jobname, amount)
|
|
if ok == false then
|
|
if not ignoreSource then
|
|
Core.Functions.AddMoney(source, amount, 'bank')
|
|
end
|
|
return { success = false, message = 'Failed to add job money' }
|
|
end
|
|
|
|
return { success = true, message = 'Money added successfully' }
|
|
end
|