From 1f2ea4de6819d29fdf5e1be89b67927053a09b75 Mon Sep 17 00:00:00 2001 From: Zerio Date: Wed, 19 Jun 2024 20:14:26 +0000 Subject: [PATCH 1/5] feat: add export for `BillPlayer` --- server/main.lua | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/server/main.lua b/server/main.lua index a146b73..2701546 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,34 +1,48 @@ - -RegisterNetEvent('esx_billing:sendBill', function(playerId, sharedAccountName, label, amount) - local xPlayer = ESX.GetPlayerFromId(source) - local xTarget = ESX.GetPlayerFromId(playerId) +function BillPlayer(targetId, senderIdentifier, sharedAccountName, label, amount) + local xTarget = ESX.GetPlayerFromId(targetId) amount = ESX.Math.Round(amount) if amount > 0 and xTarget then if string.match(sharedAccountName, "society_") then - local jobName = string.gsub(sharedAccountName, 'society_', '') - if xPlayer.job.name ~= jobName then - print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from a society (^5%s^7), but does not have the correct Job - Possibly Cheats"):format(xPlayer.source, sharedAccountName)) - return - end TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account) if account then - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, xPlayer.identifier, 'society', sharedAccountName, label, amount}, + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, senderIdentifier, 'society', sharedAccountName, label, amount}, function(rowsChanged) xTarget.showNotification(TranslateCap('received_invoice')) end) else - print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(xPlayer.source, sharedAccountName)) + print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(senderIdentifier, sharedAccountName)) end end) else - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, xPlayer.identifier, 'player', xPlayer.identifier, label, amount}, + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, senderIdentifier, 'player', senderIdentifier, label, amount}, function(rowsChanged) xTarget.showNotification(TranslateCap('received_invoice')) end) end end +end + +RegisterNetEvent('esx_billing:sendBill', function(targetId, sharedAccountName, label, amount) + local xPlayer = ESX.GetPlayerFromId(source) + local jobName = string.gsub(sharedAccountName, 'society_', '') + + if xPlayer.job.name ~= jobName then + print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from a society (^5%s^7), but does not have the correct Job - Possibly Cheats"):format(xPlayer.source, sharedAccountName)) + return + end + + SendBill(targetId, xPlayer.identifier, sharedAccountName, label, amount) end) +exports("BillPlayer", BillPlayer) + +function BillPlayerByIdentifier(playerIdentifier, sharedAccountName, label, amount) + +end + +RegisterNetEvent('esx_billing:sendBillToIdentifier', BillPlayerByIdentifier) +exports("BillPlayerByIdentifier", BillPlayerByIdentifier) + ESX.RegisterServerCallback('esx_billing:getBills', function(source, cb) local xPlayer = ESX.GetPlayerFromId(source) From ea1d69328400d93785c8cb6a9ca1b0139c188cf4 Mon Sep 17 00:00:00 2001 From: Zerio Date: Wed, 19 Jun 2024 20:15:53 +0000 Subject: [PATCH 2/5] feat: add `BillPlayerByIdentifier` event & export --- server/main.lua | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/server/main.lua b/server/main.lua index 2701546..9eae95e 100644 --- a/server/main.lua +++ b/server/main.lua @@ -36,11 +36,46 @@ RegisterNetEvent('esx_billing:sendBill', function(targetId, sharedAccountName, l end) exports("BillPlayer", BillPlayer) -function BillPlayerByIdentifier(playerIdentifier, sharedAccountName, label, amount) +function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccountName, label, amount) + local xTarget = ESX.GetPlayerFromIdentifier(targetIdentifier) + amount = ESX.Math.Round(amount) + if amount > 0 then + if string.match(sharedAccountName, "society_") then + TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account) + if account then + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'society', sharedAccountName, label, amount}, + function(rowsChanged) + if xTarget then + xTarget.showNotification(TranslateCap('received_invoice')) + end + end) + else + print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(senderIdentifier, sharedAccountName)) + end + end) + else + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'player', senderIdentifier, label, amount}, + function(rowsChanged) + if xTarget then + xTarget.showNotification(TranslateCap('received_invoice')) + end + end) + end + end end -RegisterNetEvent('esx_billing:sendBillToIdentifier', BillPlayerByIdentifier) +RegisterNetEvent('esx_billing:sendBillToIdentifier', function(targetIdentifier, sharedAccountName, label, amount) + local xPlayer = ESX.GetPlayerFromId(source) + local jobName = string.gsub(sharedAccountName, 'society_', '') + + if xPlayer.job.name ~= jobName then + print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from a society (^5%s^7), but does not have the correct Job - Possibly Cheats"):format(xPlayer.source, sharedAccountName)) + return + end + + BillPlayerByIdentifier(targetIdentifier, xPlayer.identifier, sharedAccountName, label, amount) +end) exports("BillPlayerByIdentifier", BillPlayerByIdentifier) From 5560d7bb27ad61075d19ed8559ae77f60bd1f8a9 Mon Sep 17 00:00:00 2001 From: Zerio Date: Wed, 19 Jun 2024 22:37:25 +0000 Subject: [PATCH 3/5] chore: improve code duplication --- server/main.lua | 57 +++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/server/main.lua b/server/main.lua index 9eae95e..2a4020e 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,28 +1,40 @@ -function BillPlayer(targetId, senderIdentifier, sharedAccountName, label, amount) - local xTarget = ESX.GetPlayerFromId(targetId) +function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccountName, label, amount) + local xTarget = ESX.GetPlayerFromIdentifier(targetIdentifier) amount = ESX.Math.Round(amount) - if amount > 0 and xTarget then + if amount > 0 then if string.match(sharedAccountName, "society_") then TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account) if account then - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, senderIdentifier, 'society', sharedAccountName, label, amount}, + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'society', sharedAccountName, label, amount}, function(rowsChanged) - xTarget.showNotification(TranslateCap('received_invoice')) + if xTarget then + xTarget.showNotification(TranslateCap('received_invoice')) + end end) else print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(senderIdentifier, sharedAccountName)) end end) else - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {xTarget.identifier, senderIdentifier, 'player', senderIdentifier, label, amount}, + MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'player', senderIdentifier, label, amount}, function(rowsChanged) - xTarget.showNotification(TranslateCap('received_invoice')) + if xTarget then + xTarget.showNotification(TranslateCap('received_invoice')) + end end) end end end +function BillPlayer(targetId, senderIdentiier, sharedAccountName, label, amount) + local xTarget = ESX.GetPlayerFromId(targetId) + + if xTarget then + BillPlayerByIdentifier(xTarget.identifier, senderIdentiier, sharedAccountName, label, amount) + end +end + RegisterNetEvent('esx_billing:sendBill', function(targetId, sharedAccountName, label, amount) local xPlayer = ESX.GetPlayerFromId(source) local jobName = string.gsub(sharedAccountName, 'society_', '') @@ -32,39 +44,10 @@ RegisterNetEvent('esx_billing:sendBill', function(targetId, sharedAccountName, l return end - SendBill(targetId, xPlayer.identifier, sharedAccountName, label, amount) + BillPlayer(targetId, xPlayer.identifier, sharedAccountName, label, amount) end) exports("BillPlayer", BillPlayer) -function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccountName, label, amount) - local xTarget = ESX.GetPlayerFromIdentifier(targetIdentifier) - amount = ESX.Math.Round(amount) - - if amount > 0 then - if string.match(sharedAccountName, "society_") then - TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account) - if account then - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'society', sharedAccountName, label, amount}, - function(rowsChanged) - if xTarget then - xTarget.showNotification(TranslateCap('received_invoice')) - end - end) - else - print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(senderIdentifier, sharedAccountName)) - end - end) - else - MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'player', senderIdentifier, label, amount}, - function(rowsChanged) - if xTarget then - xTarget.showNotification(TranslateCap('received_invoice')) - end - end) - end - end -end - RegisterNetEvent('esx_billing:sendBillToIdentifier', function(targetIdentifier, sharedAccountName, label, amount) local xPlayer = ESX.GetPlayerFromId(source) local jobName = string.gsub(sharedAccountName, 'society_', '') From c1733334e4616c4446e41e8fd2a42fa4314bf433 Mon Sep 17 00:00:00 2001 From: Zerio Date: Thu, 20 Jun 2024 13:41:35 +0000 Subject: [PATCH 4/5] fix: typo --- server/main.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/main.lua b/server/main.lua index 2a4020e..2ad86ae 100644 --- a/server/main.lua +++ b/server/main.lua @@ -27,11 +27,11 @@ function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccoun end end -function BillPlayer(targetId, senderIdentiier, sharedAccountName, label, amount) +function BillPlayer(targetId, senderIdentifier, sharedAccountName, label, amount) local xTarget = ESX.GetPlayerFromId(targetId) if xTarget then - BillPlayerByIdentifier(xTarget.identifier, senderIdentiier, sharedAccountName, label, amount) + BillPlayerByIdentifier(xTarget.identifier, senderIdentifier, sharedAccountName, label, amount) end end From f66f7a79f6df0cbc862f5f2e75da6068b7eceaca Mon Sep 17 00:00:00 2001 From: Zerio Date: Thu, 20 Jun 2024 21:29:22 +0000 Subject: [PATCH 5/5] fix: code style preference --- server/main.lua | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server/main.lua b/server/main.lua index 2ad86ae..ad2405e 100644 --- a/server/main.lua +++ b/server/main.lua @@ -8,9 +8,11 @@ function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccoun if account then MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'society', sharedAccountName, label, amount}, function(rowsChanged) - if xTarget then - xTarget.showNotification(TranslateCap('received_invoice')) + if not xTarget then + return end + + xTarget.showNotification(TranslateCap('received_invoice')) end) else print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(senderIdentifier, sharedAccountName)) @@ -19,9 +21,11 @@ function BillPlayerByIdentifier(targetIdentifier, senderIdentifier, sharedAccoun else MySQL.insert('INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)', {targetIdentifier, senderIdentifier, 'player', senderIdentifier, label, amount}, function(rowsChanged) - if xTarget then - xTarget.showNotification(TranslateCap('received_invoice')) + if not xTarget then + return end + + xTarget.showNotification(TranslateCap('received_invoice')) end) end end @@ -30,9 +34,11 @@ end function BillPlayer(targetId, senderIdentifier, sharedAccountName, label, amount) local xTarget = ESX.GetPlayerFromId(targetId) - if xTarget then - BillPlayerByIdentifier(xTarget.identifier, senderIdentifier, sharedAccountName, label, amount) + if not xTarget then + return end + + BillPlayerByIdentifier(xTarget.identifier, senderIdentifier, sharedAccountName, label, amount) end RegisterNetEvent('esx_billing:sendBill', function(targetId, sharedAccountName, label, amount)