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: registers #17

Merged
merged 3 commits into from
Jan 7, 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
60 changes: 59 additions & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ local function safeAnim()
TaskPlayAnim(cache.ped, 'amb@prop_human_bum_bin@idle_b', 'exit', 8.0, 8.0, -1, 50, 0, false, false, false)
end

local function checkInteractStatus(register)
if sharedConfig.registers[register].robbed then
return false
end

local leoCount = lib.callback.await('qbx_storerobbery:server:leoCount', false)
if leoCount > sharedConfig.minimumCops then
return true
end
mafewtm marked this conversation as resolved.
Show resolved Hide resolved

return false
end

local function alertPolice()
local hours = GetClockHours()
local chance = config.policeAlertChance

if IsWearingGloves() or hours >= 1 and hours <= 6 then
chance = config.policeNightAlertChance
end

if math.random() <= chance then
TriggerServerEvent('police:server:policeAlert')
end
end

local function dropFingerprint()
if IsWearingGloves() then return end
if config.fingerprintChance > math.random(0, 100) then
Expand Down Expand Up @@ -89,6 +115,7 @@ end)
RegisterNUICallback('success', function(_, cb)
startLockpick(false)
openingRegisterHandler(config.openRegisterTime)
alertPolice()
if lib.progressBar({
duration = config.openRegisterTime,
label = Lang:t('text.emptying_the_register'),
Expand All @@ -114,6 +141,7 @@ end)
RegisterNUICallback('fail', function(_, cb)
startLockpick(false)
dropFingerprint()
alertPolice()
TriggerServerEvent('qbx_storerobbery:server:registerFailed', isUsingAdvanced)
cb('ok')
end)
Expand Down Expand Up @@ -156,6 +184,36 @@ RegisterNUICallback('tryCombination', function(data, cb)
cb('ok')
end)

local function createRegisters()
CreateThread(function()
for k, v in pairs(sharedConfig.registers) do
exports.ox_target:addBoxZone({
coords = v.coords,
size = vec3(1.5, 1.5, 1.5),
rotation = 0.0,
debug = config.debugPoly,
options = {
{
name = k..'_register',
icon = 'cash-register',
label = 'Open Register',
canInteract = function()
return checkInteractStatus(k)
end,
serverEvent = 'qbx_storerobbery:server:checkStatus',
}
}
})
end
end)
end

AddEventHandler('onClientResourceStart', function(resource)
if resource ~= cache.resource then return end
createRegisters()
end)

-- Update so that the target doesnt show also
CreateThread(function()
local hasShownText
while true do
Expand Down Expand Up @@ -217,4 +275,4 @@ CreateThread(function()
if not nearby and hasShownText then hasShownText = false lib.hideTextUI() end
Wait(time)
end
end)
end)
2 changes: 0 additions & 2 deletions config/server.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
return {
minimumCops = 2,
notEnoughCopsNotify = true,
callCopsTimeout = 240000,

registerReward = {
Expand Down
1 change: 1 addition & 0 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
return {
minimumCops = 0,
registers = {
[1] = {coords = vec3(-706.08, -915.42, 19.21), robbed = false, time = 0, safeKey = 1, camId = 1},
[2] = {coords = vec3(-706.16, -913.5, 19.21), robbed = false, time = 0, safeKey = 1, camId = 1},
Expand Down
82 changes: 31 additions & 51 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local sharedConfig = require 'config.shared'
local startedRegister = {}
local startedSafe = {}
local safeCodes = {}
local calledCops = {}
local ITEMS = exports.ox_inventory:Items()

local function getClosestRegister(coords)
Expand Down Expand Up @@ -32,62 +31,46 @@ local function getClosestSafe(coords)
return closestSafeIndex
end

local function alertPolice(text, source, camId)
if calledCops[source] then return end

local chance = lib.callback.await('qbx_storerobbery:client:getAlertChance', source)
if math.random() <= chance then
calledCops[source] = true
TriggerEvent('police:server:policeAlert', text, camId, source)
end

SetTimeout(config.callCopsTimeout, function()
calledCops[source] = false
end)
end
RegisterNetEvent('qbx_storerobbery:server:checkStatus', function()
local coords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(coords)
if not closestRegisterIndex then return end

AddEventHandler('lockpicks:UseLockpick', function(playerSource, isAdvanced)
local playerCoords = GetEntityCoords(GetPlayerPed(playerSource))
local closestRegisterIndex = getClosestRegister(playerCoords)
local leoCount = exports.qbx_core:GetDutyCountType('leo')
local hasLockpick = exports.ox_inventory:Search(source, 'count', 'lockpick') > 0
local hasAdvanced = exports.ox_inventory:Search(source, 'count', 'advancedlockpick') > 0

if not closestRegisterIndex then return end
if sharedConfig.registers[closestRegisterIndex].robbed then return end
if leoCount < config.minimumCops and config.notEnoughCopsNotify then
exports.qbx_core:Notify(playerSource, Lang:t('error.no_police', {Required = config.minimumCops}), 'error')
return
if hasLockpick then
TriggerClientEvent('qbx_storerobbery:client:initRegisterAttempt', source, false)
elseif hasAdvanced then
TriggerClientEvent('qbx_storerobbery:client:initRegisterAttempt', source, true)
else
exports.qbx_core:Notify(source, 'You don\'t have the appropriate items', 'error')
end

startedRegister[playerSource] = true
startedRegister[source] = true
sharedConfig.registers[closestRegisterIndex].robbed = true

alertPolice(Lang:t('alert.register'), playerSource, sharedConfig.registers[closestRegisterIndex].camId)
TriggerClientEvent('qbx_storerobbery:client:initRegisterAttempt', playerSource, isAdvanced)
end)

RegisterNetEvent('qbx_storerobbery:server:registerFailed', function(isUsingAdvanced)
local player = exports.qbx_core:GetPlayer(source)
local playerCoords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(playerCoords)
local deleteChance = isUsingAdvanced and math.random(0, 30) or math.random(0, 60)
local coords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(coords)
local removalChance = isUsingAdvanced and math.random(0, 30) or math.random(0, 60)

startedRegister[source] = false
sharedConfig.registers[closestRegisterIndex].robbed = false
if deleteChance > math.random(0, 100) then
if removalChance > math.random(0, 100) then
exports.qbx_core:Notify(source, Lang:t('error.lockpick_broken'), 'error')
if isUsingAdvanced then
player.Functions.RemoveItem('advancedlockpick', 1)
TriggerClientEvent('inventory:client:ItemBox', source, ITEMS['advancedlockpick'], 'remove')
exports.ox_inventory:RemoveItem(source, 'advancedlockpick', 1)
else
player.Functions.RemoveItem('lockpick', 1)
TriggerClientEvent('inventory:client:ItemBox', source, ITEMS['lockpick'], 'remove')
exports.ox_inventory:RemoveItem(source, 'lockpick', 1)
end
end
end)

RegisterNetEvent('qbx_storerobbery:server:registerExited', function()
local playerCoords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(playerCoords)
local coords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(coords)
startedRegister[source] = false
sharedConfig.registers[closestRegisterIndex].robbed = false
end)
Expand All @@ -98,23 +81,17 @@ RegisterNetEvent('qbx_storerobbery:server:registerCanceled', function()
end)

RegisterNetEvent('qbx_storerobbery:server:registerOpened', function(isDone)
if not isDone then return end
local player = exports.qbx_core:GetPlayer(source)
local playerCoords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(playerCoords)
local amount = exports.qbx_core:GetDutyCountType('leo')
local coords = GetEntityCoords(GetPlayerPed(source))
local closestRegisterIndex = getClosestRegister(coords)

if not closestRegisterIndex then return end
if #(playerCoords - sharedConfig.registers[closestRegisterIndex].coords) > 2 then return end
if #(coords - sharedConfig.registers[closestRegisterIndex].coords) > 2 then return end
if not startedRegister[source] then return end
if amount < config.minimumCops and config.notEnoughCopsNotify then
exports.qbx_core:Notify(source, Lang:t('error.no_police', {Required = config.minimumCops}), 'error')
return
end

player.Functions.AddMoney('cash', math.random(config.registerReward.min, config.registerReward.max))

if not isDone then return end

TriggerClientEvent('qbx_storerobbery:client:updatedRobbables', -1, sharedConfig.registers, sharedConfig.safes)
if config.registerReward.chanceAtSticky > math.random(0, 100) then
local code = safeCodes[sharedConfig.registers[closestRegisterIndex].safeKey]
Expand All @@ -128,8 +105,8 @@ RegisterNetEvent('qbx_storerobbery:server:registerOpened', function(isDone)
label = Lang:t('text.safe_code') .. tostring(math.floor((code[1] % 360) / 3.60)) .. "-" .. tostring(math.floor((code[2] % 360) / 3.60)) .. "-" .. tostring(math.floor((code[3] % 360) / 3.60)) .. "-" .. tostring(math.floor((code[4] % 360) / 3.60)) .. "-" .. tostring(math.floor((code[5] % 360) / 3.60))
}
end
player.Functions.AddItem('stickynote', 1, false, info)
TriggerClientEvent('inventory:client:ItemBox', source, ITEMS['stickynote'], 'add')

exports.ox_inventory:AddItem(source, 'stickynote', 1, info)
end

startedRegister[source] = false
Expand All @@ -153,7 +130,6 @@ RegisterNetEvent('qbx_storerobbery:server:trySafe', function()

sharedConfig.safes[closestSafeIndex].robbed = true
startedSafe[source] = true
alertPolice(Lang:t('alert.safe'), source, sharedConfig.safes[closestSafeIndex].camId)
TriggerClientEvent('qbx_storerobbery:client:initSafeAttempt', source, closestSafeIndex, safeCodes[closestSafeIndex])
end)

Expand Down Expand Up @@ -201,6 +177,10 @@ AddEventHandler('playerJoining', function(source)
TriggerClientEvent('qbx_storerobbery:client:updatedRobbables', source, sharedConfig.registers, sharedConfig.safes)
end)

lib.callback.register('qbx_storerobbery:server:leoCount', function()
return exports.qbx_core:GetDutyCountType('leo')
end)

CreateThread(function()
while true do
safeCodes = {}
Expand Down
Loading