-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.lua
179 lines (131 loc) · 5.24 KB
/
server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
ESX = exports['es_extended']:getSharedObject()
local sleepingPeds = {}
local function Debug(msg, ...)
if Config.Debug then
print('^2[Ali_SleepOffline][SERVER] ^7' .. string.format(msg, ...))
end
end
local function maskLastname(lastname)
if not Config.NameDisplay.MaskLastname then
return lastname
end
local visibleLength = math.min(Config.NameDisplay.MaskLength, #lastname)
return lastname:sub(1, visibleLength) .. string.rep('*', #lastname - visibleLength)
end
local function getPlayerData(identifier)
local skin = nil
local playerName = Config.Locales[Config.Locale]['unknown']
local result = MySQL.Sync.fetchAll(string.format('SELECT %s FROM %s WHERE %s = @identifier',
Config.MySQL.Tables.Fields.Skin,
Config.MySQL.Tables.Users,
Config.MySQL.Tables.Fields.Identifier
), {
['@identifier'] = identifier
})
if result[1] then
skin = json.decode(result[1].skin)
Debug('Loaded skin data for player')
else
Debug('No skin data found for player')
end
local result = MySQL.Sync.fetchAll(string.format('SELECT %s, %s FROM %s WHERE %s = @identifier',
Config.MySQL.Tables.Fields.Firstname,
Config.MySQL.Tables.Fields.Lastname,
Config.MySQL.Tables.Users,
Config.MySQL.Tables.Fields.Identifier
), {
['@identifier'] = identifier
})
if result[1] then
local lastname = maskLastname(result[1][Config.MySQL.Tables.Fields.Lastname])
playerName = string.format('%s %s',
result[1][Config.MySQL.Tables.Fields.Firstname],
lastname
)
Debug('Got player name: %s', playerName)
end
return skin, playerName
end
RegisterCommand(Config.Permissions.FakeCommandName, function(source)
if source == 0 then return end
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then return end
if not xPlayer.getGroup() == Config.Permissions.FakeCommand then
Debug('Player %s tried to use test command without permission', source)
return
end
Debug('Player dropped: %s', source)
Debug('Player identifier: %s', xPlayer.identifier)
local ped = GetPlayerPed(source)
local coords = GetEntityCoords(ped)
local heading = GetEntityHeading(ped)
local skin, playerName = getPlayerData(xPlayer.identifier)
Debug('Saving sleeping ped data at coords: %s, %s, %s', coords.x, coords.y, coords.z)
sleepingPeds[xPlayer.identifier] = {
coords = coords,
heading = heading,
skin = skin,
name = playerName,
timestamp = os.time()
}
TriggerClientEvent('ali_sleepoffline:spawnSleepingPed', -1, xPlayer.identifier, coords, heading, skin, playerName)
end)
AddEventHandler('playerDropped', function()
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
Debug('Player dropped: %s', source)
if xPlayer then
Debug('Player identifier: %s', xPlayer.identifier)
local ped = GetPlayerPed(source)
local coords = GetEntityCoords(ped)
local heading = GetEntityHeading(ped)
local skin, playerName = getPlayerData(xPlayer.identifier)
Debug('Saving sleeping ped data at coords: %s, %s, %s', coords.x, coords.y, coords.z)
sleepingPeds[xPlayer.identifier] = {
coords = coords,
heading = heading,
skin = skin,
name = playerName,
timestamp = os.time()
}
TriggerClientEvent('ali_sleepoffline:spawnSleepingPed', -1, xPlayer.identifier, coords, heading, skin, playerName)
end
end)
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(source)
local xPlayer = ESX.GetPlayerFromId(source)
Debug('Player loaded: %s', source)
if xPlayer then
Debug('Player identifier: %s', xPlayer.identifier)
if sleepingPeds[xPlayer.identifier] then
Debug('Found sleeping ped for player, removing...')
TriggerClientEvent('ali_sleepoffline:removeSleepingPed', -1, xPlayer.identifier)
sleepingPeds[xPlayer.identifier] = nil
else
Debug('No sleeping ped found for player')
end
end
end)
local function removeOldPeds()
local currentTime = os.time()
local pedsRemoved = false
for identifier, data in pairs(sleepingPeds) do
if (currentTime - data.timestamp) > (Config.PedTimeout * 60) then
Debug('Removing old ped for %s (timeout exceeded)', identifier)
TriggerClientEvent('ali_sleepoffline:removeSleepingPed', -1, identifier)
sleepingPeds[identifier] = nil
pedsRemoved = true
end
end
return pedsRemoved
end
CreateThread(function()
while true do
Wait(Config.PedCheckInterval * 60 * 1000)
removeOldPeds()
end
end)
ESX.RegisterServerCallback('ali_sleepoffline:getSleepingPeds', function(source, cb)
Debug('Sending sleeping peds data to client: %s', source)
cb(sleepingPeds)
end)