Performance, respect EnableHud from ESX

This commit is contained in:
ElPumpo
2018-11-03 21:53:44 +01:00
parent 043d406461
commit 74653246b0
5 changed files with 89 additions and 124 deletions
+27 -46
View File
@@ -1,46 +1,38 @@
local Status = {}
local isPaused = false
function GetStatusData(minimal)
local status = {}
for i=1, #Status, 1 do
if minimal then
table.insert(status, {
name = Status[i].name,
val = Status[i].val,
percent = (Status[i].val / Config.StatusMax) * 100,
percent = (Status[i].val / Config.StatusMax) * 100
})
else
table.insert(status, {
name = Status[i].name,
val = Status[i].val,
color = Status[i].color,
visible = Status[i].visible(Status[i]),
max = Status[i].max,
percent = (Status[i].val / Config.StatusMax) * 100,
percent = (Status[i].val / Config.StatusMax) * 100
})
end
end
return status
end
AddEventHandler('esx_status:registerStatus', function(name, default, color, visible, tickCallback)
local s = CreateStatus(name, default, color, visible, tickCallback)
table.insert(Status, s)
local status = CreateStatus(name, default, color, visible, tickCallback)
table.insert(Status, status)
end)
RegisterNetEvent('esx_status:load')
AddEventHandler('esx_status:load', function(status)
for i=1, #Status, 1 do
for j=1, #status, 1 do
if Status[i].name == status[j].name then
@@ -50,27 +42,23 @@ AddEventHandler('esx_status:load', function(status)
end
Citizen.CreateThread(function()
while true do
for i=1, #Status, 1 do
Status[i].onTick()
end
while true do
for i=1, #Status, 1 do
Status[i].onTick()
end
SendNUIMessage({
update = true,
status = GetStatusData()
})
Citizen.Wait(Config.TickTime)
end
Citizen.Wait(Config.TickTime)
end
end)
end)
RegisterNetEvent('esx_status:set')
AddEventHandler('esx_status:set', function(name, val)
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].set(val)
@@ -84,12 +72,10 @@ AddEventHandler('esx_status:set', function(name, val)
})
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
RegisterNetEvent('esx_status:add')
AddEventHandler('esx_status:add', function(name, val)
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].add(val)
@@ -103,12 +89,10 @@ AddEventHandler('esx_status:add', function(name, val)
})
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
RegisterNetEvent('esx_status:remove')
AddEventHandler('esx_status:remove', function(name, val)
for i=1, #Status, 1 do
if Status[i].name == name then
Status[i].remove(val)
@@ -122,44 +106,40 @@ AddEventHandler('esx_status:remove', function(name, val)
})
TriggerServerEvent('esx_status:update', GetStatusData(true))
end)
AddEventHandler('esx_status:getStatus', function(name, cb)
for i=1, #Status, 1 do
if Status[i].name == name then
cb(Status[i])
return
end
end
end)
AddEventHandler('esx_status:setDisplay', function(val)
SendNUIMessage({
setDisplay = true,
display = val
})
end)
-- Pause menu disable hud display
local isPaused = false
if ESX.GetConfig().EnableHud then
Citizen.CreateThread(function()
while true do
Citizen.Wait(300)
Citizen.CreateThread(function()
while true do
Citizen.Wait(10)
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
end
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
end
end)
end
-- Loaded event
Citizen.CreateThread(function()
@@ -170,6 +150,7 @@ end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(Config.UpdateInterval)
TriggerServerEvent('esx_status:update', GetStatusData(true))
end
end)
end)
+28 -26
View File
@@ -1,15 +1,15 @@
(function(){
(function () {
let status = [];
let status = []
let renderStatus = function(){
let renderStatus = function () {
$('#status_list').html('');
for(let i=0; i<status.length; i++){
for (let i = 0; i < status.length; i++) {
if(!status[i].visible)
if (!status[i].visible) {
continue;
}
let statusDiv = $(
'<div class="status">' +
@@ -17,41 +17,43 @@
'<div class="status_val"></div>' +
'</div>' +
'</div>');
statusDiv.find('.status_inner')
.css({'border' : '1px solid ' + status[i].color})
;
.css({ 'border': '1px solid ' + status[i].color })
;
statusDiv.find('.status_val')
.css({
'background-color' : status[i].color,
'width' : (status[i].val / 10000) + '%'
'background-color': status[i].color,
'width': (status[i].val / 10000) + '%'
})
;
;
$('#status_list').append(statusDiv)
$('#status_list').append(statusDiv);
}
}
};
window.onData = function(data){
if(data.update){
window.onData = function (data) {
if (data.update) {
status.length = 0;
for(let i=0; i<data.status.length; i++)
status.push(data.status[i])
for (let i = 0; i < data.status.length; i++) {
status.push(data.status[i]);
}
renderStatus();
}
if(data.setDisplay){
$('#status_list').css({'opacity' : data.display})
if (data.setDisplay) {
$('#status_list').css({ 'opacity': data.display });
}
};
}
window.onload = function (e) {
window.addEventListener('message', function (event) {
onData(event.data);
});
};
window.onload = function(e){ window.addEventListener('message', function(event){ onData(event.data) }); }
})()
})();
+13 -11
View File
@@ -1,14 +1,16 @@
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="css/app.css" type="text/css" />
</head>
<body>
<div id="status_list"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="nui://game/ui/jquery.js" type="text/javascript"></script>
<script src="scripts/app.js"></script>
</body>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/app.css" type="text/css" />
</head>
<body>
<div id="status_list"></div>
<script src="nui://game/ui/jquery.js" type="text/javascript"></script>
<script src="scripts/app.js"></script>
</body>
</html>
+1 -1
View File
@@ -1,5 +1,5 @@
function CreateStatus(xPlayer, name, default, color, visible, tickCallback, clientAction)
local self = {}
self.val = default
+20 -40
View File
@@ -1,5 +1,4 @@
ESX = nil
local RegisteredStatus = {}
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
@@ -7,43 +6,30 @@ AddEventHandler('esx:playerLoaded', function(source)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
MySQL.Async.fetchAll(
'SELECT * FROM users WHERE identifier = @identifier',
{
MySQL.Async.fetchAll('SELECT status FROM users WHERE identifier = @identifier', {
['@identifier'] = xPlayer.identifier
},
function(result)
local data = {}
if result[1].status ~= nil then
data = json.decode(result[1].status)
end
xPlayer.set('status', data)
TriggerClientEvent('esx_status:load', _source, data)
}, function(result)
local data = {}
if result[1].status ~= nil then
data = json.decode(result[1].status)
end
)
xPlayer.set('status', data)
TriggerClientEvent('esx_status:load', _source, data)
end)
end)
AddEventHandler('esx:playerDropped', function(source)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
local data = {}
local status = xPlayer.get('status')
MySQL.Async.execute(
'UPDATE users SET status = @status WHERE identifier = @identifier',
{
['@status'] = json.encode(status),
['@identifier'] = xPlayer.identifier
}
)
MySQL.Async.execute('UPDATE users SET status = @status WHERE identifier = @identifier', {
['@status'] = json.encode(status),
['@identifier'] = xPlayer.identifier
})
end)
AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb)
@@ -56,14 +42,13 @@ AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb)
break
end
end
end)
RegisterServerEvent('esx_status:update')
AddEventHandler('esx_status:update', function(status)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
if xPlayer ~= nil then
xPlayer.set('status', status)
end
@@ -71,24 +56,19 @@ end)
function SaveData()
local xPlayers = ESX.GetPlayers()
for i=1, #xPlayers, 1 do
for i=1, #xPlayers, 1 do
local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
local data = {}
local status = xPlayer.get('status')
MySQL.Async.execute(
'UPDATE users SET status = @status WHERE identifier = @identifier',
{
['@status'] = json.encode(status),
['@identifier'] = xPlayer.identifier
}
)
MySQL.Async.execute('UPDATE users SET status = @status WHERE identifier = @identifier', {
['@status'] = json.encode(status),
['@identifier'] = xPlayer.identifier
})
end
SetTimeout(10 * 60 * 1000, SaveData)
end
SaveData()
SaveData()