-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.lua
More file actions
162 lines (139 loc) · 6.83 KB
/
server.lua
File metadata and controls
162 lines (139 loc) · 6.83 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
local QBCore = exports['qb-core']:GetCoreObject()
local vehiclePositions = {}
RegisterServerEvent("tracker:install")
AddEventHandler("tracker:install", function(plate, isPermanent)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
plate = string.upper(string.gsub(plate, "%s+", ""))
exports.oxmysql:execute('SELECT * FROM player_vehicles WHERE plate = ?', {plate}, function(result)
if result[1] and result[1].citizenid == Player.PlayerData.citizenid then
local gpsColumn = isPermanent and "perm_gps" or "gps_installed"
if tonumber(result[1][gpsColumn]) == 1 then
TriggerClientEvent("tracker:notify", src, "✅ This vehicle already has a Tracker.")
return
end
if not isPermanent then
local gpsItem = Player.Functions.GetItemByName("gps")
if not gpsItem or gpsItem.amount < 1 then
TriggerClientEvent("tracker:notify", src, "❌ You need a Tracker device to install.")
return
end
Player.Functions.RemoveItem("gps", 1)
TriggerClientEvent("inventory:client:ItemBox", src, QBCore.Shared.Items["gps"], "remove")
end
exports.oxmysql:execute('UPDATE player_vehicles SET ' .. gpsColumn .. ' = 1 WHERE plate = ?', {plate}, function()
local veh = GetVehiclePedIsIn(GetPlayerPed(src), false)
if veh ~= 0 then
local coords = GetEntityCoords(veh)
vehiclePositions[plate] = coords
exports.oxmysql:execute('UPDATE player_vehicles SET gps_last_x = ?, gps_last_y = ?, gps_last_z = ? WHERE plate = ?', {
coords.x, coords.y, coords.z, plate
})
end
TriggerClientEvent("tracker:installed", src, plate)
TriggerClientEvent("tracker:updatePlates", src, GetPlatesForPlayer(Player.PlayerData.citizenid))
end)
else
TriggerClientEvent("tracker:notify", src, "❌ You do not own this vehicle.")
end
end)
end)
RegisterCommand("removegps", function(source)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local ped = GetPlayerPed(source)
local veh = GetVehiclePedIsIn(ped, false)
if veh == 0 then
TriggerClientEvent("tracker:notify", source, "❌ You must be in a vehicle.")
return
end
local plate = string.upper(string.gsub(GetVehicleNumberPlateText(veh), "%s+", ""))
exports.oxmysql:execute('SELECT * FROM player_vehicles WHERE citizenid = ? AND gps_installed = 1', {Player.PlayerData.citizenid}, function(result)
local found = false
for _, row in pairs(result) do
local dbPlate = string.upper(string.gsub(row.plate, "%s+", ""))
if dbPlate == plate then
found = true
exports.oxmysql:execute('UPDATE player_vehicles SET gps_installed = 0 WHERE plate = ?', {row.plate})
TriggerClientEvent("tracker:notify", source, "🧰 Tracker removed from vehicle [" .. row.plate .. "]")
TriggerClientEvent("tracker:updatePlates", source, GetPlatesForPlayer(Player.PlayerData.citizenid))
vehiclePositions[dbPlate] = nil
break
end
end
if not found then
TriggerClientEvent("tracker:notify", source, "❌ This vehicle has no GPS installed or is not registered under you.")
end
end)
end)
RegisterNetEvent("tracker:updateVehiclePositions")
AddEventHandler("tracker:updateVehiclePositions", function(vehicles)
for _, v in ipairs(vehicles) do
local plate = string.upper(string.gsub(v.plate, "%s+", ""))
vehiclePositions[plate] = v.coords
exports.oxmysql:execute('UPDATE player_vehicles SET gps_last_x = ?, gps_last_y = ?, gps_last_z = ? WHERE plate = ?', {
v.coords.x, v.coords.y, v.coords.z, plate
})
end
end)
RegisterServerEvent("tracker:findVehicle")
AddEventHandler("tracker:findVehicle", function(plate)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
local cleanPlate = string.upper(string.gsub(plate, "%s+", ""))
exports.oxmysql:execute('SELECT * FROM player_vehicles WHERE citizenid = ? AND (gps_installed = 1 OR perm_gps = 1)', {
Player.PlayerData.citizenid
}, function(result)
local matched = false
for _, v in pairs(result) do
local dbPlate = string.upper(string.gsub(v.plate, "%s+", ""))
if dbPlate == cleanPlate then
matched = true
local coords = vehiclePositions[cleanPlate]
if not coords and v.gps_last_x and v.gps_last_y and v.gps_last_z then
coords = vector3(v.gps_last_x, v.gps_last_y, v.gps_last_z)
vehiclePositions[cleanPlate] = coords
end
if coords then
TriggerClientEvent("tracker:setWaypoint", src, coords)
TriggerClientEvent("tracker:notify", src, "📍 Tracker: Vehicle location added to your map.")
else
TriggerClientEvent("tracker:notify", src, "❌ Tracker: No recent location found for this vehicle.")
end
break
end
end
if not matched then
TriggerClientEvent("tracker:notify", src, "❌ This vehicle no longer has GPS installed or isn’t registered under you.")
end
end)
end)
function GetPlatesForPlayer(citizenid)
local plates = {}
local result = exports.oxmysql:executeSync([[SELECT plate FROM player_vehicles WHERE citizenid = ? AND (gps_installed = 1 OR perm_gps = 1)]], {citizenid})
for _, v in pairs(result) do
table.insert(plates, string.upper(string.gsub(v.plate, "%s+", "")))
end
return plates
end
RegisterNetEvent("tracker:playerJoined")
AddEventHandler("tracker:playerJoined", function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
exports.oxmysql:execute('SELECT plate, gps_last_x, gps_last_y, gps_last_z FROM player_vehicles WHERE citizenid = ? AND (gps_installed = 1 OR perm_gps = 1)', {
Player.PlayerData.citizenid
}, function(result)
for _, row in ipairs(result) do
local plate = string.upper(string.gsub(row.plate, "%s+", ""))
if row.gps_last_x and row.gps_last_y and row.gps_last_z then
vehiclePositions[plate] = vector3(row.gps_last_x, row.gps_last_y, row.gps_last_z)
else
vehiclePositions[plate] = nil
end
end
TriggerClientEvent("tracker:updatePlates", src, GetPlatesForPlayer(Player.PlayerData.citizenid))
end)
end)