-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.lua
566 lines (495 loc) · 22 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
local ESX, PlayerData, territoryCollection, headerBlips, circleBlips, progress, lastTerritory, showUI = nil, {}, {}, {}, {}, 0, nil, false
CreateThread(function()
ESX = exports['es_extended']:getSharedObject()
PlayerData = ESX.GetPlayerData()
ESX.TriggerServerCallback('tomic_territories:getTerritories', function(serverData)
territoryCollection = serverData
end)
Wait(1000) -- Wait for the territories to load (in case you have a lot of them, it'd be clever to keep this here...)
createBlips()
end)
RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
PlayerData = xPlayer
end)
RegisterNetEvent('esx:setJob')
AddEventHandler('esx:setJob', function(job)
PlayerData.job = job
end)
local function checkInput(input, typeRequired, checkEmpty)
checkEmpty = checkEmpty or false
if input == nil then return false end
if typeRequired == nil then return false end
if typeRequired == 'any' then return true end
if typeRequired == 'number' then input = tonumber(input) end
if typeRequired == 'string' then input = tostring(input) end
if type(input) == 'table' then
for k, v in pairs(input) do
if type(v) ~= typeRequired or (checkEmpty and (v == '' or v == nil)) then return false end
end
else
if type(input) ~= typeRequired or (checkEmpty and (input == '' or input == nil)) then return false end
end
return true
end
function createBlips()
local renderBlips = (shared.gangOnlyBlips and PlayerData.job and shared.gangs[PlayerData.job.name]) or not shared.gangOnlyBlips
if not renderBlips then return end
for i = 1, #territoryCollection do
local currentTerritory = territoryCollection[i]
local circleBlip = AddBlipForCoord(currentTerritory.coords.x, currentTerritory.coords.y, currentTerritory.coords.z)
SetBlipSprite(circleBlip, 373)
SetBlipDisplay(circleBlip, 8)
SetBlipScale(circleBlip, 4.0)
SetBlipAlpha(circleBlip, 100)
SetBlipAsShortRange(circleBlip, true)
for k, v in pairs(shared.gangs) do
if currentTerritory.owner == k then
SetBlipColour(circleBlip, v.blipColour)
break
end
end
local headerBlip = AddBlipForCoord(currentTerritory.coords.x, currentTerritory.coords.y, currentTerritory.coords.z + 15)
SetBlipSprite(headerBlip, 310)
SetBlipDisplay(headerBlip, 4)
BeginTextCommandSetBlipName('STRING')
AddTextComponentString(string.format(translateMessage('territory_blip_occupied'), currentTerritory.name, currentTerritory.owner ~= 'noone' and currentTerritory.label or translateMessage("territory_blip_unoccupied")))
EndTextCommandSetBlipName(headerBlip)
SetBlipScale(headerBlip, 0.75)
SetBlipColour(headerBlip, 0)
SetBlipAlpha(headerBlip, 250)
SetBlipAsShortRange(headerBlip, true)
insert(circleBlips, circleBlip)
insert(headerBlips, headerBlip)
end
end
RegisterNetEvent('tomic_territories:updateTerritories')
AddEventHandler('tomic_territories:updateTerritories', function(serverData)
territoryCollection = serverData
for _, blip in pairs(circleBlips) do RemoveBlip(blip) end
for _, blip in pairs(headerBlips) do RemoveBlip(blip) end
circleBlips, headerBlips = {}, {}
Wait(1000) -- Wait for blips to disappear...
createBlips()
end)
RegisterNetEvent('tomic_territories:updateBlips')
AddEventHandler('tomic_territories:updateBlips', function(id, job)
while true do
Wait(1000)
if not territoryCollection[id].isTaking then break end
for k, p in pairs(shared.gangs) do
if territoryCollection[id].owner == k then
SetBlipColour(circleBlips[id], p.blipColour)
Wait(1000)
SetBlipColour(circleBlips[id], shared.gangs[job].blipColour)
end
end
end
end)
RegisterNetEvent('tomic_territories:createTerritory')
AddEventHandler('tomic_territories:createTerritory', function()
local input = lib.inputDialog(translateMessage('territory_create_input'), {
{ type = 'input', label = translateMessage('territory_create_name') },
{ type = 'input', label = translateMessage('territory_create_radius') },
{ type = 'select', label = translateMessage('territory_create_type'), options = {
{ value = 'market', label = translateMessage('territory_create_type_market') },
{ value = 'dealer', label = translateMessage('territory_create_type_dealer') },
{ value = 'default', label = translateMessage('territory_create_type_default') },
} },
})
if not input then
return ESX.ShowNotification(translateMessage('something_went_wrong'))
end
if not checkInput({ input[1], input[3] }, 'any', true) or not checkInput(input[2], 'number') then
return ESX.ShowNotification(translateMessage('fill_all_fields_out'))
end
TriggerServerEvent('tomic_territories:createTerritory', {
id = #territoryCollection + 1,
name = input[1],
type = input[3],
owner = 'noone',
label = 'NoOne',
radius = input[2],
isTaking = false,
progress = 0,
isCooldown = false,
coords = GetEntityCoords(PlayerPedId()),
})
end)
RegisterNetEvent('tomic_territories:deleteTerritory')
AddEventHandler('tomic_territories:deleteTerritory', function()
local input = lib.inputDialog(translateMessage('territory_delete_input'), {
{ type = 'input', label = translateMessage('territory_delete_input_name') },
})
if not input then
return ESX.ShowNotification(translateMessage('something_went_wrong'))
end
if not checkInput(input[1], 'any', true) then
return ESX.ShowNotification(translateMessage('fill_all_fields_out'))
end
TriggerServerEvent('tomic_territories:deleteTerritory', input[1])
end)
RegisterNetEvent('tomic_territories:updateUI')
AddEventHandler('tomic_territories:updateUI', function(action, data)
SendNUIMessage({action = action, data = data })
end)
local function isInTerritory(isDead)
local playerPed = PlayerPedId()
local playerCoords, checkDead = GetEntityCoords(playerPed), isDead
for k, v in pairs(territoryCollection) do
local territoryCoords = vec3(v.coords.x, v.coords.y, v.coords.z)
local playerTerritoryDistance = #(playerCoords - territoryCoords)
if playerTerritoryDistance < tonumber(v.radius) and v.isTaking then
lastTerritory = v.id
TriggerServerEvent('tomic_territories:updateAttenders', lastTerritory, PlayerData.identifier, PlayerData.job.name, true, checkDead)
return true, k
end
end
if lastTerritory then
TriggerServerEvent('tomic_territories:updateAttenders', lastTerritory, PlayerData.identifier, PlayerData.job.name, false, false)
lastTerritory = nil
end
return false
end
CreateThread(function()
Wait(2000)
if PlayerData.job and shared.gangs[PlayerData.job.name] then
while true do
Wait(5000)
isInTerritory(ESX.GetPlayerData().dead)
end
end
end)
CreateThread(function()
Wait(1000) -- Wait for everything to load before displaying the markers...
while true do
local playerCoords, sleepThread = GetEntityCoords(PlayerPedId()), 2000
for k, v in pairs(territoryCollection) do
local territoryCoords = vec3(v.coords.x, v.coords.y, v.coords.z)
local playerTerritoryDistance = #(playerCoords - territoryCoords)
if PlayerData.job and shared.gangs[PlayerData.job.name] then
if playerTerritoryDistance < tonumber(v.radius) then
sleepThread = 0
DrawMarker(2, territoryCoords.x, territoryCoords.y, territoryCoords.z, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.15, 0.15, 0.15, 200, 0, 50, 230, false, false, 0, true, nil, nil, false)
DrawMarker(1, territoryCoords.x, territoryCoords.y, territoryCoords.z, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.25, 0.25, 0.02, 255, 255, 255, 255, false, false, 0, true, nil, nil, false)
if playerTerritoryDistance < 1.5 then
if IsControlJustPressed(0, 38) then
TriggerEvent('tomic_territories:infoMenu', { id = v.id, job = PlayerData.job.name, label = PlayerData.job.label, name = v.name, currentOwner = v.owner, terCoords = vec3(v.coords.x, v.coords.y, v.coords.z), isTaking = v.isTaking, isCooldown = v.isCooldown, type = v.type })
end
if not showUI then
showUI = true
lib.showTextUI(string.format(translateMessage('territory_show_text'), v.name))
end
elseif playerTerritoryDistance > 2.0 and showUI then
showUI = false
lib.hideTextUI()
end
end
end
end
Wait(sleepThread)
end
end)
RegisterNetEvent('tomic_territories:infoMenu')
AddEventHandler('tomic_territories:infoMenu', function(terData)
local defaultContext = {
id = 'infoMenu' .. terData.id,
title = string.format(translateMessage('territory_info_menu'), terData.name),
options = {
{
title = translateMessage('territory_info_menu_capture'),
event = 'tomic_territories:captureClient',
args = {
currentTerritory = terData
}
},
{
title = translateMessage('territory_info_menu_stash'),
event = 'tomic_territories:openStash',
args = {
currentTerritory = terData
}
}
}
}
local terType = {
['dealer'] = {
title = translateMessage('territory_info_menu_sell'),
event = 'tomic_territories:sellList',
args = {
data = terData
}
},
['market'] = {
title = translateMessage('territory_info_menu_buy'),
event = 'tomic_territories:buyList',
args = {
data = terData
}
}
}
insert(defaultContext.options, terType[terData.type])
lib.registerContext(defaultContext)
lib.showContext(defaultContext.id)
end)
RegisterNetEvent('tomic_territories:letCount')
AddEventHandler('tomic_territories:letCount', function(data)
local input = lib.inputDialog(data.selected.name, { translateMessage('amount') })
local count = tonumber(input[1])
if not input then
return ESX.ShowNotification(translateMessage('something_went_wrong'))
end
if not checkInput(count, 'number', true) then
return ESX.ShowNotification(translateMessage('fill_all_fields_out'))
end
if count < 1 then
return ESX.ShowNotification(translateMessage('incorrect_amount'))
end
local itemObject = {
itemKey = data.selected.key,
itemName = data.selected.name,
itemCount = count,
itemWorth = data.selected.worth,
itemCurrency = data.selected.currency
}
TriggerServerEvent('tomic_territories:marketHandler', itemObject, data.selected.type)
end)
RegisterNetEvent('tomic_territories:sellList')
AddEventHandler('tomic_territories:sellList', function(args)
if PlayerData.job.name ~= args.data.currentOwner then
return ESX.ShowNotification(translateMessage('territory_not_owned'))
end
local itemList = {}
for k, v in pairs(shared.itemsToSell) do
local item, label, price, black = k, v.label, v.worth, v.black
itemList[item] = {
title = v.label,
description = string.format(translateMessage('territory_info_menu_buy_sell_price'), v.worth),
event = 'tomic_territories:letCount',
args = {
selected = {
name = label,
key = item,
worth = price,
currency = black,
type = 'sell'
},
}
}
end
lib.registerContext({
id = 'territorySellList',
title = translateMessage('territory_info_menu_sell_title'),
options = itemList,
menu = 'infoMenu' .. args.data.id,
})
lib.showContext('territorySellList')
end)
RegisterNetEvent('tomic_territories:buyList')
AddEventHandler('tomic_territories:buyList', function(terData)
local buyList, terData = {}, terData.data
if PlayerData.job.name ~= terData.currentOwner then
return ESX.ShowNotification(translateMessage('territory_not_owned'))
end
for k, v in pairs(shared.itemsToBuy) do
local item = k
local label = v.label
local price = v.worth
local black = v.black
buyList[item] = {
title = v.label,
description = string.format(translateMessage('territory_info_menu_buy_sell_price'), v.worth),
event = 'tomic_territories:letCount',
args = {
selected = {
name = label,
key = item,
worth = price,
currency = black,
type = 'buy'
},
}
}
end
lib.registerContext({
id = 'territoryBuyList',
title = translateMessage('territory_info_menu_buy_title'),
options = buyList,
menu = 'infoMenu' .. terData.id,
})
lib.showContext('territoryBuyList')
end)
RegisterNetEvent('tomic_territories:captureClient')
AddEventHandler('tomic_territories:captureClient', function(terData)
local currentTerritory = nil
for k, v in pairs(territoryCollection) do
if v.id == terData.currentTerritory.id then
currentTerritory = v
end
end
if PlayerData.job.name == currentTerritory.owner then
return ESX.ShowNotification(translateMessage('territory_already_owned'))
end
if currentTerritory.isTaking then
return ESX.ShowNotification(translateMessage('capture_in_progress'))
end
if currentTerritory.isCooldown then
return ESX.ShowNotification(translateMessage('territory_on_cooldown'))
end
TriggerServerEvent('tomic_territories:captureServer', currentTerritory.id, PlayerData.job.name, currentTerritory.name, currentTerritory.owner)
end)
RegisterNetEvent('tomic_territories:openStash')
AddEventHandler('tomic_territories:openStash', function(terData)
terData = terData.currentTerritory
local playerTerritoryDistance = #(terData.terCoords - GetEntityCoords(PlayerPedId()))
if PlayerData.job and PlayerData.job.name ~= terData.currentOwner then
return ESX.ShowNotification(translateMessage('territory_not_owned'))
end
if playerTerritoryDistance > 3.0 then
return ESX.ShowNotification(translateMessage('too_far_away'))
end
exports.ox_inventory:openInventory('stash', { id = 'devTomic-Ter[' .. terData.name .. '][' .. terData.id .. ']' })
end)
RegisterNetEvent('tomic_territories:progressBars')
AddEventHandler('tomic_territories:progressBars', function(progressType)
if progressType == 'start' then
exports.rprogress:Custom({ x = 0.5, y = 0.9, From = 0, To = 100, Duration = shared.capturing * 60000 + 500, ShowProgress = true, Radius = 40, Stroke = 3, Width = 300, Height = 40, Label = translateMessage('territory_capture_progress_bar'), LabelPosition = 'right', Color = 'rgba(255, 0, 0, 1.0)', BGColor = 'rgba(0, 0, 0, 0.4)', onComplete = function() exports.rprogress:Stop() end })
else
exports.rprogress:Stop()
end
end)
RegisterNetEvent('tomic_territories:captureProgress')
AddEventHandler('tomic_territories:captureProgress', function(terKey, terData)
local lastTerritory = terKey
while true do
Wait(0)
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
local territoryCoords = vec3(terData.coords.x, terData.coords.y, terData.coords.z)
local playerTerritoryDistance = #(playerCoords - territoryCoords)
local isDead = ESX.GetPlayerData().dead
if not terData.isTaking then
return
end
if playerTerritoryDistance > terData.radius then
TriggerEvent('tomic_territories:progressBars', 'stop')
ESX.ShowNotification(translateMessage('territory_cause_distance'))
progress = 0
TriggerServerEvent('tomic_territories:endCapturing', lastTerritory)
lastTerritory = nil
return
end
if isDead then
TriggerEvent('tomic_territories:progressBars', 'stop')
ESX.ShowNotification(translateMessage('territory_cause_death'))
progress = 0
TriggerServerEvent('tomic_territories:endCapturing', lastTerritory)
lastTerritory = nil
return
end
if progress >= 60 then
TriggerEvent('tomic_territories:progressBars', 'stop')
TriggerServerEvent('tomic_territories:captureComplete', terData.id, PlayerData.job.name, PlayerData.job.label, terData.owner)
progress = 0
ESX.ShowNotification(string.format(translateMessage('territory_captured'), terData.name))
return
end
TriggerEvent('tomic_territories:progressBars', 'start')
Wait(shared.capturing * 60000 / 60)
progress = progress + 1
end
end)
RegisterCommand(shared.playerCommand, function(source, args, rawCommand)
local homePage = {
id = 'homePage',
title = translateMessage('territory_menu_title'),
options = {
{
title = translateMessage('territory_list_title'),
event = 'tomic_territories:listTerritories',
metadata = {
translateMessage('territory_list_metadata')
}
},
{
title = 'Info | ❓',
metadata = {
'| Made by Tomić ✅'
}
}
}
}
if shared.rankings then
insert(homePage.options, {
title = translateMessage('territory_rankings_title'),
event = 'tomic_territories:listRankings',
metadata = {
translateMessage('territory_rankings_metadata')
}
})
end
lib.registerContext(homePage)
lib.showContext(homePage.id)
end, false)
RegisterNetEvent('tomic_territories:listTerritories')
AddEventHandler('tomic_territories:listTerritories', function()
local terListCollection = {}
local territoryStatuses = {
['isCooldown'] = nil,
['isTaking'] = nil
}
if territoryCollection == nil then return end
for i = 1, #territoryCollection, 1 do
local currentTerritory = territoryCollection[i]
territoryStatuses.isTaking = currentTerritory.isTaking and translateMessage('context_yes') or translateMessage('context_no')
territoryStatuses.isCooldown = currentTerritory.isCooldown and translateMessage('context_yes') or translateMessage('context_no')
terListCollection[i] = {
title = string.format(translateMessage('territory_list_territory_name'), currentTerritory.name),
description = string.format(translateMessage('territory_list_territory_owner'), currentTerritory.label),
metadata = {
string.format(translateMessage('territory_list_territory_capturing'), territoryStatuses.isTaking),
string.format(translateMessage('territory_list_territory_cooldown'), territoryStatuses.isCooldown)
},
}
end
lib.registerContext({
id = 'listTerritories',
title = translateMessage('territory_menu_context_title'),
menu = 'homePage',
options = terListCollection,
})
lib.showContext('listTerritories')
end)
if shared.rankings then
RegisterNetEvent('tomic_territories:listRankings')
AddEventHandler('tomic_territories:listRankings', function()
if territoryCollection == nil then return end
ESX.TriggerServerCallback('tomic_territories:fetchPoints', function(pointsCollection)
local rankCollection = {}
for i = 1, #pointsCollection, 1 do
table.sort(pointsCollection, function(a, b) return a.totalPoints > b.totalPoints end)
rankCollection[i] = {
title = string.format(translateMessage('territory_rankings_gang'), pointsCollection[i].label),
description = string.format(translateMessage('territory_rankings_position'), i),
metadata = {
string.format(translateMessage('territory_rankings_all_time'), pointsCollection[i].totalPoints),
string.format(translateMessage('territory_rankings_monthly'), pointsCollection[i].monthlyPoints),
string.format(translateMessage('territory_rankings_weekly'), pointsCollection[i].weeklyPoints)
}
}
end
lib.registerContext({
id = 'listRankings',
menu = 'homePage',
title = translateMessage('territory_rankings_menu_context_title'),
options = rankCollection,
})
lib.showContext('listRankings')
end)
end)
end
AddEventHandler('onResourceStop', function(resourceName)
if resourceName ~= GetCurrentResourceName() then return end
TriggerEvent('tomic_territories:progressBars', 'stop')
progress = 0
end)