Skip to content

Commit

Permalink
refactor: fingerprints
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm committed Dec 25, 2024
1 parent 5de0bd2 commit ff9f4c3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 77 deletions.
68 changes: 22 additions & 46 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ exports.ox_inventory:displayMetadata({
collector = locale('collector'),
location = locale('location'),
caliber = locale('casing.caliber'),
bloodType = locale('blood.bloodtype')
bloodType = locale('blood.type'),
fingerprint = locale('print.label')
})

local function dropBulletCasing()
Expand Down Expand Up @@ -70,38 +71,26 @@ local function drawBloodDrop(dropId)
end
end

---@param coords vector3
---@return string
local function getStreetLabel(coords)
local s1, s2 = GetStreetNameAtCoord(coords.x, coords.y, coords.z)
local street1 = GetStreetNameFromHashKey(s1)
local street2 = GetStreetNameFromHashKey(s2)
local streetLabel = street1
if street2 then
streetLabel = streetLabel .. ' | ' .. street2
end
local sanitized = streetLabel:gsub("%'", "")
return sanitized
end
---@param printId integer
local function drawFingerprint(printId)
local coords = GetEntityCoords(cache.ped)

local function getPlayerDistanceFromCoords(coords)
local pos = GetEntityCoords(cache.ped)
return #(pos - coords)
end
if #(coords - fingerprints[printId].coords) >= 1.5 then return end

qbx.drawText3d({
text = ('[~g~G~s~] %s'):format(locale('print.label')),
coords = fingerprints[printId].coords
})

---@class DrawEvidenceIfInRangeArgs
---@field evidenceId integer
---@field coords vector3
---@field text string
---@field metadata table
---@field serverEventOnPickup string

---@param args DrawEvidenceIfInRangeArgs
local function drawEvidenceIfInRange(args)
if getPlayerDistanceFromCoords(args.coords) >= 1.5 then return end
qbx.drawText3d({text = args.text, coords = args.coords})
if IsControlJustReleased(0, 47) then
TriggerServerEvent(args.serverEventOnPickup, args.evidenceId, args.metadata)
local streets = qbx.getStreetName(fingerprints[printId].coords)
local zone = qbx.getZoneName(fingerprints[printId].coords)
local location = {
main = streets.main,
zone = zone
}

TriggerServerEvent('qbx_evidence:server:collectFingerprint', printId, location)
end
end

Expand All @@ -126,11 +115,8 @@ RegisterNetEvent('qbx_evidence:client:removeBloodDrop', function(dropId)
currentBloodDrop = 0
end)

RegisterNetEvent('qbx_evidence:client:addFingerPrint', function(fingerId, fingerprint, coords)
fingerprints[fingerId] = {
fingerprint = fingerprint,
coords = vec3(coords.x, coords.y, coords.z - 0.9)
}
RegisterNetEvent('qbx_evidence:client:addFingerprint', function(printId, newPrint)
fingerprints[printId] = newPrint
end)

RegisterNetEvent('qbx_evidence:client:removeFingerprint', function(fingerId)
Expand Down Expand Up @@ -239,17 +225,7 @@ CreateThread(function()
end

if currentFingerprint and currentFingerprint ~= 0 then
drawEvidenceIfInRange({
evidenceId = currentFingerprint,
coords = fingerprints[currentFingerprint].coords,
text = locale('fingerprint_text'),
metadata = {
type = locale('fingerprint'),
street = getStreetLabel(fingerprints[currentFingerprint].coords),
fingerprint = fingerprints[currentFingerprint].fingerprint
},
serverEventOnPickup = 'qbx_evidence:server:addFingerprintToInventory'
})
drawFingerprint(currentFingerprint)
end
end
end)
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"caliber": "Caliber"
},
"blood": {
"label": "Blood drop"
"label": "Blood drop",
"type": "Blood type"
},
"print": {
"label": "Fingerprint"
Expand Down
70 changes: 40 additions & 30 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
local sharedConfig = require 'config.shared'
local casings = {}
local bloodDrops = {}
local fingerDrops = {}
local fingerprints = {}
local playerGSR = {}

local function generateId(table)
local id = lib.string.random('11111')
if not table then return id end
while table[id] do
id = lib.string.random('11111')
end
return id
end

---@param source integer
---@return boolean
local function hasGSR(source)
Expand Down Expand Up @@ -78,6 +69,7 @@ RegisterNetEvent('qbx_evidence:server:createBloodDrop', function(coords)
local bloodType = exports.qbx_core:GetMetadata(source, 'bloodtype')
local dropId = lib.string.random('111111')
local bloodData = {
label = locale('blood.label'),
bloodType = bloodType,
coords = vec3(coords.x, coords.y, coords.z - 0.9),
created = GetGameTimer(),
Expand Down Expand Up @@ -118,31 +110,49 @@ RegisterNetEvent('qbx_evidence:server:collectBlood', function(dropId, location)
bloodDrops[dropId] = nil
end)

RegisterNetEvent('qbx_evidence:server:createFingerDrop', function(coords)
local player = exports.qbx_core:GetPlayer(source)
local fingerId = generateId(fingerDrops)
fingerDrops[fingerId] = player.PlayerData.metadata.fingerprint
TriggerClientEvent('qbx_evidence:client:addFingerPrint', -1, fingerId, player.PlayerData.metadata.fingerprint, coords)
RegisterNetEvent('qbx_evidence:server:createBloodDrop', function(coords)
local fingerprint = exports.qbx_core:GetMetadata(source, 'fingerprint')
local printId = lib.string.random('111111')
local printData = {
label = locale('print.label'),
fingerprint = fingerprint,
coords = vec3(coords.x, coords.y, coords.z - 0.9),
created = GetGameTimer(),
}

fingerprints[printId] = printData

TriggerClientEvent('qbx_evidence:client:addFingerprint', -1, printId, printData)
end)

RegisterNetEvent('qbx_evidence:server:addFingerprintToInventory', function(fingerId, fingerInfo)
RegisterNetEvent('qbx_evidence:server:collectFingerprint', function(printId, location)
if not fingerprints[printId] then return end

local src = source
local print = fingerprints[printId]
local player = exports.qbx_core:GetPlayer(src)
local playerName = player.PlayerData.charinfo.firstname..' '..player.PlayerData.charinfo.lastname
local streetName = fingerInfo.street
local fingerprint = fingerInfo.fingerprint
local metadata = {}
metadata.type = 'Fingerprint Evidence'
metadata.description = 'Fingerprint ID: '..fingerprint
metadata.description = metadata.description..'\n\nCollected By: '..playerName
metadata.description = metadata.description..'\n\nCollected At: '..streetName
if not exports.ox_inventory:RemoveItem(src, 'empty_evidence_bag', 1) then
return exports.qbx_core:Notify(src, locale('error.have_evidence_bag'), 'error')
end
if exports.ox_inventory:AddItem(src, 'filled_evidence_bag', 1, metadata) then
TriggerClientEvent('qbx_evidence:client:removeFingerprint', -1, fingerId)
fingerDrops[fingerId] = nil
local playerPed = GetPlayerPed(src)
local playerCoords = GetEntityCoords(playerPed)

if not player or #(playerCoords - print.coords) > 5.0 then return end

local name = ('%s %s'):format(player.PlayerData.charinfo.firstname, player.PlayerData.charinfo.lastname)
local metadata = {
label = locale('print.label'),
fingerprint = print.fingerprint,
collector = name,
location = ('%s, %s'):format(location.main, location.zone),
}

local collected = exports.ox_inventory:AddItem(src, 'evidence', 1, metadata)

if not collected then
exports.qbx_core:Notify(src, 'Your inventory is full...', 'error')
return
end

TriggerClientEvent('qbx_evidence:client:removeFingerprint', -1, printId)
fingerprints[printId] = nil
end)

CreateThread(function()
Expand Down

0 comments on commit ff9f4c3

Please sign in to comment.