Improvements

This commit is contained in:
ElPumpo
2018-07-06 22:33:35 +02:00
parent 9fee9d2c31
commit 7bbd1258b3
4 changed files with 50 additions and 80 deletions
+44 -72
View File
@@ -4,10 +4,9 @@ TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterServerEvent('esx_billing:sendBill')
AddEventHandler('esx_billing:sendBill', function(playerId, sharedAccountName, label, amount)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local xPlayers = ESX.GetPlayers()
local xTarget = ESX.GetPlayerFromId(playerId)
TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account)
@@ -15,64 +14,49 @@ AddEventHandler('esx_billing:sendBill', function(playerId, sharedAccountName, la
print('esx_billing: ' .. GetPlayerName(_source) .. ' tried sending a negative bill!')
elseif account == nil then
for i=1, #xPlayers, 1 do
local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
if xPlayer2.source == playerId then
MySQL.Async.execute(
'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
{
['@identifier'] = xPlayer2.identifier,
['@sender'] = xPlayer.identifier,
['@target_type'] = 'player',
['@target'] = xPlayer.identifier,
['@label'] = label,
['@amount'] = amount
},
function(rowsChanged)
TriggerClientEvent('esx:showNotification', xPlayer2.source, _U('received_invoice'))
end
)
break
end
if xTarget ~= nil then
MySQL.Async.execute(
'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
{
['@identifier'] = xTarget.identifier,
['@sender'] = xPlayer.identifier,
['@target_type'] = 'player',
['@target'] = xPlayer.identifier,
['@label'] = label,
['@amount'] = amount
},
function(rowsChanged)
TriggerClientEvent('esx:showNotification', xTarget.source, _U('received_invoice'))
end
)
end
else
for i=1, #xPlayers, 1 do
local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
if xPlayer2.source == playerId then
MySQL.Async.execute(
'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
{
['@identifier'] = xPlayer2.identifier,
['@sender'] = xPlayer.identifier,
['@target_type'] = 'society',
['@target'] = sharedAccountName,
['@label'] = label,
['@amount'] = amount
},
function(rowsChanged)
TriggerClientEvent('esx:showNotification', xPlayer2.source, _U('received_invoice'))
end
)
break
end
if xTarget ~= nil then
MySQL.Async.execute(
'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
{
['@identifier'] = xTarget.identifier,
['@sender'] = xPlayer.identifier,
['@target_type'] = 'society',
['@target'] = sharedAccountName,
['@label'] = label,
['@amount'] = amount
},
function(rowsChanged)
TriggerClientEvent('esx:showNotification', xTarget.source, _U('received_invoice'))
end
)
end
end
end)
end)
ESX.RegisterServerCallback('esx_billing:getBills', function(source, cb)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local xPlayer = ESX.GetPlayerFromId(source)
MySQL.Async.fetchAll(
'SELECT * FROM billing WHERE identifier = @identifier',
@@ -104,9 +88,7 @@ end)
ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local xPlayer = ESX.GetPlayerFromId(source)
MySQL.Async.fetchAll(
'SELECT * FROM billing WHERE id = @id',
@@ -119,22 +101,12 @@ ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
local targetType = result[1].target_type
local target = result[1].target
local amount = result[1].amount
local xPlayers = ESX.GetPlayers()
local foundPlayer = nil
for i=1, #xPlayers, 1 do
local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
if xPlayer2.identifier == sender then
foundPlayer = xPlayer2
break
end
end
local xTarget = ESX.GetPlayerFromId(sender)
if targetType == 'player' then
if foundPlayer ~= nil then
if xTarget ~= nil then
if xPlayer.get('money') >= amount then
@@ -146,10 +118,10 @@ ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
function(rowsChanged)
xPlayer.removeMoney(amount)
foundPlayer.addMoney(amount)
xTarget.addMoney(amount)
TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_invoice', amount))
TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('received_payment', amount))
TriggerClientEvent('esx:showNotification', xTarget.source, _U('received_payment', amount))
cb()
@@ -157,7 +129,7 @@ ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
)
else
TriggerClientEvent('esx:showNotification', _source, _U('player_not_logged'))
TriggerClientEvent('esx:showNotification', source, _U('player_not_logged'))
cb()
end
@@ -175,15 +147,15 @@ ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
xPlayer.removeMoney(amount)
account.addMoney(amount)
TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_invoice', amount))
if foundPlayer ~= nil then
TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('received_payment', amount))
if xTarget ~= nil then
TriggerClientEvent('esx:showNotification', xTarget.source, _U('received_payment', amount))
end
cb()
end)
else
TriggerClientEvent('esx:showNotification', xPlayer.source, _U('no_money'))
if foundPlayer ~= nil then
TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('target_no_money'))
if xTarget ~= nil then
TriggerClientEvent('esx:showNotification', xTarget.source, _U('target_no_money'))
end
end
end)