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

💱 Added money transfer functions #1099

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
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
72 changes: 72 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,75 @@ function QBCore.Functions.PrepForSQL(source, data, pattern)
end
return true
end

---Do a money transactio 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)
local TargetPlayer = QBCore.Functions.GetPlayerByCitizenId(targetcid)
if not tonumber(amount) then return false end
amount = tonumber(amount) or 0
if tonumber(amount) <= 0 then return true end
local errorOnLast = false
if SourcePlayer then
if not SourcePlayer.Functions.RemoveMoney(amount, sourcemoneytype, reason) then return false end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { sourcecid })
if not result then return false end
result = json.decode(result)
result[sourcemoneytype] -= amount
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), sourcecid }) then errorOnLast = true end
end
if TargetPlayer then
if not TargetPlayer.Functions.AddMoney(amount, targetmoneytype, reason) then errorOnLast = true end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { targetcid })
if not result then errorOnLast = true end
result = json.decode(result)
result[targetmoneytype] += amount
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), targetcid }) then errorOnLast = true end
end

if errorOnLast then
if SourcePlayer then
if not SourcePlayer.Functions.AddMoney(amount, sourcemoneytype, reason) then return false end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { sourcecid })
if not result then return false end
result = json.decode(result)
result[sourcemoneytype] += amount
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), sourcecid }) then return false end
end
return false
end

if SourcePlayer 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)
end

if TargetPlayer 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)
end

if not SourcePlayer and not TargetPlayer then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. sourcecid .. '** gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. targetcid .. '** reason: ' .. reason .. ' | (Both offline)')
elseif not TargetPlayer and SourcePlayer 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 SourcePlayer and TargetPlayer 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 SourcePlayer and TargetPlayer 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
41 changes: 41 additions & 0 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,47 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
return true
end

function self.Functions.TransferMoneyTo(sourcemoneytype, targetcid, targetmoneytype, amount, reason)
local TargetPlayer = QBCore.Functions.GetPlayerByCitizenId(targetcid)
if not tonumber(amount) then return false end
amount = tonumber(amount) or 0
if amount <= 0 then return true end
local errorOnLast = false
if not self.Functions.RemoveMoney(amount, sourcemoneytype, reason) then return false end
if TargetPlayer then
if not TargetPlayer.Functions.AddMoney(amount, targetmoneytype, reason) then errorOnLast = true end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { targetcid })
if not result then errorOnLast = true end
result = json.decode(result)
result[targetmoneytype] += amount
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), targetcid }) then errorOnLast = true end
end

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

TriggerClientEvent('hud:client:OnMoneyChange', self.PlayerData.source, sourcemoneytype, true)
TriggerClientEvent('QBCore:Client:OnMoneyChange', self.PlayerData.source, sourcemoneytype, amount, 'transfer', reason)
TriggerEvent('QBCore:Server:OnMoneyChange', self.PlayerData.source, sourcemoneytype, amount, 'transfer', reason)

if TargetPlayer 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)
end

if not TargetPlayer then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. GetPlayerName(self.PlayerData.source) .. ' (citizenid: ' .. self.PlayerData.citizenid .. ' | id: ' .. self.PlayerData.source .. ')**, new balance: ' .. self.PlayerData.money[sourcemoneytype] .. ' gived $' .. amount .. ' (' .. sourcemoneytype .. ') to **' .. targetcid .. ' in ('..targetmoneytype..') reason: ' .. reason .. ' | (Target offline)')
elseif TargetPlayer then
TriggerEvent('qb-log:server:CreateLog', 'playermoney', 'TransferMoney', 'yellow', '**' .. GetPlayerName(self.PlayerData.source) .. ' (citizenid: ' .. self.PlayerData.citizenid .. ' | id: ' .. self.PlayerData.source .. ')**, new balance: ' .. self.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

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