-
Notifications
You must be signed in to change notification settings - Fork 968
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
f09c665
e6274cb
927e070
fbef0f7
ca7084e
422ff81
d503820
7566d12
e29b762
67a355d
e2e4788
16c662f
90f3966
8bd7c88
07c7927
a689f37
e67d7e3
6327c19
94de247
f9f9739
07dba6b
e66427e
8bf000a
dce305e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not return false here but do |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,6 +337,29 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline) | |
return true | ||
end | ||
|
||
function self.Functions.TransferMoneyTo(emitermoneytype, receivercid, receivermoneytype, quant, reason) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
local ReceiverPlayer = QBCore.Functions.GetPlayerByCitizenId(receivercid) | ||
if not tonumber(quant) then return false end | ||
quant = tonumber(quant) or 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of |
||
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() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quant
should be replaced byamount
since it's the wording use by default for this kind of propertyThere was a problem hiding this comment.
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 ?