Skip to content

Commit

Permalink
chore: bpt_dmvschool 🚑 Fix some errors, and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Sep 30, 2024
1 parent bd0ad41 commit 13b9265
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
62 changes: 35 additions & 27 deletions server-data/resources/[bpt_addons]/bpt_dmvschool/client/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local CurrentAction = nil
local CurrentActionMsg = nil
local CurrentActionData = nil
local Licenses = {}
local CurrentTest = nil
local CurrentTestType = nil
Expand All @@ -9,6 +10,7 @@ local LastCheckPoint = -1
local CurrentBlip = nil
local CurrentZoneType = nil
local LastVehicleHealth = nil
local failedTest = false

function DrawMissionText(msg, time)
ClearPrints()
Expand Down Expand Up @@ -57,6 +59,7 @@ function StartDriveTest(type)
IsAboveSpeedLimit = false
CurrentVehicle = vehicle
LastVehicleHealth = GetEntityHealth(vehicle)
failedTest = false

local playerPed = PlayerPedId()
TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
Expand All @@ -69,6 +72,7 @@ function StopDriveTest(success)
if success then
TriggerServerEvent("bpt_dmvschool:addLicense", CurrentTestType)
ESX.ShowNotification(TranslateCap("passed_test"))
ESX.ShowNotification(TranslateCap("driving_test_complete"))
else
ESX.ShowNotification(TranslateCap("failed_test"))
end
Expand Down Expand Up @@ -152,6 +156,7 @@ function OpenDMVSchoolMenu()
end, function(menu)
CurrentAction = "dmvschool_menu"
CurrentActionMsg = TranslateCap("press_open_menu")
CurrentActionData = {}
end)
end

Expand All @@ -177,6 +182,7 @@ AddEventHandler("bpt_dmvschool:hasEnteredMarker", function(zone)
if zone == "DMVSchool" then
CurrentAction = "dmvschool_menu"
CurrentActionMsg = TranslateCap("press_open_menu")
CurrentActionData = {}
end
end)

Expand Down Expand Up @@ -239,14 +245,7 @@ CreateThread(function()
end

CurrentTest = nil

ESX.ShowNotification(TranslateCap("driving_test_complete"))

if DriveErrors < Config.MaxErrors then
StopDriveTest(true)
else
StopDriveTest(false)
end
StopDriveTest(DriveErrors < Config.MaxErrors)
else
if CurrentCheckPoint ~= LastCheckPoint then
if DoesBlipExist(CurrentBlip) then
Expand Down Expand Up @@ -309,48 +308,57 @@ CreateThread(function()
end
end)

function TestFailedGoToLastCheckPoint()
CurrentCheckPoint = #Config.CheckPoints - 1
failedTest = true
end

-- Speed / Damage control
CreateThread(function()
while true do
local sleep = 1500
if CurrentTest == "drive" then
sleep = 0
local playerPed = PlayerPedId()

if IsPedInAnyVehicle(playerPed, false) then
local vehicle = GetVehiclePedIsIn(playerPed, false)
local speed = GetEntitySpeed(vehicle) * Config.SpeedMultiplier
local health = GetEntityHealth(vehicle)

-- Speed check
for k, v in pairs(Config.SpeedLimits) do
if CurrentZoneType == k and speed > v then
DriveErrors += 1
ESX.ShowNotification(TranslateCap("driving_too_fast", v))
ESX.ShowNotification(TranslateCap("errors", DriveErrors, Config.MaxErrors))

if DriveErrors <= Config.MaxErrors then
ESX.ShowNotification(TranslateCap("driving_too_fast", v))
ESX.ShowNotification(TranslateCap("errors", DriveErrors, Config.MaxErrors))
end

sleep = (Config.SpeedingErrorDelay < 5000) and 5000 or Config.SpeedingErrorDelay
end
end
end
end
Wait(sleep)
end
end)

CreateThread(function()
while true do
local sleep = 1500
if CurrentTest == "drive" then
sleep = 0
local playerPed = PlayerPedId()
if IsPedInAnyVehicle(playerPed, false) then
local vehicle = GetVehiclePedIsIn(playerPed, false)
local health = GetEntityHealth(vehicle)
-- Vehicle damage check
if health < LastVehicleHealth then
DriveErrors += 1
ESX.ShowNotification(TranslateCap("you_damaged_veh"))
ESX.ShowNotification(TranslateCap("errors", DriveErrors, Config.MaxErrors))
if DriveErrors <= Config.MaxErrors then
ESX.ShowNotification(TranslateCap("you_damaged_veh"))
ESX.ShowNotification(TranslateCap("errors", DriveErrors, Config.MaxErrors))
end

-- avoid stacking faults
LastVehicleHealth = health
sleep = 1500
end

if DriveErrors > Config.MaxErrors then
ESX.ShowNotification(TranslateCap("test_failed_go_to_start_point"))
if not failedTest then
TestFailedGoToLastCheckPoint()
end
sleep = 5000
end
end
end
Wait(sleep)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Locales["en"] = {
["passed_test"] = "you passed the test, congratulations!",
["failed_test"] = "you ~r~failed the test, better luck next time!",
["theory_test"] = "theoretical Driving Test",
['test_failed_go_to_start_point'] = '~r~Go to the starting point, because you failed the test!',
["road_test_car"] = "driving Test",
["road_test_bike"] = "motorcycle Skills Test",
["road_test_truck"] = "truck Skills Test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Locales["it"] = {
["passed_test"] = "hai superato il test, congratulazioni!",
["failed_test"] = "~r~non hai superato il test, buona fortuna per la prossima volta!",
["theory_test"] = "Quiz teorico",
['test_failed_go_to_start_point'] = '~r~Vai al punto di partenza, perché hai fallito il test!',
["road_test_car"] = "Test pratico auto",
["road_test_bike"] = "Test pratico moto",
["road_test_truck"] = "Test pratico camion",
Expand Down

0 comments on commit 13b9265

Please sign in to comment.