-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.lua
More file actions
185 lines (162 loc) · 5.94 KB
/
Copy pathserver.lua
File metadata and controls
185 lines (162 loc) · 5.94 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
local function debugPrint(message)
if Config.debug then
print(message)
end
end
local function isInArray(array, value)
for _, v in ipairs(array) do
debugPrint("Checking array value: " .. tostring(v) .. " against: " .. tostring(value))
if v == value then
return true
end
end
return false
end
local function performVehicleLookup(plate, callback)
local url = Config.cadURL
local data = json.encode({plate = plate})
local headers = {
["Content-Type"] = "application/json",
["token"] = Config.apiKey
}
PerformHttpRequest(url, function(err, text, headers)
if err == 200 then
local responseData = json.decode(text)
if responseData then
debugPrint("HTTP request successful: " .. text)
callback(true, responseData)
else
debugPrint("Failed to decode JSON response.")
callback(false)
end
else
debugPrint("HTTP error while performing request: " .. err .. "\nResponse body: " .. text)
callback(false)
end
end, 'POST', data, headers)
end
RegisterNetEvent('wk:onPlateScanned')
AddEventHandler('wk:onPlateScanned', function(cam, plate, index, vehicle)
local src = source
debugPrint("Source captured: " .. tostring(src))
if isInArray(Config.vehicleTypeFilter, tonumber(vehicle.class)) then
debugPrint("Vehicle class " .. vehicle.class .. " is filtered; skipping...")
return
end
local camCapitalized = (cam == 'front' and 'Front' or 'Rear')
performVehicleLookup(plate, function(success, data)
if success and data.success and data.data and #data.data > 0 then
handleVehicleData(src, cam, plate, data.data[1])
else
if Config.noRegistrationAlerts then
notifyClient(src, camCapitalized, plate, "Not Registered", "error")
end
end
end)
end)
local function performVehicleLock(plate, userId, savePlate, callback)
local url = Config.cadURL
local data = json.encode({
plate = plate,
user_id = userId,
save_plate = savePlate
})
local headers = {
["Content-Type"] = "application/json",
["token"] = Config.apiKey
}
PerformHttpRequest(url, function(err, text, headers)
if err == 200 then
local responseData = json.decode(text)
if responseData then
debugPrint("HTTP request successful: " .. text)
callback(true, responseData)
else
debugPrint("Failed to decode JSON response.")
callback(false)
end
else
debugPrint("HTTP error while locking plate: " .. err .. "\nResponse body: " .. text)
callback(false)
end
end, 'POST', data, headers)
end
function getDiscordIdFromSource(source)
local identifiers = GetPlayerIdentifiers(source)
for _, id in pairs(identifiers) do
if string.sub(id, 1, string.len("discord:")) == "discord:" then
debugPrint("Discord ID found: " .. id)
return string.sub(id, 9)
end
end
debugPrint("No Discord ID found for user.")
return nil
end
RegisterNetEvent('wk:onPlateLocked')
AddEventHandler('wk:onPlateLocked', function(cam, plate, index, vehicle)
local src = source
debugPrint("Source captured: " .. tostring(src))
local userId = getDiscordIdFromSource(src)
if not userId then
debugPrint("No Discord ID found for user.")
return
end
local savePlate = true
local camCapitalized = (cam == 'front' and 'Front' or 'Rear')
performVehicleLock(plate, userId, savePlate, function(success, data)
if success then
if data.success and data.data and #data.data > 0 then
handleVehicleData(src, cam, plate, data.data[1])
else
if Config.noRegistrationAlerts then
notifyClient(src, camCapitalized, plate, "Not Registered", "error")
end
end
else
if Config.noRegistrationAlerts then
notifyClient(src, camCapitalized, plate, "Failed to perform lookup", "error")
end
end
end)
end)
function handleVehicleData(src, cam, plate, vehicleData)
local camCapitalized = (cam == 'front' and 'Front' or 'Rear')
local status = vehicleData.status_name
local expires = Config.showExpirationDate and ('Expires: %s'):format(vehicleData.registration_expire) or ''
local owner = vehicleData.civilian and vehicleData.civilian.full_name or 'Unknown'
notifyClient(src, camCapitalized, plate, status, "success", expires, owner)
debugPrint("Notifying client: " .. tostring(src) .. ", " .. status)
end
function notifyClient(src, cam, plate, status, type, expires, owner)
expires = expires or ''
owner = owner or 'Unknown'
local color = 'white'
local backgroundColor = '#333'
if type == "error" then
backgroundColor = '#C53030'
color = 'white'
elseif type == "success" then
backgroundColor = '#2F855A'
color = 'white'
elseif type == "warning" then
backgroundColor = '#DD6B20'
color = 'black'
end
local message = string.format(
"<div style='color: %s; background-color: %s; padding: 8px; border-radius: 5px;'>"
.. "<strong>%s ALPR</strong><br/>"
.. "Plate: <strong>%s</strong><br/>"
.. "Status: <strong>%s</strong><br/>"
.. "%s"
.. "Owner: <strong>%s</strong>"
.. "</div>",
color, backgroundColor, cam, plate:upper(), status, expires and ("Expires: <strong>" .. expires .. "</strong><br/>") or "", owner
)
TriggerClientEvent('pNotify:SendNotification', src, {
text = message,
type = type,
queue = 'alpr',
timeout = type == "success" and 30000 or 5000,
layout = 'centerLeft'
})
end