-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from esx-framework/refactor
refactor(esx_service)
- Loading branch information
Showing
2 changed files
with
30 additions
and
104 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,49 @@ | ||
local function setPlayerStatus(xPlayer, data) | ||
data = data and json.decode(data) or {} | ||
---@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() }) | ||
|
||
xPlayer.set('status', data) | ||
ESX.Players[xPlayer.source] = data | ||
TriggerClientEvent('esx_status:load', xPlayer.source, data) | ||
end | ||
status = status and json.decode(status) or {} | ||
xPlayer.set('status', status) | ||
|
||
AddEventHandler('onResourceStart', function(resourceName) | ||
if (GetCurrentResourceName() ~= resourceName) then | ||
return | ||
end | ||
TriggerClientEvent('esx_status:load', xPlayer.source, status) | ||
end) | ||
|
||
for _, xPlayer in pairs(ESX.Players) do | ||
MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result) | ||
setPlayerStatus(xPlayer, result) | ||
end) | ||
---@param src number | ||
---@param reason string | ||
AddEventHandler('esx:playerDropped', function(src, reason) | ||
local xPlayer = ESX.GetPlayerFromId(src) | ||
if not xPlayer then | ||
return | ||
end | ||
end) | ||
|
||
AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) | ||
MySQL.scalar('SELECT status FROM users WHERE identifier = ?', { xPlayer.identifier }, function(result) | ||
setPlayerStatus(xPlayer, result) | ||
end) | ||
local status = xPlayer.get('status') or {} | ||
MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.getIdentifier() }) | ||
end) | ||
|
||
AddEventHandler('esx:playerDropped', function(playerId, reason) | ||
local xPlayer = ESX.GetPlayerFromId(playerId) | ||
local status = ESX.Players[xPlayer.source] | ||
|
||
MySQL.update('UPDATE users SET status = ? WHERE identifier = ?', { json.encode(status), xPlayer.identifier }) | ||
ESX.Players[xPlayer.source] = nil | ||
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 | ||
|
||
AddEventHandler('esx_status:getStatus', function(playerId, statusName, cb) | ||
local status = ESX.Players[playerId] | ||
local status = xPlayer.get('status') or {} | ||
for i = 1, #status do | ||
if status[i].name == statusName then | ||
return cb(status[i]) | ||
end | ||
end | ||
end) | ||
|
||
RegisterServerEvent('esx_status:update') | ||
AddEventHandler('esx_status:update', function(status) | ||
---@param status table | ||
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 | ||
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 | ||
xPlayer.set('status', status) | ||
end) |