forked from kontarkantor/villamos_vehshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
402 lines (369 loc) · 13.5 KB
/
client.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
local openedShop = false
local selling = false
local testing = false
local shops = {}
local blips = {}
CreateThread(function()
while not ESX or not ESX.PlayerData or not ESX.PlayerData.job do
Wait(10)
end
TriggerEvent('chat:addSuggestion', '/vsadd', _U("command_vsadd"), {
{ name="shop", help=_U("command_shop") },
{ name="model", help=_U("command_model") },
{ name="price", help=_U("command_price") },
{ name="category", help=_U("command_category") },
{ name="name", help=_U("command_name") },
})
TriggerEvent('chat:addSuggestion', '/vsdel', _U("command_vsdel"), {
{ name="shop", help=_U("command_shop") },
{ name="model", help=_U("command_model") }
})
TriggerEvent('chat:addSuggestion', '/vsphoto', _U("command_vsphoto"), {
{ name="shop", help=_U("command_shop") }
})
TriggerEvent('chat:addSuggestion', '/vsrefresh', _U("command_vsrefresh"), {})
TriggerEvent('chat:addSuggestion', '/vsget', _U("command_vsget"), {})
AddTextEntry('vehshop_sell_msg', _U("sell_msg"))
RefreshShops()
while true do
local sleep = 1000
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
if not openedShop and not selling then
for shop, data in pairs(shops) do
local dis = #(coords - data.coords)
if dis < 20 then
sleep = 1
DrawMarker(6, data.coords, 0.0, 0.0, 0.0, -90.0, 0.0, 0.0, 2.0, 2.0, 2.0, 0, 155, 20, 100, false, true, 2, false, false, false, false)
DrawMarker((data.marker and data.marker or 36), data.coords+vector3(0.0, 0.0, 0.6), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0, 155, 20, 100, false, true, 2, false, false, false, false)
if dis < 2.0 then
AddTextEntry('vehshop_open_msg', _U("open_msg", data.label))
DisplayHelpTextThisFrame('vehshop_open_msg')
if IsControlJustReleased(0, 38) then
OpenShop(shop)
end
end
end
if data.sellcoords and IsPedInAnyVehicle(ped) then
local sdis = #(coords - data.sellcoords)
if sdis < 20 then
sleep = 1
DrawMarker(6, data.sellcoords, 0.0, 0.0, 0.0, -90.0, 0.0, 0.0, 3.0, 3.0, 3.0, 204, 35, 40, 100, false, true, 2, false, false, false, false)
DrawMarker((data.marker and data.marker or 36), data.sellcoords+vector3(0.0, 0.0, 0.6), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 204, 35, 40, 100, false, true, 2, false, false, false, false)
if sdis < 2.0 then
DisplayHelpTextThisFrame('vehshop_sell_msg')
if IsControlJustReleased(0, 38) then
TriggerServerEvent("villamos_vehshop:sellVehicle", shop)
Wait(2000)
end
end
end
end
end
end
Wait(sleep)
end
end)
RegisterNetEvent("esx:setJob", function()
RefreshShops()
end)
function RefreshShops()
for _, blip in pairs(blips) do
RemoveBlip(blip)
end
blips = {}
shops = {}
for shop, data in pairs(Config.Shops) do
if HaveJob(data.job) then
shops[shop] = { coords = data.coords, label = data.label, sellcoords = (data.sell and data.sell.coords or false), marker = data.marker }
if data.blip then
local blip = AddBlipForCoord(data.coords)
SetBlipSprite(blip, data.blip.sprite)
SetBlipScale(blip, 1.0)
SetBlipColour(blip, data.blip.color)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(data.label)
EndTextCommandSetBlipName(blip)
blips[#blips+1] = blip
end
end
end
end
function HaveJob(jobobj)
if not jobobj then return true end
if not jobobj[ESX.PlayerData.job.name] then return false end
for i=1, #jobobj[ESX.PlayerData.job.name], 1 do
if jobobj[ESX.PlayerData.job.name][i] == ESX.PlayerData.job.grade_name then
return true
end
end
return false
end
function OpenShop(shop)
if not Config.Shops[shop] then return end
openedShop = shop
ESX.TriggerServerCallback("villamos_vehshop:openShop", function(cars, money)
if not cars then
openedShop = false
return
end
money.test = Config.Shops[shop].testcoords and true or false
SetNuiFocus(true, true)
SendNUIMessage({
type = "show",
enable = true,
shopdata = money,
cars = cars,
name = Config.Shops[shop].label
})
end, shop)
end
function CloseShop()
SetNuiFocus(false, false)
openedShop = false
SendNUIMessage({
type = "show",
enable = false
})
end
RegisterNUICallback('locales', function(data, cb)
local nuilocales = {}
if not Config.Locale or not Locales[Config.Locale] then return print("^1SCRIPT ERROR: Invilaid locales configuartion") end
for k, v in pairs(Locales[Config.Locale]) do
if string.find(k, "nui") then
nuilocales[k] = v
end
end
cb(nuilocales)
end)
RegisterNUICallback('exit', function(data, cb)
CloseShop()
cb(1)
end)
RegisterNUICallback('buy', function(data, cb)
if not openedShop then
CloseShop()
return cb(1)
end
TriggerServerEvent('villamos_vehshop:buyVehicle', openedShop, data.model, 'money')
CloseShop()
cb(1)
end)
RegisterNUICallback('buybank', function(data, cb)
if not openedShop then
CloseShop()
return cb(1)
end
TriggerServerEvent('villamos_vehshop:buyVehicle', openedShop, data.model, 'bank')
CloseShop()
cb(1)
end)
RegisterNUICallback('buyfaction', function(data, cb)
if not openedShop then
CloseShop()
return cb(1)
end
TriggerServerEvent('villamos_vehshop:buyVehicleFaction', openedShop, data.model)
CloseShop()
cb(1)
end)
RegisterNUICallback('test', function(data, cb)
if not openedShop then
CloseShop()
return cb(1)
end
local testcoords = Config.Shops[openedShop].testcoords
if not testcoords then
return cb(1)
end
local testtime = Config.Shops[openedShop].testtime
local shopcoords = Config.Shops[openedShop].coords
CloseShop()
cb(1)
local hash = GetHashKey(data.model)
if not IsModelInCdimage(hash) then
return print("^1SCRIPT ERROR: Invalid model: "..data.model)
end
while not HasModelLoaded(hash) do
RequestModel(hash)
Wait(10)
end
local vehicle = CreateVehicle(hash, testcoords, false, true)
TaskWarpPedIntoVehicle(PlayerPedId(), vehicle, -1)
SetModelAsNoLongerNeeded(hash)
local start = GetGameTimer()
testing = true
CreateThread(function()
while testing do
Wait(0)
local rem = testtime - (GetGameTimer() - start)
if rem <= 0 or GetVehiclePedIsIn(PlayerPedId(), false) ~= vehicle then
testing = false
end
SetTextFont(4)
SetTextScale(0.5, 0.5)
SetTextColour(255, 255, 255, 255)
SetTextCentre(1)
BeginTextCommandDisplayText("STRING")
AddTextComponentString(_U("test_msg", math.floor(rem/1000)))
EndTextCommandDisplayText(0.5, 0.9)
end
DeleteVehicle(vehicle)
SetEntityCoords(PlayerPedId(), shopcoords, false, false, false, false)
end)
end)
RegisterNetEvent('villamos_vehshop:spawnCar', function(coords, model, plate)
local hash = GetHashKey(model)
if not IsModelInCdimage(hash) then
return print("^1SCRIPT ERROR: Invalid model: "..model)
end
while not HasModelLoaded(hash) do
RequestModel(hash)
Wait(10)
end
local vehicle = CreateVehicle(hash, coords, true, true)
SetVehicleNumberPlateText(vehicle, plate)
TaskWarpPedIntoVehicle(PlayerPedId(), vehicle, -1)
SetModelAsNoLongerNeeded(hash)
end)
RegisterCommand("vsget", function(s, a, r)
local coords = GetEntityCoords(PlayerPedId())
local closestshop, closestdis = false, 20
for shop, data in pairs(Config.Shops) do
local dis = #(coords - data.coords)
if dis < closestdis then
closestshop = shop
closestdis = dis
end
end
if not closestshop then
return Config.Notify(_U("no_shop_near"))
end
Config.Notify(_U("closest_shop", closestshop))
end)
RegisterNetEvent("villamos_vehshop:takePhotos", function(shop, api, cars)
Config.Notify(_U("taking_photos"))
DisplayHud(false)
DisplayRadar(false)
FreezeEntityPosition(PlayerPedId(), true)
if not api then
return print("^1SCRIPT ERROR: Invalid API")
end
local uploadurl
if api.service == "fivemanage" then
uploadurl = "https://api.fivemanage.com/api/image?apiKey="..api.key
elseif api.service == "imgbb" then
uploadurl = "https://api.imgbb.com/1/upload?key="..api.key
else
return print("^1SCRIPT ERROR: Invalid API")
end
local cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
SetCamCoord(cam, Config.Shops[shop].showroomcam)
SetCamActive(cam, true)
RenderScriptCams(true, false, 0, true, false)
for i=1, #cars, 1 do
local model = cars[i].model
local hash = GetHashKey(model)
if IsModelInCdimage(hash) and IsModelValid(hash) then
if not HasModelLoaded(hash) then
RequestModel(hash)
while not HasModelLoaded(hash) do
Wait(0)
end
end
local vehicle = CreateVehicle(hash, Config.Shops[shop].showroom, false, true)
SetModelAsNoLongerNeeded(hash)
FreezeEntityPosition(vehicle, true)
PointCamAtEntity(cam, vehicle, 0.0, 0.0, 0.0, true)
SetFocusEntity(vehicle)
local p = promise.new()
Wait(500)
exports['screenshot-basic']:requestScreenshotUpload(uploadurl, "image", {
encoding = "jpg"
}, function(data)
if not data then
print("^1SCRIPT ERROR: Error while uploading image")
return p:resolve(false)
end
local resp = json.decode(data)
if api.service == "fivemanage" then
if not resp or not resp.url then
print("^1SCRIPT ERROR: Error while uploading image")
return p:resolve(false)
end
p:resolve(resp.url)
elseif api.service == "imgbb" then
if not resp or not resp.data then
print("^1SCRIPT ERROR: Error while uploading image")
return p:resolve(false)
end
local img = resp.data.url
if not img then
print("^1SCRIPT ERROR: Error while uploading image")
return p:resolve(false)
end
p:resolve(img)
end
end)
local image = Citizen.Await(p)
if image then
TriggerServerEvent("villamos_vehshop:savePhoto", shop, model, image)
end
DeleteEntity(vehicle)
SetModelAsNoLongerNeeded(hash)
else
print("^1SCRIPT ERROR: Invalid model: "..model)
end
end
Wait(2000)
ClearFocus()
DisplayHud(true)
DisplayRadar(true)
FreezeEntityPosition(PlayerPedId(), false)
RenderScriptCams(false)
DestroyCam(cam, true)
SetCamActive(cam, false)
Config.Notify(_U("photos_done"))
TriggerServerEvent("villamos_vehshop:refresh")
end)
ESX.RegisterClientCallback("villamos_vehshop:confirmSell", function(cb, plate, price)
selling = true
local eles = {
{
unselectable = true,
icon = "fas fa-info-circle",
title = _U("confirm_sell", plate, price),
},
{
icon = "fas fa-check",
title = _U("yes"),
name = "yes"
},
{
icon = "fas fa-times",
title = _U("no"),
name = "no"
},
}
ESX.OpenContext("right", eles, function(menu, ele)
if ele and ele.name and ele.name == "yes" then
cb(true)
else
cb(false)
end
ESX.CloseContext()
selling = false
end, function(menu)
cb(false)
selling = false
end)
end)
exports("GeneratePlate", function()
local p = promise.new()
ESX.TriggerServerCallback('villamos_vehshop:GeneratePlate', function(plate)
p:resolve(plate)
end)
local plate = Citizen.Await(p)
return plate
end)