From 7e0452a2b3e6e67450dd0484b266407f7a2c1203 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:52:30 +0100 Subject: [PATCH 1/6] remove unused class --- server/classes/status.lua | 56 --------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 server/classes/status.lua diff --git a/server/classes/status.lua b/server/classes/status.lua deleted file mode 100644 index 0ec74da..0000000 --- a/server/classes/status.lua +++ /dev/null @@ -1,56 +0,0 @@ -function CreateStatus(xPlayer, name, default, color, visible, tickCallback, clientAction) - - local self = {} - - self.val = default - self.xPlayer = xPlayer - self.name = name - self.default = default - self.color = color - self.visible = visible - self.tickCallback = tickCallback - self.clientAction = clientAction - - function self._set(k, v) - self[k] = v - end - - function self._get(k) - return self[k] - end - - function self.onTick() - self.tickCallback(self) - end - - function self.set(val) - self.val = val - end - - function self.add(val) - if self.val + val > Config.StatusMax then - self.val = Config.StatusMax - else - self.val = self.val + val - end - end - - function self.remove(val) - if self.val - val < 0 then - self.val = 0 - else - self.val = self.val - val - end - end - - function self.getPercent() - return (self.val / Config.StatusMax) * 100 - end - - function self.updateClient() - TriggerEvent('esx_status:updateClient', self.xPlayer.source) - end - - return self - -end From 5f857960745ab7b40886da1d240befaf371e04bf Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:56:06 +0100 Subject: [PATCH 2/6] remove save thread --- server/main.lua | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/server/main.lua b/server/main.lua index 0b8f582..5d50ba5 100644 --- a/server/main.lua +++ b/server/main.lua @@ -49,19 +49,3 @@ AddEventHandler('esx_status:update', function(status) ESX.Players[xPlayer.source] = status -- save locally for performance end end) - -CreateThread(function() - while true do - Wait(10 * 60 * 1000) - 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 - end - if #parameters > 0 then - MySQL.prepare('UPDATE users SET status = ? WHERE identifier = ?', parameters) - end - end -end) From d7ad6b9e2fa44026e8bbc2edacc9b8046bd9f9d2 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:00:00 +0100 Subject: [PATCH 3/6] remove deprecated RegisterServerEvent --- server/main.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/main.lua b/server/main.lua index 5d50ba5..1cb1f50 100644 --- a/server/main.lua +++ b/server/main.lua @@ -41,8 +41,7 @@ AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb) end end) -RegisterServerEvent('esx_status:update') -AddEventHandler('esx_status:update', function(status) +RegisterNetEvent('esx_status:update', function(status) local xPlayer = ESX.GetPlayerFromId(source) if xPlayer then xPlayer.set('status', status) -- save to xPlayer for compatibility From 58823ee0716a3d951b4ab22dd0cf0c26d931625d Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:03:58 +0100 Subject: [PATCH 4/6] add guard clauses --- server/main.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/main.lua b/server/main.lua index 1cb1f50..fcaf35e 100644 --- a/server/main.lua +++ b/server/main.lua @@ -26,6 +26,9 @@ end) AddEventHandler('esx:playerDropped', function(playerId, reason) local xPlayer = ESX.GetPlayerFromId(playerId) + if not xPlayer then + return + end local status = ESX.Players[xPlayer.source] MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.identifier }) @@ -43,8 +46,10 @@ end) RegisterNetEvent('esx_status:update', function(status) local xPlayer = ESX.GetPlayerFromId(source) - if xPlayer then - xPlayer.set('status', status) -- save to xPlayer for compatibility - ESX.Players[xPlayer.source] = status -- save locally for performance + if not xPlayer then + return end + + xPlayer.set('status', status) -- save to xPlayer for compatibility + ESX.Players[xPlayer.source] = status -- save locally for performance end) From b4776657f5ae299d712a7f5222c5963430ffeda4 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:11:25 +0100 Subject: [PATCH 5/6] remove caching, use xPlayer getter functions, improve sql query --- server/main.lua | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/server/main.lua b/server/main.lua index fcaf35e..5902650 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,42 +1,27 @@ -local function setPlayerStatus(xPlayer, data) - data = data and json.decode(data) or {} +AddEventHandler('esx:playerLoaded', function(src, xPlayer) + local status = MySQL.scalar.await('SELECT `status` FROM `users` WHERE `identifier` = ? LIMIT 1', { xPlayer.getIdentifier() }) - 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 - MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result) - setPlayerStatus(xPlayer, result) - end) - end + status = status and json.decode(status) or {} + xPlayer.set('status', status) + TriggerClientEvent('esx_status:load', xPlayer.source, status) end) -AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) - MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result) - setPlayerStatus(xPlayer, result) - end) -end) - -AddEventHandler('esx:playerDropped', function(playerId, reason) - local xPlayer = ESX.GetPlayerFromId(playerId) +AddEventHandler('esx:playerDropped', function(src, reason) + local xPlayer = ESX.GetPlayerFromId(src) if not xPlayer then return end - local status = ESX.Players[xPlayer.source] + local status = xPlayer.get('status') or {} - MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.identifier }) - ESX.Players[xPlayer.source] = nil + MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.getIdentifier() }) end) -AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb) - local status = ESX.Players[playerId] +AddEventHandler('esx_status:getStatus', function(src, statusName, cb) + local xPlayer = ESX.GetPlayerFromId(src) + if not xPlayer then + return + end + local status = xPlayer.get('status') or {} for i = 1, #status do if status[i].name == statusName then return cb(status[i]) @@ -50,6 +35,5 @@ RegisterNetEvent('esx_status:update', function(status) return end - xPlayer.set('status', status) -- save to xPlayer for compatibility - ESX.Players[xPlayer.source] = status -- save locally for performance + xPlayer.set('status', status) end) From 53e8df2c3b0ab20c5f7ade948fbfe9cfb16e4cc2 Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:14:12 +0100 Subject: [PATCH 6/6] add annotations --- server/main.lua | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/main.lua b/server/main.lua index 5902650..33945aa 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,26 +1,35 @@ +---@param src number +---@param xPlayer table AddEventHandler('esx:playerLoaded', function(src, xPlayer) local status = MySQL.scalar.await('SELECT `status` FROM `users` WHERE `identifier` = ? LIMIT 1', { xPlayer.getIdentifier() }) status = status and json.decode(status) or {} xPlayer.set('status', status) + TriggerClientEvent('esx_status:load', xPlayer.source, status) end) +---@param src number +---@param reason string AddEventHandler('esx:playerDropped', function(src, reason) local xPlayer = ESX.GetPlayerFromId(src) if not xPlayer then return end - local status = xPlayer.get('status') or {} + local status = xPlayer.get('status') or {} MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.getIdentifier() }) end) +---@param src number +---@param statusName string +---@param cb function|table AddEventHandler('esx_status:getStatus', function(src, statusName, cb) local xPlayer = ESX.GetPlayerFromId(src) if not xPlayer then return end + local status = xPlayer.get('status') or {} for i = 1, #status do if status[i].name == statusName then @@ -29,6 +38,7 @@ AddEventHandler('esx_status:getStatus', function(src, statusName, cb) end end) +---@param status table RegisterNetEvent('esx_status:update', function(status) local xPlayer = ESX.GetPlayerFromId(source) if not xPlayer then