-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.lua
220 lines (203 loc) · 8.18 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
local QBCore = exports['qb-core']:GetCoreObject()
local Balance = 0
local location = {}
local BMItems = {}
RegisterNetEvent('ik-blackmarket:server:RandomLocation', function()
for k, v in pairs(Config.Locations) do
if v["products"] == nil then
print("Config.Locations['"..k.."'] can't find its product table")
end
if Config.RandomLocation then
local m = math.random(1, #v["coords"])
location = {bm = k, loc= m, data = v}
m = 0
end
end
end)
if Config.UseTimer then
CreateThread(function()
local minutes = Config.ChangeLocationTime
while true do
Wait(60000)
minutes = minutes - 1
if minutes == 0 then
TriggerEvent('ik-blackmarket:server:RandomLocation')
TriggerClientEvent("ik-blackmarket:client:removeall", -1)
TriggerClientEvent('ik-blackmarket:client:CreatePed', -1)
minutes = Config.ChangeLocationTime
end
end
end)
end
QBCore.Functions.CreateCallback("ik-blackmarket:server:PedLocation", function (_, cb)
cb(location)
end)
QBCore.Functions.CreateCallback("ik-blackmarket:server:GetBMLocation", function (_, cb)
cb(location)
end)
-- ##### Functions ##### --
local function getMarkedBillWorth(source)
local Player = QBCore.Functions.GetPlayer(source)
local markedbilltotal = 0
for _, v in pairs(Player.PlayerData.items) do
if v.name == "markedbills" then
markedbilltotal = markedbilltotal + (v.info.worth * v.amount)
end
end
return markedbilltotal
end
local function payByMarkedBills(balance,source)
local Player = QBCore.Functions.GetPlayer(source)
info = {
worth = balance
}
for _, v in pairs(Player.PlayerData.items) do
if v.name == "markedbills" then
Player.Functions.RemoveItem("markedbills", v.amount)
end
end
Player.Functions.AddItem("markedbills", 1 , false ,info)
end
local function DeductAmount(bm, item, amount)
for _,v in pairs(BMItems) do
if v.bm == bm and v.itemname == item then
v.amount -= amount
end
end
end
local function GiveAndCheckItem(item,amount,weapon,price,balance,billtype,bm)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local BlackMoneyName = Config.BlackMoneyName
local newbalance = balance - price
if weapon then
for i = 1, tonumber(amount) do
if Player.Functions.AddItem(item, 1) then
if tonumber(i) == tonumber(amount) then
if Config.Payment == "blackmoney" and BlackMoneyName == "markedbills" and Config.UseDirtyMoney then
payByMarkedBills(newbalance,src)
elseif Config.Payment == "blackmoney" and BlackMoneyName ~= "markedbills" and Config.UseDirtyMoney then
Player.Functions.RemoveItem(BlackMoneyName, tonumber(price))
else
Player.Functions.RemoveMoney(tostring(billtype), tonumber(price), 'shop-payment')
end
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount)
end
if Config.Stock then DeductAmount(bm, item, amount) end
else
TriggerClientEvent('QBCore:Notify', src, Lang:t("error.cant_give"), "error") break
end
Wait(5)
end
else
if Player.Functions.AddItem(item, amount) then
if Config.Payment == "blackmoney" and BlackMoneyName == "markedbills" and Config.UseDirtyMoney then
payByMarkedBills(newbalance, src)
elseif Config.Payment == "blackmoney" and BlackMoneyName ~= "markedbills" and Config.UseDirtyMoney then
Player.Functions.RemoveItem(BlackMoneyName, tonumber(price))
else
Player.Functions.RemoveMoney(tostring(billtype), tonumber(price), 'shop-payment')
end
TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "add", amount)
if Config.Stock then DeductAmount(bm, item, amount) end
else
TriggerClientEvent('QBCore:Notify', src, Lang:t("error.cant_give"), "error")
end
end
end
local function GetTotalWeight(items)
local weight = 0
if not items then return 0 end
for _, item in pairs(items) do
weight += item.weight * item.amount
end
return tonumber(weight)
end
-- ##### Events ##### --
RegisterServerEvent('ik-blackmarket:GetItem', function(amount, billtype, item, shoptable, price, removeitem, bm)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local BlackMoneyName = Config.BlackMoneyName
local TotalPrice = tonumber(price) * tonumber(amount)
local totalweight = GetTotalWeight(Player.PlayerData.items)
local slots = 0
for _ in pairs(Player.PlayerData.items) do slots = slots +1 end
slots = Config.MaxSlots - slots
if (totalweight + (QBCore.Shared.Items[item].weight * amount)) > Config.MaxWeight then TriggerClientEvent("QBCore:Notify", src, "Not enough space in inventory", "error") return end
if QBCore.Shared.Items[item].unique and (tonumber(slots) < tonumber(amount)) then TriggerClientEvent("QBCore:Notify", src, "Not enough slots in inventory", "error") return end
if billtype == "blackmoney" then
if BlackMoneyName == "markedbills" then
Balance = getMarkedBillWorth(src)
else
Balance = Player.Functions.GetItemByName(BlackMoneyName).amount
end
elseif billtype == "crypto" then
Balance = Player.PlayerData.money.crypto
else
Balance = Player.Functions.GetMoney(tostring(billtype))
end
if Balance < TotalPrice then
TriggerClientEvent("QBCore:Notify", src, Lang:t("error.no_money"), "error") return
end
if QBCore.Shared.Items[item].type == "weapon" or QBCore.Shared.Items[item].unique then
GiveAndCheckItem(item,amount,true,TotalPrice,Balance,billtype,bm)
else
GiveAndCheckItem(item,amount,false,TotalPrice,Balance,billtype,bm)
end
local data = {}
data.products = shoptable
data.shoptable = shoptable
data.k = bm
custom = true
if removeitem == nil and not Config.RemoveItem then
TriggerClientEvent('ik-blackmarket:ShopMenu', src, data, custom)
-- Player.Functions.AddItem(Config.ItemName, 1)
end
end)
AddEventHandler('onResourceStart', function(resource)
for k, v in pairs(Config.Products) do
for i = 1, #v do
if not QBCore.Shared.Items[Config.Products[k][i].name] then
print("Config.Products['"..k.."'] can't find item: "..Config.Products[k][i].name)
end
BMItems[#BMItems+1] = {bm = k, itemname = Config.Products[k][i].name, amount = Config.Products[k][i].amount}
end
end
for k, v in pairs(Config.Locations) do
if v["products"] == nil then
print("Config.Locations['"..k.."'] can't find its product table")
end
if Config.RandomLocation then
local m = math.random(1, #v["coords"])
location = {bm = k, loc= m, data = v}
m = 0
end
end
end)
QBCore.Functions.CreateCallback('ik-blackmarket:server:GetItemAmount', function(source, cb, bm, item)
for _,v in pairs(BMItems) do
if v.bm == bm and v.itemname == item then
cb(v.amount)
return
end
end
cb(nil)
end)
RegisterNetEvent('ik-blackmarket:server:AddRemoveItem', function (action)
local Player = QBCore.Functions.GetPlayer(source)
if action == 'add' then
Player.Functions.AddItem(Config.ItemName, 1)
end
if action == 'remove' then
Player.Functions.RemoveItem(Config.ItemName, 1)
end
end)
RegisterNetEvent("ik-blackmarket:server:callCops", function(coords)
local alertData = {
title = "10-33 | Shop Robbery",
coords = {x = coords.x, y = coords.y, z = coords.z},
description = "Someone Is WireTapping Phonecalls!"
}
TriggerClientEvent("ik-blackmarket:client:wiretappingCall", -1, coords)
TriggerClientEvent("qb-phone:client:addPoliceAlert", -1, alertData)
end)