diff --git a/README.md b/README.md index efc4fb4d..c4f408ee 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,13 @@ git clone https://github.com/ESX-Org/esx_billing [esx]/esx_billing ## Installation - Import `esx_billing.sql` in your database -- Add this in your server.cfg : +- Add this to your `server.cfg`: ``` start esx_billing ``` ## Usage - Press `[F7]` To show the billing menu ```lua diff --git a/__resource.lua b/__resource.lua index 090223ed..5363db2f 100644 --- a/__resource.lua +++ b/__resource.lua @@ -2,7 +2,7 @@ resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937' description 'ESX Billing' -version '1.0.1' +version '1.1.0' server_scripts { '@mysql-async/lib/MySQL.lua', @@ -30,3 +30,5 @@ client_scripts { 'config.lua', 'client/main.lua' } + +dependency 'es_extended' \ No newline at end of file diff --git a/client/main.lua b/client/main.lua index 68cce371..84af3132 100644 --- a/client/main.lua +++ b/client/main.lua @@ -11,9 +11,7 @@ local Keys = { } ESX = nil -local GUI = {} local isDead = false -GUI.Time = 0 Citizen.CreateThread(function() while ESX == nil do @@ -64,10 +62,9 @@ end -- Key controls Citizen.CreateThread(function() while true do - Citizen.Wait(25) - if IsControlPressed(0, Keys["F7"]) and not isDead and not ESX.UI.Menu.IsOpen('default', GetCurrentResourceName(), 'billing') and (GetGameTimer() - GUI.Time) > 150 then + Citizen.Wait(10) + if IsControlJustReleased(0, Keys["F7"]) and not isDead and not ESX.UI.Menu.IsOpen('default', GetCurrentResourceName(), 'billing') then ShowBillsMenu() - GUI.Time = GetGameTimer() end end end) diff --git a/server/main.lua b/server/main.lua index ae66ab5f..b420be06 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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)