forked from alberttheprince/popcornrp-customs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
116 lines (96 loc) · 3.37 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local isNDCoreStarted = GetResourceState('ND_Core') == 'started'
if isNDCoreStarted then
print('NDCore found, and is being used.')
else
print('ND_Core is not started, modifications won\'t cost anything')
end
---@return number
local function getModPrice(mod, level)
if mod == 'cosmetic' or mod == 'colors' or mod == 18 then
return Config.Prices[mod] --[[@as number]]
else
return Config.Prices[mod][level]
end
end
---@param source number
---@param amount number
---@return boolean
local function removeMoney(source, amount)
if NDCore then
local player = NDCore.getPlayer(source)
local cashBalance = player.getData("cash")
local bankBalance = player.getData("bank")
if cashBalance >= amount then
player.deductMoney('cash', amount, "Customs")
return true
elseif bankBalance >= amount then
player.deductMoney('bank', amount, "Customs")
lib.notify(source, {
title = 'Customs',
description = ('You paid $%s from your bank account'):format(amount),
type = 'success',
})
return true
end
end
return false
end
-- Won't charge money for mods if the player's job is in the list
lib.callback.register('customs:server:pay', function(source, mod, level)
local zone = lib.callback.await('customs:client:zone', source)
local player = NDCore.getPlayer(source)
if not player then
return false
end
for i, v in ipairs(Config.Zones) do
if i == zone then
local playerJob = player.getData("job")
if v.freeMods then
for _, job in ipairs(v.freeMods) do
if playerJob == job then
return true
end
end
end
end
end
return removeMoney(source, getModPrice(mod, level))
end)
-- Won't charge money for repairs if the player's job is in the list
lib.callback.register('customs:server:repair', function(source, bodyHealth)
local zone = lib.callback.await('customs:client:zone', source)
local player = NDCore.getPlayer(source)
if not player then
return false
end
for i, v in ipairs(Config.Zones) do
if i == zone then
local playerJob = player.getData("job")
if v.freeRepair then
for _, job in ipairs(v.freeRepair) do
if playerJob == job then
return true
end
end
end
end
end
local price = math.ceil(1000 - bodyHealth)
return removeMoney(source, price)
end)
local function IsVehicleOwned(plate)
local result = MySQL.scalar.await('SELECT 1 from player_vehicles WHERE plate = ?', {plate})
if result then
return true
else
return false
end
end
-- Copied from qb-mechanicjob
RegisterNetEvent('customs:server:saveVehicleProps', function()
local src = source --[[@as number]]
local vehicleProps = lib.callback.await('customs:client:vehicleProps', src)
if IsVehicleOwned(vehicleProps.plate) then
MySQL.update('UPDATE player_vehicles SET mods = ? WHERE plate = ?', {json.encode(vehicleProps), vehicleProps.plate})
end
end)