tweak(status): Store status in the ESX.Players table that exists locally

This commit is contained in:
Linden
2021-06-29 21:28:35 +10:00
parent 8f6fd761cc
commit 5ea3cc1a2f
+24 -22
View File
@@ -1,26 +1,27 @@
local Status = {}
AddEventHandler('onResourceStart', function(resourceName) AddEventHandler('onResourceStart', function(resourceName)
if (GetCurrentResourceName() ~= resourceName) then if (GetCurrentResourceName() ~= resourceName) then
return return
end end
local xPlayers = ESX.GetExtendedPlayers()
for playerId, xPlayer in pairs(xPlayers) do for _, xPlayer in pairs(ESX.Players) do
MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', { local status = xPlayer.get('status')
['@identifier'] = xPlayer.identifier if status then
}, function(result) ESX.Players[xPlayer.source] = status
local data = {} else
MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
if result[1].status then ['@identifier'] = xPlayer.identifier
data = json.decode(result[1].status) }, function(result)
end local data = {}
xPlayer.set('status', data) -- save to xPlayer for compatibility if result[1].status then
Status[xPlayer.source] = data -- save locally for performance data = json.decode(result[1].status)
TriggerClientEvent('esx_status:load', xPlayer.source, data) end
end)
xPlayer.set('status', data) -- save to xPlayer for compatibility
ESX.Players[xPlayer.source] = data -- save locally for performance
end)
end
TriggerClientEvent('esx_status:load', xPlayer.source, data)
end end
end) end)
@@ -35,24 +36,25 @@ AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
end end
xPlayer.set('status', data) xPlayer.set('status', data)
ESX.Players[xPlayer.source] = data
TriggerClientEvent('esx_status:load', playerId, data) TriggerClientEvent('esx_status:load', playerId, data)
end) end)
end) end)
AddEventHandler('esx:playerDropped', function(playerId, reason) AddEventHandler('esx:playerDropped', function(playerId, reason)
local xPlayer = ESX.GetPlayerFromId(playerId) local xPlayer = ESX.GetPlayerFromId(playerId)
local status = Status[xPlayer.source] local status = ESX.Players[xPlayer.source]
MySQL.Async.execute('UPDATE users SET status = @status WHERE identifier = @identifier', { MySQL.Async.execute('UPDATE users SET status = @status WHERE identifier = @identifier', {
['@status'] = json.encode(status), ['@status'] = json.encode(status),
['@identifier'] = xPlayer.identifier ['@identifier'] = xPlayer.identifier
}, function(result) }, function(result)
Status[xPlayer.source] = nil ESX.Players[xPlayer.source] = nil
end end)
end) end)
AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb) AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb)
for i=1, #Status[xPlayer.source], 1 do for i=1, #ESX.Players, 1 do
if status[i].name == statusName then if status[i].name == statusName then
cb(status[i]) cb(status[i])
break break
@@ -65,7 +67,7 @@ AddEventHandler('esx_status:update', function(status)
local xPlayer = ESX.GetPlayerFromId(source) local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer then if xPlayer then
xPlayer.set('status', status) -- save to xPlayer for compatibility xPlayer.set('status', status) -- save to xPlayer for compatibility
Status[xPlayer.source] = status -- save locally for performance ESX.Players[xPlayer.source] = status -- save locally for performance
end end
end) end)