197 lines
5.6 KiB
Lua
197 lines
5.6 KiB
Lua
-- client/gangbank.lua
|
|
print("^2[turfwar]^7 gangbank.lua loaded (client, robust + rgb)")
|
|
|
|
local currentGang = 0
|
|
local lastBalance = 0
|
|
local lastRequestAt = 0
|
|
local REQUEST_COOLDOWN_MS = 800
|
|
|
|
local function isHiddenGang(gangId)
|
|
gangId = tonumber(gangId) or 0
|
|
return gangId == 0 or gangId == 3
|
|
end
|
|
|
|
-- ✅ Get gang RGB from Config.GANGS (supports numeric or string keys)
|
|
local function getGangRGB(gangId)
|
|
gangId = tonumber(gangId) or 0
|
|
local fallback = { 255, 255, 255 }
|
|
|
|
if not (Config and Config.GANGS) then
|
|
return fallback
|
|
end
|
|
|
|
local g = Config.GANGS[gangId] or Config.GANGS[tostring(gangId)]
|
|
if g and type(g.rgb) == "table" then
|
|
local r = tonumber(g.rgb[1]) or tonumber(g.rgb.r)
|
|
local gg = tonumber(g.rgb[2]) or tonumber(g.rgb.g)
|
|
local b = tonumber(g.rgb[3]) or tonumber(g.rgb.b)
|
|
if r and gg and b then
|
|
return { r, gg, b }
|
|
end
|
|
end
|
|
|
|
return fallback
|
|
end
|
|
|
|
local function setLabelForGang(gangId)
|
|
local label = "Gang Bank"
|
|
gangId = tonumber(gangId) or 0
|
|
|
|
if Config and Config.GANGS then
|
|
local g = Config.GANGS[gangId] or Config.GANGS[tostring(gangId)]
|
|
if g and g.name then label = g.name end
|
|
end
|
|
|
|
local rgb = getGangRGB(gangId)
|
|
|
|
-- ✅ Send label + rgb so NUI can color it
|
|
SendNUIMessage({
|
|
type = "gangbank:label",
|
|
label = label,
|
|
gangId = gangId,
|
|
rgb = rgb
|
|
})
|
|
end
|
|
|
|
local function show()
|
|
SendNUIMessage({ type = "gangbank:show" })
|
|
end
|
|
|
|
local function hide()
|
|
SendNUIMessage({ type = "gangbank:hide" })
|
|
end
|
|
|
|
local function setBalance(balance)
|
|
local b = tonumber(balance) or 0
|
|
lastBalance = b
|
|
|
|
-- ✅ debug print shows the correct updated balance
|
|
print(("[GangBank] sending NUI gang=%d balance=%d"):format(currentGang, lastBalance))
|
|
|
|
local rgb = getGangRGB(currentGang)
|
|
|
|
-- ✅ Send rgb here too (in case update arrives without a label)
|
|
SendNUIMessage({
|
|
type = "gangbank:update",
|
|
gangId = currentGang,
|
|
balance = lastBalance,
|
|
rgb = rgb
|
|
})
|
|
end
|
|
|
|
local function applyVisibility()
|
|
if isHiddenGang(currentGang) then
|
|
hide()
|
|
return
|
|
end
|
|
setLabelForGang(currentGang)
|
|
show()
|
|
end
|
|
|
|
local function requestMyGangBank(force)
|
|
local now = GetGameTimer()
|
|
if not force and (now - lastRequestAt) < REQUEST_COOLDOWN_MS then return end
|
|
lastRequestAt = now
|
|
TriggerServerEvent("turfwar:gangbank:request")
|
|
end
|
|
|
|
-- Retry helper (handles "request too early" / race conditions)
|
|
local function requestWithRetries(maxTries, delayMs)
|
|
CreateThread(function()
|
|
maxTries = tonumber(maxTries) or 6
|
|
delayMs = tonumber(delayMs) or 800
|
|
|
|
for i = 1, maxTries do
|
|
requestMyGangBank(true)
|
|
Wait(delayMs)
|
|
|
|
if currentGang ~= 0 then
|
|
-- If gangs can legitimately be $0, you can remove this check.
|
|
if lastBalance > 0 then
|
|
return
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
|
|
-- Ask shortly after load (covers resource restart / joining late)
|
|
CreateThread(function()
|
|
Wait(900)
|
|
requestWithRetries(8, 900)
|
|
end)
|
|
|
|
-- ------------------------------------------------------------
|
|
-- Gang changes (server sends BOTH of these in your main.lua)
|
|
-- ------------------------------------------------------------
|
|
|
|
-- Broadcast to all clients: (src, gangId)
|
|
RegisterNetEvent("turfwar:playerGang", function(src, gangId)
|
|
if tonumber(src) ~= tonumber(GetPlayerServerId(PlayerId())) then return end
|
|
|
|
currentGang = tonumber(gangId) or 0
|
|
print(("^5[GangBank]^7 turfwar:playerGang -> gang=%d"):format(currentGang))
|
|
|
|
applyVisibility()
|
|
requestWithRetries(6, 700)
|
|
end)
|
|
|
|
-- Sent directly to the player: (gangId)
|
|
RegisterNetEvent("turfwar:gangUpdate", function(gangId)
|
|
currentGang = tonumber(gangId) or 0
|
|
print(("^5[GangBank]^7 turfwar:gangUpdate -> gang=%d"):format(currentGang))
|
|
|
|
applyVisibility()
|
|
requestWithRetries(6, 700)
|
|
end)
|
|
|
|
-- Compatibility: some parts of your system also send this
|
|
RegisterNetEvent("turfwar:setFaction", function(gangId)
|
|
currentGang = tonumber(gangId) or 0
|
|
print(("^5[GangBank]^7 turfwar:setFaction -> gang=%d"):format(currentGang))
|
|
|
|
applyVisibility()
|
|
requestWithRetries(6, 700)
|
|
end)
|
|
|
|
-- ------------------------------------------------------------
|
|
-- Balance update from server
|
|
-- ------------------------------------------------------------
|
|
RegisterNetEvent("turfwar:gangbank:update", function(gangId, balance)
|
|
gangId = tonumber(gangId) or 0
|
|
balance = tonumber(balance) or 0
|
|
|
|
-- ⚠️ IMPORTANT RACE FIX:
|
|
-- If we ALREADY know we're in a real gang, ignore "gangId=0" updates
|
|
if currentGang ~= 0 and gangId == 0 then
|
|
print(("^3[GangBank]^7 Ignored update gangId=0 (known gang=%d) balance=%d"):format(currentGang, balance))
|
|
return
|
|
end
|
|
|
|
currentGang = gangId
|
|
print(("^2[GangBank]^7 UPDATE gang=%d balance=%d"):format(currentGang, balance))
|
|
|
|
if isHiddenGang(currentGang) then
|
|
hide()
|
|
return
|
|
end
|
|
|
|
setLabelForGang(currentGang)
|
|
show()
|
|
setBalance(balance)
|
|
end)
|
|
|
|
-- ------------------------------------------------------------
|
|
-- Resource restart
|
|
-- ------------------------------------------------------------
|
|
AddEventHandler("onClientResourceStart", function(res)
|
|
if res ~= GetCurrentResourceName() then return end
|
|
currentGang = 0
|
|
lastBalance = 0
|
|
hide()
|
|
CreateThread(function()
|
|
Wait(900)
|
|
requestWithRetries(8, 900)
|
|
end)
|
|
end)
|