Skip to content

Commit

Permalink
feat: decay alcohol on login using lastLoggedOut (#4)
Browse files Browse the repository at this point in the history
* feat: decay alcohol on login using lastLoggedOut

* setMetadata on logout

* lastLoggedOut can be nil
  • Loading branch information
TonybynMp4 committed Mar 7, 2024
1 parent d5d0f98 commit a2d0446
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
5 changes: 3 additions & 2 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require('config.client')
local sharedConfig = require('config.shared')
local playerState = LocalPlayer.state
local alcoholLevel = playerState.alcohol or 0
local playerWalk
Expand Down Expand Up @@ -40,10 +41,10 @@ local function drunkLoop()
drunkEffect(severity)
end

Wait(60000 * config.alcoholDecayTime)
Wait(60000 * sharedConfig.alcoholDecayTime)

if alcoholLevel > 0 then
alcoholLevel -= config.alcoholDecayAmount
alcoholLevel -= sharedConfig.alcoholDecayAmount
end

if alcoholLevel >= config.ethylComaValue then
Expand Down
4 changes: 0 additions & 4 deletions config/client.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
return {
--- time in minutes before the alcohol level is updated
alcoholDecayTime = 1,
--- amount of alcohol to decay per alcoholDecayTime
alcoholDecayAmount = 0.1,
--- delay in ms before the effect is first applied
delayEffect = 15000,
effect = {
Expand Down
6 changes: 6 additions & 0 deletions config/shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
return {
--- time in minutes before the alcohol level is updated
alcoholDecayTime = 1,
--- amount of alcohol to decay per alcoholDecayTime
alcoholDecayAmount = 0.1,
}
5 changes: 4 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ server_scripts {
'server/main.lua'
}

file 'config/client.lua'
files {
'config/client.lua',
'config/shared.lua'
}

lua54 'yes'
use_experimental_fxv2_oal 'yes'
17 changes: 16 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lib.versionCheck('TonybynMp4/qbx_alcoholism')
local config = require 'config.server'
local sharedConfig = require 'config.shared'

for alcohol, params in pairs(config.alcoholItems) do
exports.qbx_core:CreateUseableItem(alcohol, function(source, item)
Expand Down Expand Up @@ -74,7 +75,20 @@ end)

RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function()
local player = exports.qbx_core:GetPlayer(source)
Player(source).state:set('alcohol', player.PlayerData.metadata.alcohol or 0, true)
local lastLoggedOut = player.PlayerData.lastLoggedOut
if not lastLoggedOut then return end
local timePassed = (os.time() - lastLoggedOut/1000) / 60
local alcohol = player.PlayerData.metadata.alcohol
alcohol -= (timePassed / sharedConfig.alcoholDecayTime) * sharedConfig.alcoholDecayAmount

Player(source).state:set('alcohol', alcohol, true)
end)

RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
local player = exports.qbx_core:GetPlayer(source)
if not player then return end
player.Functions.SetMetaData('alcohol', Player(source).state.alcohol)
Player(source).state:set('alcohol', 0, true)
end)

AddStateBagChangeHandler('alcohol', nil, function(bagName, _, value)
Expand All @@ -83,5 +97,6 @@ AddStateBagChangeHandler('alcohol', nil, function(bagName, _, value)
if value == Player(source).state.alcohol then return end
local player = exports.qbx_core:GetPlayer(source)
if not player then return end
if value == player.PlayerData.metadata.alcohol then return end
player.Functions.SetMetaData('alcohol', value)
end)

0 comments on commit a2d0446

Please sign in to comment.