Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added money transfer functions #1099

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f09c665
💱 Added money transfer functions
Cocodrulo Apr 18, 2024
e6274cb
👅 Fixed annotation to `TransferMoney` function
Cocodrulo Apr 19, 2024
927e070
🔁 Renamed `TransferTo` function in Player object to `TransferMoneyTo`
Cocodrulo Apr 19, 2024
fbef0f7
Merge branch 'main' into main
GhzGarage May 11, 2024
ca7084e
Merge branch 'main' into main
GhzGarage May 11, 2024
422ff81
Merge branch 'main' into main
GhzGarage May 11, 2024
d503820
Merge branch 'main' into main
GhzGarage May 21, 2024
7566d12
Merge branch 'qbcore-framework:main' into main
Cocodrulo May 21, 2024
e29b762
Merge branch 'qbcore-framework:main' into main
Cocodrulo Jul 20, 2024
67a355d
Refactor money transfer function for clarity and consistency
Cocodrulo Jul 20, 2024
e2e4788
chore: Refactor money transfer function for clarity and consistency
Cocodrulo Jul 20, 2024
16c662f
feat: Add discord field to players table
Cocodrulo Jul 20, 2024
90f3966
feat: Add discord field to players table
Cocodrulo Jul 20, 2024
8bd7c88
Merge pull request #1 from Cocodrulo/discord
Cocodrulo Jul 20, 2024
07c7927
Revert "feat: Add discord field to players table"
Cocodrulo Jul 21, 2024
a689f37
Revert "feat: Add discord field to players table"
Cocodrulo Jul 21, 2024
e67d7e3
chore: Added logs and money control events
Cocodrulo Jul 26, 2024
6327c19
Merge pull request #2 from qbcore-framework/main
Cocodrulo Sep 8, 2024
94de247
Merge branch 'main' into main
GhzGarage Nov 13, 2024
f9f9739
Merge branch 'main' into main
Cocodrulo Nov 13, 2024
07dba6b
Merge branch 'main' into main
Cocodrulo Nov 26, 2024
e66427e
fix(server/functions): correct spelling and improve money transfer lo…
Cocodrulo Jan 8, 2025
8bf000a
refactor(server/player): simplify money transfer logic by utilizing e…
Cocodrulo Jan 8, 2025
dce305e
Merge branch 'main' into main
Cocodrulo Jan 8, 2025
7d01841
Merge branch 'main' into main
Cocodrulo Jan 12, 2025
12c1a9b
Merge branch 'main' into main
Cocodrulo Jan 27, 2025
41238ef
Merge branch 'main' into main
Cocodrulo Mar 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,66 @@ function QBCore.Functions.PrepForSQL(source, data, pattern)
return true
end

---Do a money transaction between players
---@param sourcecid string
---@param sourcemoneytype string -- Only money types in QBConfig.Money.MoneyTypes
---@param targetcid string
---@param targetmoneytype string -- Only money types in QBConfig.Money.MoneyTypes
---@param amount number
---@param reason string
---@return boolean
function QBCore.Functions.TransferMoney(sourcecid, sourcemoneytype, targetcid, targetmoneytype, amount, reason)
local SourcePlayer = QBCore.Functions.GetPlayerByCitizenId(sourcecid) or QBCore.Functions.GetOfflinePlayerByCitizenId(sourcecid)
local TargetPlayer = QBCore.Functions.GetPlayerByCitizenId(targetcid) or QBCore.Functions.GetOfflinePlayerByCitizenId(targetcid)

if not SourcePlayer or not TargetPlayer then return false end

if not tonumber(amount) then return false end
amount = tonumber(amount) or 0
if tonumber(amount) <= 0 then return true end

local errorOnLast = false

local offSource = SourcePlayer.PlayerData.source == nil
local offTarget = TargetPlayer.PlayerData.source == nil

if not SourcePlayer.Functions.RemoveMoney(amount, sourcemoneytype, reason) then return false end
if not TargetPlayer.Functions.AddMoney(amount, targetmoneytype, reason) then errorOnLast = true end

if errorOnLast then
SourcePlayer.Functions.AddMoney(amount, sourcemoneytype, reason)
return false
end

if not offSource then
TriggerClientEvent('hud:client:OnMoneyChange', SourcePlayer.PlayerData.source, sourcemoneytype, true)
TriggerClientEvent('QBCore:Client:OnMoneyChange', SourcePlayer.PlayerData.source, sourcemoneytype, amount, 'transfer', reason)
TriggerEvent('QBCore:Server:OnMoneyChange', SourcePlayer.PlayerData.source, sourcemoneytype, amount, 'transfer', reason)
else
SourcePlayer.Functions.Save()
end

if not offTarget then
TriggerClientEvent('hud:client:OnMoneyChange', TargetPlayer.PlayerData.source, targetmoneytype, true)
TriggerClientEvent('QBCore:Client:OnMoneyChange', TargetPlayer.PlayerData.source, targetmoneytype, amount, 'transfer', reason)
TriggerEvent('QBCore:Server:OnMoneyChange', TargetPlayer.PlayerData.source, targetmoneytype, amount, 'transfer', reason)
else
TargetPlayer.Functions.Save()
end

if not offSource and not offTarget then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. sourcecid .. '** gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. targetcid .. '** reason: ' .. reason .. ' | (Both offline)')
elseif not offTarget and offSource then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. GetPlayerName(SourcePlayer.PlayerData.source) .. ' (citizenid: ' .. SourcePlayer.PlayerData.citizenid .. ' | id: ' .. SourcePlayer.PlayerData.source .. ')**, new balance: ' .. SourcePlayer.PlayerData.money[sourcemoneytype] .. ' gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. targetcid .. ' in ('..targetmoneytype..') reason: ' .. reason .. ' | (Target offline)')
elseif not offSource and offTarget then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. sourcecid .. '** gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. GetPlayerName(TargetPlayer.PlayerData.source) .. ' (citizenid: ' .. TargetPlayer.PlayerData.citizenid .. ' | id: ' .. TargetPlayer.PlayerData.source .. ')**, new balance: ' .. TargetPlayer.PlayerData.money[targetmoneytype] .. ' in ('..targetmoneytype..') reason: ' .. reason .. ' | (Source offline)')
elseif offSource and offTarget then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. GetPlayerName(SourcePlayer.PlayerData.source) .. ' (citizenid: ' .. SourcePlayer.PlayerData.citizenid .. ' | id: ' .. SourcePlayer.PlayerData.source .. ')**, new balance: ' .. SourcePlayer.PlayerData.money[sourcemoneytype] .. ' gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. GetPlayerName(TargetPlayer.PlayerData.source) .. ' (citizenid: ' .. TargetPlayer.PlayerData.citizenid .. ' | id: ' .. TargetPlayer.PlayerData.source .. ')**, new balance: ' .. TargetPlayer.PlayerData.money[targetmoneytype] .. ' in ('..targetmoneytype..') reason: ' .. reason)
end

return true
end

for functionName, func in pairs(QBCore.Functions) do
if type(func) == 'function' then
exports(functionName, func)
Expand Down
4 changes: 4 additions & 0 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
return true
end

function self.Functions.TransferMoneyTo(sourcemoneytype, targetcid, targetmoneytype, amount, reason)
return QBCore.Functions.TransferMoney(self.PlayerData.source, sourcemoneytype, targetcid, targetmoneytype, amount, reason)
end

function self.Functions.GetMoney(moneytype)
if not moneytype then return false end
moneytype = moneytype:lower()
Expand Down
Loading