feat(server): money logs

logging of all transaction with a stack trace
This commit is contained in:
Andyyy7666
2026-03-16 05:46:44 +01:00
parent df0d0a8160
commit 72a0b8c53f
2 changed files with 42 additions and 0 deletions

View File

@@ -253,6 +253,7 @@ MySQL.ready(function()
"database/users.sql", "database/users.sql",
"database/characters.sql", "database/characters.sql",
"database/vehicles.sql", "database/vehicles.sql",
"database/moneylogs.sql"
}, resourceName) }, resourceName)
end) end)

View File

@@ -1,4 +1,5 @@
local avoidSavingLastLocations = {} local avoidSavingLastLocations = {}
local moneyLogs = {}
local function removeCharacterFunctions(character) local function removeCharacterFunctions(character)
local newData = {} local newData = {}
@@ -10,6 +11,13 @@ local function removeCharacterFunctions(character)
return newData return newData
end end
local function logMoney(charId, action, account, amount, reason, trace)
if not trace or (action == "set" and not reason) then return end
local traceText = trace:match("%((.-)%)")
traceText = traceText and traceText:gsub("%^%d", "") or traceText or ""
table.insert(moneyLogs, { charId, action, account, amount, reason or "[NO REASON GIVEN]", traceText })
end
local function createCharacterTable(info) local function createCharacterTable(info)
local playerInfo = PlayersInfo[info.source] or {} local playerInfo = PlayersInfo[info.source] or {}
@@ -46,6 +54,9 @@ local function createCharacterTable(info)
self.triggerEvent("ND:updateMoney", self.cash, self.bank) self.triggerEvent("ND:updateMoney", self.cash, self.bank)
TriggerEvent("ND:moneyChange", self.source, account, amount, "remove", reason) TriggerEvent("ND:moneyChange", self.source, account, amount, "remove", reason)
end end
logMoney(self.id, "deduct", account, amount, reason, Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()))
return true return true
end end
@@ -61,6 +72,9 @@ local function createCharacterTable(info)
self.triggerEvent("ND:updateMoney", self.cash, self.bank) self.triggerEvent("ND:updateMoney", self.cash, self.bank)
TriggerEvent("ND:moneyChange", self.source, account, amount, "add", reason) TriggerEvent("ND:moneyChange", self.source, account, amount, "add", reason)
end end
logMoney(self.id, "add", account, amount, reason, Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString()))
return true return true
end end
@@ -667,3 +681,30 @@ function NDCore.setActiveCharacter(src, id)
character.active() character.active()
return character return character
end end
-- runs every 10 minutes, saves all characters and money logs.
lib.cron.new("*/10 * * * *", function()
local players = NDCore.getPlayers()
for _, player in pairs(players) do
player.saveLastLocation()
player.save()
end
if #moneyLogs == 0 then return end
local placeholders = {}
local params = {}
local logs = moneyLogs
moneyLogs = {}
for i=1, #logs do
local row = logs[i]
for j = 1, 6 do
table.insert(params, row[j])
end
table.insert(placeholders, "(?, ?, ?, ?, ?, ?)")
end
local query = "INSERT INTO nd_money_log (character_id, action, account, amount, reason, trace) VALUES " .. table.concat(placeholders, ",")
MySQL.prepare(query, params)
end)