Skip to content

Commit

Permalink
refactor: extract cop alerting
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed Nov 8, 2023
1 parent da73dfb commit a705e7a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Lua.diagnostics.globals": [
"cache",
"lib"
"lib",
"QBX"
]
}
29 changes: 29 additions & 0 deletions client/cops.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
RegisterNetEvent('qbx_diving:client:addCrimeAlert', function(coords, msg)
PlaySound(-1, 'Lose_1st', 'GTAO_FM_Events_Soundset', false, 0, true)
TriggerEvent('chatMessage', Lang:t('error.911_chatmessage'), 'error', msg)
local transG = 100
local blip = AddBlipForRadius(coords.x, coords.y, coords.z, 100.0)
SetBlipSprite(blip, 9)
SetBlipColour(blip, 1)
SetBlipAlpha(blip, transG)
SetBlipAsShortRange(blip, false)
BeginTextCommandSetBlipName('STRING')
AddTextComponentString(Lang:t('info.blip_text'))
EndTextCommandSetBlipName(blip)

repeat
Wait(180 * 4)
transG -= 1
SetBlipAlpha(blip, transG)
until transG == 0

SetBlipSprite(blip, 2)
RemoveBlip(blip)

local alertData = {
title = Lang:t("info.cop_title"),
coords = coords,
description = msg
}
TriggerEvent("qb-phone:client:addPoliceAlert", alertData)
end)
35 changes: 0 additions & 35 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@ local coralTargetZones = {}

local blips = {}

-- Functions
local function callCops()
local call = math.random(1, 3)
local chance = math.random(1, 3)
local coords = GetEntityCoords(cache.ped)
if call ~= chance then return end

TriggerServerEvent('qb-diving:server:CallCops', coords)
end

local function takeCoral(coralIndex)
if math.random() > Config.CopsChance then callCops() end

local times = math.random(2, 5)
FreezeEntityPosition(cache.ped, true)

Expand Down Expand Up @@ -233,29 +221,6 @@ RegisterNetEvent('qbx_diving:client:coralTaken', function(coralIndex)
end
end)

RegisterNetEvent('qb-diving:client:CallCops', function(coords, msg)
PlaySound(-1, 'Lose_1st', 'GTAO_FM_Events_Soundset', false, 0, true)
TriggerEvent('chatMessage', Lang:t('error.911_chatmessage'), 'error', msg)
local transG = 100
local blip = AddBlipForRadius(coords.x, coords.y, coords.z, 100.0)
SetBlipSprite(blip, 9)
SetBlipColour(blip, 1)
SetBlipAlpha(blip, transG)
SetBlipAsShortRange(blip, false)
BeginTextCommandSetBlipName('STRING')
AddTextComponentString(Lang:t('info.blip_text'))
EndTextCommandSetBlipName(blip)

repeat
Wait(180 * 4)
transG -= 1
SetBlipAlpha(blip, transG)
until transG == 0

SetBlipSprite(blip, 2)
RemoveBlip(blip)
end)

CreateThread(function()
if not isLoggedIn then return end
init()
Expand Down
4 changes: 3 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ shared_script {
'@qb-core/shared/locale.lua',
'locales/en.lua',
'locales/*.lua',
'config.lua'
'config.lua',
'@qbx_core/import.lua',
'qbx_core:playerdata',
}

server_scripts {
Expand Down
9 changes: 9 additions & 0 deletions server/cops.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AddEventHandler('qbx_diving:server:coralTaken', function(coords)
if math.random() > Config.CopsChance then return end
for _, player in pairs(exports.qbx_core:GetQBPlayers()) do
if player.PlayerData.job.type == 'leo' and player.PlayerData.job.onduty then
local msg = Lang:t("info.cop_msg")
TriggerClientEvent('qbx_diving:client:addCrimeAlert', player.PlayerData.source, coords, msg)
end
end
end)
16 changes: 1 addition & 15 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ local function getCoralInInventory(src)
return availableCoral
end

RegisterNetEvent('qb-diving:server:CallCops', function(coords)
for _, player in pairs(exports.qbx_core:GetQBPlayers()) do
if player.PlayerData.job.type == 'leo' and player.PlayerData.job.onduty then
local msg = Lang:t("info.cop_msg")
TriggerClientEvent('qb-diving:client:CallCops', player.PlayerData.source, coords, msg)
local alertData = {
title = Lang:t("info.cop_title"),
coords = coords,
description = msg
}
TriggerClientEvent("qb-phone:client:addPoliceAlert", -1, alertData)
end
end
end)

RegisterNetEvent('qbx_diving:server:sellCoral', function()
local src = source
local player = exports.qbx_core:GetPlayer(src)
Expand Down Expand Up @@ -83,6 +68,7 @@ RegisterNetEvent('qbx_diving:server:takeCoral', function(coralIndex)
exports.ox_inventory:AddItem(src, coralType.item, amount)
pickedUpCoralIndexes[coralIndex] = true
TriggerClientEvent('qbx_diving:client:coralTaken', -1, coralIndex)
TriggerEvent('qbx_diving:server:coralTaken', Config.CoralLocations[currentAreaIndex].corals[coralIndex].coords)
if #pickedUpCoralIndexes == Config.CoralLocations[currentAreaIndex].maxHarvestAmount then
pickedUpCoralIndexes = {}
currentAreaIndex = getNewLocation()
Expand Down

0 comments on commit a705e7a

Please sign in to comment.