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

---Do a money transactio between players
---@param emitercid string
---@param emitermoneytype string -- Only money types in QBConfig.Money.MoneyTypes
---@param receivercid string
---@param receivermoneytype string -- Only money types in QBConfig.Money.MoneyTypes
---@param quant number
---@param reason string
---@return boolean
function QBCore.Functions.TransferMoney(emitercid, emitermoneytype, receivercid, receivermoneytype, quant, reason)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quant should be replaced by amount since it's the wording use by default for this kind of property

Copy link

@Scartane Scartane Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sourcecid and targetcid wil lbe better than emitercid / receivercid no ? and same for all properties then ? What do you think ?

local EmiterPlayer = QBCore.Functions.GetPlayerByCitizenId(emitercid)
local ReceiverPlayer = QBCore.Functions.GetPlayerByCitizenId(receivercid)
if not tonumber(quant) then return false end
quant = tonumber(quant) or 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operation with 0 should not be done I think. Probably you can do if not tonumber(quant) or tonumber(quant) <= 0 then return false end or just if tonumber(quant) <= 0 then return true end

local errorOnLast = false
if EmiterPlayer then
if not EmiterPlayer.Functions.RemoveMoney(quant, emitermoneytype, reason) then return false end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { emitercid })
if not result then return false end
result = json.decode(result)
result[emitermoneytype] -= quant
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), emitercid }) then return false end
end
if ReceiverPlayer then
if not ReceiverPlayer.Functions.AddMoney(quant, receivermoneytype, reason) then errorOnLast = true end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { receivercid })
if not result then errorOnLast = true end
result = json.decode(result)
result[receivermoneytype] += quant
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), receivercid }) then return false end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not return false here but do errorOnLast = true or the emiter will loose the money if their is an error on the receiver player

end

if errorOnLast then
if EmiterPlayer then
if not EmiterPlayer.Functions.AddMoney(quant, emitermoneytype, reason) then return false end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { emitercid })
if not result then return false end
result = json.decode(result)
result[emitermoneytype] += quant
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), emitercid }) then return false end
end
return false
end
return true
end
23 changes: 23 additions & 0 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
return true
end

function self.Functions.TransferMoneyTo(emitermoneytype, receivercid, receivermoneytype, quant, reason)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quant should be replaced by amount since it's the wording use by default for this kind of property

local ReceiverPlayer = QBCore.Functions.GetPlayerByCitizenId(receivercid)
if not tonumber(quant) then return false end
quant = tonumber(quant) or 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark at the first file regarding 0 transaction handling

local errorOnLast = false
if not self.Functions.RemoveMoney(quant, emitermoneytype, reason) then return false end
if ReceiverPlayer then
if not ReceiverPlayer.Functions.AddMoney(quant, receivermoneytype, reason) then errorOnLast = true end
else
local result = MySQL.single.await('SELECT money FROM players WHERE citizendid = ?', { receivercid })
if not result then errorOnLast = true end
result = json.decode(result)
result[receivermoneytype] += quant
if not MySQL.update.await('UPDATE players SET money = ? WHERE citizenid = ?', { json.encode(result), receivercid }) then return false end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of return false it should be errorOnLast = true

end

if errorOnLast then
self.Functions.AddMoney(quant, emitermoneytype, reason)
return false
end
return true
end

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