diff --git a/server/functions.lua b/server/functions.lua index b7b969185..9cfe78e76 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -721,75 +721,3 @@ 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 diff --git a/server/player.lua b/server/player.lua index 6f4354371..cbc503222 100644 --- a/server/player.lua +++ b/server/player.lua @@ -382,47 +382,6 @@ 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()