- UID
- 67
- 在线时间
- 小时
- 阅读权限
- 70
- 注册时间
- 2021-3-17
- 最后登录
- 1970-1-1
【高级】合伙人
|
好的,我们可以在服务端脚本中添加一个定时任务,用于检查玩家的贷款到期情况,并进行强制还款。同时,如果玩家的银行账户余额不足以还款,可以采取其他措施(例如扣除现金或冻结资产)。下面是更新后的代码。
### Config.luaConfig = {}
-- 银行坐标,可以在此修改
Config.Banks = {
{name = "Bank1", coords = vector3(-1212.98, -330.84, 37.78)},
{name = "Bank2", coords = vector3(149.46, -1040.34, 29.37)},
-- 添加更多银行坐标
}
-- ATM模型,可以在此修改
Config.ATMs = {
'prop_atm_01',
'prop_atm_02',
'prop_atm_03'
}
-- 银行卡物品名称
Config.BankCardItem = "bank_card"
-- 可修改的转账金额选项
Config.TransferAmounts = {1000, 2000, 3000, 4000, 5000}
-- 可修改的贷款金额选项
Config.LoanAmounts = {1000, 2000, 3000, 4000, 5000}
-- 利率配置
Config.InterestRates = {
[1] = 0.02,
[2] = 0.04,
[3] = 0.06,
[4] = 0.08,
[5] = 0.10,
[6] = 0.12,
[7] = 0.14,
[8] = 0.16,
[9] = 0.18,
[10] = 0.20,
[11] = 0.22,
[12] = 0.24
}
### Client.luaESX = nil
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
end)
-- 绑定OX_TARGET给银行和ATM
Citizen.CreateThread(function()
for _, bank in pairs(Config.Banks) do
exports.ox_target:addSphere({
coords = bank.coords,
radius = 1,
debugPoly = false,
options = {
{
name = 'bank_use',
icon = 'fas fa-university',
label = '打开银行菜单',
onSelect = function()
OpenBankMenu()
end
}
}
})
end
exports.ox_target:addModel(Config.ATMs, {
{
name = 'atm_use',
icon = 'fas fa-credit-card',
label = '打开ATM菜单',
onSelect = function(entity)
OpenATMMenu(entity)
end
}
})
end)
function OpenBankMenu()
if HasItem(Config.BankCardItem) then
-- 显示银行菜单
-- 使用ox_lib生成UI
TriggerEvent('ox_lib:menu', {
title = '银行菜单',
options = {
{title = '转账', description = '转账给他人', onSelect = function() OpenTransferMenu() end},
{title = '收款', description = '从他人处收款', onSelect = function() OpenReceiveMenu() end},
{title = '贷款', description = '申请贷款', onSelect = function() OpenLoanMenu() end},
{title = '查询贷款信息', description = '查看当前贷款信息', onSelect = function() GetLoanInfo() end},
}
})
else
exports.ox_lib:notify({type = 'error', text = '你没有银行卡'})
end
end
function OpenATMMenu(entity)
if HasItem(Config.BankCardItem) then
-- 显示ATM菜单
-- 使用ox_lib生成UI
TriggerEvent('ox_lib:menu', {
title = 'ATM菜单',
options = {
{title = '转账', description = '转账给他人', onSelect = function() OpenTransferMenu() end},
{title = '收款', description = '从他人处收款', onSelect = function() OpenReceiveMenu() end},
}
})
else
exports.ox_lib:notify({type = 'error', text = '你没有银行卡'})
end
end
function OpenTransferMenu()
-- 打开转账菜单
end
function OpenReceiveMenu()
-- 打开收款菜单
end
function OpenLoanMenu()
-- 打开贷款菜单
end
function GetLoanInfo()
ESX.TriggerServerCallback('bank:getLoanInfo', function(data)
if data then
-- 显示贷款信息
local info = string.format("你的贷款金额: %s\n利息: %s\n期限: %s天", data.loanAmount, data.interest, data.daysLeft)
exports.ox_lib:notify({type = 'info', text = info})
else
exports.ox_lib:notify({type = 'error', text = '你没有贷款'})
end
end)
end
function HasItem(item)
-- 检查玩家是否有指定物品
local xPlayer = ESX.GetPlayerData()
for i=1, #xPlayer.inventory, 1 do
if xPlayer.inventory.name == item and xPlayer.inventory.count > 0 then
return true
end
end
return false
end
### Server.luaESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
local PlayerLoans = {} -- 用于存储玩家的贷款信息
-- 处理转账请求
RegisterNetEvent('bank:transfer')
AddEventHandler('bank:transfer', function(targetId, amount)
local xPlayer = ESX.GetPlayerFromId(source)
local tPlayer = ESX.GetPlayerFromId(targetId)
if tPlayer then
if xPlayer.getAccount('bank').money >= amount then
xPlayer.removeAccountMoney('bank', amount)
tPlayer.addAccountMoney('bank', amount)
TriggerClientEvent('ox_lib:notify', source, {type = 'success', text = '转账成功'})
TriggerClientEvent('ox_lib:notify', targetId, {type = 'success', text = '你收到了一笔转账'})
else
TriggerClientEvent('ox_lib:notify', source, {type = 'error', text = '你的银行余额不足'})
end
else
TriggerClientEvent('ox_lib:notify', source, {type = 'error', text = '目标玩家不存在'})
end
end)
-- 查询玩家贷款信息
ESX.RegisterServerCallback('bank:getLoanInfo', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
if PlayerLoans[xPlayer.identifier] then
cb(PlayerLoans[xPlayer.identifier])
else
cb(nil)
end
end)
-- 处理贷款请求
RegisterNetEvent('bank:loan')
AddEventHandler('bank:loan', function(amount, months)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = xPlayer.identifier
local interestRate = Config.InterestRates[months]
local totalAmount = amount * (1 + interestRate)
local dueDate = os.time() + (months * 30 * 86400) -- 计算还款期限
PlayerLoans[identifier] = {
loanAmount = amount,
interest = interestRate * 100 .. '%',
totalAmount = totalAmount,
dueDate = dueDate,
daysLeft = months * 30
}
xPlayer.addAccountMoney('bank', amount)
TriggerClientEvent('ox_lib:notify', source, {type = 'success', text = '贷款成功'})
end)
-- 定期检查贷款到期情况并强制还款
Citizen.CreateThread(function()
while true do
Citizen.Wait(60000) -- 每分钟检查一次
local currentTime = os.time()
for identifier, loan in pairs(PlayerLoans) do
if loan.dueDate <= currentTime then
local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
if xPlayer then
if xPlayer.getAccount('bank').money >= loan.totalAmount then
xPlayer.removeAccountMoney('bank', loan.totalAmount)
TriggerClientEvent('ox_lib:notify', xPlayer.source, {type = 'success', text = '贷款已自动还款'})
else
-- 如果银行账户余额不足,扣除现金或冻结资产
local remainingAmount = loan.totalAmount - xPlayer.getAccount('bank').money
xPlayer.setAccountMoney('bank', 0)
if xPlayer.getMoney() >= remainingAmount then
xPlayer.removeMoney(remainingAmount)
TriggerClientEvent('ox_lib:notify', xPlayer.source, {type = 'warning', text = '贷款已部分还款,余额不足部分已从现金中扣除'})
else
-- 采取其他措施,例如冻结资产
TriggerClientEvent('ox_lib:notify', xPlayer.source, {type = 'error', text = '贷款超期未还,资产已被冻结'})
end
end
end
PlayerLoans[identifier] = nil -- 清除已还款的贷款信息
end
end
end
end)
在这个版本的脚本中,增加了一个定时任务(每分钟检查一次),用于检查玩家的贷款到期情况。如果贷款到期且玩家的银行账户余额足够,则自动进行还款;如果余额不足,则尝试从现金中扣除,或者采取其他措施(例如冻结资产)。希望这能帮到你。 |
|