-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.lua
277 lines (237 loc) · 10.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
discord = {
['webhook'] = '', ---PUT YOUR WEBHOOK URL HERE
['name'] = 'Storage Units'
}
----Gets ESX-----
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterNetEvent('koe_storageunitsv2:checkUnit')
AddEventHandler('koe_storageunitsv2:checkUnit', function(storageID, balance)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local identifier = ESX.GetPlayerFromId(src).identifier
MySQL.Async.fetchAll('SELECT * FROM storageunits WHERE id = @id',
{
['@identifier'] = owner,
['@id'] = storageID,
['@balance'] = balance
},
function(result)
local balance = result[1].balance
if result[1].identifier == nil then
TriggerClientEvent('koe_storageunitsv2:buyMenu', src, storageID, balance)
elseif result[1].identifier == identifier then
TriggerClientEvent('koe_storageunitsv2:ownerMenu', src, storageID, balance)
else
TriggerClientEvent('koe_storageunitsv2:otherMenu', src, storageID, balance)
end
end)
end)
RegisterNetEvent('koe_storageunitsv2:addTimeToDb')
AddEventHandler('koe_storageunitsv2:addTimeToDb', function(storageID)
local time = os.time()
MySQL.Async.fetchAll("UPDATE storageunits SET time = @time WHERE id =@id",{['@time'] = time, ['@id'] = storageID}, function(result)
end)
end)
RegisterNetEvent('koe_storageunitsv2:checkTime')
AddEventHandler('koe_storageunitsv2:checkTime', function(storageID, time)
local currentTime = os.time()
for k, v in pairs (Config.Storages) do
MySQL.Async.fetchAll('SELECT * FROM storageunits WHERE id = @id',{['@id'] = v.id}, function(result)
for _ , t in pairs(result) do
local currentBalance = t.balance
local savedTime = t.time
local timeCheck = savedTime - currentTime
local rentTime = -604800 --(-604800 = 1 week in seconds)--------------------------
--Checks the balance of the unit, if after 1 week has passed then it will remove the amount defined in the Config (Config.RentPrice)
if currentBalance >= Config.RentPrice then
if timeCheck <= rentTime then
local newBalance = t.balance - Config.RentPrice
MySQL.Async.fetchAll("UPDATE storageunits SET balance = @balance WHERE id =@id",{['@balance'] = newBalance, ['@id'] = v.id}, function(result)
end)
TriggerEvent('koe_storageunitsv2:addTimeToDb',storageID)
end
end
--If the balance of the unit is 0 or less it will set the balance to 0 and remove the owner of the unit
if currentBalance <= 0 and timeCheck <= rentTime then
MySQL.Async.fetchAll("UPDATE storageunits SET balance = @balance WHERE id =@id",{['@balance'] = 0, ['@id'] = v.id}, function(result)
end)
MySQL.Async.fetchAll("UPDATE storageunits SET identifier = @identifier WHERE id =@id",{['@identifier'] = nil, ['@id'] = v.id}, function(result)
end)
MySQL.Async.fetchAll("UPDATE storageunits SET time = @time WHERE id =@id",{['@time'] = 0, ['@id'] = v.id}, function(result)
end)
end
end
end)
end
end)
ESX.RegisterServerCallback('koe_storageunitsv2:checkPin', function(source, cb, storageID, pinnum)
MySQL.Async.fetchAll("SELECT pin FROM storageunits WHERE id = @id",{
["@id"] = storageID,
},
function(result)
if result[1].pin == pinnum then
cb(true)
else
cb(false)
end
end)
end)
RegisterNetEvent('koe_storageunitsv2:pinChange')
AddEventHandler('koe_storageunitsv2:pinChange', function(storageID, newpin)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local identifier = ESX.GetPlayerFromId(src).identifier
MySQL.Async.fetchAll("UPDATE storageunits SET pin = @pin WHERE id =@id",{['@pin'] = newpin, ['@id'] = storageID}, function(result)
end)
end)
RegisterNetEvent('koe_storageunitsv2:addRentBalance')
AddEventHandler('koe_storageunitsv2:addRentBalance', function(rent, balance, storageID)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local newRent = rent + balance
if xPlayer.getMoney() >= tonumber(rent) then
MySQL.Async.fetchAll("UPDATE storageunits SET balance = @balance WHERE id =@id",{['@balance'] = newRent, ['@id'] = storageID}, function(result)
xPlayer.removeMoney(rent)
if Config.Notify == 'ox_lib' then
TriggerClientEvent('ox_lib:notify', source, {type = 'success', description = "You've added some money to your rent balance", duration = 8000, position = 'top'})
end
if Config.Notify == 'swt' then
TriggerClientEvent("swt_notifications:default",src,'Youve added some money to your rent balance','top',8000)
end
if Config.Notify == 'okok' then
TriggerClientEvent('okokNotify:Alert', src, "Storage Units", "You've added some money to your rent balance", 8000, 'success')
end
if Config.Notify == 'esx' then
TriggerClientEvent('esx:showNotification', src, 'Youve added some money to your rent balance')
end
if Config.Notify == 'custom' then
--Enter custom code here
end
end)
else
if Config.Notify == 'ox_lib' then
TriggerClientEvent('ox_lib:notify', source, {type = 'error', description = "Not enough cash", duration = 8000, position = 'top'})
end
if Config.Notify == 'swt' then
TriggerClientEvent("swt_notifications:default",src,'Not enough cash','top',8000)
end
if Config.Notify == 'okok' then
TriggerClientEvent('okokNotify:Alert', src, "Storage Units", "Not enough cash", 8000, 'error')
end
if Config.Notify == 'esx' then
TriggerClientEvent('esx:showNotification', src, 'Not enough cash')
end
if Config.Notify == 'custom' then
--Enter custom code here
end
end
end)
RegisterNetEvent('koe_storageunitsv2:removeRentBalance')
AddEventHandler('koe_storageunitsv2:removeRentBalance', function(amount, balance, storageID)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local adjustedAmount = balance - amount
if balance >= amount then
MySQL.Async.fetchAll("UPDATE storageunits SET balance = @balance WHERE id =@id",{['@balance'] = adjustedAmount, ['@id'] = storageID}, function(result)
xPlayer.addMoney(amount)
if Config.Notify == 'ox_lib' then
TriggerClientEvent('ox_lib:notify', source, {type = 'success', description = "You took out $", duration = 8000, position = 'top'})
end
if Config.Notify == 'swt' then
TriggerClientEvent("swt_notifications:default",src,'you took out $' ..amount,'top',8000)
end
if Config.Notify == 'okok' then
TriggerClientEvent('okokNotify:Alert', src, "Storage Units", 'you took out $' ..amount , 8000, 'success')
end
if Config.Notify == 'esx' then
TriggerClientEvent('esx:showNotification', src, 'you took out $' ..amount)
end
if Config.Notify == 'custom' then
--Enter custom code here
end
end)
else
if Config.Notify == 'ox_lib' then
TriggerClientEvent('ox_lib:notify', source, {type = 'error', description = "Not enough in account.", duration = 8000, position = 'top'})
end
if Config.Notify == 'swt' then
TriggerClientEvent("swt_notifications:default",src,'Not enough in account','top',8000)
end
if Config.Notify == 'okok' then
TriggerClientEvent('okokNotify:Alert', src, "Storage Units", "Not enough in account", 8000, 'error')
end
if Config.Notify == 'esx' then
TriggerClientEvent('esx:showNotification', src, 'Not enough in account')
end
if Config.Notify == 'custom' then
--Enter custom code here
end
end
end)
RegisterNetEvent('koe_storageunitsv2:buyUnit')
AddEventHandler('koe_storageunitsv2:buyUnit', function(storageID,pin)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local identifier = ESX.GetPlayerFromId(src).identifier
MySQL.Async.fetchAll('SELECT * FROM storageunits WHERE id = @id',
{
['@identifier'] = owner,
['@id'] = storageID
},
function(result2)
if xPlayer.getMoney() >= Config.UnitPrice then
MySQL.Async.fetchAll("UPDATE storageunits SET identifier = @identifier WHERE id =@id",{['@identifier'] = identifier, ['@id'] = storageID}, function(result)
xPlayer.removeMoney(Config.UnitPrice)
end)
MySQL.Async.fetchAll("UPDATE storageunits SET pin = @pin WHERE id =@id",{['@pin'] = pin, ['@id'] = storageID}, function(result)
end)
end
end)
end)
RegisterNetEvent('koe_storageunitsv2:sellUnit')
AddEventHandler('koe_storageunitsv2:sellUnit', function(storageID)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
MySQL.Async.fetchAll('SELECT * FROM storageunits WHERE id = @id',
{
['@identifier'] = owner,
['@id'] = storageID
},
function(result2)
MySQL.Async.fetchAll("UPDATE storageunits SET identifier = @identifier, pin = @pin, time = @time WHERE id =@id",{['@identifier'] = identifier, ['@id'] = storageID, ['@pin'] = pin, ['@time'] = 0}, function(result)
xPlayer.addMoney(Config.SellPrice)
end)
end)
end)
RegisterNetEvent('koe_storageunitsv2:registerStash')
AddEventHandler('koe_storageunitsv2:registerStash', function(storageID)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
local identifier = ESX.GetPlayerFromId(src).identifier
MySQL.Async.fetchAll('SELECT id FROM storageunits WHERE id = @id',
{
['@id'] = storageID
},
function(result3)
stashID = json.encode(result3)
exports.ox_inventory:RegisterStash(stashID, "Storage Unit", Config.Slots, Config.Weight)
TriggerClientEvent('koe_storageunitsv2:openStash', src, stashID)
end)
end)
RegisterNetEvent('koe_storageunitsv2:breachLog')
AddEventHandler('koe_storageunitsv2:breachLog', function(storageID)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
discordLog(xPlayer.getName() .. ' - ' .. xPlayer.getIdentifier(), 'has breached the unit #' ..storageID)
end)
function discordLog(name, message)
local data = {
{
["color"] = '3553600',
["title"] = "**".. name .."**",
["description"] = message,
}
}
PerformHttpRequest(discord['webhook'], function(err, text, headers) end, 'POST', json.encode({username = discord['name'], embeds = data}), { ['Content-Type'] = 'application/json' })
end