· 6 years ago · Jun 08, 2019, 02:30 PM
1--[[
2 Cops_FiveM - A cops script for FiveM RP servers.
3 Copyright (C) 2018 FiveM-Scripts
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU Affero General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU Affero General Public License for more details.
13You should have received a copy of the GNU Affero General Public License
14along with Cops_FiveM in the file "LICENSE". If not, see <http://www.gnu.org/licenses/>.
15]]
16
17
18if config.useCopWhitelist then
19 local setupTable = "CREATE TABLE IF NOT EXISTS `police` (`identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`dept` int(11) NOT NULL DEFAULT '0',`rank` int(11) NOT NULL DEFAULT '0')"
20 exports.ghmattimysql:execute(setupTable, {}, function()
21 IsDatabaseVerified = true
22 end)
23end
24
25if GetResourceMetadata(GetCurrentResourceName(), 'resource_Isdev', 0) == "yes" then
26 RconPrint("/!\\ You are running a dev version of Cops FiveM !\n")
27end
28
29if config.enableVersionNotifier then
30 PerformHttpRequest("https://raw.githubusercontent.com/FiveM-Scripts/Cops_FiveM/master/police/__resource.lua", function(errorCode, result, headers)
31 local version = GetResourceMetadata(GetCurrentResourceName(), 'resource_version', 0)
32
33 if string.find(tostring(result), version) == nil then
34 print("\n\r[Cops_FiveM] The version on this server is not up to date. Please update now.\n\r")
35 end
36 end, "GET", "", "")
37end
38
39local inServiceCops = {}
40
41function addCop(identifier)
42 exports.ghmattimysql:scalar("SELECT identifier FROM police WHERE identifier = @identifier", { ['identifier'] = tostring(identifier)}, function (result)
43 if not result then
44 exports.ghmattimysql:execute("INSERT INTO police (`identifier`) VALUES ('"..identifier.."')", {['@identifier'] = identifier})
45 end
46 end)
47end
48
49function setDept(source, player,playerDept)
50 local identifier = getPlayerID(player)
51 if(config.departments.label[playerDept]) then
52 exports.ghmattimysql:execute("SELECT * FROM police WHERE identifier = '"..identifier.."'", { ['@identifier'] = identifier}, function (result)
53 if(result[1]) then
54 if(result[1].dept ~= playerDept) then
55 exports.ghmattimysql:execute("UPDATE police SET dept="..playerDept.." WHERE identifier='"..identifier.."'", { ['identifier'] = identifier})
56 TriggerClientEvent('chatMessage', source, i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("command_received"))
57 TriggerClientEvent("police:notify", player, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("new_dept") .. " " .. config.departments.label[playerDept])
58 TriggerClientEvent('police:receiveIsCop', source, result[1].rank, playerDept)
59 else
60 TriggerClientEvent('chatMessage', source, i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("same_dept"))
61 end
62 else
63 TriggerClientEvent('chatMessage', source, i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("player_not_cop"))
64 end
65 end)
66 else
67 TriggerClientEvent('chatMessage', source, i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("dept_not_exist"))
68 end
69end
70
71function remCop(identifier)
72 exports.ghmattimysql:execute("DELETE FROM police WHERE identifier = '"..identifier.."'", { ['identifier'] = identifier})
73end
74
75AddEventHandler('playerDropped', function()
76 if(inServiceCops[source]) then
77 inServiceCops[source] = nil
78
79 for i, c in pairs(inServiceCops) do
80 TriggerClientEvent("police:resultAllCopsInService", i, inServiceCops)
81 end
82 end
83end)
84
85RegisterServerEvent('police:checkIsCop')
86AddEventHandler('police:checkIsCop', function()
87 local identifier = getPlayerID(source)
88 local src = source
89
90 if config.useCopWhitelist then
91 exports.ghmattimysql:scalar("SELECT `identifier` FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function(result)
92 if not result then
93 TriggerClientEvent('police:receiveIsCop', src, -1)
94 else
95 exports.ghmattimysql:execute("SELECT * FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function(data)
96 if data then
97 TriggerClientEvent('police:receiveIsCop', src, data[1].rank, data[1].dept)
98 end
99 end)
100 end
101 end)
102 else
103 TriggerClientEvent('police:receiveIsCop', src, 0, 1)
104 end
105end)
106
107RegisterServerEvent('police:takeService')
108AddEventHandler('police:takeService', function()
109
110 if(not inServiceCops[source]) then
111 inServiceCops[source] = getPlayerID(source)
112
113 for i, c in pairs(inServiceCops) do
114 TriggerClientEvent("police:resultAllCopsInService", i, inServiceCops)
115 end
116 end
117end)
118
119RegisterServerEvent('police:breakService')
120AddEventHandler('police:breakService', function()
121
122 if(inServiceCops[source]) then
123 inServiceCops[source] = nil
124
125 for i, c in pairs(inServiceCops) do
126 TriggerClientEvent("police:resultAllCopsInService", i, inServiceCops)
127 end
128 end
129end)
130
131RegisterServerEvent('police:getAllCopsInService')
132AddEventHandler('police:getAllCopsInService', function()
133 TriggerClientEvent("police:resultAllCopsInService", source, inServiceCops)
134end)
135
136RegisterServerEvent('police:removeWeapons')
137AddEventHandler('police:removeWeapons', function(target)
138 local identifier = getPlayerID(target)
139 TriggerClientEvent("police:removeWeapons", target)
140end)
141
142RegisterServerEvent('police:confirmUnseat')
143AddEventHandler('police:confirmUnseat', function(t)
144 TriggerClientEvent("police:notify", source, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("unseat_sender_notification_part_1") .. GetPlayerName(t) .. i18n.translate("unseat_sender_notification_part_2"))
145 TriggerClientEvent('police:unseatme', t)
146end)
147
148RegisterServerEvent('police:dragRequest')
149AddEventHandler('police:dragRequest', function(t)
150 TriggerClientEvent("police:notify", source, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("drag_sender_notification_part_1").. GetPlayerName(t) .. i18n.translate("drag_sender_notification_part_2"))
151 TriggerClientEvent('police:toggleDrag', t, source)
152end)
153
154RegisterServerEvent('police:finesGranted')
155AddEventHandler('police:finesGranted', function(target, amount)
156 TriggerClientEvent('police:payFines', target, amount, source)
157 TriggerClientEvent("police:notify", source, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("send_fine_request_part_1")..amount..i18n.translate("send_fine_request_part_2")..GetPlayerName(target))
158end)
159
160RegisterServerEvent('police:finesETA')
161AddEventHandler('police:finesETA', function(officer, code)
162 if(code==1) then
163 TriggerClientEvent("police:notify", officer, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, GetPlayerName(source)..i18n.translate("already_have_a_pendind_fine_request"))
164 elseif(code==2) then
165 TriggerClientEvent("police:notify", officer, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, GetPlayerName(source)..i18n.translate("request_fine_timeout"))
166 elseif(code==3) then
167 TriggerClientEvent("police:notify", officer, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, GetPlayerName(source)..i18n.translate("request_fine_refused"))
168 elseif(code==0) then
169 TriggerClientEvent("police:notify", officer, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, GetPlayerName(source)..i18n.translate("request_fine_accepted"))
170 end
171end)
172
173RegisterServerEvent('police:cuffGranted')
174AddEventHandler('police:cuffGranted', function(t)
175 TriggerClientEvent("police:notify", source, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("toggle_cuff_player_part_1")..GetPlayerName(t)..i18n.translate("toggle_cuff_player_part_2"))
176 TriggerClientEvent('police:getArrested', t)
177end)
178
179RegisterServerEvent('police:forceEnterAsk')
180AddEventHandler('police:forceEnterAsk', function(t, v)
181 TriggerClientEvent("police:notify", source, "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("force_player_get_in_vehicle_part_1")..GetPlayerName(t)..i18n.translate("force_player_get_in_vehicle_part_2"))
182 TriggerClientEvent('police:forcedEnteringVeh', t, v)
183end)
184
185RegisterServerEvent('CheckPoliceVeh')
186AddEventHandler('CheckPoliceVeh', function(vehicle)
187 TriggerClientEvent('FinishPoliceCheckForVeh',source)
188 TriggerClientEvent('policeveh:spawnVehicle', source, vehicle)
189end)
190
191RegisterServerEvent('police:UpdateNotifier')
192AddEventHandler('police:UpdateNotifier', function()
193 local src = source
194 PerformHttpRequest("https://raw.githubusercontent.com/FiveM-Scripts/Cops_FiveM/master/police/__resource.lua", function(errorCode, result, headers)
195 local version = GetResourceMetadata(GetCurrentResourceName(), 'resource_version', 0)
196 if string.find(tostring(result), version) == nil then
197 TriggerClientEvent('police:Update', src, true)
198 end
199 end, "GET", "", "")
200end)
201
202RegisterCommand("CopAddAdmin", function(source,args,raw)
203 if #args ~= 1 then
204 RconPrint("Usage: CopAddAdmin [ingame-id]\n")
205 CancelEvent()
206 return
207 else
208 local maxi = -1
209 for key, value in pairs(config.rank.label) do
210 if key > maxi then
211 maxi = key
212 end
213 end
214
215 if(GetPlayerName(tonumber(args[1])) == nil)then
216 RconPrint("Player is not ingame\n")
217 CancelEvent()
218
219 return
220 end
221
222 local identifier = getPlayerID(tonumber(args[1]))
223 exports.ghmattimysql:scalar("SELECT identifier FROM police WHERE identifier = @identifier", {['identifier'] = identifier}, function(result)
224 if not result then
225 exports.ghmattimysql:execute("INSERT INTO police (`identifier`, `dept`, `rank`) VALUES (@identifier, @dept, @maxi)", { ['identifier'] = identifier, ['dept'] = 1, ['maxi'] = maxi})
226 TriggerClientEvent("police:notify", tonumber(args[1]), "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("become_cop_success"))
227
228 RconPrint(GetPlayerName(tonumber(args[1])) .. " is now added to the police database.\n")
229 TriggerClientEvent('police:receiveIsCop', tonumber(args[1]), maxi, 1)
230 else
231 RconPrint(GetPlayerName(tonumber(args[1])) .. ' already exists.\n')
232 end
233 end)
234 CancelEvent()
235 end
236end, true)
237
238RegisterCommand("CopAdd", function(source,args,raw)
239 if #args ~= 1 then
240 RconPrint("Usage: CopAdd [ingame-id]\n")
241 CancelEvent()
242 return
243 else
244 if GetPlayerName(tonumber(args[1])) == nil then
245 RconPrint("Player is not ingame\n")
246 CancelEvent()
247 return
248 end
249
250 local identifier = getPlayerID(tonumber(args[1]))
251 exports.ghmattimysql:scalar("SELECT identifier FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function (result)
252 if not result then
253 print('Adding record for player to the database')
254 addCop(identifier)
255
256 TriggerClientEvent("police:notify", tonumber(args[1]), "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("become_cop_success"))
257 TriggerClientEvent('police:receiveIsCop', tonumber(args[1]), 0, 1)
258
259 RconPrint(GetPlayerName(tonumber(args[1])) .. " is now added to the police database.\n")
260 else
261 RconPrint(GetPlayerName(tonumber(args[1])) .. " is already a police officer.\n")
262 end
263 end)
264 end
265end, true)
266
267RegisterCommand("CopRem", function(source,args,raw)
268 if #args ~= 1 then
269 RconPrint("Usage: CopRem [ingame-id]\n")
270 CancelEvent()
271 return
272 else
273 if(GetPlayerName(tonumber(args[1])) == nil)then
274 RconPrint("Player is not ingame\n")
275 CancelEvent()
276 return
277 end
278
279 local identifier = getPlayerID(tonumber(args[1]))
280
281 exports.ghmattimysql:scalar("SELECT identifier FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function (result)
282 if not result then
283 RconPrint(GetPlayerName(tonumber(args[1])) .. " isn't here.\n")
284 else
285 exports.ghmattimysql:execute("DELETE FROM police WHERE identifier = @identifier", { ['identifier'] = identifier})
286 TriggerClientEvent('police:noLongerCop', tonumber(args[1]))
287 TriggerClientEvent("police:notify", tonumber(args[1]), "CHAR_AGENT14", 1, i18n.translate("title_notification"), false, i18n.translate("remove_from_cops"))
288 RconPrint(GetPlayerName(tonumber(args[1])) .. " is now removed from the police database.\n")
289 end
290 end)
291
292 CancelEvent()
293 end
294end, true)
295
296RegisterCommand("CopRank", function(source,args,raw)
297 if #args ~= 2 then
298 RconPrint("Usage: CopRank [ingame-id] [rank]\n")
299 CancelEvent()
300 return
301 elseif(not config.rank.label[tonumber(args[2])]) then
302 RconPrint("You have to enter a valid rank !\n")
303 CancelEvent()
304 return
305 else
306 if(GetPlayerName(tonumber(args[1])) == nil)then
307 RconPrint("Player is not ingame\n")
308 CancelEvent()
309 return
310 end
311
312 local identifier = getPlayerID(tonumber(args[1]))
313 exports.ghmattimysql:scalar("SELECT `identifier` FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function (rank)
314 if(rank == nil) then
315 RconPrint(GetPlayerName(tonumber(args[1])) .. " isn't here.\n")
316 else
317 exports.ghmattimysql:execute("UPDATE police SET `rank` = @rank WHERE identifier = @identifier", { ['identifier'] = identifier, ['rank'] = args[2]})
318 TriggerClientEvent('police:receiveIsCop', tonumber(args[1]), tonumber(args[2]))
319 RconPrint(GetPlayerName(tonumber(args[1])) .. " information has been updated.\n")
320 end
321 end)
322
323 CancelEvent()
324 end
325end, true)
326
327RegisterCommand("CopDept", function(source,args,raw)
328 if #args ~= 2 then
329 RconPrint("Usage: CopDept [ingame-id] [department]\n")
330 CancelEvent()
331 return
332 else
333 if(GetPlayerName(tonumber(args[1])) == nil) then
334 RconPrint("Player is not ingame\n")
335 CancelEvent()
336 return
337 end
338
339 local identifier = getPlayerID(tonumber(args[1]))
340 exports.ghmattimysql:scalar("SELECT `identifier` FROM police WHERE identifier = @identifier", { ['identifier'] = identifier}, function (result)
341 if result then
342 if GetPlayerName(tonumber(args[1])) ~= nil then
343 local player = tonumber(args[1])
344 local dept = tonumber(args[2])
345
346 setDept(args[1], player, dept)
347 else
348 TriggerClientEvent('chatMessage', args[1], i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("no_player_with_this_id"))
349 end
350 else
351 TriggerClientEvent('chatMessage', args[1], i18n.translate("title_notification"), {255, 0, 0}, i18n.translate("not_enough_permission"))
352 end
353 end)
354
355 CancelEvent()
356 end
357end, true)
358
359function getPlayerID(source)
360 local identifiers = GetPlayerIdentifiers(source)
361 local player = getIdentifiant(identifiers)
362 return player
363end
364
365function getIdentifiant(id)
366 for _, v in ipairs(id) do
367 return v
368 end
369end