-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
303 lines (246 loc) · 10.1 KB
/
Copy pathserver.lua
File metadata and controls
303 lines (246 loc) · 10.1 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
local QBCore = exports['qb-core']:GetCoreObject()
-- ============================================================================
-- SERVER STATE (authoritative)
-- ============================================================================
local BusinessState = {}
local AdvanceCooldown = {}
local ADVANCE_COOLDOWN_SEC = 2
-- ============================================================================
-- HELPERS
-- ============================================================================
local function GetBusinessConfig(name)
for _, biz in ipairs(Config.Businesses) do
if biz.name == name then
return biz
end
end
return nil
end
local function GetPlaylistLength(businessName)
local biz = GetBusinessConfig(businessName)
if not biz or not biz.playlist then return 0 end
return #biz.playlist
end
local function IsValidBusiness(businessName)
if type(businessName) ~= 'string' then return false end
return GetBusinessConfig(businessName) ~= nil
end
local function BroadcastUpdate(businessName)
local state = BusinessState[businessName]
if state then
TriggerClientEvent('djb:client:UpdateBusiness', -1, businessName, state)
end
end
-- ============================================================================
-- SHUFFLE LOGIC
-- ============================================================================
local function GenerateShuffleOrder(businessName)
local totalTracks = GetPlaylistLength(businessName)
if totalTracks == 0 then return {} end
local order = {}
for i = 1, totalTracks do
order[i] = i
end
-- Fisher-Yates shuffle
for i = totalTracks, 2, -1 do
local j = math.random(1, i)
order[i], order[j] = order[j], order[i]
end
return order
end
local function GetNextTrackIndex(businessName, direction)
local state = BusinessState[businessName]
if not state then return 1 end
local totalTracks = GetPlaylistLength(businessName)
if totalTracks == 0 then return 1 end
if state.shuffle and state.shuffleOrder and #state.shuffleOrder > 0 then
-- Find current position in shuffle order
local currentPos = 1
for i, idx in ipairs(state.shuffleOrder) do
if idx == state.trackIndex then
currentPos = i
break
end
end
if direction == 'next' then
currentPos = currentPos + 1
if currentPos > #state.shuffleOrder then
-- Reshuffle and start over
state.shuffleOrder = GenerateShuffleOrder(businessName)
currentPos = 1
end
elseif direction == 'prev' then
currentPos = currentPos - 1
if currentPos < 1 then
currentPos = #state.shuffleOrder
end
end
return state.shuffleOrder[currentPos]
else
-- Sequential mode
local newIndex = state.trackIndex
if direction == 'next' then
newIndex = newIndex + 1
if newIndex > totalTracks then newIndex = 1 end
elseif direction == 'prev' then
newIndex = newIndex - 1
if newIndex < 1 then newIndex = totalTracks end
end
return newIndex
end
end
-- ============================================================================
-- INITIALIZATION
-- ============================================================================
local function InitializeStates()
math.randomseed(os.time())
for _, biz in ipairs(Config.Businesses) do
local shuffleOrder = nil
local startTrack = 1
if Config.ShuffleMode then
shuffleOrder = GenerateShuffleOrder(biz.name)
if #shuffleOrder > 0 then
startTrack = shuffleOrder[1]
end
end
BusinessState[biz.name] = {
playing = Config.AutoStartOnResourceStart,
trackIndex = startTrack,
volume = biz.defaultVolume or Config.DefaultVolume,
shuffle = Config.ShuffleMode,
shuffleOrder = shuffleOrder,
}
print('[dynamic-jukebox] Initialized: ' .. biz.label .. ' (' .. biz.name .. ') | ' .. #(biz.playlist or {}) .. ' tracks' .. (Config.ShuffleMode and ' | SHUFFLE' or ''))
end
end
-- ============================================================================
-- EVENTS
-- ============================================================================
RegisterNetEvent('djb:server:RequestState', function()
local src = source
TriggerClientEvent('djb:client:SyncState', src, BusinessState)
end)
RegisterNetEvent('djb:server:TogglePlay', function(businessName)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
state.playing = not state.playing
print('[dynamic-jukebox] ' .. businessName .. ' → ' .. (state.playing and 'PLAYING' or 'PAUSED'))
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:SkipTrack', function(businessName)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
if GetPlaylistLength(businessName) == 0 then return end
state.trackIndex = GetNextTrackIndex(businessName, 'next')
state.playing = true
AdvanceCooldown[businessName] = os.time()
print('[dynamic-jukebox] ' .. businessName .. ' skipped → track ' .. state.trackIndex)
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:PrevTrack', function(businessName)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
if GetPlaylistLength(businessName) == 0 then return end
state.trackIndex = GetNextTrackIndex(businessName, 'prev')
state.playing = true
AdvanceCooldown[businessName] = os.time()
print('[dynamic-jukebox] ' .. businessName .. ' previous → track ' .. state.trackIndex)
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:AdvanceTrack', function(businessName)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state or not state.playing then return end
local now = os.time()
local lastAdvance = AdvanceCooldown[businessName] or 0
if (now - lastAdvance) < ADVANCE_COOLDOWN_SEC then
return
end
AdvanceCooldown[businessName] = now
if GetPlaylistLength(businessName) == 0 then return end
state.trackIndex = GetNextTrackIndex(businessName, 'next')
print('[dynamic-jukebox] ' .. businessName .. ' auto-advanced → track ' .. state.trackIndex)
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:SetVolume', function(businessName, volume)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
state.volume = math.max(0.0, math.min(1.0, tonumber(volume) or Config.DefaultVolume))
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:SelectTrack', function(businessName, trackIndex)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
local totalTracks = GetPlaylistLength(businessName)
trackIndex = tonumber(trackIndex) or 1
if trackIndex < 1 or trackIndex > totalTracks then return end
state.trackIndex = trackIndex
state.playing = true
AdvanceCooldown[businessName] = os.time()
print('[dynamic-jukebox] ' .. businessName .. ' selected track ' .. trackIndex)
BroadcastUpdate(businessName)
end)
RegisterNetEvent('djb:server:ToggleShuffle', function(businessName)
if not IsValidBusiness(businessName) then return end
local state = BusinessState[businessName]
if not state then return end
state.shuffle = not state.shuffle
if state.shuffle then
state.shuffleOrder = GenerateShuffleOrder(businessName)
else
state.shuffleOrder = nil
end
print('[dynamic-jukebox] ' .. businessName .. ' shuffle → ' .. (state.shuffle and 'ON' or 'OFF'))
BroadcastUpdate(businessName)
end)
-- ============================================================================
-- ADMIN COMMANDS
-- ============================================================================
QBCore.Commands.Add('jukeboxstop', 'Stop all jukeboxes (Admin)', {}, false, function(source)
for name, state in pairs(BusinessState) do
state.playing = false
BroadcastUpdate(name)
end
TriggerClientEvent('QBCore:Notify', source, 'All jukeboxes stopped', 'success')
end, 'admin')
QBCore.Commands.Add('jukeboxstart', 'Start all jukeboxes (Admin)', {}, false, function(source)
for name, state in pairs(BusinessState) do
state.playing = true
BroadcastUpdate(name)
end
TriggerClientEvent('QBCore:Notify', source, 'All jukeboxes started', 'success')
end, 'admin')
QBCore.Commands.Add('jukeboxvol', 'Set jukebox volume (Admin)', {
{ name = 'business', help = 'Business name from config' },
{ name = 'volume', help = 'Volume 0-100' },
}, true, function(source, args)
local businessName = args[1]
local volume = tonumber(args[2])
if not IsValidBusiness(businessName) then
TriggerClientEvent('QBCore:Notify', source, 'Invalid business: ' .. tostring(businessName), 'error')
return
end
if not volume or volume < 0 or volume > 100 then
TriggerClientEvent('QBCore:Notify', source, 'Volume must be 0-100', 'error')
return
end
local state = BusinessState[businessName]
if state then
state.volume = volume / 100
BroadcastUpdate(businessName)
TriggerClientEvent('QBCore:Notify', source, businessName .. ' volume set to ' .. volume .. '%', 'success')
end
end, 'admin')
-- ============================================================================
-- STARTUP
-- ============================================================================
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
InitializeStates()
end)
InitializeStates()