Skip to content

Commit

Permalink
refactor(server/functions): Core.SavePlayers()
Browse files Browse the repository at this point in the history
format: Core.SavePlayer
  • Loading branch information
CsokiHUN committed Jan 22, 2023
1 parent da62365 commit 94c36df
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,44 +176,69 @@ function ESX.TriggerServerCallback(name, requestId, source,Invoke, cb, ...)
end

function Core.SavePlayer(xPlayer, cb)
local parameters <const> = {
json.encode(xPlayer.getAccounts(true)),
xPlayer.job.name,
xPlayer.job.grade,
xPlayer.group,
json.encode(xPlayer.getCoords()),
json.encode(xPlayer.getInventory(true)),
json.encode(xPlayer.getLoadout(true)),
xPlayer.identifier
}

MySQL.prepare(
'UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?',
{json.encode(xPlayer.getAccounts(true)), xPlayer.job.name, xPlayer.job.grade, xPlayer.group, json.encode(xPlayer.getCoords()),
json.encode(xPlayer.getInventory(true)), json.encode(xPlayer.getLoadout(true)), xPlayer.identifier}, function(affectedRows)
parameters,
function(affectedRows)
if affectedRows == 1 then
print(('[^2INFO^7] Saved player ^5"%s^7"'):format(xPlayer.name))
TriggerEvent('esx:playerSaved', xPlayer.playerId, xPlayer)
end
if cb then
cb()
end
end)
end
)
end

function Core.SavePlayers(cb)
local xPlayers = ESX.GetExtendedPlayers()
local count = #xPlayers
if count > 0 then
local parameters = {}
local time = os.time()
for i = 1, count do
local xPlayer = xPlayers[i]
parameters[#parameters + 1] = {json.encode(xPlayer.getAccounts(true)), xPlayer.job.name, xPlayer.job.grade, xPlayer.group,
json.encode(xPlayer.getCoords()), json.encode(xPlayer.getInventory(true)), json.encode(xPlayer.getLoadout(true)),
xPlayer.identifier}
end
MySQL.prepare(
"UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?",
parameters, function(results)
if results then
if type(cb) == 'function' then
cb()
else
print(('[^2INFO^7] Saved ^5%s^7 %s over ^5%s^7 ms'):format(count, count > 1 and 'players' or 'player', ESX.Math.Round((os.time() - time) / 1000000, 2)))
end
end
end)
local xPlayers <const> = ESX.Players
if not next(xPlayers) then
return
end

local startTime <const> = os.time()
local parameters = {}

for _, xPlayer in pairs(ESX.Players) do
parameters[#parameters + 1] = {
json.encode(xPlayer.getAccounts(true)),
xPlayer.job.name,
xPlayer.job.grade,
xPlayer.group,
json.encode(xPlayer.getCoords()),
json.encode(xPlayer.getInventory(true)),
json.encode(xPlayer.getLoadout(true)),
xPlayer.identifier
}
end

MySQL.prepare(
"UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?",
parameters,
function(results)
if not results then
return
end

if type(cb) == 'function' then
return cb()
end

print(('[^2INFO^7] Saved ^5%s^7 %s over ^5%s^7 ms'):format(#parameters, #parameters > 1 and 'players' or 'player', ESX.Math.Round((os.time() - startTime) / 1000000, 2)))
end
)
end

ESX.GetPlayers = GetPlayers
Expand Down

0 comments on commit 94c36df

Please sign in to comment.