-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.lua
96 lines (86 loc) · 4.5 KB
/
server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local rentedVehicles = {}
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterNetEvent('sqz_carrental:RentVehicle')
AddEventHandler('sqz_carrental:RentVehicle', function(model, insurance, price, time, rentalIndex)
time = time:gsub('min', '')
time = tonumber(time)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer.getMoney() >= price + Config.DownPayment then
xPlayer.removeMoney(price + Config.DownPayment)
xPlayer.showNotification('You have paid '..price..'$')
xPlayer.showNotification('You have paid '..Config.DownPayment..'$ as a down payment')
TriggerClientEvent('sqz_carrental:SpawnVehicle', source, model, insurance, price, time, rentalIndex)
elseif xPlayer.getAccount('bank').money >= price + Config.DownPayment then
xPlayer.removeAccountMoney('bank', price + Config.DownPayment)
xPlayer.showNotification('You have paid '..Config.DownPayment..'$ as a down payment from your bank account')
xPlayer.showNotification('You have paid '..price..'$ from your bank account')
TriggerClientEvent('sqz_carrental:SpawnVehicle', source, model, insurance, price, time, rentalIndex)
else
xPlayer.showNotification('You can not afford renting this vehicle')
end
end)
RegisterNetEvent('sqz_carrental:VehicleSpawned')
AddEventHandler('sqz_carrental:VehicleSpawned', function(plate, insurance, time, netId)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
if not rentedVehicles[plate] then
rentedVehicles[plate] = {
owner = xPlayer.identifier,
insurance = insurance,
netId = netId,
downPayment = Config.DownPayment
}
SetTimeout(time * 60 * 1000 + 5000, function()
local plate = GetVehicleNumberPlateText(NetworkGetEntityFromNetworkId(netId))
if rentedVehicles[plate] then
if GetPlayerPing(rentedVehicles[plate].owner) > 5 then
Citizen.CreateThread(function()
while true do
Wait(1000 * 60)
if rentedVehicles[plate].downPayment >= Config.ExtraChargePerMinute then
rentedVehicles[plate].downPayment = rentedVehicles[plate].downPayment - Config.ExtraChargePerMinute
else
if ESX.GetPlayerFromId(_source) then
xPlayer.showNotification('The deposit will not be refunded, because you have not returned the vehicle')
xPlayer.showNotification('The vehicle has been impounded')
end
DeleteEntity(NetworkGetEntityFromNetworkId(netId))
rentedVehicles[plate] = nil
end
end
end)
else
rentedVehicles[plate] = nil
DeleteEntity(NetworkGetEntityFromNetworkId(netId))
end
end
end)
end
end)
RegisterNetEvent('sqz_carrental:ReturnVehicle')
AddEventHandler('sqz_carrental:ReturnVehicle', function(plate, damageIndex)
local xPlayer = ESX.GetPlayerFromId(source)
if not rentedVehicles[plate] then
xPlayer.showNotification('You can not return this vehicle because this one has not been rented.')
return
end
if rentedVehicles[plate].owner ~= xPlayer.identifier then
xPlayer.showNotification('You can not return this vehicle because you are not borrower.')
return
end
if rentedVehicles[plate].insurance then
damageIndex = 1
end
local moneyToGive = math.floor(rentedVehicles[plate].downPayment * damageIndex)
if damageIndex < 1 then
local reducedBy = Config.DownPayment - Config.DownPayment * damageIndex
xPlayer.showNotification('Down payment you should receive has been lowered by '..reducedBy..'$ because you have returned the vehicle damaged')
end
xPlayer.addAccountMoney('bank', moneyToGive)
xPlayer.showNotification('The down payment of amount '..moneyToGive..'$ has been returned you.')
TaskLeaveVehicle(GetPlayerPed(source), NetworkGetEntityFromNetworkId(rentedVehicles[plate].netId), 0)
Wait(1700)
DeleteEntity(NetworkGetEntityFromNetworkId(rentedVehicles[plate].netId))
rentedVehicles[plate] = nil
TriggerClientEvent('sqz_carrental:VehicleSuccessfulyReturned', xPlayer.source)
end)