Merge pull request #56 from thelindat/master

(legacy): Resolve server hitches; add support for relogging; (general): improved clientside performance
This commit is contained in:
Taso
2021-06-04 21:41:32 -04:00
committed by GitHub
4 changed files with 84 additions and 55 deletions
+59 -39
View File
@@ -13,20 +13,19 @@ function GetStatusData(minimal)
for i=1, #Status, 1 do
if minimal then
table.insert(status, {
status[#status+1] = {
name = Status[i].name,
val = Status[i].val,
percent = (Status[i].val / Config.StatusMax) * 100
})
}
else
table.insert(status, {
status[#status+1] {
name = Status[i].name,
val = Status[i].val,
color = Status[i].color,
visible = Status[i].visible(Status[i]),
max = Status[i].max,
visible = Status[i].visible(),
percent = (Status[i].val / Config.StatusMax) * 100
})
}
end
end
@@ -47,6 +46,17 @@ AddEventHandler('esx_status:unregisterStatus', function(name)
end
end)
RegisterNetEvent('esx:onPlayerLogout')
AddEventHandler('esx:onPlayerLogout', function()
Status = {}
if Config.Display then
SendNUIMessage({
update = true,
status = Status
})
end
end)
RegisterNetEvent('esx_status:load')
AddEventHandler('esx_status:load', function(status)
TriggerEvent('esx_status:loaded')
@@ -59,17 +69,25 @@ AddEventHandler('esx_status:load', function(status)
end
Citizen.CreateThread(function()
while true do
while #Status > 0 do
for i=1, #Status, 1 do
Status[i].onTick()
end
local data = GetStatusData(true)
if Config.Display then
local fullData = data
for i=1, #data, 1 do
fullData[i].color = Status[i].color
fullData[i].visible = Status[i].visible()
end
SendNUIMessage({
update = true,
status = fullData
})
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
TriggerEvent('esx_status:onTick', GetStatusData(true))
TriggerEvent('esx_status:onTick', data)
Citizen.Wait(Config.TickTime)
end
end)
@@ -83,12 +101,12 @@ AddEventHandler('esx_status:set', function(name, val)
break
end
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
if Config.Display then
SendNUIMessage({
update = true,
status = GetStatusData()
})
end
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
@@ -100,12 +118,12 @@ AddEventHandler('esx_status:add', function(name, val)
break
end
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
if Config.Display then
SendNUIMessage({
update = true,
status = GetStatusData()
})
end
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
@@ -117,12 +135,12 @@ AddEventHandler('esx_status:remove', function(name, val)
break
end
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
if Config.Display then
SendNUIMessage({
update = true,
status = GetStatusData()
})
end
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
@@ -145,15 +163,17 @@ end)
-- Pause menu disable hud display
Citizen.CreateThread(function()
while true do
Citizen.Wait(300)
if Config.Display then
Citizen.Wait(300)
if IsPauseMenuActive() and not isPaused then
isPaused = true
TriggerEvent('esx_status:setDisplay', 0.0)
elseif not IsPauseMenuActive() and isPaused then
isPaused = false
TriggerEvent('esx_status:setDisplay', 0.5)
end
if IsPauseMenuActive() and not isPaused then
isPaused = true
TriggerEvent('esx_status:setDisplay', 0.0)
elseif not IsPauseMenuActive() and isPaused then
isPaused = false
TriggerEvent('esx_status:setDisplay', 0.5)
end
else Citizen.Wait(1000) end
end
end)
+2 -1
View File
@@ -2,4 +2,5 @@ Config = {}
Config.StatusMax = 1000000
Config.TickTime = 1000
Config.UpdateInterval = 10000
Config.UpdateInterval = 10000
Config.Display = true -- Enable the esx_status bars (disable if you are using another HUD)
+2 -2
View File
@@ -25,7 +25,7 @@
statusDiv.find('.status_val')
.css({
'background-color': status[i].color,
'width': (status[i].val / 10000) + '%'
'width': (status[i].percent) + '%'
})
;
@@ -56,4 +56,4 @@
});
};
})();
})();
+21 -13
View File
@@ -7,22 +7,26 @@ AddEventHandler('onResourceStart', function(resourceName)
return
end
local players = ESX.GetPlayers()
for _,playerId in ipairs(players) do
local xPlayer = ESX.GetPlayerFromId(playerId)
local xPlayers
if ESX.GetExtendedPlayers then
xPlayers = ESX.GetExtendedPlayers() -- Retrieve all xPlayer data directly (ESX Legacy)
else
xPlayers = ESX.GetPlayers() -- Retrieves player ids and gets xPlayer data one-by-one (ESX 1.2 support)
end
for k,v in pairs(xPlayers) do
local xPlayer = type(v) ~= 'table' and v or ESX.GetPlayerFromId(k)
MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
}, function(result)
local data = {}
if result[1].status then
data = json.decode(result[1].status)
end
xPlayer.set('status', data)
TriggerClientEvent('esx_status:load', playerId, data)
TriggerClientEvent('esx_status:load', k, data)
end)
end
end)
@@ -84,8 +88,6 @@ Citizen.CreateThread(function()
end)
function SaveData()
local xPlayers = ESX.GetPlayers()
-- Example of a bulk update statement that we are building below
--[[
UPDATE users
@@ -102,8 +104,15 @@ function SaveData()
local firstItem = true
local playerCount = 0
for i=1, #xPlayers, 1 do
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
local xPlayers
if ESX.GetExtendedPlayers then
xPlayers = ESX.GetExtendedPlayers() -- Retrieve all xPlayer data directly (ESX Legacy)
else
xPlayers = ESX.GetPlayers() -- Retrieves player ids and gets xPlayer data one-by-one (ESX 1.2 support)
end
for k,v in pairs(xPlayers) do
local xPlayer = type(v) ~= 'table' and v or ESX.GetPlayerFromId(k)
local status = xPlayer.get('status')
whenList = whenList .. string.format('when identifier = \'%s\' then \'%s\' ', xPlayer.identifier, json.encode(status))
@@ -119,7 +128,6 @@ function SaveData()
if playerCount > 0 then
local sql = string.format(updateStatement, whenList, whereList)
MySQL.Async.execute(sql)
end