161 lines
4.6 KiB
Lua
161 lines
4.6 KiB
Lua
print("^2[turfwar]^7 finance server loaded")
|
|
|
|
-- Uses your existing Cash API:
|
|
-- Cash.Get(src), Cash.Add(src, amt), Cash.Remove(src, amt)
|
|
-- Cash.GetBank(src), Cash.SetBank(src, amt)
|
|
|
|
local function Notify(src, msg)
|
|
-- This is your existing chat/notify path
|
|
TriggerClientEvent('turfwar:finance:notify', src, msg)
|
|
end
|
|
|
|
local function Feed(src, kind, amount)
|
|
-- NEW: GTA feed popup (client will render it)
|
|
-- kind: "deposit" | "withdraw" | "balance"
|
|
TriggerClientEvent('turfwar:finance:feed', src, kind, amount)
|
|
end
|
|
|
|
local function PushMoney(src)
|
|
-- Keep using your existing HUD/stat updater (in main.lua)
|
|
TriggerClientEvent('turfwar:money:update', src, Cash.Get(src), Cash.GetBank(src))
|
|
end
|
|
|
|
local function parseAmount(arg)
|
|
local n = tonumber(arg)
|
|
if not n then return nil end
|
|
n = math.floor(n)
|
|
if n <= 0 then return nil end
|
|
return n
|
|
end
|
|
|
|
-- Simple anti-spam
|
|
local lastCmd = {}
|
|
local CMD_COOLDOWN_MS = 700
|
|
|
|
local function canRun(src)
|
|
local now = GetGameTimer()
|
|
local last = lastCmd[src] or 0
|
|
if (now - last) < CMD_COOLDOWN_MS then
|
|
return false
|
|
end
|
|
lastCmd[src] = now
|
|
return true
|
|
end
|
|
|
|
AddEventHandler('playerDropped', function()
|
|
lastCmd[source] = nil
|
|
end)
|
|
|
|
-- Make sure Cash is loaded before commands are used
|
|
CreateThread(function()
|
|
Wait(0)
|
|
if type(Cash) ~= "table" then
|
|
print("^1[turfwar]^7 finance.lua: Cash table missing. Ensure cash.lua loads before finance.lua in fxmanifest.")
|
|
return
|
|
end
|
|
if type(Cash.Get) ~= "function" or type(Cash.Add) ~= "function" or type(Cash.Remove) ~= "function" then
|
|
print("^1[turfwar]^7 finance.lua: Missing Cash.Get/Add/Remove. Check cash.lua.")
|
|
end
|
|
if type(Cash.GetBank) ~= "function" or type(Cash.SetBank) ~= "function" then
|
|
print("^1[turfwar]^7 finance.lua: Missing Cash.GetBank/SetBank. Check cash.lua.")
|
|
end
|
|
end)
|
|
|
|
-- ------------------------------------------------
|
|
-- /deposit <amount>
|
|
-- ------------------------------------------------
|
|
RegisterCommand("deposit", function(src, args)
|
|
if not src or src <= 0 then return end
|
|
if not canRun(src) then return end
|
|
|
|
local amount = parseAmount(args[1])
|
|
if not amount then
|
|
Notify(src, "~r~Usage: /deposit <amount>")
|
|
return
|
|
end
|
|
|
|
local cash = Cash.Get(src)
|
|
if cash < amount then
|
|
Notify(src, "~r~Not enough cash.")
|
|
return
|
|
end
|
|
|
|
-- Move cash -> bank
|
|
Cash.Remove(src, amount)
|
|
Cash.SetBank(src, Cash.GetBank(src) + amount)
|
|
|
|
-- Update your HUD (no duplication)
|
|
PushMoney(src)
|
|
|
|
-- NEW: GTA feed popup (single line)
|
|
Feed(src, "deposit", amount)
|
|
|
|
-- Keep your existing notify too (optional; remove if you only want GTA feed)
|
|
Notify(src, ("~g~Deposited $%d"):format(amount))
|
|
end, false)
|
|
|
|
-- ------------------------------------------------
|
|
-- /withdraw <amount>
|
|
-- ------------------------------------------------
|
|
RegisterCommand("withdraw", function(src, args)
|
|
if not src or src <= 0 then return end
|
|
if not canRun(src) then return end
|
|
|
|
local amount = parseAmount(args[1])
|
|
if not amount then
|
|
Notify(src, "~r~Usage: /withdraw <amount>")
|
|
return
|
|
end
|
|
|
|
local bank = Cash.GetBank(src)
|
|
if bank < amount then
|
|
Notify(src, "~r~Not enough in bank.")
|
|
return
|
|
end
|
|
|
|
-- Move bank -> cash
|
|
Cash.SetBank(src, bank - amount)
|
|
Cash.Add(src, amount)
|
|
|
|
-- Update your HUD (no duplication)
|
|
PushMoney(src)
|
|
|
|
-- NEW: GTA feed popup (single line)
|
|
Feed(src, "withdraw", amount)
|
|
|
|
-- Keep your existing notify too (optional)
|
|
Notify(src, ("~g~Withdrew $%d"):format(amount))
|
|
end, false)
|
|
|
|
-- ------------------------------------------------
|
|
-- /balance
|
|
-- ------------------------------------------------
|
|
RegisterCommand("balance", function(src)
|
|
if not src or src <= 0 then return end
|
|
|
|
local cash = Cash.Get(src)
|
|
local bank = Cash.GetBank(src)
|
|
|
|
PushMoney(src)
|
|
|
|
-- Optional: feed (shows bank balance)
|
|
Feed(src, "balance", bank)
|
|
|
|
Notify(src, ("~g~Cash:~w~ $%d ~b~Bank:~w~ $%d"):format(cash, bank))
|
|
end, false)
|
|
|
|
|
|
local atmOpenUntil = 0
|
|
|
|
local function OpenATMInterface()
|
|
atmOpenUntil = GetGameTimer() + 15000 -- 15s window
|
|
PlaySoundFrontend(-1, "ATM_WINDOW", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
|
|
|
|
BeginTextCommandThefeedPost("STRING")
|
|
AddTextComponentSubstringPlayerName("~g~ATM~s~: /deposit <amt> /withdraw <amt> /balance")
|
|
EndTextCommandThefeedPostTicker(false, false)
|
|
end
|
|
|
|
-- When player presses E at ATM, call this instead of the old help:
|
|
-- OpenATMInterface()
|