mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
refactor(status): various changes
Replaced mysql-async. Resolved bug with the getStatus event. Replace bulk query with prepared statement. Minor optimisation tweaks.
This commit is contained in:
@@ -65,12 +65,17 @@ AddEventHandler('esx_status:load', function(status)
|
||||
if Config.Display then TriggerEvent('esx_status:setDisplay', 0.5) end
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
local data = {}
|
||||
while ESX.PlayerLoaded do
|
||||
for i=1, #Status, 1 do
|
||||
for i=1, #Status do
|
||||
Status[i].onTick()
|
||||
table.insert(data, {
|
||||
name = Status[i].name,
|
||||
val = Status[i].val,
|
||||
percent = (Status[i].val / 1000000) * 100
|
||||
})
|
||||
end
|
||||
local data = GetStatusData(true)
|
||||
|
||||
|
||||
if Config.Display then
|
||||
local fullData = data
|
||||
for i=1, #data, 1 do
|
||||
@@ -84,6 +89,7 @@ AddEventHandler('esx_status:load', function(status)
|
||||
end
|
||||
|
||||
TriggerEvent('esx_status:onTick', data)
|
||||
table.wipe(data)
|
||||
Citizen.Wait(Config.TickTime)
|
||||
end
|
||||
end)
|
||||
@@ -154,9 +160,9 @@ AddEventHandler('esx_status:setDisplay', function(val)
|
||||
end)
|
||||
|
||||
-- Pause menu disable hud display
|
||||
Citizen.CreateThread(function()
|
||||
while true do
|
||||
if Config.Display then
|
||||
if Config.Display then
|
||||
Citizen.CreateThread(function()
|
||||
while true do
|
||||
Citizen.Wait(300)
|
||||
|
||||
if IsPauseMenuActive() and not isPaused then
|
||||
@@ -166,9 +172,9 @@ Citizen.CreateThread(function()
|
||||
isPaused = false
|
||||
TriggerEvent('esx_status:setDisplay', 0.5)
|
||||
end
|
||||
else Citizen.Wait(1000) end
|
||||
end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- Loading screen off event
|
||||
AddEventHandler('esx:loadingScreenOff', function()
|
||||
|
||||
@@ -6,6 +6,8 @@ description 'ESX Status'
|
||||
|
||||
version 'legacy'
|
||||
|
||||
lua54 'yes'
|
||||
|
||||
shared_script '@es_extended/imports.lua'
|
||||
|
||||
server_scripts {
|
||||
@@ -27,3 +29,5 @@ files {
|
||||
'html/css/app.css',
|
||||
'html/scripts/app.js'
|
||||
}
|
||||
|
||||
dependency 'es_extended'
|
||||
|
||||
@@ -1,43 +1,26 @@
|
||||
local function setPlayerStatus(xPlayer, data)
|
||||
data = data and json.decode(data) or {}
|
||||
|
||||
xPlayer.set('status', data)
|
||||
ESX.Players[xPlayer.source] = data
|
||||
TriggerClientEvent('esx_status:load', xPlayer.source, data)
|
||||
end
|
||||
|
||||
AddEventHandler('onResourceStart', function(resourceName)
|
||||
if (GetCurrentResourceName() ~= resourceName) then
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
for _, xPlayer in pairs(ESX.Players) do
|
||||
local status = xPlayer.get('status')
|
||||
if status then
|
||||
ESX.Players[xPlayer.source] = status
|
||||
else
|
||||
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) -- save to xPlayer for compatibility
|
||||
ESX.Players[xPlayer.source] = data -- save locally for performance
|
||||
end)
|
||||
end
|
||||
TriggerClientEvent('esx_status:load', xPlayer.source, data)
|
||||
MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result)
|
||||
setPlayerStatus(xPlayer, result)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
|
||||
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)
|
||||
ESX.Players[xPlayer.source] = data
|
||||
TriggerClientEvent('esx_status:load', playerId, data)
|
||||
MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result)
|
||||
setPlayerStatus(xPlayer, result)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -45,19 +28,15 @@ AddEventHandler('esx:playerDropped', function(playerId, reason)
|
||||
local xPlayer = ESX.GetPlayerFromId(playerId)
|
||||
local status = ESX.Players[xPlayer.source]
|
||||
|
||||
MySQL.Async.execute('UPDATE users SET status = @status WHERE identifier = @identifier', {
|
||||
['@status'] = json.encode(status),
|
||||
['@identifier'] = xPlayer.identifier
|
||||
}, function(result)
|
||||
ESX.Players[xPlayer.source] = nil
|
||||
end)
|
||||
MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.identifier })
|
||||
ESX.Players[xPlayer.source] = nil
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb)
|
||||
for i=1, #ESX.Players, 1 do
|
||||
local status = ESX.Players[playerId]
|
||||
for i = 1, #status do
|
||||
if status[i].name == statusName then
|
||||
cb(status[i])
|
||||
break
|
||||
return cb(status[i])
|
||||
end
|
||||
end
|
||||
end)
|
||||
@@ -72,50 +51,17 @@ AddEventHandler('esx_status:update', function(status)
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
while(true) do
|
||||
while true do
|
||||
Citizen.Wait(10 * 60 * 1000)
|
||||
SaveData()
|
||||
end
|
||||
end)
|
||||
|
||||
function SaveData()
|
||||
-- Example of a bulk update statement that we are building below
|
||||
--[[
|
||||
UPDATE users
|
||||
SET status = (case when identifier = 'license:123' then '{hunger:45, thirst:23}'
|
||||
when identifier = 'license:456' then '{hunger:1000, thirst:1000}'
|
||||
when identifier = 'license:789' then '{hunger:1000, thirst:1000}'
|
||||
end)
|
||||
WHERE identifier in ('license:123', 'license:456', 'license:789')
|
||||
|
||||
]]
|
||||
local updateStatement = 'UPDATE users SET status = (case %s end) where identifier in (%s)'
|
||||
local whenList = ''
|
||||
local whereList = ''
|
||||
local firstItem = true
|
||||
local playerCount = 0
|
||||
|
||||
local xPlayers = ESX.GetExtendedPlayers()
|
||||
|
||||
for _, xPlayer in pairs(xPlayers) do
|
||||
local status = ESX.Players[xPlayer.source]
|
||||
if status then
|
||||
whenList = whenList .. string.format('when identifier = \'%s\' then \'%s\' ', xPlayer.identifier, json.encode(status))
|
||||
|
||||
if firstItem == false then
|
||||
whereList = whereList .. ', '
|
||||
local parameters = {}
|
||||
for _, xPlayer in pairs(ESX.GetExtendedPlayers()) do
|
||||
local status = ESX.Players[xPlayer.source]
|
||||
if status and next(status) then
|
||||
parameters[#parameters+1] = {json.encode(status), xPlayer.identifier}
|
||||
end
|
||||
whereList = whereList .. string.format('\'%s\'', xPlayer.identifier)
|
||||
|
||||
firstItem = false
|
||||
playerCount = playerCount + 1
|
||||
end
|
||||
if #parameters > 0 then
|
||||
MySQL.prepare('UPDATE users SET status = ? WHERE identifier = ?', parameters)
|
||||
end
|
||||
end
|
||||
|
||||
if playerCount > 0 then
|
||||
local sql = string.format(updateStatement, whenList, whereList)
|
||||
MySQL.Async.execute(sql)
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user