201 lines
6.4 KiB
Lua
201 lines
6.4 KiB
Lua
-- server/payouts.lua
|
|
print("^2[turfwar]^7 server/payouts.lua LOADED (turf owners + gang bank + payouts)")
|
|
|
|
TurfwarPayouts = TurfwarPayouts or {}
|
|
|
|
-- ======================================================
|
|
-- CONFIG (matches your existing DB)
|
|
-- ======================================================
|
|
local GANGBANK_TABLE = "turfwar_gang_accounts" -- (gang_id, balance, updated_at)
|
|
local TURFOWNERS_TABLE = "turfwar_turf_owners" -- change if your table name differs
|
|
|
|
-- Ensure globals exist
|
|
PlayerGang = PlayerGang or {}
|
|
|
|
local function i(v) return math.floor(tonumber(v) or 0) end
|
|
|
|
local function dbReady()
|
|
return MySQL and MySQL.query and MySQL.update
|
|
end
|
|
|
|
-- ======================================================
|
|
-- Turf owner persistence (used by server/main.lua)
|
|
-- ======================================================
|
|
function TurfwarPayouts.SaveTurfOwner(turfId, gangId)
|
|
turfId = tostring(turfId or "")
|
|
gangId = i(gangId)
|
|
if turfId == "" then return end
|
|
if not dbReady() then
|
|
print("^1[turfwar]^7 SaveTurfOwner: MySQL not ready")
|
|
return
|
|
end
|
|
|
|
MySQL.update.await(
|
|
("INSERT INTO %s (turf_id, owner_gang) VALUES (?, ?) " ..
|
|
"ON DUPLICATE KEY UPDATE owner_gang = VALUES(owner_gang)"):format(TURFOWNERS_TABLE),
|
|
{ turfId, gangId }
|
|
)
|
|
end
|
|
|
|
function TurfwarPayouts.LoadTurfOwnersIntoRuntime(Turfs)
|
|
if not dbReady() then
|
|
print("^1[turfwar]^7 LoadTurfOwnersIntoRuntime: MySQL not ready")
|
|
return
|
|
end
|
|
|
|
local rows = MySQL.query.await(
|
|
("SELECT turf_id, owner_gang FROM %s"):format(TURFOWNERS_TABLE),
|
|
{}
|
|
) or {}
|
|
|
|
local applied = 0
|
|
for _, r in ipairs(rows) do
|
|
local id = tostring(r.turf_id)
|
|
local owner = i(r.owner_gang)
|
|
if Turfs and Turfs[id] then
|
|
Turfs[id].owner = owner
|
|
Turfs[id].progress = 0
|
|
Turfs[id].contestingGang = 0
|
|
applied = applied + 1
|
|
end
|
|
end
|
|
|
|
print(("^2[turfwar]^7 Turf owners loaded from DB: %d applied"):format(applied))
|
|
end
|
|
|
|
-- ======================================================
|
|
-- Gang bank helpers
|
|
-- ======================================================
|
|
local function ensureGangRow(gangId)
|
|
gangId = i(gangId)
|
|
if gangId <= 0 then return end
|
|
if not dbReady() then return end
|
|
|
|
-- requires gang_id to be UNIQUE/PK
|
|
MySQL.update.await(
|
|
("INSERT INTO %s (gang_id, balance) VALUES (?, 0) " ..
|
|
"ON DUPLICATE KEY UPDATE gang_id = gang_id"):format(GANGBANK_TABLE),
|
|
{ gangId }
|
|
)
|
|
end
|
|
|
|
local function getGangBalance(gangId)
|
|
gangId = i(gangId)
|
|
if gangId <= 0 then return 0 end
|
|
if not dbReady() then return 0 end
|
|
|
|
local rows = MySQL.query.await(
|
|
("SELECT balance FROM %s WHERE gang_id=? LIMIT 1"):format(GANGBANK_TABLE),
|
|
{ gangId }
|
|
) or {}
|
|
|
|
return tonumber(rows[1] and rows[1].balance) or 0
|
|
end
|
|
|
|
local function addGangMoney(gangId, amount)
|
|
gangId = i(gangId)
|
|
amount = i(amount)
|
|
if gangId <= 0 or amount == 0 then return getGangBalance(gangId) end
|
|
if not dbReady() then return 0 end
|
|
|
|
ensureGangRow(gangId)
|
|
|
|
-- Atomic increment
|
|
MySQL.update.await(
|
|
("UPDATE %s SET balance = balance + ? WHERE gang_id = ?"):format(GANGBANK_TABLE),
|
|
{ amount, gangId }
|
|
)
|
|
|
|
return getGangBalance(gangId)
|
|
end
|
|
|
|
-- Expose for other scripts if you want them
|
|
function TurfwarPayouts.GetGangBalance(gangId)
|
|
return getGangBalance(gangId)
|
|
end
|
|
|
|
function TurfwarPayouts.AddGangMoney(gangId, amount)
|
|
return addGangMoney(gangId, amount)
|
|
end
|
|
|
|
-- ======================================================
|
|
-- Push to HUD (server -> client)
|
|
-- ======================================================
|
|
function TurfwarPayouts.PushGangBankToPlayer(src, gangId)
|
|
gangId = i(gangId)
|
|
local bal = 0
|
|
if gangId > 0 then
|
|
bal = getGangBalance(gangId)
|
|
end
|
|
TriggerClientEvent("turfwar:gangbank:update", src, gangId, bal)
|
|
end
|
|
|
|
-- Client requests "my gang bank"
|
|
RegisterNetEvent("turfwar:gangbank:request", function()
|
|
local src = source
|
|
local gid = i(PlayerGang[src] or 0)
|
|
TurfwarPayouts.PushGangBankToPlayer(src, gid)
|
|
end)
|
|
|
|
-- ======================================================
|
|
-- Payout tick (called from server/main.lua)
|
|
-- ======================================================
|
|
function TurfwarPayouts.DoPayoutTick(Turfs)
|
|
if not dbReady() then
|
|
print("^1[turfwar]^7 DoPayoutTick: MySQL not ready")
|
|
return
|
|
end
|
|
|
|
local inc = (Config and Config.INCOME) or {}
|
|
local excluded = (inc and inc.excludedGangs) or {}
|
|
local defaultPayout = i(inc.defaultTurfPayout or 10)
|
|
|
|
local totals = {} -- [gangId] = payout this tick
|
|
|
|
for turfId, t in pairs(Turfs or {}) do
|
|
local owner = i(t.owner)
|
|
if owner ~= 0 and not excluded[owner] then
|
|
local cfg = (Config and Config.TURFS and Config.TURFS[turfId]) or nil
|
|
local payout = defaultPayout
|
|
if cfg and cfg.payout ~= nil then payout = i(cfg.payout) end
|
|
|
|
if payout > 0 then
|
|
totals[owner] = (totals[owner] or 0) + payout
|
|
end
|
|
end
|
|
end
|
|
|
|
for gangId, amount in pairs(totals) do
|
|
local newBal = addGangMoney(gangId, amount)
|
|
print(("^2[turfwar]^7 Payout: gang %d +$%d => $%d"):format(gangId, amount, newBal))
|
|
|
|
-- Push updated balance to online players in that gang
|
|
for _, pid in ipairs(GetPlayers()) do
|
|
local src = tonumber(pid)
|
|
if src and i(PlayerGang[src] or 0) == gangId then
|
|
TriggerClientEvent("turfwar:gangbank:update", src, gangId, newBal)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- ======================================================
|
|
-- Debug
|
|
-- ======================================================
|
|
RegisterCommand("tw_gangbank_dump", function()
|
|
if not dbReady() then
|
|
print("^1[turfwar]^7 tw_gangbank_dump: MySQL not ready")
|
|
return
|
|
end
|
|
|
|
local rows = MySQL.query.await(
|
|
("SELECT gang_id, balance, updated_at FROM %s ORDER BY gang_id"):format(GANGBANK_TABLE),
|
|
{}
|
|
) or {}
|
|
|
|
print(("^2[turfwar]^7 Gang accounts (%d rows):"):format(#rows))
|
|
for _, r in ipairs(rows) do
|
|
print((" gang=%s balance=%s updated=%s"):format(r.gang_id, r.balance, r.updated_at))
|
|
end
|
|
end, true)
|