-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.lua
497 lines (461 loc) · 16.6 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
local QBCore = exports['qb-core']:GetCoreObject()
if not lib then
lib = exports['ox_lib']:GetLib()
end
local gameState = {
deck = {},
playerHand = {},
dealerHand = {},
currentBet = 0,
isPlaying = false
}
local function createDeck()
local deck = {}
local suits = {
hearts = {
symbol = "♥️",
cards = {
A = "🂱",
["2"] = "🂲",
["3"] = "🂳",
["4"] = "🂴",
["5"] = "🂵",
["6"] = "🂶",
["7"] = "🂷",
["8"] = "🂸",
["9"] = "🂹",
["10"] = "🂺",
J = "🂻",
Q = "🂽",
K = "🂾"
}
},
diamonds = {
symbol = "♦️",
cards = {
A = "🃁",
["2"] = "🃂",
["3"] = "🃃",
["4"] = "🃄",
["5"] = "🃅",
["6"] = "🃆",
["7"] = "🃇",
["8"] = "🃈",
["9"] = "🃉",
["10"] = "🃊",
J = "🃋",
Q = "🃍",
K = "🃎"
}
},
clubs = {
symbol = "♣️",
cards = {
A = "🃑",
["2"] = "🃒",
["3"] = "🃓",
["4"] = "🃔",
["5"] = "🃕",
["6"] = "🃖",
["7"] = "🃗",
["8"] = "🃘",
["9"] = "🃙",
["10"] = "🃚",
J = "🃛",
Q = "🃝",
K = "🃞"
}
},
spades = {
symbol = "♠️",
cards = {
A = "🂡",
["2"] = "🂢",
["3"] = "🂣",
["4"] = "🂤",
["5"] = "🂥",
["6"] = "🂦",
["7"] = "🂧",
["8"] = "🂨",
["9"] = "🂩",
["10"] = "🂪",
J = "🂫",
Q = "🂭",
K = "🂮"
}
}
}
local values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
for suitName, suitData in pairs(suits) do
for _, value in ipairs(values) do
table.insert(deck, {
value = value,
suit = suitData.symbol,
display = suitData.cards[value]
})
end
end
for i = #deck, 2, -1 do
local j = math.random(i)
deck[i], deck[j] = deck[j], deck[i]
end
return deck
end
local function calculateHandValue(hand)
local value = 0
local aces = 0
for _, card in ipairs(hand) do
if card.value == "A" then
aces = aces + 1
value = value + 11
elseif card.value == "K" or card.value == "Q" or card.value == "J" then
value = value + 10
else
value = value + tonumber(card.value)
end
end
while value > 21 and aces > 0 do
value = value - 10
aces = aces - 1
end
return value
end
local function formatHand(hand, hideSecond)
local formatted = {}
for i, card in ipairs(hand) do
if hideSecond and i == 2 then
table.insert(formatted, "🂠") -- Card back
else
table.insert(formatted, card.display) -- Use the specific card emoji
end
end
return table.concat(formatted, " ")
end
local showBettingMenu
local showRulesMenu
local dealerPlay
local showGameMenu
local startBlackjackGame
showBettingMenu = function()
lib.registerContext({
id = 'blackjack_betting',
title = '🎰 Welcome to Blackjack',
options = {
{
title = '💵 Small Bet ($50)',
description = 'Place a modest bet of $50',
icon = 'fas fa-dollar-sign',
metadata = {
{label = 'Minimum Bet', value = '$50'},
{label = 'Maximum Win', value = '$100'}
},
onSelect = function()
print("Selected $50 bet") -- Debug print
startBlackjackGame(50)
end
},
{
title = '💰 Medium Bet ($100)',
description = 'Place a medium bet of $100',
icon = 'fas fa-coins',
metadata = {
{label = 'Minimum Bet', value = '$100'},
{label = 'Maximum Win', value = '$200'}
},
onSelect = function()
startBlackjackGame(100)
end
},
{
title = '💎 High Roller ($500)',
description = 'Place a high bet of $500',
icon = 'fas fa-gem',
metadata = {
{label = 'Minimum Bet', value = '$500'},
{label = 'Maximum Win', value = '$1000'}
},
onSelect = function()
startBlackjackGame(500)
end
},
{
title = '🎲 Custom Bet',
description = 'Enter your own bet amount',
icon = 'fas fa-dice',
onSelect = function()
local input = lib.inputDialog('Custom Bet', {
{
type = 'number',
label = 'Bet Amount',
description = 'Enter bet amount (Minimum: $50)',
icon = 'fas fa-dollar-sign',
required = true,
min = 50,
-- Removed the max = 1000 line to allow unlimited betting
}
})
if input then
local amount = tonumber(input[1])
if amount then
startBlackjackGame(amount)
end
end
end
},
{
title = '❓ How to Play',
description = 'View game rules and payouts',
icon = 'fas fa-question-circle',
onSelect = function()
showRulesMenu()
end
}
}
})
lib.showContext('blackjack_betting')
end
showRulesMenu = function()
lib.registerContext({
id = 'blackjack_rules',
title = '📋 Blackjack Rules',
menu = 'blackjack_betting',
options = {
{
title = '🎯 Objective',
description = 'Beat the dealer\'s hand without going over 21',
metadata = {
{label = 'Blackjack Pays', value = '3:2'},
{label = 'Regular Win', value = '1:1'},
{label = 'Insurance', value = '2:1'}
}
},
{
title = '🃏 Card Values',
description = 'A (1 or 11), Face Cards (10), Others (Face Value)',
},
{
title = '🎮 Game Actions',
description = 'Hit, Stand, Double Down, Split',
},
{
title = '🔙 Back to Betting',
description = 'Return to betting menu',
onSelect = function()
showBettingMenu()
end
}
}
})
lib.showContext('blackjack_rules')
end
dealerPlay = function()
while calculateHandValue(gameState.dealerHand) < 17 do
table.insert(gameState.dealerHand, table.remove(gameState.deck))
Wait(500)
end
local playerValue = calculateHandValue(gameState.playerHand)
local dealerValue = calculateHandValue(gameState.dealerHand)
local outcomeTitle = ""
local outcomeMessage = ""
local handComparison = string.format([[
Your Hand (%d): %s
Dealer's Hand (%d): %s]],
playerValue,
formatHand(gameState.playerHand, false),
dealerValue,
formatHand(gameState.dealerHand, false)
)
if playerValue > 21 then
outcomeTitle = "🔴 Bust! You Lost!"
outcomeMessage = string.format("You went over 21 and lost $%d\n\n%s",
gameState.currentBet, handComparison)
TriggerServerEvent('blackjack:loseBet', gameState.currentBet)
elseif dealerValue > 21 then
outcomeTitle = "🟢 Dealer Bust! You Won!"
outcomeMessage = string.format("Dealer went over 21! You won $%d\n\n%s",
gameState.currentBet * 2, handComparison)
TriggerServerEvent('blackjack:winBet', gameState.currentBet)
elseif playerValue > dealerValue then
outcomeTitle = "🟢 You Won!"
outcomeMessage = string.format("You beat the dealer! Won $%d\n\n%s",
gameState.currentBet * 2, handComparison)
TriggerServerEvent('blackjack:winBet', gameState.currentBet)
elseif dealerValue > playerValue then
outcomeTitle = "🔴 Dealer Wins!"
outcomeMessage = string.format("Dealer wins with %d vs your %d! You lost $%d\n\n%s",
dealerValue, playerValue, gameState.currentBet, handComparison)
TriggerServerEvent('blackjack:loseBet', gameState.currentBet)
else
outcomeTitle = "🟡 Push! It's a Tie!"
outcomeMessage = string.format("It's a tie! Your bet of $%d has been returned\n\n%s",
gameState.currentBet, handComparison)
TriggerServerEvent('blackjack:tieBet', gameState.currentBet)
end
lib.registerContext({
id = 'blackjack_results',
title = outcomeTitle,
description = outcomeMessage,
options = {
{
title = '📊 Match Results',
description = string.format([[
Dealer's Hand: %s (%d)
Your Hand: %s (%d)
%s]],
formatHand(gameState.dealerHand, false), dealerValue,
formatHand(gameState.playerHand, false), playerValue,
dealerValue > playerValue and playerValue <= 21
and string.format("💸 You lost $%d", gameState.currentBet)
or dealerValue < playerValue and playerValue <= 21
and string.format("💰 You won $%d", gameState.currentBet * 2)
or dealerValue == playerValue and "🤝 Push - Bet Returned"
or playerValue > 21 and string.format("💸 Bust - Lost $%d", gameState.currentBet)
),
icon = 'fas fa-chart-bar'
},
{
title = '🎲 Play Again',
description = 'Start a new game',
onSelect = function()
gameState.isPlaying = false
showBettingMenu()
end
},
{
title = '🚶♂️ Leave Table',
description = 'Exit the game',
onSelect = function()
gameState.isPlaying = false
end
}
}
})
lib.showContext('blackjack_results')
end
showGameMenu = function()
local playerValue = calculateHandValue(gameState.playerHand)
local dealerValue = calculateHandValue({gameState.dealerHand[1]})
lib.registerContext({
id = 'blackjack_game',
title = '🎰 Blackjack Table',
options = {
{
title = '🃏 Game Cards',
description = string.format([[
Dealer:
• %d %s %s
Your Hand:
• %d %s]],
dealerValue,
gameState.dealerHand[1].display,
"🂠",
playerValue,
formatHand(gameState.playerHand, false)
),
metadata = {
{label = 'Dealer\'s Visible Card', value = string.format('%s (Value: %d)', gameState.dealerHand[1].display, dealerValue)},
{label = 'Dealer\'s Hidden Card', value = '🂠 (Hidden)'},
{label = 'Your Cards', value = formatHand(gameState.playerHand, false)},
{label = 'Your Hand Value', value = string.format('%d', playerValue)},
{label = 'Current Bet', value = string.format('$%d', gameState.currentBet)},
{label = 'Game Status', value = playerValue > 21 and '🔴 Bust!' or playerValue == 21 and '🟡 Blackjack!' or '🟢 In Play'}
}
},
{
title = '👆 Hit',
description = 'Draw another card',
onSelect = function()
table.insert(gameState.playerHand, table.remove(gameState.deck))
local newValue = calculateHandValue(gameState.playerHand)
if newValue > 21 then
QBCore.Functions.Notify('💥 Bust! You went over 21!', 'error')
dealerPlay()
else
showGameMenu()
end
end
},
{
title = '✋ Stand',
description = 'Keep your current hand',
onSelect = function()
dealerPlay()
end
},
{
title = '💰 Double Down',
description = 'Double bet and take one card',
disabled = playerValue > 11,
onSelect = function()
QBCore.Functions.TriggerCallback('blackjack:checkMoney', function(hasEnough)
if hasEnough then
gameState.currentBet = gameState.currentBet * 2
table.insert(gameState.playerHand, table.remove(gameState.deck))
dealerPlay()
else
QBCore.Functions.Notify('Not enough money to double down!', 'error')
showGameMenu()
end
end, gameState.currentBet)
end
}
}
})
lib.showContext('blackjack_game')
end
startBlackjackGame = function(bet)
print("Starting blackjack game with bet: " .. tostring(bet)) -- Debug print
QBCore.Functions.TriggerCallback('blackjack:checkMoney', function(hasEnough)
print("Money check result: " .. tostring(hasEnough)) -- Debug print
if hasEnough then
-- Initialize game state
gameState.deck = createDeck()
gameState.playerHand = {}
gameState.dealerHand = {}
gameState.currentBet = bet
gameState.isPlaying = true
-- Deal initial cards
table.insert(gameState.playerHand, table.remove(gameState.deck))
table.insert(gameState.dealerHand, table.remove(gameState.deck))
table.insert(gameState.playerHand, table.remove(gameState.deck))
table.insert(gameState.dealerHand, table.remove(gameState.deck))
print("Initial cards dealt, showing game menu") -- Debug print
showGameMenu()
else
QBCore.Functions.Notify('Not enough money to place bet!', 'error')
end
end, bet)
end
CreateThread(function()
local pedModel = GetHashKey("A_M_Y_SmartCasPat_01")
RequestModel(pedModel)
while not HasModelLoaded(pedModel) do
Wait(1)
end
local blackjackPed = CreatePed(4, pedModel, 1993.972, 3050.766, 46.215, 145.177, false, true)
FreezeEntityPosition(blackjackPed, true)
SetEntityInvincible(blackjackPed, true)
SetBlockingOfNonTemporaryEvents(blackjackPed, true)
local blip = AddBlipForCoord(1993.972, 3050.766, 46.215)
SetBlipSprite(blip, 679)
SetBlipDisplay(blip, 4)
SetBlipScale(blip, 0.8)
SetBlipColour(blip, 50)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Temp's Casino & Bar")
EndTextCommandSetBlipName(blip)
exports.ox_target:addLocalEntity(blackjackPed, {
{
name = 'play_blackjack',
icon = 'fas fa-play',
label = 'Play Blackjack',
onSelect = function()
if not gameState.isPlaying then
showBettingMenu()
else
QBCore.Functions.Notify('Game already in progress!', 'error')
end
end
}
})
end)