Repository navigation
Expand file tree
/
Copy pathclient.lua
More file actions
515 lines (441 loc) · 14.3 KB
/
Copy pathclient.lua
File metadata and controls
515 lines (441 loc) · 14.3 KB
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
local identifier = "utune_music2"
CreateThread(function()
while GetResourceState("lb-phone") ~= "started" do
Wait(500)
end
local function AddApp()
local added, errorMessage = exports["lb-phone"]:AddCustomApp({
identifier = identifier,
name = "uTune Music",
description = "Play your favorite music",
developer = "RK Development",
defaultApp = false,
size = 59812,
images = {},
ui = GetCurrentResourceName() .. "/ui/index.html",
icon = "https://cfx-nui-" .. GetCurrentResourceName() .. "/ui/assets/icon.png"
})
if not added then
print("Could not add app:", errorMessage)
end
end
AddApp()
AddEventHandler("onResourceStart", function(resource)
if resource == "lb-phone" then
AddApp()
end
end)
end)
local xSound = exports.xsound
local playing = false
local volume = Config.DEFAULT_VOLUME
local utuneUrl = nil
local myMusicId = "phone_utuneemusic_id_" .. GetPlayerServerId(PlayerId())
local playlist = {}
local currentPlaylistIndex = 0
local isPaused = false
local function loadPlaylist()
local savedPlaylist = GetResourceKvpString("utune_playlist")
if savedPlaylist then
playlist = json.decode(savedPlaylist) or {}
else
playlist = {}
end
end
local function savePlaylist()
SetResourceKvp("utune_playlist", json.encode(playlist))
end
loadPlaylist()
RegisterNUICallback("playSound", function(data, cb)
local coords = GetEntityCoords(PlayerPedId())
utuneUrl = data.url
playing = true
TriggerServerEvent("phone:utune_music:soundStatus", "play", {
position = coords,
link = utuneUrl,
volume = volume
})
CreateThread(function()
Wait(500)
local pos = GetEntityCoords(PlayerPedId())
TriggerServerEvent("phone:utune_music:soundStatus", "position", {
position = pos
})
Wait(1000)
local pos2 = GetEntityCoords(PlayerPedId())
TriggerServerEvent("phone:utune_music:soundStatus", "position", {
position = pos2
})
end)
cb({})
end)
RegisterNUICallback("stopSound", function(_, cb)
playing = false
isPaused = false
currentPlaylistIndex = 0
TriggerServerEvent("phone:utune_music:soundStatus", "stop", {})
cb({})
end)
RegisterNUICallback("changeVolume", function(data, cb)
volume = data.volume
TriggerServerEvent("phone:utune_music:soundStatus", "volume", {
volume = volume
})
cb({})
end)
RegisterNUICallback("getData", function(_, cb)
cb({
isPlay = playing,
volume = volume,
utuneUrl = utuneUrl,
playlist = playlist,
currentPlaylistIndex = currentPlaylistIndex
})
end)
RegisterNUICallback("addToPlaylist", function(data, cb)
table.insert(playlist, {
url = data.url,
title = data.title,
thumbnail = data.thumbnail
})
savePlaylist()
cb({ success = true, playlist = playlist })
end)
RegisterNUICallback("removeFromPlaylist", function(data, cb)
table.remove(playlist, data.index)
savePlaylist()
cb({ success = true, playlist = playlist })
end)
RegisterNUICallback("playFromPlaylist", function(data, cb)
if playlist[data.index] then
currentPlaylistIndex = data.index
local song = playlist[data.index]
local coords = GetEntityCoords(PlayerPedId())
utuneUrl = song.url
playing = true
TriggerServerEvent("phone:utune_music:soundStatus", "play", {
position = coords,
link = utuneUrl,
volume = volume
})
CreateThread(function()
Wait(500)
local pos = GetEntityCoords(PlayerPedId())
TriggerServerEvent("phone:utune_music:soundStatus", "position", {
position = pos
})
Wait(1000)
local pos2 = GetEntityCoords(PlayerPedId())
TriggerServerEvent("phone:utune_music:soundStatus", "position", {
position = pos2
})
end)
end
cb({})
end)
RegisterNUICallback("getPlaylist", function(_, cb)
cb({ playlist = playlist })
end)
RegisterNUICallback("playNext", function(_, cb)
if #playlist > 0 then
currentPlaylistIndex = currentPlaylistIndex + 1
if currentPlaylistIndex > #playlist then
currentPlaylistIndex = 1
end
local song = playlist[currentPlaylistIndex]
if song then
local coords = GetEntityCoords(PlayerPedId())
utuneUrl = song.url
playing = true
TriggerServerEvent("phone:utune_music:soundStatus", "play", {
position = coords,
link = utuneUrl,
volume = volume
})
end
end
cb({})
end)
RegisterNUICallback("playPrevious", function(_, cb)
if #playlist > 0 then
currentPlaylistIndex = currentPlaylistIndex - 1
if currentPlaylistIndex < 1 then
currentPlaylistIndex = #playlist
end
local song = playlist[currentPlaylistIndex]
if song then
local coords = GetEntityCoords(PlayerPedId())
utuneUrl = song.url
playing = true
TriggerServerEvent("phone:utune_music:soundStatus", "play", {
position = coords,
link = utuneUrl,
volume = volume
})
end
end
cb({})
end)
local function getSpeed(ped)
local vel = GetEntityVelocity(ped)
return #(vector3(vel.x, vel.y, vel.z)) * 2.23694
end
local lastTier = 0
local tierExpireTime = 0
local lastPosition = vector3(0, 0, 0)
local tiers = Config.UPDATE_TIERS
CreateThread(function()
Wait(1000)
while true do
if playing then
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
local speed = getSpeed(ped)
local currentTime = GetGameTimer()
local selectedWait = 1000
local selectedTier = 0
for _, tier in ipairs(tiers) do
if speed >= tier.speed then
selectedWait = tier.wait
selectedTier = tier.level
break
end
end
if selectedTier > lastTier then
lastTier = selectedTier
tierExpireTime = currentTime + Config.TIER_STICKY_TIME
elseif currentTime < tierExpireTime then
for _, tier in ipairs(tiers) do
if tier.level == lastTier then
selectedWait = tier.wait
break
end
end
else
lastTier = selectedTier
end
local distanceMoved = #(coords - lastPosition)
local shouldUpdate = false
if selectedTier >= 3 then
shouldUpdate = true
elseif distanceMoved > Config.MIN_DISTANCE_THRESHOLD then
shouldUpdate = true
elseif selectedWait >= 1000 and distanceMoved > 0.5 then
shouldUpdate = true
end
if shouldUpdate then
TriggerServerEvent("phone:utune_music:soundStatus", "position", {
position = coords
})
lastPosition = coords
end
Wait(selectedWait)
else
Wait(1000)
end
end
end)
RegisterNetEvent("phone:utune_music:soundStatus", function(type, musicId, data)
if type == "position" and data.position then
local myPed = PlayerPedId()
local myCoords = GetEntityCoords(myPed)
local distance = #(myCoords - vector3(data.position.x, data.position.y, data.position.z))
if distance > Config.MUSIC_RANGE then
if xSound:soundExists(musicId) then
xSound:Destroy(musicId)
end
return
end
if not xSound:soundExists(musicId) then
local playerId = tonumber(string.match(musicId, "(%d+)$"))
if playerId then
TriggerServerEvent("phone:utune_music:requestSync", playerId)
end
end
end
if type == "play" then
if data.position then
local myPed = PlayerPedId()
local myCoords = GetEntityCoords(myPed)
local distance = #(myCoords - vector3(data.position.x, data.position.y, data.position.z))
if distance > Config.MUSIC_RANGE then
return
end
end
xSound:PlayUrlPos(musicId, data.link, 1.0, data.position)
xSound:setSoundDynamic(musicId, true)
xSound:Distance(musicId, Config.XSOUND_DISTANCE)
xSound:destroyOnFinish(musicId, true)
local vol = data.volume or volume or Config.DEFAULT_VOLUME
xSound:setVolumeMax(musicId, vol / 100)
if data.elapsedTime and data.elapsedTime > 0 then
CreateThread(function()
Wait(1500)
local attempts = 0
while attempts < 3 do
if xSound:soundExists(musicId) then
local success = pcall(function()
xSound:setTimeStamp(musicId, data.elapsedTime)
end)
if success then
break
end
end
attempts = attempts + 1
Wait(500)
end
end)
end
elseif type == "stop" then
if xSound:soundExists(musicId) then
xSound:Destroy(musicId)
end
elseif type == "volume" then
if xSound:soundExists(musicId) then
xSound:setVolumeMax(musicId, data.volume / 100)
end
elseif type == "position" then
if xSound:soundExists(musicId) then
xSound:Position(musicId, data.position)
end
elseif type == "pause" then
if xSound:soundExists(musicId) then
xSound:Pause(musicId)
end
elseif type == "resume" then
if xSound:soundExists(musicId) then
xSound:Resume(musicId)
end
end
end)
RegisterNetEvent("phone:utune_music:syncPlay", function(musicId, syncData)
local myPed = PlayerPedId()
local myCoords = GetEntityCoords(myPed)
local distance = #(myCoords - vector3(syncData.position.x, syncData.position.y, syncData.position.z))
if distance > Config.MUSIC_RANGE then
return
end
if xSound:soundExists(musicId) then
xSound:Destroy(musicId)
end
xSound:PlayUrlPos(musicId, syncData.link, 1.0, syncData.position)
xSound:setSoundDynamic(musicId, true)
xSound:Distance(musicId, Config.XSOUND_DISTANCE)
xSound:destroyOnFinish(musicId, true)
local vol = syncData.volume or Config.DEFAULT_VOLUME
xSound:setVolumeMax(musicId, vol / 100)
CreateThread(function()
Wait(1500)
local attempts = 0
while attempts < 3 do
if xSound:soundExists(musicId) then
local success = pcall(function()
xSound:setTimeStamp(musicId, syncData.elapsedTime)
end)
if success then
if syncData.isPaused then
xSound:Pause(musicId)
end
break
end
end
attempts = attempts + 1
Wait(500)
end
end)
end)
local syncAttempts = {}
CreateThread(function()
while true do
Wait(5000)
local myPed = PlayerPedId()
local myCoords = GetEntityCoords(myPed)
local players = GetActivePlayers()
for _, player in ipairs(players) do
if player ~= PlayerId() then
local ped = GetPlayerPed(player)
local coords = GetEntityCoords(ped)
local distance = #(myCoords - coords)
if distance <= Config.MUSIC_RANGE then
local serverId = GetPlayerServerId(player)
if serverId then
local musicId = "phone_utuneemusic_id_" .. serverId
if not xSound:soundExists(musicId) then
syncAttempts[serverId] = syncAttempts[serverId] or 0
if syncAttempts[serverId] < 3 then
TriggerServerEvent("phone:utune_music:requestSync", serverId)
syncAttempts[serverId] = syncAttempts[serverId] + 1
end
else
syncAttempts[serverId] = 0
end
end
else
local serverId = GetPlayerServerId(player)
if serverId then
syncAttempts[serverId] = nil
end
end
end
end
for serverId in pairs(syncAttempts) do
if serverId then
local found = false
for _, player in ipairs(players) do
if GetPlayerServerId(player) == serverId then
found = true
break
end
end
if not found then
syncAttempts[serverId] = nil
end
end
end
end
end)
CreateThread(function()
while true do
Wait(1000)
if playing and currentPlaylistIndex > 0 and not isPaused then
local musicId = "phone_utuneemusic_id_" .. GetPlayerServerId(PlayerId())
if not xSound:soundExists(musicId) then
if currentPlaylistIndex < #playlist then
currentPlaylistIndex = currentPlaylistIndex + 1
local song = playlist[currentPlaylistIndex]
if song then
Wait(500)
local coords = GetEntityCoords(PlayerPedId())
utuneUrl = song.url
TriggerServerEvent("phone:utune_music:soundStatus", "play", {
position = coords,
link = utuneUrl,
volume = volume
})
SendNUIMessage({
action = "songChanged",
url = song.url,
index = currentPlaylistIndex
})
end
else
playing = false
currentPlaylistIndex = 0
SendNUIMessage({
action = "playlistEnded"
})
end
end
end
end
end)
RegisterNUICallback("pauseSound", function(_, cb)
isPaused = true
TriggerServerEvent("phone:utune_music:soundStatus", "pause", {})
cb({})
end)
RegisterNUICallback("resumeSound", function(_, cb)
isPaused = false
TriggerServerEvent("phone:utune_music:soundStatus", "resume", {})
cb({})
end)