Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: for use by any job #1

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
local officerBlips = {}
local sharedConfig = require 'config.shared'
local playerBlips = {}

---@param playerId number
local function removeOfficer(playerId)
local blip = officerBlips[playerId]
local function removePlayer(playerId)
local blip = playerBlips[playerId]

if blip then
RemoveBlip(blip)
officerBlips[playerId] = nil
playerBlips[playerId] = nil
end
end

RegisterNetEvent('qbx_police:client:removeOfficer', removeOfficer)
RegisterNetEvent('qbx_dutyblips:client:removePlayer', removePlayer)

RegisterNetEvent('qbx_police:client:updatePositions', function(officers)
for i = 1, #officers do
local officer = officers[i]
local blip = officerBlips[officer.playerId]
RegisterNetEvent('qbx_dutyblips:client:updatePositions', function(players)
for i = 1, #players do
local player = players[i]
local blip = playerBlips[player.playerId]

if not blip then
local label = ('leo:%s'):format(officer.playerId)
local name = ('%s | %s. %s'):format(officer.callsign, officer.firstName:sub(1, 1):upper(), officer.lastName)
local label = ('player:%s'):format(player.playerId)
local name = ('%s. %s'):format(player.firstName:sub(1, 1):upper(), player.lastName)

blip = AddBlipForEntity(GetPlayerPed(GetPlayerFromServerId(officer.playerId)))
if player.group == 'police' or player.group == 'ambulance' then
name = ('%s | %s. %s'):format(player.callsign, player.firstName:sub(1, 1):upper(), player.lastName)
end

officerBlips[officer.playerId] = blip
blip = AddBlipForEntity(GetPlayerPed(GetPlayerFromServerId(player.playerId)))

SetBlipSprite(blip, 1)
SetBlipColour(blip, 42)
playerBlips[player.playerId] = blip

local group = sharedConfig.groups[player.group]

SetBlipSprite(blip, group.sprite or 1)
SetBlipColour(blip, group.color or 42)
SetBlipDisplay(blip, 3)
SetBlipAsShortRange(blip, true)
SetBlipDisplay(blip, 2)
Expand Down
2 changes: 1 addition & 1 deletion config/server.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
return {
refreshRate = 1000, -- Time in ms between the refreshing of officer blips
refreshRate = 1000, -- Time in ms between the refreshing of blips
}
12 changes: 12 additions & 0 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
return {
groups = { -- Groups which will have duty blips
police = {
sprite = 1,
color = 42,
},
ambulance = {
sprite = 1,
color = 42,
},
}
}
12 changes: 4 additions & 8 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ description 'Duty blips for Qbox'
repository 'https://github.com/Qbox-project/qbx_dutyblips'
version '1.0.0'

shared_scripts {
'@ox_lib/init.lua',
'@qbx_core/modules/lib.lua'
}
shared_script '@ox_lib/init.lua'

client_scripts {
'@qbx_core/modules/playerdata.lua',
'client/main.lua'
}
client_script 'client/main.lua'

server_script 'server/main.lua'

file 'config/shared.lua'

lua54 'yes'
use_experimental_fxv2_oal 'yes'
84 changes: 43 additions & 41 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,95 +1,97 @@
local config = require 'config.server'
local activeOfficers = {}
local sharedConfig = require 'config.shared'
local activePlayers = {}

---@param name string
---@param data any
local function triggerOfficerEvent(name, data)
for playerId in pairs(activeOfficers) do
local function triggerPlayersEvent(name, data)
for playerId in pairs(activePlayers) do
TriggerClientEvent(name, playerId, data)
end
end

---@param playerId number
local function getOfficer(playerId)
return activeOfficers[playerId]
local function getPlayer(playerId)
return activePlayers[playerId]
end

---@param playerId number
local function removeOfficer(playerId)
triggerOfficerEvent('qbx_police:client:removeOfficer', playerId)
local function removePlayer(playerId)
triggerPlayersEvent('qbx_dutyblips:client:removePlayer', playerId)

activeOfficers[playerId] = nil
activePlayers[playerId] = nil
end

---@param playerId number
local function addOfficer(playerId)
local function addPlayer(playerId)
local player = exports.qbx_core:GetPlayer(playerId)

if not player or player.PlayerData.job.type ~= 'leo' then return end
if not player then return end

activeOfficers[playerId] = {
local groups = {}

for group in pairs(sharedConfig.groups) do
groups[group] = true
end

local hasGroup = exports.qbx_core:HasPrimaryGroup(playerId, groups)

if not hasGroup then return end

activePlayers[playerId] = {
firstName = player.PlayerData.charinfo.firstname,
lastName = player.PlayerData.charinfo.lastname,
callsign = player.PlayerData.metadata.callsign,
playerId = playerId,
group = player.PlayerData.job.name,
grade = player.PlayerData.job.grade.label,
position = {},
}

if player.PlayerData.job.name == 'police' or player.PlayerData.job.name == 'ambulance' then
activePlayers[playerId].callsign = player.PlayerData.metadata.callsign
end
end

RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
addOfficer(source)
addPlayer(source)
end)

AddEventHandler('QBCore:Server:OnJobUpdate', function(source, job)
local officer = getOfficer(source)
local player = getPlayer(source)

if officer then
if officer.group == job.name then
activeOfficers[source].grade = job.grade.label
if player then
if player.group == job.name then
activePlayers[source].grade = job.grade.label
return
else
local playerJob = exports.qbx_core:GetJob(job.name)

if playerJob.type ~= 'leo' then
removeOfficer(source)
removePlayer(source)
return
end

activeOfficers[source].group = job.name
activeOfficers[source].grade = job.grade.label
activePlayers[source].group = job.name
activePlayers[source].grade = job.grade.label
return
end
end

addOfficer(source)
addPlayer(source)
end)

AddEventHandler('QBCore:Server:SetDuty', function(source, onDuty)
local player = exports.qbx_core:GetPlayer(source)

if player?.PlayerData.job.type ~= 'leo' then return end
local player = getPlayer(source)

if not onDuty then
removeOfficer(source)
return
if player then
if not onDuty then
removePlayer(source)
return
end
end

addOfficer(source)
addPlayer(source)
end)

SetInterval(function()
local officersArray = {}

for playerId, officer in pairs(activeOfficers) do
local coords = GetEntityCoords(GetPlayerPed(officer.playerId))

officer.position = coords

officersArray[playerId] = officer
end

triggerOfficerEvent('qbx_police:client:updatePositions', officersArray)
table.wipe(officersArray)
triggerPlayersEvent('qbx_dutyblips:client:updatePositions', activePlayers)
end, config.refreshRate)