forked from Project-Sloth/ps-mdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
1353 lines (1217 loc) · 49.9 KB
/
main.lua
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local QBCore = exports['qb-core']:GetCoreObject()
-- Maybe cache?
local incidents = {}
local convictions = {}
local bolos = {}
-- TODO make it departments compatible
local activeUnits = {}
local impound = {}
local dispatchMessages = {}
local isDispatchRunning = false
local function IsPolice(job)
for k, v in pairs(Config.PoliceJobs) do
if job == k then
return true
end
end
return false
end
local function GetActiveData(cid)
local player = type(cid) == "string" and cid or tostring(cid)
if player then
return activeUnits[player] and true or false
end
return false
end
RegisterServerEvent("ps-mdt:dispatchStatus", function(bool)
isDispatchRunning = bool
end)
if Config.UseWolfknightRadar == true then
RegisterNetEvent("wk:onPlateScanned")
AddEventHandler("wk:onPlateScanned", function(cam, plate, index)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local bolo = GetBoloStatus(plate)
if bolo == true then
TriggerClientEvent("wk:togglePlateLock", src, cam, true, bolo)
end
end)
end
RegisterNetEvent("ps-mdt:server:OnPlayerUnload", function()
--// Delete player from the MDT on logout
local src = source
local player = QBCore.Functions.GetPlayer(src)
if GetActiveData(player.PlayerData.citizenid) then
activeUnits[player.PlayerData.citizenid] = nil
end
end)
AddEventHandler("playerDropped", function(reason)
--// Delete player from the MDT on logout
local src = source
local player = QBCore.Functions.GetPlayer(src)
if player ~= nil then
if GetActiveData(player.PlayerData.citizenid) then
activeUnits[player.PlayerData.citizenid] = nil
end
else
local license = QBCore.Functions.GetIdentifier(src, "license")
local citizenids = GetCitizenID(license)
for _, v in pairs(citizenids) do
if GetActiveData(v.citizenid) then
activeUnits[v.citizenid] = nil
end
end
end
end)
RegisterNetEvent("ps-mdt:server:ToggleDuty", function()
local src = source
local player = QBCore.Functions.GetPlayer(src)
if not player.PlayerData.job.onduty then
--// Remove from MDT
if GetActiveData(player.PlayerData.citizenid) then
activeUnits[player.PlayerData.citizenid] = nil
end
end
end)
RegisterNetEvent('mdt:server:openMDT', function()
local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(src, PlayerData) then return end
local Radio = Player(src).state.radioChannel or 0
--[[ if Radio > 100 then
Radio = 0
end ]]
activeUnits[PlayerData.citizenid] = {
cid = PlayerData.citizenid,
callSign = PlayerData.metadata['callsign'],
firstName = PlayerData.charinfo.firstname:sub(1,1):upper()..PlayerData.charinfo.firstname:sub(2),
lastName = PlayerData.charinfo.lastname:sub(1,1):upper()..PlayerData.charinfo.lastname:sub(2),
radio = Radio,
unitType = PlayerData.job.name,
duty = PlayerData.job.onduty
}
local JobType = GetJobType(PlayerData.job.name)
local bulletin = GetBulletins(JobType)
local calls = exports['ps-dispatch']:GetDispatchCalls()
--TriggerClientEvent('mdt:client:dashboardbulletin', src, bulletin)
TriggerClientEvent('mdt:client:open', src, bulletin, activeUnits, calls, PlayerData.citizenid)
--TriggerClientEvent('mdt:client:GetActiveUnits', src, activeUnits)
end)
QBCore.Functions.CreateCallback('mdt:server:SearchProfile', function(source, cb, sentData)
if not sentData then return cb({}) end
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType ~= nil then
local people = MySQL.query.await("SELECT p.citizenid, p.charinfo, md.pfp FROM players p LEFT JOIN mdt_data md on p.citizenid = md.cid WHERE LOWER(CONCAT(JSON_VALUE(p.charinfo, '$.firstname'), ' ', JSON_VALUE(p.charinfo, '$.lastname'))) LIKE :query OR LOWER(`charinfo`) LIKE :query OR LOWER(`citizenid`) LIKE :query OR LOWER(`fingerprint`) LIKE :query AND jobtype = :jobtype LIMIT 20", { query = string.lower('%'..sentData..'%'), jobtype = JobType })
local citizenIds = {}
local citizenIdIndexMap = {}
if not next(people) then cb({}) return end
for index, data in pairs(people) do
people[index]['warrant'] = false
people[index]['convictions'] = 0
people[index]['licences'] = GetPlayerLicenses(data.citizenid)
people[index]['pp'] = ProfPic(data.gender, data.pfp)
citizenIds[#citizenIds+1] = data.citizenid
citizenIdIndexMap[data.citizenid] = index
end
local convictions = GetConvictions(citizenIds)
if next(convictions) then
for _, conv in pairs(convictions) do
if conv.warrant then people[citizenIdIndexMap[conv.cid]].warrant = true end
local charges = json.decode(conv.charges)
people[citizenIdIndexMap[conv.cid]].convictions = people[citizenIdIndexMap[conv.cid]].convictions + #charges
end
end
return cb(people)
end
end
return cb({})
end)
QBCore.Functions.CreateCallback("mdt:server:getWarrants", function(source, cb)
local WarrantData = {}
local data = MySQL.query.await("SELECT * FROM mdt_convictions", {})
for _, value in pairs(data) do
if value.warrant == "1" then
WarrantData[#WarrantData+1] = {
cid = value.cid,
linkedincident = value.linkedincident,
name = GetNameFromId(value.cid),
time = value.time
}
end
end
cb(WarrantData)
end)
QBCore.Functions.CreateCallback('mdt:server:OpenDashboard', function(source, cb)
local PlayerData = GetPlayerData(source)
if not PermCheck(source, PlayerData) then return end
local JobType = GetJobType(PlayerData.job.name)
local bulletin = GetBulletins(JobType)
cb(bulletin)
end)
RegisterNetEvent('mdt:server:NewBulletin', function(title, info, time)
local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(src, PlayerData) then return end
local JobType = GetJobType(PlayerData.job.name)
local playerName = GetNameFromPlayerData(PlayerData)
local newBulletin = MySQL.insert.await('INSERT INTO `mdt_bulletin` (`title`, `desc`, `author`, `time`, `jobtype`) VALUES (:title, :desc, :author, :time, :jt)', {
title = title,
desc = info,
author = playerName,
time = tostring(time),
jt = JobType
})
AddLog(("A new bulletin was added by %s with the title: %s!"):format(playerName, title))
TriggerClientEvent('mdt:client:newBulletin', -1, src, {id = newBulletin, title = title, info = info, time = time, author = PlayerData.CitizenId}, JobType)
end)
RegisterNetEvent('mdt:server:deleteBulletin', function(id, title)
if not id then return false end
local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(src, PlayerData) then return end
local JobType = GetJobType(PlayerData.job.name)
MySQL.query.await('DELETE FROM `mdt_bulletin` where id = ?', {id})
AddLog("Bulletin with Title: "..title.." was deleted by " .. GetNameFromPlayerData(PlayerData) .. ".")
end)
QBCore.Functions.CreateCallback('mdt:server:GetProfileData', function(source, cb, sentId)
if not sentId then return cb({}) end
local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(src, PlayerData) then return cb({}) end
local JobType = GetJobType(PlayerData.job.name)
local target = GetPlayerDataById(sentId)
local JobName = PlayerData.job.name
if not target or not next(target) then return cb({}) end
-- Convert to string because bad code, yes?
if type(target.job) == 'string' then target.job = json.decode(target.job) end
if type(target.charinfo) == 'string' then target.charinfo = json.decode(target.charinfo) end
if type(target.metadata) == 'string' then target.metadata = json.decode(target.metadata) end
local licencesdata = target.metadata['licences'] or {
['driver'] = false,
['business'] = false,
['weapon'] = false,
['pilot'] = false
}
local job, grade = UnpackJob(target.job)
local person = {
cid = target.citizenid,
firstname = target.charinfo.firstname,
lastname = target.charinfo.lastname,
job = job.label,
grade = grade.name,
pp = ProfPic(target.charinfo.gender),
licences = licencesdata,
dob = target.charinfo.birthdate,
mdtinfo = '',
fingerprint = '',
tags = {},
vehicles = {},
properties = {},
gallery = {},
isLimited = false
}
if Config.PoliceJobs[JobName] then
local convictions = GetConvictions({person.cid})
person.convictions2 = {}
local convCount = 1
if next(convictions) then
for _, conv in pairs(convictions) do
if conv.warrant then person.warrant = true end
local charges = json.decode(conv.charges)
for _, charge in pairs(charges) do
person.convictions2[convCount] = charge
convCount = convCount + 1
end
end
end
local hash = {}
person.convictions = {}
for _,v in ipairs(person.convictions2) do
if (not hash[v]) then
person.convictions[#person.convictions+1] = v -- found this dedupe method on sourceforge somewhere, copy+pasta dev, needs to be refined later
hash[v] = true
end
end
local vehicles = GetPlayerVehicles(person.cid)
if vehicles then
person.vehicles = vehicles
end
local Coords = {}
local Houses = {}
local properties= GetPlayerProperties(person.cid)
for k, v in pairs(properties) do
Coords[#Coords+1] = {
coords = json.decode(v["coords"]),
}
end
for index = 1, #Coords, 1 do
Houses[#Houses+1] = {
label = properties[index]["label"],
coords = tostring(Coords[index]["coords"]["enter"]["x"]..",".. Coords[index]["coords"]["enter"]["y"].. ",".. Coords[index]["coords"]["enter"]["z"]),
}
end
-- if properties then
person.properties = Houses
-- end
end
local mdtData = GetPersonInformation(sentId, JobType)
if mdtData then
person.mdtinfo = mdtData.information
person.fingerprint = mdtData.fingerprint
person.profilepic = mdtData.pfp
person.tags = json.decode(mdtData.tags)
person.gallery = json.decode(mdtData.gallery)
end
local mdtData2 = GetPfpFingerPrintInformation(sentId)
if mdtData2 then
person.fingerprint = mdtData2.fingerprint
person.profilepic = mdtData and mdtData.pfp or ""
end
return cb(person)
end)
RegisterNetEvent("mdt:server:saveProfile", function(pfp, information, cid, fName, sName, tags, gallery, fingerprint, licenses)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
ManageLicenses(cid, licenses)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'doj' then JobType = 'police' end
MySQL.Async.insert('INSERT INTO mdt_data (cid, information, pfp, jobtype, tags, gallery, fingerprint) VALUES (:cid, :information, :pfp, :jobtype, :tags, :gallery, :fingerprint) ON DUPLICATE KEY UPDATE cid = :cid, information = :information, pfp = :pfp, tags = :tags, gallery = :gallery, fingerprint = :fingerprint', {
cid = cid,
information = information,
pfp = pfp,
jobtype = JobType,
tags = json.encode(tags),
gallery = json.encode(gallery),
fingerprint = fingerprint,
})
end
end)
RegisterNetEvent("mdt:server:updateLicense", function(cid, type, status)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
if GetJobType(Player.PlayerData.job.name) == 'police' then
ManageLicense(cid, type, status)
end
end
end)
-- Incidents
RegisterNetEvent('mdt:server:getAllIncidents', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local matches = MySQL.query.await("SELECT * FROM `mdt_incidents` ORDER BY `id` DESC LIMIT 30", {})
TriggerClientEvent('mdt:client:getAllIncidents', src, matches)
end
end
end)
RegisterNetEvent('mdt:server:searchIncidents', function(query)
if query then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local matches = MySQL.query.await("SELECT * FROM `mdt_incidents` WHERE `id` LIKE :query OR LOWER(`title`) LIKE :query OR LOWER(`author`) LIKE :query OR LOWER(`details`) LIKE :query OR LOWER(`tags`) LIKE :query OR LOWER(`officersinvolved`) LIKE :query OR LOWER(`civsinvolved`) LIKE :query OR LOWER(`author`) LIKE :query ORDER BY `id` DESC LIMIT 50", {
query = string.lower('%'..query..'%') -- % wildcard, needed to search for all alike results
})
TriggerClientEvent('mdt:client:getIncidents', src, matches)
end
end
end
end)
RegisterNetEvent('mdt:server:getIncidentData', function(sentId)
if sentId then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local matches = MySQL.query.await("SELECT * FROM `mdt_incidents` WHERE `id` = :id", {
id = sentId
})
local data = matches[1]
data['tags'] = json.decode(data['tags'])
data['officersinvolved'] = json.decode(data['officersinvolved'])
data['civsinvolved'] = json.decode(data['civsinvolved'])
data['evidence'] = json.decode(data['evidence'])
local convictions = MySQL.query.await("SELECT * FROM `mdt_convictions` WHERE `linkedincident` = :id", {
id = sentId
})
if convictions ~= nil then
for i=1, #convictions do
local res = GetNameFromId(convictions[i]['cid'])
if res ~= nil then
convictions[i]['name'] = res
else
convictions[i]['name'] = "Unknown"
end
convictions[i]['charges'] = json.decode(convictions[i]['charges'])
end
end
TriggerClientEvent('mdt:client:getIncidentData', src, data, convictions)
end
end
end
end)
RegisterNetEvent('mdt:server:getAllBolos', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'ambulance' then
local matches = MySQL.query.await("SELECT * FROM `mdt_bolos` WHERE jobtype = :jobtype", {jobtype = JobType})
TriggerClientEvent('mdt:client:getAllBolos', src, matches)
end
end)
RegisterNetEvent('mdt:server:searchBolos', function(sentSearch)
if sentSearch then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'ambulance' then
local matches = MySQL.query.await("SELECT * FROM `mdt_bolos` WHERE `id` LIKE :query OR LOWER(`title`) LIKE :query OR `plate` LIKE :query OR LOWER(`owner`) LIKE :query OR LOWER(`individual`) LIKE :query OR LOWER(`detail`) LIKE :query OR LOWER(`officersinvolved`) LIKE :query OR LOWER(`tags`) LIKE :query OR LOWER(`author`) LIKE :query AND jobtype = :jobtype", {
query = string.lower('%'..sentSearch..'%'), -- % wildcard, needed to search for all alike results
jobtype = JobType
})
TriggerClientEvent('mdt:client:getBolos', src, matches)
end
end
end)
RegisterNetEvent('mdt:server:getBoloData', function(sentId)
if sentId then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'ambulance' then
local matches = MySQL.query.await("SELECT * FROM `mdt_bolos` WHERE `id` = :id AND jobtype = :jobtype LIMIT 1", {
id = sentId,
jobtype = JobType
})
local data = matches[1]
data['tags'] = json.decode(data['tags'])
data['officersinvolved'] = json.decode(data['officersinvolved'])
data['gallery'] = json.decode(data['gallery'])
TriggerClientEvent('mdt:client:getBoloData', src, data)
end
end
end)
RegisterNetEvent('mdt:server:newBolo', function(existing, id, title, plate, owner, individual, detail, tags, gallery, officersinvolved, time)
if id then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'ambulance' then
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
local function InsertBolo()
MySQL.insert('INSERT INTO `mdt_bolos` (`title`, `author`, `plate`, `owner`, `individual`, `detail`, `tags`, `gallery`, `officersinvolved`, `time`, `jobtype`) VALUES (:title, :author, :plate, :owner, :individual, :detail, :tags, :gallery, :officersinvolved, :time, :jobtype)', {
title = title,
author = fullname,
plate = plate,
owner = owner,
individual = individual,
detail = detail,
tags = json.encode(tags),
gallery = json.encode(gallery),
officersinvolved = json.encode(officersinvolved),
time = tostring(time),
jobtype = JobType
}, function(r)
if r then
TriggerClientEvent('mdt:client:boloComplete', src, r)
TriggerEvent('mdt:server:AddLog', "A new BOLO was created by "..fullname.." with the title ("..title..") and ID ("..id..")")
end
end)
end
local function UpdateBolo()
MySQL.update("UPDATE mdt_bolos SET `title`=:title, plate=:plate, owner=:owner, individual=:individual, detail=:detail, tags=:tags, gallery=:gallery, officersinvolved=:officersinvolved WHERE `id`=:id AND jobtype = :jobtype LIMIT 1", {
title = title,
plate = plate,
owner = owner,
individual = individual,
detail = detail,
tags = json.encode(tags),
gallery = json.encode(gallery),
officersinvolved = json.encode(officersinvolved),
id = id,
jobtype = JobType
}, function(r)
if r then
TriggerClientEvent('mdt:client:boloComplete', src, id)
TriggerEvent('mdt:server:AddLog', "A BOLO was updated by "..fullname.." with the title ("..title..") and ID ("..id..")")
end
end)
end
if existing then
UpdateBolo()
elseif not existing then
InsertBolo()
end
end
end
end)
RegisterNetEvent('mdt:server:deleteBolo', function(id)
if id then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' then
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
MySQL.update("DELETE FROM `mdt_bolos` WHERE id=:id", { id = id, jobtype = JobType })
TriggerEvent('mdt:server:AddLog', "A BOLO was deleted by "..fullname.." with the ID ("..id..")")
end
end
end)
RegisterNetEvent('mdt:server:deleteICU', function(id)
if id then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'ambulance' then
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
MySQL.update("DELETE FROM `mdt_bolos` WHERE id=:id", { id = id, jobtype = JobType })
TriggerEvent('mdt:server:AddLog', "A ICU Check-in was deleted by "..fullname.." with the ID ("..id..")")
end
end
end)
RegisterNetEvent('mdt:server:incidentSearchPerson', function(query)
if query then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local function ProfPic(gender, profilepic)
if profilepic then return profilepic end;
if gender == "f" then return "img/female.png" end;
return "img/male.png"
end
local result = MySQL.query.await("SELECT p.citizenid, p.charinfo, md.pfp from players p LEFT JOIN mdt_data md on p.citizenid = md.cid WHERE LOWER(`charinfo`) LIKE :query OR LOWER(`citizenid`) LIKE :query AND `jobtype` = :jobtype LIMIT 30", {
query = string.lower('%'..query..'%'), -- % wildcard, needed to search for all alike results
jobtype = JobType
})
local data = {}
for i=1, #result do
local charinfo = json.decode(result[i].charinfo)
data[i] = {id = result[i].citizenid, firstname = charinfo.firstname, lastname = charinfo.lastname, profilepic = ProfPic(charinfo.gender, result[i].pfp)}
end
TriggerClientEvent('mdt:client:incidentSearchPerson', src, data)
end
end
end
end)
RegisterNetEvent('mdt:server:getAllReports', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' or JobType == 'ambulance' then
if JobType == 'doj' then JobType = 'police' end
local matches = MySQL.query.await("SELECT * FROM `mdt_reports` WHERE jobtype = :jobtype ORDER BY `id` DESC LIMIT 30", {
jobtype = JobType
})
TriggerClientEvent('mdt:client:getAllReports', src, matches)
end
end
end)
RegisterNetEvent('mdt:server:getReportData', function(sentId)
if sentId then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' or JobType == 'ambulance' then
if JobType == 'doj' then JobType = 'police' end
local matches = MySQL.query.await("SELECT * FROM `mdt_reports` WHERE `id` = :id AND `jobtype` = :jobtype LIMIT 1", {
id = sentId,
jobtype = JobType
})
local data = matches[1]
data['tags'] = json.decode(data['tags'])
data['officersinvolved'] = json.decode(data['officersinvolved'])
data['civsinvolved'] = json.decode(data['civsinvolved'])
data['gallery'] = json.decode(data['gallery'])
TriggerClientEvent('mdt:client:getReportData', src, data)
end
end
end
end)
RegisterNetEvent('mdt:server:searchReports', function(sentSearch)
if sentSearch then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' or JobType == 'ambulance' then
if JobType == 'doj' then JobType = 'police' end
local matches = MySQL.query.await("SELECT * FROM `mdt_reports` WHERE `id` LIKE :query OR LOWER(`author`) LIKE :query OR LOWER(`title`) LIKE :query OR LOWER(`type`) LIKE :query OR LOWER(`details`) LIKE :query OR LOWER(`tags`) LIKE :query AND `jobtype` = :jobtype ORDER BY `id` DESC LIMIT 50", {
query = string.lower('%'..sentSearch..'%'), -- % wildcard, needed to search for all alike results
jobtype = JobType
})
TriggerClientEvent('mdt:client:getAllReports', src, matches)
end
end
end
end)
RegisterNetEvent('mdt:server:newReport', function(existing, id, title, reporttype, details, tags, gallery, officers, civilians, time)
if id then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType ~= nil then
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
local function InsertReport()
MySQL.insert('INSERT INTO `mdt_reports` (`title`, `author`, `type`, `details`, `tags`, `gallery`, `officersinvolved`, `civsinvolved`, `time`, `jobtype`) VALUES (:title, :author, :type, :details, :tags, :gallery, :officersinvolved, :civsinvolved, :time, :jobtype)', {
title = title,
author = fullname,
type = reporttype,
details = details,
tags = json.encode(tags),
gallery = json.encode(gallery),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
time = tostring(time),
jobtype = JobType,
}, function(r)
if r then
TriggerClientEvent('mdt:client:reportComplete', src, r)
TriggerEvent('mdt:server:AddLog', "A new report was created by "..fullname.." with the title ("..title..") and ID ("..id..")")
end
end)
end
local function UpdateReport()
MySQL.update("UPDATE `mdt_reports` SET `title` = :title, type = :type, details = :details, tags = :tags, gallery = :gallery, officersinvolved = :officersinvolved, civsinvolved = :civsinvolved, jobtype = :jobtype WHERE `id` = :id LIMIT 1", {
title = title,
type = reporttype,
details = details,
tags = json.encode(tags),
gallery = json.encode(gallery),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
jobtype = JobType,
id = id,
}, function(affectedRows)
if affectedRows > 0 then
TriggerClientEvent('mdt:client:reportComplete', src, id)
TriggerEvent('mdt:server:AddLog', "A report was updated by "..fullname.." with the title ("..title..") and ID ("..id..")")
end
end)
end
if existing then
UpdateReport()
elseif not existing then
InsertReport()
end
end
end
end
end)
QBCore.Functions.CreateCallback('mdt:server:SearchVehicles', function(source, cb, sentData)
if not sentData then return cb({}) end
local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(source, PlayerData) then return cb({}) end
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local vehicles = MySQL.query.await("SELECT pv.id, pv.citizenid, pv.plate, pv.vehicle, pv.mods, pv.state, p.charinfo FROM `player_vehicles` pv LEFT JOIN players p ON pv.citizenid = p.citizenid WHERE LOWER(`plate`) LIKE :query OR LOWER(`vehicle`) LIKE :query LIMIT 25", {
query = string.lower('%'..sentData..'%')
})
if not next(vehicles) then cb({}) return end
for _, value in ipairs(vehicles) do
if value.state == 0 then
value.state = "Out"
elseif value.state == 1 then
value.state = "Garaged"
elseif value.state == 2 then
value.state = "Impounded"
end
value.bolo = false
local boloResult = GetBoloStatus(value.plate)
if boloResult then
value.bolo = true
end
value.code = false
value.stolen = false
value.image = "img/not-found.webp"
local info = GetVehicleInformation(value.plate)
if info then
value.code = info['code5']
value.stolen = info['stolen']
value.image = info['image']
end
local ownerResult = json.decode(value.charinfo)
value.owner = ownerResult['firstname'] .. " " .. ownerResult['lastname']
end
-- idk if this works or I have to call cb first then return :shrug:
return cb(vehicles)
end
return cb({})
end
end)
RegisterNetEvent('mdt:server:getVehicleData', function(plate)
if plate then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local JobType = GetJobType(Player.PlayerData.job.name)
if JobType == 'police' or JobType == 'doj' then
local vehicle = MySQL.query.await("select pv.*, p.charinfo from player_vehicles pv LEFT JOIN players p ON pv.citizenid = p.citizenid where pv.plate = :plate LIMIT 1", { plate = string.gsub(plate, "^%s*(.-)%s*$", "%1")})
if vehicle and vehicle[1] then
vehicle[1]['impound'] = false
if vehicle[1].state == 2 then
vehicle[1]['impound'] = true
end
vehicle[1]['bolo'] = GetBoloStatus(vehicle[1]['plate'])
vehicle[1]['information'] = ""
vehicle[1]['name'] = "Unknown Person"
local ownerResult = json.decode(vehicle[1].charinfo)
vehicle[1]['name'] = ownerResult['firstname'] .. " " .. ownerResult['lastname']
local color1 = json.decode(vehicle[1].mods)
vehicle[1]['color1'] = color1['color1']
vehicle[1]['dbid'] = 0
local info = GetVehicleInformation(vehicle[1]['plate'])
if info then
vehicle[1]['information'] = info['information']
vehicle[1]['dbid'] = info['id']
vehicle[1]['image'] = info['image']
vehicle[1]['code'] = info['code5']
vehicle[1]['stolen'] = info['stolen']
end
if vehicle[1]['image'] == nil then vehicle[1]['image'] = "img/not-found.webp" end -- Image
end
TriggerClientEvent('mdt:client:getVehicleData', src, vehicle)
end
end
end
end)
RegisterNetEvent('mdt:server:saveVehicleInfo', function(dbid, plate, imageurl, notes, stolen, code5, impoundInfo)
if plate then
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
if GetJobType(Player.PlayerData.job.name) == 'police' then
if dbid == nil then dbid = 0 end;
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
TriggerEvent('mdt:server:AddLog', "A vehicle with the plate ("..plate..") has a new image ("..imageurl..") edited by "..fullname)
if tonumber(dbid) == 0 then
MySQL.insert('INSERT INTO `mdt_vehicleinfo` (`plate`, `information`, `image`, `code5`, `stolen`) VALUES (:plate, :information, :image, :code5, :stolen)', { plate = string.gsub(plate, "^%s*(.-)%s*$", "%1"), information = notes, image = imageurl, code5 = code5, stolen = stolen }, function(infoResult)
if infoResult then
TriggerClientEvent('mdt:client:updateVehicleDbId', src, infoResult)
TriggerEvent('mdt:server:AddLog', "A vehicle with the plate ("..plate..") was added to the vehicle information database by "..fullname)
end
end)
elseif tonumber(dbid) > 0 then
MySQL.update("UPDATE mdt_vehicleinfo SET `information`= :information, `image`= :image, `code5`= :code5, `stolen`= :stolen WHERE `plate`= :plate LIMIT 1", { plate = string.gsub(plate, "^%s*(.-)%s*$", "%1"), information = notes, image = imageurl, code5 = code5, stolen = stolen })
end
if impoundInfo.impoundChanged then
local vehicle = MySQL.single.await("SELECT p.id, p.plate, i.vehicleid AS impoundid FROM `player_vehicles` p LEFT JOIN `mdt_impound` i ON i.vehicleid = p.id WHERE plate=:plate", { plate = string.gsub(plate, "^%s*(.-)%s*$", "%1") })
if impoundInfo.impoundActive then
local plate, linkedreport, fee, time = impoundInfo['plate'], impoundInfo['linkedreport'], impoundInfo['fee'], impoundInfo['time']
if (plate and linkedreport and fee and time) then
if vehicle.impoundid == nil then
-- This section is copy pasted from request impound and needs some attention.
-- sentVehicle doesnt exist.
-- data is defined twice
-- INSERT INTO will not work if it exists already (which it will)
local data = vehicle
MySQL.insert('INSERT INTO `mdt_impound` (`vehicleid`, `linkedreport`, `fee`, `time`) VALUES (:vehicleid, :linkedreport, :fee, :time)', {
vehicleid = data['id'],
linkedreport = linkedreport,
fee = fee,
time = os.time() + (time * 60)
}, function(res)
-- notify?
local data = {
vehicleid = data['id'],
plate = plate,
beingcollected = 0,
vehicle = sentVehicle,
officer = Player.PlayerData.charinfo.firstname.. " "..Player.PlayerData.charinfo.lastname,
number = Player.PlayerData.charinfo.phone,
time = os.time() * 1000,
src = src,
}
local vehicle = NetworkGetEntityFromNetworkId(sentVehicle)
FreezeEntityPosition(vehicle, true)
impound[#impound+1] = data
TriggerClientEvent("police:client:ImpoundVehicle", src, true, fee)
end)
-- Read above comment
end
end
else
if vehicle.impoundid ~= nil then
local data = vehicle
local result = MySQL.single.await("SELECT id, vehicle, fuel, engine, body FROM `player_vehicles` WHERE plate=:plate LIMIT 1", { plate = string.gsub(plate, "^%s*(.-)%s*$", "%1")})
if result then
local data = result
MySQL.update("DELETE FROM `mdt_impound` WHERE vehicleid=:vehicleid", { vehicleid = data['id'] })
result.currentSelection = impoundInfo.CurrentSelection
result.plate = plate
TriggerClientEvent('ps-mdt:client:TakeOutImpound', src, result)
end
end
end
end
end
end
end
end)
RegisterNetEvent('mdt:server:getAllLogs', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
if Config.LogPerms[Player.PlayerData.job.name] then
if Config.LogPerms[Player.PlayerData.job.name][Player.PlayerData.job.grade.level] then
local JobType = GetJobType(Player.PlayerData.job.name)
local infoResult = MySQL.query.await('SELECT * FROM mdt_logs WHERE `jobtype` = :jobtype ORDER BY `id` DESC LIMIT 250', {jobtype = JobType})
TriggerLatentClientEvent('mdt:client:getAllLogs', src, 30000, infoResult)
end
end
end
end)
-- Penal Code
local function IsCidFelon(sentCid, cb)
if sentCid then
local convictions = MySQL.query.await('SELECT charges FROM mdt_convictions WHERE cid=:cid', { cid = sentCid })
local Charges = {}
for i=1, #convictions do
local currCharges = json.decode(convictions[i]['charges'])
for x=1, #currCharges do
Charges[#Charges+1] = currCharges[x]
end
end
local PenalCode = Config.PenalCode
for i=1, #Charges do
for p=1, #PenalCode do
for x=1, #PenalCode[p] do
if PenalCode[p][x]['title'] == Charges[i] then
if PenalCode[p][x]['class'] == 'Felony' then
cb(true)
return
end
break
end
end
end
end
cb(false)
end
end
exports('IsCidFelon', IsCidFelon) -- exports['erp_mdt']:IsCidFelon()
RegisterCommand("isfelon", function(source, args, rawCommand)
IsCidFelon(1998, function(res)
end)
end, false)
RegisterNetEvent('mdt:server:getPenalCode', function()
local src = source
TriggerClientEvent('mdt:client:getPenalCode', src, Config.PenalCodeTitles, Config.PenalCode)
end)
RegisterNetEvent('mdt:server:setCallsign', function(cid, newcallsign)
local Player = QBCore.Functions.GetPlayerByCitizenId(cid)
Player.Functions.SetMetaData("callsign", newcallsign)
end)
RegisterNetEvent('mdt:server:saveIncident', function(id, title, information, tags, officers, civilians, evidence, associated, time)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player then
if GetJobType(Player.PlayerData.job.name) == 'police' then
if id == 0 then
local fullname = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname
MySQL.insert('INSERT INTO `mdt_incidents` (`author`, `title`, `details`, `tags`, `officersinvolved`, `civsinvolved`, `evidence`, `time`, `jobtype`) VALUES (:author, :title, :details, :tags, :officersinvolved, :civsinvolved, :evidence, :time, :jobtype)',
{
author = fullname,
title = title,
details = information,
tags = json.encode(tags),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
evidence = json.encode(evidence),
time = time,
jobtype = 'police',
}, function(infoResult)
if infoResult then
for i=1, #associated do
MySQL.insert('INSERT INTO `mdt_convictions` (`cid`, `linkedincident`, `warrant`, `guilty`, `processed`, `associated`, `charges`, `fine`, `sentence`, `recfine`, `recsentence`, `time`) VALUES (:cid, :linkedincident, :warrant, :guilty, :processed, :associated, :charges, :fine, :sentence, :recfine, :recsentence, :time)', {
cid = associated[i]['Cid'],
linkedincident = infoResult,
warrant = associated[i]['Warrant'],
guilty = associated[i]['Guilty'],
processed = associated[i]['Processed'],
associated = associated[i]['Isassociated'],
charges = json.encode(associated[i]['Charges']),
fine = tonumber(associated[i]['Fine']),
sentence = tonumber(associated[i]['Sentence']),
recfine = tonumber(associated[i]['recfine']),
recsentence = tonumber(associated[i]['recsentence']),
time = time
})
end
TriggerClientEvent('mdt:client:updateIncidentDbId', src, infoResult)
--TriggerEvent('mdt:server:AddLog', "A vehicle with the plate ("..plate..") was added to the vehicle information database by "..player['fullname'])
end
end)
elseif id > 0 then
MySQL.update("UPDATE mdt_incidents SET title=:title, details=:details, civsinvolved=:civsinvolved, tags=:tags, officersinvolved=:officersinvolved, evidence=:evidence WHERE id=:id", {
title = title,
details = information,
tags = json.encode(tags),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
evidence = json.encode(evidence),
id = id
})
for i=1, #associated do
TriggerEvent('mdt:server:handleExistingConvictions', associated[i], id, time)
end
end
end
end
end)
RegisterNetEvent('mdt:server:handleExistingConvictions', function(data, incidentid, time)
MySQL.query('SELECT * FROM mdt_convictions WHERE cid=:cid AND linkedincident=:linkedincident', {
cid = data['Cid'],
linkedincident = incidentid
}, function(convictionRes)
if convictionRes and convictionRes[1] and convictionRes[1]['id'] then
MySQL.update('UPDATE mdt_convictions SET cid=:cid, linkedincident=:linkedincident, warrant=:warrant, guilty=:guilty, processed=:processed, associated=:associated, charges=:charges, fine=:fine, sentence=:sentence, recfine=:recfine, recsentence=:recsentence WHERE cid=:cid AND linkedincident=:linkedincident', {
cid = data['Cid'],
linkedincident = incidentid,
warrant = data['Warrant'],
guilty = data['Guilty'],
processed = data['Processed'],
associated = data['Isassociated'],
charges = json.encode(data['Charges']),
fine = tonumber(data['Fine']),