-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.lua
More file actions
92 lines (69 loc) · 2.67 KB
/
server.lua
File metadata and controls
92 lines (69 loc) · 2.67 KB
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
local positions = {}
local autoSave = true
local timeOut = 60 -- Delay between saving positions in database (in seconds)
local printInfo = false -- Print 'Saved location for: ..' ?
-- Not recommended, only use for debugging
RegisterServerEvent('syncpos:sync')
AddEventHandler('syncpos:sync', function(vec)
savePosition(source, vec)
end)
RegisterServerEvent('syncpos:manualSave')
AddEventHandler('syncpos:manualSave', function()
steamId = GetPlayerIdentifier(source, 0)
if(steamId ~= nil) then
x, y, z = table.unpack(positions[steamId])
encoded = '{' .. x .. ', ' .. y .. ', ' .. z .. '}'
MySQL.Async.execute('UPDATE users SET lastpos = "' .. encoded .. '" WHERE identifier = "' .. steamId .. '";', {}, function(success)
if(success == 0) then
print("MySQL failure")
end
end)
else
print("Failed to save position for " .. steamId)
end
end)
RegisterServerEvent('syncpos:spawn')
AddEventHandler('syncpos:spawn', function()
local steamId = GetPlayerIdentifier(source, 0)
cachedPos = positions[steamId]
if(cachedPos ~= nil) then
x, y, z = table.unpack(positions[steamId])
TriggerClientEvent('syncpos:clientspawn', source, x, y, z)
else
MySQL.Async.fetchScalar('SELECT lastpos FROM users WHERE identifier = "' .. steamId .. '";', {}, function(result)
array = json.decode(result)
TriggerClientEvent('syncpos:clientspawn', source, array[1], array[2], array[3])
end)
end
end)
AddEventHandler('onResourceStop', function(resourceName)
if (GetCurrentResourceName() ~= resourceName) then
return
end
print('[SyncPos] [<ALERT>] Saving all positions due to resource stop')
mysqlSync()
end)
function savePosition(playerId, positionVec)
local steamId = GetPlayerIdentifier(playerId, 0)
positions[steamId] = positionVec
-- This function caches the players last location
-- It will get saved manually or via AutoSave function
end
function mysqlSync()
local queryString = ''
for k,v in pairs(positions) do
if(printInfo) then
print('[SyncPos] Saving location for ' .. k)
end
x, y, z = table.unpack(v)
encoded = '{' .. x .. ', ' .. y .. ', ' .. z .. '}'
queryString = queryString .. 'UPDATE users SET lastpos = "' .. encoded .. '" WHERE identifier = "' .. k .. '";'
end
MySQL.Async.execute(queryString)
end
Citizen.CreateThread(function()
while autoSave do
Citizen.Wait((timeOut) * 1000)
mysqlSync()
end
end)