-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
546 lines (472 loc) · 22.4 KB
/
Copy pathclient.lua
File metadata and controls
546 lines (472 loc) · 22.4 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
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
local QBCore = exports['qb-core']:GetCoreObject()
-- ─────────────────────────────────────────────────────────────────────────────
-- Start Raffle — multi-step prize selection flow
-- Called after server sends back vehicle + item lists via openStartDialog
-- ─────────────────────────────────────────────────────────────────────────────
local function startRaffleFlow(payload)
local raffleName = payload.raffleName or 'Mystery Raffle'
local vehicleList = payload.vehicleList or {}
local itemList = payload.itemList or {}
local presets = payload.presets or {}
-- ── Step 1: Raffle name + prize type ────────────────────────────────────
local step1 = lib.inputDialog('🎰 New Raffle — Step 1 of 2', {
{
type = 'input',
label = 'Raffle Name',
default = raffleName,
max = 50,
required = true,
},
{
type = 'select',
label = 'Prize Type',
description = 'Choose how the prize will be delivered',
options = {
{ label = '🎁 Custom / Manual', value = 'custom' },
{ label = '🚗 Vehicle to Garage', value = 'vehicle' },
{ label = '💰 Cash or Bank', value = 'cash' },
{ label = '📦 Inventory Item', value = 'item' },
},
required = true,
},
})
if not step1 or not step1[1] or not step1[2] then return end
local name = step1[1]:match('^%s*(.-)%s*$')
if name == '' then name = 'Mystery Raffle' end
local prizeType = step1[2]
-- ── Step 2: Prize details (depends on type) ──────────────────────────────
if prizeType == 'custom' then
-- Custom: text input with optional preset quick-pick
local presetOpts = {}
for i, p in ipairs(presets) do
presetOpts[i] = { label = p, value = p }
end
local step2 = lib.inputDialog('🎁 Custom Prize — Step 2 of 2', {
{
type = 'input',
label = 'Prize Description',
description = 'Staff delivers this manually after /endraffle',
placeholder = Config.DefaultPrizeHint,
max = 100,
},
{
type = 'select',
label = '— OR pick a preset —',
description = 'Leave the text field blank to use a preset instead',
options = presetOpts,
required = false,
},
})
if not step2 then return end
local typed = step2[1] and step2[1]:match('^%s*(.-)%s*$') or ''
local preset = step2[2] or ''
local prizeDesc = (typed ~= '') and typed or (preset ~= '' and preset or nil)
if not prizeDesc then
lib.notify({ title = 'Raffle', description = 'A prize description is required.', type = 'error' })
return
end
TriggerServerEvent('dynamic-raffle:server:startRaffle', name, 'custom', { description = prizeDesc })
elseif prizeType == 'vehicle' then
-- Vehicle: searchable select from QBCore.Shared.Vehicles
local vehOpts = {}
for i, v in ipairs(vehicleList) do
vehOpts[i] = { label = v.label, value = v.model }
end
local step2 = lib.inputDialog(
'🚗 Vehicle Prize — Step 2 of 2 (' .. #vehicleList .. ' vehicles)', {
{
type = 'select',
label = 'Select Prize Vehicle',
description = 'Vehicle will be inserted directly into the winner\'s garage',
options = vehOpts,
required = true,
},
})
if not step2 or not step2[1] then return end
TriggerServerEvent('dynamic-raffle:server:startRaffle', name, 'vehicle', {
model = step2[1],
})
elseif prizeType == 'cash' then
-- Cash: amount + bank or cash-on-hand
local step2 = lib.inputDialog('💰 Cash Prize — Step 2 of 2', {
{
type = 'number',
label = 'Prize Amount ($)',
description = 'Delivered automatically when /endraffle is called',
default = 50000,
min = 1,
max = 10000000,
required = true,
},
{
type = 'select',
label = 'Deliver To',
options = {
{ label = '🏦 Bank Account', value = 'bank' },
{ label = '💵 Cash on Hand', value = 'cash' },
},
required = true,
},
})
if not step2 or not step2[1] then return end
TriggerServerEvent('dynamic-raffle:server:startRaffle', name, 'cash', {
amount = tonumber(step2[1]) or 50000,
account = step2[2] or 'bank',
})
elseif prizeType == 'item' then
-- Item: ox_inventory item list + quantity
local itemOpts = {}
for i, item in ipairs(itemList) do
itemOpts[i] = { label = item.label .. ' [' .. item.name .. ']', value = item.name }
end
local step2 = lib.inputDialog(
'📦 Item Prize — Step 2 of 2 (' .. #itemList .. ' items)', {
{
type = 'select',
label = 'Select Prize Item',
description = 'Item will be added directly to the winner\'s ox_inventory',
options = itemOpts,
required = true,
},
{
type = 'number',
label = 'Quantity',
default = 1,
min = 1,
max = 100,
required = true,
},
})
if not step2 or not step2[1] then return end
TriggerServerEvent('dynamic-raffle:server:startRaffle', name, 'item', {
name = step2[1],
count = math.floor(tonumber(step2[2]) or 1),
})
end
end
-- Server fires this back after /startraffle or "Start New Raffle" in manage menu
RegisterNetEvent('dynamic-raffle:client:openStartDialog', function(payload)
startRaffleFlow(payload)
end)
-- ─────────────────────────────────────────────────────────────────────────────
-- Commands
-- ─────────────────────────────────────────────────────────────────────────────
-- /startraffle [name] — quick shortcut, asks server for lists then opens flow
RegisterCommand('startraffle', function(_, args)
local raffleName = table.concat(args, ' ')
if raffleName == '' then raffleName = 'Mystery Raffle' end
TriggerServerEvent('dynamic-raffle:server:requestStartDialog', raffleName)
end, false)
RegisterCommand('endraffle', function()
TriggerServerEvent('dynamic-raffle:server:endRaffle')
end, false)
RegisterCommand('joinraffle', function()
TriggerServerEvent('dynamic-raffle:server:joinRaffle')
end, false)
RegisterCommand('raffleping', function()
TriggerServerEvent('dynamic-raffle:server:ping')
end, false)
RegisterCommand('rafflestatus', function()
TriggerServerEvent('dynamic-raffle:server:status')
end, false)
-- /rafflemanage — unified admin hub
RegisterCommand('rafflemanage', function()
TriggerServerEvent('dynamic-raffle:server:getManageData')
end, false)
-- ─────────────────────────────────────────────────────────────────────────────
-- Manage Menu Builder
-- Builds the full ox_lib context menu tree from server data and opens it.
-- Also re-invoked automatically after any preset change.
-- ─────────────────────────────────────────────────────────────────────────────
local function GetPrizeTypeIcon(prizeType)
if prizeType == 'vehicle' then return '🚗'
elseif prizeType == 'cash' then return '💰'
elseif prizeType == 'item' then return '📦'
else return '🎁' end
end
local function BuildManageMenus(data)
local mainOptions = {}
-- ── Section 1: Start / Current Raffle ────────────────────────────────────
if data.active and data.raffle then
-- Active raffle — show participants + quick controls
local r = data.raffle
local typeIcon = GetPrizeTypeIcon(r.prizeType)
local currentOpts = {
{ title = typeIcon .. ' Prize', description = r.prize, disabled = true },
{ title = '🕐 Started by', description = r.startedBy, disabled = true },
{ title = '👥 Entries', description = tostring(#data.entries) .. ' entered', disabled = true },
{ title = '────────────────────────────────────', disabled = true },
{
title = '📣 Send Reminder Now',
description = 'Blast the reminder notification to all players',
event = 'dynamic-raffle:client:menuPing',
},
{
title = '🏁 End Raffle & Draw Winner',
description = 'Picks a winner from ' .. #data.entries .. ' eligible entries',
event = 'dynamic-raffle:client:menuEndRaffle',
},
{ title = '────────────────────────────────────', disabled = true },
}
if #data.entries > 0 then
table.insert(currentOpts, { title = '👥 Participants', disabled = true })
for _, pname in ipairs(data.entries) do
table.insert(currentOpts, { title = ' 👤 ' .. pname, disabled = true })
end
else
table.insert(currentOpts, { title = 'No entries yet — raffle is open', disabled = true })
end
lib.registerContext({
id = 'raffle_manage_current',
title = '📋 ' .. r.name,
menu = 'raffle_manage_main',
options = currentOpts,
})
table.insert(mainOptions, {
title = '📋 Active: ' .. r.name,
description = r.prize .. ' · ' .. #data.entries .. ' entries',
arrow = true,
menu = 'raffle_manage_current',
})
else
-- No active raffle — show Start button
table.insert(mainOptions, {
title = '🚀 Start New Raffle',
description = 'Pick prize type: vehicle, cash, item, or custom',
event = 'dynamic-raffle:client:menuStartRaffle',
})
end
-- ── Section 2: Raffle History ─────────────────────────────────────────────
local historySubOpts = {}
if #data.history == 0 then
table.insert(historySubOpts, { title = 'No completed raffles yet', disabled = true })
else
for i = #data.history, 1, -1 do -- newest first
local h = data.history[i]
local subId = 'raffle_manage_history_' .. i
local icon = GetPrizeTypeIcon(h.prizeType)
local detailOpts = {
{ title = '🏆 Winner', description = h.winner, disabled = true },
{ title = icon .. ' Prize', description = h.prize, disabled = true },
{ title = '👥 Total Entries', description = tostring(#h.entries), disabled = true },
{ title = '🕐 Completed', description = h.date, disabled = true },
{ title = '────────────────────────────────────', disabled = true },
}
for _, pname in ipairs(h.entries) do
table.insert(detailOpts, {
title = (pname == h.winner and '🏆 ' or ' 👤 ') .. pname,
description = pname == h.winner and 'Winner' or nil,
disabled = true,
})
end
lib.registerContext({
id = subId,
title = h.name,
menu = 'raffle_manage_history',
options = detailOpts,
})
table.insert(historySubOpts, {
title = h.name,
description = '🏆 ' .. h.winner .. ' · ' .. h.prize .. ' · ' .. h.date,
arrow = true,
menu = subId,
})
end
end
lib.registerContext({
id = 'raffle_manage_history',
title = '🏆 Raffle History',
menu = 'raffle_manage_main',
options = historySubOpts,
})
table.insert(mainOptions, {
title = '🏆 Raffle History',
description = #data.history .. ' completed raffle(s)',
arrow = true,
menu = 'raffle_manage_history',
})
-- ── Section 3: Winner Cooldowns ─────────────────────────────────────────────
if data.cooldownLimit and data.cooldownLimit > 0 then
local cooldownOpts = {}
if not data.cooldowns or #data.cooldowns == 0 then
table.insert(cooldownOpts, {
title = 'No active cooldowns',
description = 'Everyone is eligible to win',
disabled = true,
})
else
for _, cd in ipairs(data.cooldowns) do
table.insert(cooldownOpts, {
title = '⏳ ' .. cd.name,
description = cd.remaining .. ' raffle(s) remaining on cooldown',
disabled = true,
})
end
end
lib.registerContext({
id = 'raffle_manage_cooldowns',
title = '⏳ Winner Cooldowns',
menu = 'raffle_manage_main',
options = cooldownOpts,
})
table.insert(mainOptions, {
title = '⏳ Winner Cooldowns',
description = (#(data.cooldowns or {})) .. ' player(s) sitting out · ' .. data.cooldownLimit .. ' raffle cool-off',
arrow = true,
menu = 'raffle_manage_cooldowns',
})
end
-- ── Section 3: Preset Prizes ──────────────────────────────────────────────
local presetOpts = {}
for i, preset in ipairs(data.presets) do
local subId = 'raffle_preset_' .. i
lib.registerContext({
id = subId,
title = '🎁 ' .. preset,
menu = 'raffle_manage_presets',
options = {
{
title = '✏️ Edit',
description = 'Update this preset description',
event = 'dynamic-raffle:client:editPreset',
args = { index = i, current = preset },
},
{
title = '🗑️ Remove',
description = 'Permanently delete this preset',
event = 'dynamic-raffle:client:removePreset',
args = { index = i, label = preset },
},
},
})
table.insert(presetOpts, {
title = preset,
description = 'Edit or remove',
arrow = true,
menu = subId,
})
end
table.insert(presetOpts, {
title = '➕ Add New Preset',
description = 'Add a new quick-select prize option',
event = 'dynamic-raffle:client:addPreset',
})
lib.registerContext({
id = 'raffle_manage_presets',
title = '🎁 Preset Prizes',
menu = 'raffle_manage_main',
options = presetOpts,
})
table.insert(mainOptions, {
title = '🎁 Preset Prizes',
description = #data.presets .. ' preset(s) · Edit quick-select options',
arrow = true,
menu = 'raffle_manage_presets',
})
-- ── Register & open main ──────────────────────────────────────────────────
lib.registerContext({
id = 'raffle_manage_main',
title = '🎰 Raffle Manager',
options = mainOptions,
})
lib.showContext('raffle_manage_main')
end
-- ─────────────────────────────────────────────────────────────────────────────
-- Manage Menu Action Handlers
-- ─────────────────────────────────────────────────────────────────────────────
-- "Start New Raffle" inside /rafflemanage
AddEventHandler('dynamic-raffle:client:menuStartRaffle', function()
TriggerServerEvent('dynamic-raffle:server:requestStartDialog', 'Mystery Raffle')
end)
-- "End Raffle & Draw Winner" inside manage menu
AddEventHandler('dynamic-raffle:client:menuEndRaffle', function()
local confirm = lib.alertDialog({
header = '🏁 End Raffle',
content = 'Draw a winner now and deliver the prize automatically?',
centered = true,
cancel = true,
})
if confirm ~= 'confirm' then return end
TriggerServerEvent('dynamic-raffle:server:endRaffle')
end)
-- "Send Reminder Now" inside manage menu
AddEventHandler('dynamic-raffle:client:menuPing', function()
TriggerServerEvent('dynamic-raffle:server:ping')
end)
-- ─────────────────────────────────────────────────────────────────────────────
-- Preset CRUD Handlers
-- ─────────────────────────────────────────────────────────────────────────────
AddEventHandler('dynamic-raffle:client:editPreset', function(args)
local result = lib.inputDialog('✏️ Edit Preset', {
{
type = 'input',
label = 'Prize Description',
default = args.current,
max = 100,
required = true,
},
})
if not result or not result[1] or result[1]:match('^%s*$') then return end
TriggerServerEvent('dynamic-raffle:server:editPreset', args.index, result[1])
end)
AddEventHandler('dynamic-raffle:client:removePreset', function(args)
local confirm = lib.alertDialog({
header = '🗑️ Remove Preset',
content = 'Remove **' .. (args.label or 'this preset') .. '**? This cannot be undone.',
centered = true,
cancel = true,
})
if confirm ~= 'confirm' then return end
TriggerServerEvent('dynamic-raffle:server:removePreset', args.index)
end)
AddEventHandler('dynamic-raffle:client:addPreset', function()
local result = lib.inputDialog('➕ Add New Preset', {
{
type = 'input',
label = 'Prize Description',
placeholder = 'e.g. 🎮 Custom Controller',
max = 100,
required = true,
},
})
if not result or not result[1] or result[1]:match('^%s*$') then return end
TriggerServerEvent('dynamic-raffle:server:addPreset', result[1])
end)
-- ─────────────────────────────────────────────────────────────────────────────
-- Server → Client net events
-- ─────────────────────────────────────────────────────────────────────────────
-- Open / refresh the manage menu (also fires after preset changes)
RegisterNetEvent('dynamic-raffle:client:openManage', function(data)
BuildManageMenus(data)
end)
-- Generic notification
RegisterNetEvent('dynamic-raffle:notify', function(msg, msgType)
lib.notify({
title = '🎰 Raffle',
description = msg,
type = msgType or 'inform',
duration = 6000,
})
end)
-- Server-wide winner announcement
RegisterNetEvent('dynamic-raffle:announce', function(winnerName, prize, raffleName)
lib.notify({
title = '🏆 We Have a Winner!',
description = winnerName .. ' won the ' .. raffleName .. '!\n🎁 Prize: ' .. prize,
type = 'success',
duration = 10000,
})
end)
-- Winner-only golden NUI overlay
RegisterNetEvent('dynamic-raffle:youWon', function(prize, raffleName)
SetNuiFocus(false, false) -- keep game control
SendNUIMessage({
action = 'showWinner',
prize = prize,
raffle = raffleName,
duration = Config.WinnerDisplayTime,
})
SetTimeout(Config.WinnerDisplayTime + 500, function()
SendNUIMessage({ action = 'hide' })
end)
end)