This repository was archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.lua
More file actions
156 lines (141 loc) · 4.58 KB
/
server.lua
File metadata and controls
156 lines (141 loc) · 4.58 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
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
local Zones = nil
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
function isPlayerAuthorized(source)
if (Config.USE_ESX_ADMIN_SYSTEM) then
-- Fetch the user and check permissions
local xPlayer = ESX.GetPlayerFromId(source)
local plyGroup = xPlayer.getGroup()
for i = 1, #Config.ESX_GROUPS_ENABLED, 1 do
if Config.ESX_GROUPS_ENABLED[i] == plyGroup then
return true
end
end
elseif (Config.USE_ESSENTIALMODE_ADMIN_SYSTEM) then
-- Fetch the user and check permissions
TriggerEvent('es:getPlayerFromId', source, function(user)
if user and user.getPermissions() > Config.ESSENTIALMODE_PERMISSION_LEVEL_REQUIRED then
return true
end
return false
end)
else
for _,authorizedIdentifier in ipairs(Config.ADMINS) do
for _,playerIdentifier in ipairs(GetPlayerIdentifiers(source)) do
if playerIdentifier == authorizedIdentifier then
return true
end
end
end
end
return false
end
function fetchZone()
MySQL.ready(function()
MySQL.Async.fetchAll('SELECT * FROM zones', {}, function(zones)
for i,v in ipairs(zones) do
zones[i].points = json.decode(zones[i].points)
zones[i].center = json.decode(zones[i].center)
end
Zones = zones
for i,playerServerId in ipairs(GetPlayers()) do
TriggerClientEvent("izone:refreshClientZones", playerServerId, Zones, isPlayerAuthorized(playerServerId))
end
end)
end)
end
fetchZone()
RegisterNetEvent("izone:deleteZone")
AddEventHandler("izone:deleteZone", function(id)
if (isPlayerAuthorized(source)) then
local source = tonumber(source)
MySQL.Async.execute("DELETE FROM zones WHERE id=@id", {
['@id']=id
}, function(res)
TriggerClientEvent("izone:notification", source, "Successfully deleted the zone")
fetchZone()
end)
else
if Config.ENABLE_UNAUTHORIZE_WARNING_LOGS then
printUnauthorizedLogForPlayer()
end
end
end)
RegisterNetEvent("izone:requestZones")
AddEventHandler("izone:requestZones", function()
-- still not initialized
if Zones == nil then
fetchZone()
else
print("from request Zone " .. tostring(isPlayerAuthorized(source)))
TriggerClientEvent("izone:refreshClientZones", source, Zones, isPlayerAuthorized(source))
end
end)
RegisterNetEvent("izone:saveZone")
AddEventHandler("izone:saveZone", function(points, name, cat)
local source = source
local points = ceilPoints(points)
local encodedPoints = json.encode(points)
local center = getCenter(points)
local encodedCenter = json.encode(center)
local maxLength = getMaxLength(points, center)
MySQL.Async.execute("INSERT INTO zones (points, center, max_length, name, cat) VALUES (@points, @center, @max_length, @name, @cat)", {
['@points']=encodedPoints,
['@center']=encodedCenter,
['@max_length']=maxLength,
['@name']=name,
['@cat']=cat
}, function(res)
TriggerClientEvent("izone:notification", source, "Successfully saved the zone: " .. name, 1)
fetchZone()
end)
end)
function getCenter(points)
local allX = 0
local allY = 0
local allZ = 0
for i=1, #points do
allX = allX + tonumber(points[i].x)
allY = allY + tonumber(points[i].y)
allZ = allZ + tonumber(points[i].z)
end
local resultX = allX / #points
local resultY = allY / #points
local resultZ = allZ / #points
return {x=resultX, y=resultY, z=resultZ}
end
function getMaxLength(points, center)
local listDist = { }
for i=1, #points do
table.insert(listDist, getDistance(tonumber(center.x), tonumber(center.y), tonumber(points[i].x), tonumber(points[i].y)))
end
return getMax(listDist)
end
function getMax(table)
local max = 0
for i=1, #table do
if table[i] > max then max = table[i] end
end
return max
end
function getDistance(x1,y1,x2,y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
function ceilPoints(points)
local _points = {}
for i,v in ipairs(points) do
table.insert(_points, {
x = math.ceil(v.x*100)/100,
y = math.ceil(v.y*100)/100,
z = math.ceil(v.z*100)/100
})
end
return _points
end
function PrintPoints(points)
for i,v in ipairs(points) do
for k,v in pairs(points[i]) do
print(k,v)
end
end
end