Skip to content

Commit

Permalink
Refactor and implement police alert
Browse files Browse the repository at this point in the history
  • Loading branch information
Giana committed Feb 19, 2023
1 parent 4b8d877 commit 202ab4c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 50 deletions.
47 changes: 29 additions & 18 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,36 @@ end

-- Events --

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
TriggerEvent('g-drugselling:client:spawnNpcs')
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
IsSelling = false
end
end)

AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
for k, v in pairs(Peds) do
deleteClientNpc(k)
end
end
end)

RegisterNetEvent('g-drugselling:client:spawnNpcs', function()
RegisterNetEvent('g-drugselling:client:registerTarget', function()
local ped = PlayerPedId()
local pos = GetEntityCoords(ped)
for k, v in pairs(Config.SellLocations) do
if v.npc.enabled then
local npc = v.npc
RequestModel(GetHashKey(npc.ped))
while not HasModelLoaded(GetHashKey(npc.ped)) do
Citizen.Wait(100)
end
local sellPed = CreatePed(7, GetHashKey(npc.Ped), v.coords.x, v.coords.y, v.coords.z - 0.97, npc.heading, 0, true, true)
FreezeEntityPosition(sellPed, true)
SetBlockingOfNonTemporaryEvents(sellPed, true)
TaskStartScenarioInPlace(sellPed, npc.scenario, 0, false)
SetEntityInvincible(sellPed, true)
local dist = #(pos - v.coords)
if dist < 2 then
attemptSell(k)
break
end
end
end)

RegisterNetEvent('g-drugselling:client:policeAlert', function(message)
TriggerServerEvent('police:server:policeAlert', message)
end)

-- Threads --

-- Blip setup
Expand Down Expand Up @@ -150,15 +159,17 @@ Citizen.CreateThread(function()
for k, v in pairs(Config.SellLocations) do
if v.active then
local dist = #(pos - v.coords)
if dist < 10 then
if dist < 20 then
if v.marker.enabled then
if (v.type == 'walkup' and not IsPedInAnyVehicle(ped, false)) or (v.type == 'driveup' and IsPedInAnyVehicle(ped, false)) or v.type == 'any' or v.type == 'target' then
DrawMarker(v.marker.type, v.coords.x, v.coords.y, v.coords.z + 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.marker.scaleX, v.marker.scaleY, v.marker.scaleZ, v.marker.red, v.marker.green, v.marker.blue, v.marker.alpha, v.marker.bob, v.marker.faceCamera, 0, 1, 0, 0, 0)
DrawMarker(v.marker.type, v.coords.x, v.coords.y, v.coords.z - 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, v.marker.scaleX, v.marker.scaleY, v.marker.scaleZ, v.marker.red, v.marker.green, v.marker.blue, v.marker.alpha, v.marker.bob, v.marker.faceCamera, 0, 1, 0, 0, 0)
end
end
if dist < 1.5 then
if dist < 2 then
if (v.type == 'walkup' and not IsPedInAnyVehicle(ped, false)) or (v.type == 'driveup' and IsPedInAnyVehicle(ped, false)) or v.type == 'any' then
TriggerServerEvent('g-drugselling:server:sell', v)
if not IsSelling then
attemptSell(k)
end
end
end
inRange = true
Expand Down
109 changes: 77 additions & 32 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,89 @@
local QBCore = exports['qb-core']:GetCoreObject()

local CopsCalled = false
local CachedPolice = {}

-- Functions --

function rollForNotifyingPolice(source, policeAlertChance)
local src = source
local notifyPoliceRoll = math.random(1, 100)
if not CopsCalled and notifyPoliceRoll <= policeAlertChance then
CopsCalled = true
TriggerClientEvent('g-drugselling:client:policeAlert', src, Lang:t('info.police_alert'))
Citizen.Wait(Config.CopCallCooldown * 1000)
CopsCalled = false
end
end

-- Events --

RegisterNetEvent('g-drugselling:server:sell', function(sellLocation)
RegisterNetEvent('g-drugselling:server:sellItems', function(sellLocationIndex, items, moneyType)
local src = source
local player = QBCore.Functions.GetPlayer(src)
for k, v in pairs(sellLocation.sellable_items) do
local hasItem = player.Functions.GetItemByName(k)
if hasItem then
if hasItem.amount >= v.sell_quantity then
local bundlesToSell = math.floor(hasItem.amount / v.sell_quantity)
local quantityToSell = bundlesToSell * v.sell_quantity
if player.Functions.RemoveItem(k, quantityToSell) then
for k2, v2 in pairs(v.money_rewards) do
local amountOwed = v2 * bundlesToSell
if not player.Functions.AddMoney(k2, amountOwed) then
player.Functions.AddItem(k, quantityToSell)
return
end
end
for k2, v2 in pairs(v.item_rewards) do
local amountOwed = v2 * bundlesToSell
if not player.Functions.AddItem(k2, amountOwed) then
player.Functions.AddItem(k, quantityToSell)
return
end
if sellLocation.itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[k2], 'add', amountOwed)
end
end
local notifyPoliceRoll = math.random(1, 100)
if notifyPoliceRoll <= sellLocation.policeAlertChance then
TriggerEvent('police:server:policeAlert', Lang:t('info.police_alert'))
end
if CachedPolice[src] == nil then
DropPlayer(src, "Exploiting")
return
end
if Config.SellLocations[sellLocationIndex].sellAll then
for k, v in pairs(items) do
if player.Functions.RemoveItem(v.name, v.sellableQuantity) then
Citizen.Wait(900)
if not player.Functions.AddMoney(moneyType, v.price) then
player.Functions.AddItem(v.name, v.sellableQuantity)
return
end
else
if sellLocation.notificationsEnabled then
TriggerClientEvent('QBCore:Notify', src, Lang:t('error.not_enough', { itemLabel = QBCore.Shared.Items[k]['label'] }), 'error')
if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[v.name], 'remove', v.sellableQuantity)
end
end
end
rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance)
else
if player.Functions.RemoveItem(items[1].name, items[1].sellableQuantity) then
Citizen.Wait(800)
if not player.Functions.AddMoney(moneyType, items[1].price) then
player.Functions.AddItem(items[1].name, items[1].sellableQuantity)
return
end
if Config.SellLocations[sellLocationIndex].itemNotificationsEnabled then
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[items[1].name], 'remove', items[1].sellableQuantity)
end
end
rollForNotifyingPolice(src, Config.SellLocations[sellLocationIndex].policeAlertChance)
end
end)

-- Callbacks --

QBCore.Functions.CreateCallback('g-drugselling:server:getSellableItems', function(source, cb, sellLocationIndex)
local src = source
local player = QBCore.Functions.GetPlayer(src)
local items = {}
for k, v in pairs(Config.SellLocations[sellLocationIndex].sellable_items) do
local hasItem = player.Functions.GetItemByName(k)
if hasItem and hasItem.amount >= v.sell_quantity then
local bundlesToSell = math.floor(hasItem.amount / v.sell_quantity)
local sellableQuantity = bundlesToSell * v.sell_quantity
local price = v.money_amount * bundlesToSell
local item = {}
item.name = k
item.sellableQuantity = sellableQuantity
item.price = price
table.insert(items, item)
end
end
cb(items)
end)

QBCore.Functions.CreateCallback('g-drugselling:server:getCopCount', function(source, cb)
local src = source
local policeCount = 0
for _, v in pairs(QBCore.Functions.GetQBPlayers()) do
if v.PlayerData.job.name == "police" and v.PlayerData.job.onduty then
policeCount = policeCount + 1
end
end
CachedPolice[src] = policeCount
cb(policeCount)
end)

0 comments on commit 202ab4c

Please sign in to comment.