From a705e7aa5b937c7295a4b3041df59dc9eeb5100b Mon Sep 17 00:00:00 2001 From: Manason Date: Wed, 8 Nov 2023 09:10:29 -0800 Subject: [PATCH] refactor: extract cop alerting --- .vscode/settings.json | 3 ++- client/cops.lua | 29 +++++++++++++++++++++++++++++ client/main.lua | 35 ----------------------------------- fxmanifest.lua | 4 +++- server/cops.lua | 9 +++++++++ server/main.lua | 16 +--------------- 6 files changed, 44 insertions(+), 52 deletions(-) create mode 100644 client/cops.lua create mode 100644 server/cops.lua diff --git a/.vscode/settings.json b/.vscode/settings.json index fb8005c..8ae4b49 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "Lua.diagnostics.globals": [ "cache", - "lib" + "lib", + "QBX" ] } \ No newline at end of file diff --git a/client/cops.lua b/client/cops.lua new file mode 100644 index 0000000..6a7a36a --- /dev/null +++ b/client/cops.lua @@ -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) \ No newline at end of file diff --git a/client/main.lua b/client/main.lua index 845837a..adcc723 100644 --- a/client/main.lua +++ b/client/main.lua @@ -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) @@ -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() diff --git a/fxmanifest.lua b/fxmanifest.lua index a047536..f7e9578 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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 { diff --git a/server/cops.lua b/server/cops.lua new file mode 100644 index 0000000..433dbfe --- /dev/null +++ b/server/cops.lua @@ -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) \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index 5e01175..9df5289 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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) @@ -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()