-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
633e252
commit 22efa31
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
server-data/resources/[bpt_addons]/bpt_status/server/classes/status.lua
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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('bpt_status:updateClient', self.xPlayer.source) | ||
end | ||
|
||
return self | ||
end |
67 changes: 67 additions & 0 deletions
67
server-data/resources/[bpt_addons]/bpt_status/server/main.lua
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
local function setPlayerStatus(xPlayer, data) | ||
data = data and json.decode(data) or {} | ||
|
||
xPlayer.set('status', data) | ||
ESX.Players[xPlayer.source] = data | ||
TriggerClientEvent('bpt_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 | ||
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) | ||
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) | ||
|
||
AddEventHandler('bpt_status:getStatus', function(playerId, statusName, cb) | ||
local status = ESX.Players[playerId] | ||
for i = 1, #status do | ||
if status[i].name == statusName then | ||
return cb(status[i]) | ||
end | ||
end | ||
end) | ||
|
||
RegisterServerEvent('bpt_status:update') | ||
AddEventHandler('bpt_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 | ||
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) |