Skip to content

Commit

Permalink
refactor: remove acquirevehiclekeys event (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Sep 28, 2024
1 parent 146c7df commit 0e5bdcd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
10 changes: 3 additions & 7 deletions client/carjack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ local function onCarjackSuccess(occupants, vehicle)
makePedFlee(ped)
end)
end
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
TriggerServerEvent('qb-vehiclekeys:server:setVehLockState', NetworkGetNetworkIdFromEntity(vehicle), 1)
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle))
end

local function onCarjackFail(driver)
exports.qbx_core:Notify(locale('notify.carjack_failed'), 'error')
makePedFlee(driver)
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
end

local function carjackVehicle(driver, vehicle)
Expand Down Expand Up @@ -98,14 +94,14 @@ local function carjackVehicle(driver, vehicle)
},
}) then
if cache.weapon and isCarjacking then
local carjackChance = config.carjackChance[GetWeapontypeGroup(cache.weapon) --[[@as string]]] or 0.5
isCarjacking = false -- make this false to stop TaskVehicleTempAction from preventing ped to leave the car

if math.random() <= carjackChance then
local success = lib.callback.await('qbx_vehiclekeys:server:carjack', false, VehToNet(vehicle), GetWeapontypeGroup(cache.weapon))
if success then
onCarjackSuccess(occupants, vehicle)
else
onCarjackFail(driver)
end
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
Wait(2000)
sendPoliceAlertAttempt('carjack')
end
Expand Down
2 changes: 1 addition & 1 deletion client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ end
---Will be executed when the lock opening is successful.
---@param vehicle number The entity number of the vehicle.
local function hotwireSuccessCallback(vehicle)
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle))
TriggerServerEvent('qbx_vehiclekeys:server:hotwiredVehicle', VehToNet(vehicle))
end

---Operations done after the LockpickDoor quickevent done.
Expand Down
12 changes: 5 additions & 7 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ end
exports('SetVehicleDoorLock', setVehicleDoorLock)

local function findKeys(vehicleModel, vehicleClass, vehicle)
local vehicleConfig = sharedFunctions.getVehicleConfig(vehicle)
local hotwireTime = math.random(config.minKeysSearchTime, config.maxKeysSearchTime)

local anim = config.anims.lockpick.model[vehicleModel]
Expand All @@ -75,13 +74,12 @@ local function findKeys(vehicleModel, vehicleClass, vehicle)
combat = true,
}
}) then
if math.random() <= vehicleConfig.findKeysChance then
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle))
return true
else
local success = lib.callback.await('qbx_vehiclekeys:server:findKeys', false, VehToNet(vehicle))
if not success then
TriggerServerEvent('hud:server:GainStress', math.random(1, 4))
exports.qbx_core:Notify(locale("notify.failed_keys"), 'error')
end
return success
end
end

Expand Down Expand Up @@ -118,7 +116,7 @@ local function onEnteringDriverSeat()
local isVehicleRunning = GetIsVehicleEngineRunning(vehicle)
if config.getKeysWhenEngineIsRunning and isVehicleRunning then
lib.print.debug("giving keys because engine is running")
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(vehicle))
TriggerServerEvent('qbx_vehiclekeys:server:playerEnteredVehicleWithEngineOn', VehToNet(vehicle))
return
end

Expand Down Expand Up @@ -230,7 +228,7 @@ RegisterNetEvent('QBCore:Client:VehicleInfo', function(data)
car = true,
},
}) then
TriggerServerEvent('qb-vehiclekeys:server:AcquireVehicleKeys', VehToNet(data.vehicle))
TriggerServerEvent('qbx_vehiclekeys:server:tookKeys', VehToNet(data.vehicle))
end
end
isTakingKeys = false
Expand Down
13 changes: 0 additions & 13 deletions config/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ return {
carjackEnable = true, -- Enables the ability to carjack pedestrian vehicles, stealing them by pointing a weapon at them
carjackingTimeInMs = 7500, -- Time it takes to successfully carjack in miliseconds
delayBetweenCarjackingsInMs = 10000, -- Time before you can attempt another carjack in miliseconds
---@type table<VehicleClass, number>
carjackChance = { -- Probability of successful carjacking based on weapon used
[WeaponTypeGroup.MELEE] = 0.0,
[WeaponTypeGroup.HANDGUN] = 0.5,
[WeaponTypeGroup.SMG] = 0.75,
[WeaponTypeGroup.SHOTGUN] = 0.90,
[WeaponTypeGroup.RIFLE] = 0.90,
[WeaponTypeGroup.LMG] = 0.99,
[WeaponTypeGroup.SNIPER] = 0.99,
[WeaponTypeGroup.HEAVY] = 0.99,
[WeaponTypeGroup.THROWABLE] = 0.0,
[WeaponTypeGroup.MISC] = 0.0,
},

-- Hotwire Settings
timeBetweenHotwires = 5000, -- Time in milliseconds between hotwire attempts
Expand Down
13 changes: 13 additions & 0 deletions config/server.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
return {
runClearCronMinutes = 5,
---@type table<WeaponTypeGroup, number>
carjackChance = { -- Probability of successful carjacking based on weapon used
[WeaponTypeGroup.MELEE] = 0.0,
[WeaponTypeGroup.HANDGUN] = 0.5,
[WeaponTypeGroup.SMG] = 0.75,
[WeaponTypeGroup.SHOTGUN] = 0.90,
[WeaponTypeGroup.RIFLE] = 0.90,
[WeaponTypeGroup.LMG] = 0.99,
[WeaponTypeGroup.SNIPER] = 0.99,
[WeaponTypeGroup.HEAVY] = 0.99,
[WeaponTypeGroup.THROWABLE] = 0.0,
[WeaponTypeGroup.MISC] = 0.0,
},
}
6 changes: 6 additions & 0 deletions server/keys.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
local config = require 'config.server'
local debug = GetConvarInt(('%s-debug'):format(GetCurrentResourceName()), 0) == 1

---@alias CitizenId string
---@alias SessionId integer
---@type table<CitizenId, table<SessionId, boolean>>
local keysList = {} ---holds key status for some time after player logs out (Prevents frustration by crashing the client)

---@alias LogoutTime integer
---@type table<CitizenId, LogoutTime>
local keysLifetime = {} ---Life timestamp of the keys of a character who has logged out

---Gets Citizen Id based on source
Expand Down
34 changes: 33 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local config = require 'config.server'
local sharedFunctions = require 'shared.functions'

local getIsVehicleAlwaysUnlocked = sharedFunctions.getIsVehicleAlwaysUnlocked
Expand All @@ -12,7 +13,38 @@ local EntityType = {
Object = 3
}

RegisterNetEvent('qb-vehiclekeys:server:AcquireVehicleKeys', function(netId)
lib.callback.register('qbx_vehiclekeys:server:findKeys', function(source, netId)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if math.random() <= sharedFunctions.getVehicleConfig(vehicle).findKeysChance then
GiveKeys(source, vehicle)
return true
end
end)

lib.callback.register('qbx_vehiclekeys:server:carjack', function(source, netId, weaponTypeGroup)
local chance = config.carjackChance[weaponTypeGroup] or 0.5
if math.random() <= chance then
local vehicle = NetworkGetEntityFromNetworkId(netId)
GiveKeys(source, vehicle)
TriggerEvent('qb-vehiclekeys:server:setVehLockState', netId, 1)
return true
end
end)

RegisterNetEvent('qbx_vehiclekeys:server:playerEnteredVehicleWithEngineOn', function(netId)
local src = source
local vehicle = NetworkGetEntityFromNetworkId(netId)
if not GetIsVehicleEngineRunning(vehicle) then return end
GiveKeys(src, vehicle)
end)

---TODO: secure this event
RegisterNetEvent('qbx_vehiclekeys:server:tookKeys', function(netId)
GiveKeys(source, NetworkGetEntityFromNetworkId(netId))
end)

---TODO: secure this event
RegisterNetEvent('qbx_vehiclekeys:server:hotwiredVehicle', function(netId)
GiveKeys(source, NetworkGetEntityFromNetworkId(netId))
end)

Expand Down

0 comments on commit 0e5bdcd

Please sign in to comment.