Skip to content

Commit

Permalink
fix(testdrives): pass timer via statebag
Browse files Browse the repository at this point in the history
  • Loading branch information
solareon committed Sep 26, 2024
1 parent 012bf62 commit 8c51bb9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 4 additions & 10 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,26 +518,20 @@ end

--- End test drive
local function endTestDrive()
LocalPlayer.state:set('isInTestDrive', false, true)
exports.qbx_core:Notify(locale('general.testdrive_complete'), 'success')
LocalPlayer.state:set('isInTestDrive', nil, true)
end

--- Starts the test drive timer based on time and shop
---@param time integer
local function startTestDriveTimer(time)
local gameTimer = GetGameTimer()
local timeMs = time * 1000

CreateThread(function()
local playerState = LocalPlayer.state
while playerState.isInTestDrive do
local currentGameTime = GetGameTimer()
local secondsLeft = currentGameTime - gameTimer

if currentGameTime < gameTimer + timeMs and secondsLeft >= timeMs - 50 then
endTestDrive()
end

qbx.drawText2d({
text = locale('general.testdrive_timer')..math.ceil(time - secondsLeft / 1000),
coords = vec2(1.0, 1.38),
Expand All @@ -546,6 +540,7 @@ local function startTestDriveTimer(time)

Wait(0)
end
exports.qbx_core:Notify(locale('general.testdrive_complete'), 'success')
end)
end

Expand All @@ -556,9 +551,8 @@ AddStateBagChangeHandler('isInTestDrive', ('player:%s'):format(cache.serverId),
Wait(10)
end

local testDrive = sharedConfig.shops[insideShop].testDrive
exports.qbx_core:Notify(locale('general.testdrive_timenoti', testDrive.limit), 'inform')
startTestDriveTimer(testDrive.limit * 60)
exports.qbx_core:Notify(locale('general.testdrive_timenoti', value), 'inform')
startTestDriveTimer(value * 60)
end)

--- Swaps the chosen vehicle with another one
Expand Down
5 changes: 4 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@ RegisterNetEvent('qbx_vehicleshop:server:testDrive', function(data)
})

testDrives[src] = netId
Player(src).state:set('isInTestDrive', true, true)
Player(src).state:set('isInTestDrive', testDrive.limit, true)
SetTimeout(testDrive.limit * 60000, function()
Player(src).state:set('isInTestDrive', nil, true)
end)
end)

---@param vehicle string
Expand Down

0 comments on commit 8c51bb9

Please sign in to comment.