Skip to content

feat: live database updates #681

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion config/server.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
return {
updateInterval = 5, -- how often to update player data in minutes
updateInterval = 5, -- how often to update player thirst and hunger in minutes
dbUpdateInterval = 15, -- how often to update the database for player data in seconds

money = {
---@alias MoneyType 'cash' | 'bank' | 'crypto'
Expand Down
2 changes: 1 addition & 1 deletion server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ AddEventHandler('playerDropped', function(reason)
color = 'red',
message = ('**%s** (%s) left...\n **Reason:** %s'):format(GetPlayerName(src), player.PlayerData.license, reason),
})
player.Functions.Save()
Save(player.PlayerData.source)
QBX.Player_Buckets[player.PlayerData.license] = nil
QBX.Players[src] = nil
end)
Expand Down
2 changes: 1 addition & 1 deletion server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function ToggleOptin(source)
if not license or not IsPlayerAceAllowed(source --[[@as string]], 'admin') then return end
local player = GetPlayer(source)
player.PlayerData.optin = not player.PlayerData.optin
player.Functions.SetPlayerData('optin', player.PlayerData.optin)
SetPlayerData(player.PlayerData.source, 'optin', player.PlayerData.optin, nil, true)
end

exports('ToggleOptin', ToggleOptin)
Expand Down
11 changes: 9 additions & 2 deletions server/loops.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require 'config.server'
local storage = require 'server.storage.main'

local function removeHungerAndThirst(src, player)
local playerState = Player(src).state
Expand All @@ -8,8 +9,6 @@ local function removeHungerAndThirst(src, player)

player.Functions.SetMetaData('thirst', math.max(0, newThirst))
player.Functions.SetMetaData('hunger', math.max(0, newHunger))

player.Functions.Save()
end

CreateThread(function()
Expand All @@ -22,6 +21,14 @@ CreateThread(function()
end
end)

CreateThread(function()
local interval = 1000 * config.dbUpdateInterval
while true do
Wait(interval)
storage.sendPlayerDataUpdates()
end
end)

local function pay(player)
local job = player.PlayerData.job
local payment = GetJob(job.name).grades[job.grade.level].payment or job.payment
Expand Down
Loading